mirror of
https://github.com/logos-messaging/logos-messaging-go.git
synced 2026-01-05 23:43:05 +00:00
chore: utils tests (#661)
* chore: add tests for utils * chore:delete unused code * fix: ipv6 validation issue
This commit is contained in:
parent
54c02213cd
commit
bc06867cc9
@ -5,6 +5,8 @@ import (
|
|||||||
"strings"
|
"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) {
|
func DecodeHexString(input string) ([]byte, error) {
|
||||||
input = strings.TrimPrefix(input, "0x")
|
input = strings.TrimPrefix(input, "0x")
|
||||||
return hex.DecodeString(input)
|
return hex.DecodeString(input)
|
||||||
|
|||||||
27
waku/v2/utils/hex_test.go
Normal file
27
waku/v2/utils/hex_test.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
@ -5,11 +5,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// IsIPv4 validates if string is a valid IPV4 address
|
||||||
func IsIPv4(str string) bool {
|
func IsIPv4(str string) bool {
|
||||||
ip := net.ParseIP(str)
|
ip := net.ParseIP(str).To4()
|
||||||
return ip != nil && !strings.Contains(str, ":")
|
return ip != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsIPv6 validates if string is a valid IPV6 address
|
||||||
func IsIPv6(str string) bool {
|
func IsIPv6(str string) bool {
|
||||||
ip := net.ParseIP(str)
|
ip := net.ParseIP(str)
|
||||||
return ip != nil && strings.Contains(str, ":")
|
return ip != nil && strings.Contains(str, ":")
|
||||||
|
|||||||
20
waku/v2/utils/ip_test.go
Normal file
20
waku/v2/utils/ip_test.go
Normal file
@ -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)
|
||||||
|
|
||||||
|
}
|
||||||
@ -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
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user