From bc06867cc9d9a626c976cf59e0bce5ec31a2e376 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Tue, 22 Aug 2023 19:18:43 +0530 Subject: [PATCH] chore: utils tests (#661) * chore: add tests for utils * chore:delete unused code * fix: ipv6 validation issue --- waku/v2/utils/hex.go | 2 ++ waku/v2/utils/hex_test.go | 27 +++++++++++++++++++++++++++ waku/v2/utils/ip.go | 6 ++++-- waku/v2/utils/ip_test.go | 20 ++++++++++++++++++++ waku/v2/utils/ntp.go | 38 -------------------------------------- 5 files changed, 53 insertions(+), 40 deletions(-) create mode 100644 waku/v2/utils/hex_test.go create mode 100644 waku/v2/utils/ip_test.go delete mode 100644 waku/v2/utils/ntp.go diff --git a/waku/v2/utils/hex.go b/waku/v2/utils/hex.go index cdc767a2..ad467731 100644 --- a/waku/v2/utils/hex.go +++ b/waku/v2/utils/hex.go @@ -5,6 +5,8 @@ import ( "strings" ) +// DecodeHexString decodes input string into a hex string. +// Note that if the string is prefixed by 0x , it is trimmed func DecodeHexString(input string) ([]byte, error) { input = strings.TrimPrefix(input, "0x") return hex.DecodeString(input) diff --git a/waku/v2/utils/hex_test.go b/waku/v2/utils/hex_test.go new file mode 100644 index 00000000..b56a9ce8 --- /dev/null +++ b/waku/v2/utils/hex_test.go @@ -0,0 +1,27 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestHexDecoding(t *testing.T) { + const s = "0x48656c6c6f20476f7068657221" + decodedString, err := DecodeHexString(s) + require.NoError(t, err) + require.Equal(t, decodedString, []byte("Hello Gopher!")) + + const s1 = "48656c6c6f20476f7068657221" + _, err = DecodeHexString(s1) + require.NoError(t, err) + require.Equal(t, decodedString, []byte("Hello Gopher!")) + + const s2 = "jk" + _, err = DecodeHexString(s2) + require.Error(t, err) + + const s3 = "48656c6c6f20476f706865722" + _, err = DecodeHexString(s3) + require.Error(t, err) +} diff --git a/waku/v2/utils/ip.go b/waku/v2/utils/ip.go index d74473e8..90685de3 100644 --- a/waku/v2/utils/ip.go +++ b/waku/v2/utils/ip.go @@ -5,11 +5,13 @@ import ( "strings" ) +// IsIPv4 validates if string is a valid IPV4 address func IsIPv4(str string) bool { - ip := net.ParseIP(str) - return ip != nil && !strings.Contains(str, ":") + ip := net.ParseIP(str).To4() + return ip != nil } +// IsIPv6 validates if string is a valid IPV6 address func IsIPv6(str string) bool { ip := net.ParseIP(str) return ip != nil && strings.Contains(str, ":") diff --git a/waku/v2/utils/ip_test.go b/waku/v2/utils/ip_test.go new file mode 100644 index 00000000..474ffcf0 --- /dev/null +++ b/waku/v2/utils/ip_test.go @@ -0,0 +1,20 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIPValidation(t *testing.T) { + require.Equal(t, IsIPv4("127.0.0.1"), true) + require.Equal(t, IsIPv4("abcd"), false) + require.Equal(t, IsIPv4("::1"), false) + require.Equal(t, IsIPv4("1000.40.210.253"), false) + + require.Equal(t, IsIPv6("::1"), true) + require.Equal(t, IsIPv6("fe80::6c4a:6aff:fecf:8097"), true) + require.Equal(t, IsIPv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334:3445"), false) + require.Equal(t, IsIPv6("127.0.0.1"), false) + +} diff --git a/waku/v2/utils/ntp.go b/waku/v2/utils/ntp.go deleted file mode 100644 index 4033094a..00000000 --- a/waku/v2/utils/ntp.go +++ /dev/null @@ -1,38 +0,0 @@ -package utils - -import ( - "time" - - "github.com/beevik/ntp" -) - -var NTPServer = "pool.ntp.org" - -func GetNTPTime() (time.Time, error) { - t, err := ntp.Time(NTPServer) - if err != nil { - return t, err - } - - return t, nil -} - -func GetNTPMetadata() (*ntp.Response, error) { - options := ntp.QueryOptions{Timeout: 60 * time.Second, TTL: 10} - response, err := ntp.QueryWithOptions(NTPServer, options) - if err != nil { - return nil, err - } - - return response, nil -} - -func GetTimeOffset() (time.Duration, error) { - options := ntp.QueryOptions{Timeout: 60 * time.Second, TTL: 10} - response, err := ntp.QueryWithOptions(NTPServer, options) - if err != nil { - return 0, err - } - - return response.ClockOffset, nil -}