-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lang Chi <21860405@zju.edu.cn>
- Loading branch information
1 parent
6ef73ce
commit c43bbc7
Showing
6 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package opts | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// ValidateExtraHost validates the provided string is a valid extra-host. | ||
func ValidateExtraHost(val string) error { | ||
// allow for IPv6 addresses in extra hosts by only splitting on first ":" | ||
arr := strings.SplitN(val, ":", 2) | ||
if len(arr) != 2 || len(arr[0]) == 0 { | ||
return errors.Errorf("bad format for add-host: %q", val) | ||
} | ||
// TODO(lang710): Skip ipaddr validation for special "host-gateway" string | ||
// If the IP Address is a string called "host-gateway", replace this | ||
// value with the IP address stored in the daemon level HostGatewayIP | ||
// config variable | ||
if _, err := validateIPAddress(arr[1]); err != nil { | ||
return errors.Wrapf(err, "invalid IP address in add-host: %q", arr[1]) | ||
} | ||
return nil | ||
} | ||
|
||
// validateIPAddress validates an Ip address. | ||
func validateIPAddress(val string) (string, error) { | ||
var ip = net.ParseIP(strings.TrimSpace(val)) | ||
if ip != nil { | ||
return ip.String(), nil | ||
} | ||
return "", fmt.Errorf("%s is not an ip address", val) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package opts | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestValidateExtraHosts(t *testing.T) { | ||
valid := []string{ | ||
`myhost:192.168.0.1`, | ||
`thathost:10.0.2.1`, | ||
`anipv6host:2003:ab34:e::1`, | ||
`ipv6local:::1`, | ||
} | ||
|
||
invalid := map[string]string{ | ||
`myhost:192.notanipaddress.1`: `invalid IP`, | ||
`thathost-nosemicolon10.0.0.1`: `bad format`, | ||
`anipv6host:::::1`: `invalid IP`, | ||
`ipv6local:::0::`: `invalid IP`, | ||
} | ||
|
||
for _, extrahost := range valid { | ||
if err := ValidateExtraHost(extrahost); err != nil { | ||
t.Fatalf("ValidateExtraHost(`"+extrahost+"`) should succeed: error %v", err) | ||
} | ||
} | ||
|
||
for extraHost, expectedError := range invalid { | ||
if err := ValidateExtraHost(extraHost); err == nil { | ||
t.Fatalf("ValidateExtraHost(`%q`) should have failed validation", extraHost) | ||
} else { | ||
if !strings.Contains(err.Error(), expectedError) { | ||
t.Fatalf("ValidateExtraHost(`%q`) error should contain %q", extraHost, expectedError) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters