Skip to content
Snippets Groups Projects
Commit 8fc78473 authored by Neil-Jocelyn Schark's avatar Neil-Jocelyn Schark
Browse files

added hostname and ip checking

parent 4efe4930
No related branches found
No related tags found
1 merge request!941Resolve "Disable checking of IP address in gosdnc"
Pipeline #213599 failed
This commit is part of merge request !941. Comments created here will be created in the context of that merge request.
...@@ -39,3 +39,9 @@ integration-test-gosdn: ...@@ -39,3 +39,9 @@ integration-test-gosdn:
script: script:
- go test -p 1 ./integration-tests/* - go test -p 1 ./integration-tests/*
<<: *test <<: *test
unit-test:
script:
- make unit-test-new
<<: *test
...@@ -106,6 +106,9 @@ integration-tests-debug-up: generate-certs containerize-gosdn containerize-plugi ...@@ -106,6 +106,9 @@ integration-tests-debug-up: generate-certs containerize-gosdn containerize-plugi
integration-tests-debug-down: integration-tests-debug-down:
docker-compose -f dev_env_data/docker-compose/integration-test_docker-compose.yml down docker-compose -f dev_env_data/docker-compose/integration-test_docker-compose.yml down
unit-test-new: install-tools
go test -p 1 ./cli/...
# Warning: Depending on your go and development configuration might also clean caches, modules and docker containers from your other projects. # Warning: Depending on your go and development configuration might also clean caches, modules and docker containers from your other projects.
clean: clean:
rm -rf $(BUILD_ARTIFACTS_PATH) rm -rf $(BUILD_ARTIFACTS_PATH)
......
...@@ -49,7 +49,7 @@ if they diverge from the default credentials (user:'admin' and pw:'arista').`, ...@@ -49,7 +49,7 @@ if they diverge from the default credentials (user:'admin' and pw:'arista').`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
spinner, _ := pterm.DefaultSpinner.Start("Creating new network element") spinner, _ := pterm.DefaultSpinner.Start("Creating new network element")
err := checkIPPort(address) err := checkIPorHostnameAndPort(address)
if err != nil { if err != nil {
spinner.Fail(err) spinner.Fail(err)
return return
......
...@@ -41,20 +41,30 @@ import ( ...@@ -41,20 +41,30 @@ import (
gpb "github.com/openconfig/gnmi/proto/gnmi" gpb "github.com/openconfig/gnmi/proto/gnmi"
gnmiv "github.com/openconfig/gnmi/value" gnmiv "github.com/openconfig/gnmi/value"
"github.com/openconfig/goyang/pkg/yang" "github.com/openconfig/goyang/pkg/yang"
"golang.org/x/net/idna"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
) )
func checkIPPort(string) error { func checkIPorHostnameAndPort(address string) error {
// check if address is in the format <IP>:<port> // check if address is in the format <IP>:<port>
ip, _, err := net.SplitHostPort(address) ipOrHostname, _, err := net.SplitHostPort(address)
if err != nil { if err != nil || ipOrHostname == "" {
return err errorMessage := fmt.Sprintf("address '%s' is not in the format <IP/Hostname>:<port>", address)
return errors.New(errorMessage)
} }
// Check IP
ip2 := net.ParseIP(ip) // If not IP, check if valid Hostname
if ip2 == nil { _, err = idna.Lookup.ToASCII(ipOrHostname)
return errors.New("invalid IP") if err != nil {
// Check if valid ipIP
ip2 := net.ParseIP(ipOrHostname)
if ip2 == nil {
errorMessage := fmt.Sprintf("address '%s' is neither a valid IP nor a hostname", ipOrHostname)
return errors.New(errorMessage)
}
} }
return nil return nil
} }
......
package cmd
import (
"testing"
)
func TestCheckIPorHostnameAndPort(t *testing.T) {
// Test cases for valid IP addresses or hostnames with ports
validCases := []string{
"127.0.0.1:8080",
"localhost:8080",
"example.com:8080",
"[::1]:8080",
"[2001:db8::1]:1234",
}
for _, addr := range validCases {
err := checkIPorHostnameAndPort(addr)
if err != nil {
t.Errorf("Expected no error for address %s, but got: %v", addr, err)
}
}
}
func TestCheckIPorHostnameAndPortInvalid(t *testing.T) {
// Test cases for invalid IP addresses or hostnames with ports
invalidCases := []string{
":8080", // Missing IP address or hostname
"example.com", // Missing port
}
for _, addr := range invalidCases {
err := checkIPorHostnameAndPort(addr)
if err == nil {
t.Errorf("Expected error for address %s, but got no error", addr)
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment