Skip to content

Commit

Permalink
Merge pull request moby#11037 from coolljt0725/add_parse_mac
Browse files Browse the repository at this point in the history
Add validate the input mac address on docker run command
  • Loading branch information
Jessie Frazelle committed Feb 27, 2015
2 parents 13030ad + 2ba0fbb commit 314a678
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
14 changes: 14 additions & 0 deletions integration-cli/docker_cli_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,20 @@ func TestRunInspectMacAddress(t *testing.T) {
logDone("run - inspecting MAC address")
}

// test docker run use a invalid mac address
func TestRunWithInvalidMacAddress(t *testing.T) {
defer deleteAllContainers()

runCmd := exec.Command(dockerBinary, "run", "--mac-address", "92:d0:c6:0a:29", "busybox")
out, _, err := runCommandWithOutput(runCmd)
//use a invalid mac address should with a error out
if err == nil || !strings.Contains(out, "is not a valid mac address") {
t.Fatalf("run with an invalid --mac-address should with error out")
}

logDone("run - can't use an invalid mac address")
}

func TestRunDeallocatePortOnMissingIptablesRule(t *testing.T) {
defer deleteAllContainers()
testRequires(t, SameHostDaemon)
Expand Down
9 changes: 9 additions & 0 deletions opts/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ func ValidateIPAddress(val string) (string, error) {
return "", fmt.Errorf("%s is not an ip address", val)
}

func ValidateMACAddress(val string) (string, error) {
_, err := net.ParseMAC(strings.TrimSpace(val))
if err != nil {
return "", err
} else {
return val, nil
}
}

// Validates domain for resolvconf search configuration.
// A zero length domain is represented by .
func ValidateDnsSearch(val string) (string, error) {
Expand Down
14 changes: 14 additions & 0 deletions opts/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ func TestValidateIPAddress(t *testing.T) {

}

func TestValidateMACAddress(t *testing.T) {
if _, err := ValidateMACAddress(`92:d0:c6:0a:29:33`); err != nil {
t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:29:33`) got %s", err)
}

if _, err := ValidateMACAddress(`92:d0:c6:0a:33`); err == nil {
t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:33`) succeeded; expected failure on invalid MAC")
}

if _, err := ValidateMACAddress(`random invalid string`); err == nil {
t.Fatalf("ValidateMACAddress(`random invalid string`) succeeded; expected failure on invalid MAC")
}
}

func TestListOpts(t *testing.T) {
o := NewListOpts(nil)
o.Set("foo")
Expand Down
6 changes: 6 additions & 0 deletions runconfig/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
return nil, nil, cmd, ErrInvalidWorkingDirectory
}

// Validate the input mac address
if *flMacAddress != "" {
if _, err := opts.ValidateMACAddress(*flMacAddress); err != nil {
return nil, nil, cmd, fmt.Errorf("%s is not a valid mac address", *flMacAddress)
}
}
var (
attachStdin = flAttach.Get("stdin")
attachStdout = flAttach.Get("stdout")
Expand Down

0 comments on commit 314a678

Please sign in to comment.