Archived
2
0
This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
kerma/helpers/tests/validator_test.go

92 lines
3.6 KiB
Go
Raw Permalink Normal View History

2023-03-02 14:28:43 +00:00
package tests
import (
"io/ioutil"
"kerma/helpers"
"os"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestValidator(t *testing.T) {
Convey("Given a new validator", t, func() {
var validator helpers.Validator
validator.Construct()
Convey("When the IsValidFQDN method is called", func() {
Convey("When the input is an invalid FQDN, 'false' should be returned", func() {
So(validator.IsValidFQDN("!!!$"), ShouldEqual, false)
})
Convey("When the input is a valid FQDN, 'true' should be returned", func() {
So(validator.IsValidFQDN("example.com"), ShouldEqual, true)
So(validator.IsValidFQDN("sub.example.com"), ShouldEqual, true)
So(validator.IsValidFQDN("sub1.example.com"), ShouldEqual, true)
})
})
Convey("When the IsValidIP method is called", func() {
Convey("When the input is an invalid IP, 'false' should be returned", func() {
So(validator.IsValidIP("!!!$"), ShouldEqual, false)
So(validator.IsValidIP("example.com"), ShouldEqual, false)
So(validator.IsValidIP("127.0.0.1"), ShouldEqual, false)
So(validator.IsValidIP(validator.Config.IPAddress), ShouldEqual, false)
})
Convey("When the input is a valid FQDN, 'true' should be returned", func() {
So(validator.IsValidIP("1.1.1.1"), ShouldEqual, true)
So(validator.IsValidIP("172.16.2.1"), ShouldEqual, true)
So(validator.IsValidIP("192.168.150.2"), ShouldEqual, true)
So(validator.IsValidIP("10.11.231.2"), ShouldEqual, true)
So(validator.IsValidIP("254.254.252.251"), ShouldEqual, true)
})
})
Convey("When the IsValidPort method is called", func() {
Convey("When the input is an invalid port, 'false' should be returned", func() {
So(validator.IsValidPort("-1"), ShouldEqual, false)
So(validator.IsValidPort("123456"), ShouldEqual, false)
So(validator.IsValidPort("0"), ShouldEqual, false)
So(validator.IsValidPort("example.com"), ShouldEqual, false)
})
Convey("When the input is a valid port, 'true' should be returned", func() {
So(validator.IsValidPort("18018"), ShouldEqual, true)
So(validator.IsValidPort("65535"), ShouldEqual, true)
})
})
Convey("When the IsValidPeerName method is called", func() {
Convey("When the input is an invalid peer name, 'false' should be returned", func() {
So(validator.IsValidPeerName("192.168.150.2:-1"), ShouldEqual, false)
So(validator.IsValidPeerName("172.16.2.1:123456"), ShouldEqual, false)
So(validator.IsValidPeerName("127.0.0.1:18018"), ShouldEqual, false)
So(validator.IsValidPeerName("example.com:111111"), ShouldEqual, false)
})
Convey("When the input is a valid peer name, 'true' should be returned", func() {
So(validator.IsValidPeerName("example.com:18018"), ShouldEqual, true)
So(validator.IsValidPeerName("sub.example.com:11111"), ShouldEqual, true)
So(validator.IsValidPeerName("172.16.2.1:18018"), ShouldEqual, true)
So(validator.IsValidPeerName("192.168.150.2:18018"), ShouldEqual, true)
So(validator.IsValidPeerName("10.11.231.2:18018"), ShouldEqual, true)
So(validator.IsValidPeerName("254.254.252.251:18018"), ShouldEqual, true)
})
})
Convey("When the CheckIfFileExists method is called", func() {
Convey("When the input is a non-existent file, 'false' should be returned", func() {
So(validator.CheckIfFileExists("nonexisting.test"), ShouldEqual, false)
})
Convey("When the input is an existing file, 'true' should be returned", func() {
buff := make([]byte, 100)
ioutil.WriteFile("existing.test", buff, 0666)
So(validator.CheckIfFileExists("existing.test"), ShouldEqual, true)
os.Remove("existing.test")
})
})
})
}