Skip to content

Commit

Permalink
Network Validation Test Cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkasun committed May 1, 2021
1 parent c533bf7 commit 0e1a5f5
Show file tree
Hide file tree
Showing 9 changed files with 742 additions and 507 deletions.
148 changes: 74 additions & 74 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"net"
"time"

"github.com/go-playground/validator/v10"
"github.com/gravitl/netmaker/functions"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/mongoconn"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"golang.org/x/crypto/bcrypt"
"gopkg.in/go-playground/validator.v9"
)

func GetPeersList(networkName string) ([]models.PeersResponse, error) {
Expand Down Expand Up @@ -66,11 +66,11 @@ func ValidateNodeCreate(networkName string, node models.Node) error {
empty := node.Address == ""
return (empty || isIpv4)
})
_ = v.RegisterValidation("address6_check", func(fl validator.FieldLevel) bool {
isIpv6 := functions.IsIpNet(node.Address6)
empty := node.Address6 == ""
return (empty || isIpv6)
})
_ = v.RegisterValidation("address6_check", func(fl validator.FieldLevel) bool {
isIpv6 := functions.IsIpNet(node.Address6)
empty := node.Address6 == ""
return (empty || isIpv6)
})
_ = v.RegisterValidation("endpoint_check", func(fl validator.FieldLevel) bool {
//var isFieldUnique bool = functions.IsFieldUnique(networkName, "endpoint", node.Endpoint)
isIp := functions.IsIpNet(node.Endpoint)
Expand Down Expand Up @@ -126,66 +126,66 @@ func ValidateNodeCreate(networkName string, node models.Node) error {

func ValidateNodeUpdate(networkName string, node models.Node) error {

v := validator.New()
_ = v.RegisterValidation("address_check", func(fl validator.FieldLevel) bool {
isIpv4 := functions.IsIpNet(node.Address)
empty := node.Address == ""
return (empty || isIpv4)
})
_ = v.RegisterValidation("address6_check", func(fl validator.FieldLevel) bool {
isIpv6 := functions.IsIpNet(node.Address6)
empty := node.Address6 == ""
return (empty || isIpv6)
})
_ = v.RegisterValidation("endpoint_check", func(fl validator.FieldLevel) bool {
//var isFieldUnique bool = functions.IsFieldUnique(networkName, "endpoint", node.Endpoint)
isIp := functions.IsIpNet(node.Address)
v := validator.New()
_ = v.RegisterValidation("address_check", func(fl validator.FieldLevel) bool {
isIpv4 := functions.IsIpNet(node.Address)
empty := node.Address == ""
return (empty || isIpv4)
})
_ = v.RegisterValidation("address6_check", func(fl validator.FieldLevel) bool {
isIpv6 := functions.IsIpNet(node.Address6)
empty := node.Address6 == ""
return (empty || isIpv6)
})
_ = v.RegisterValidation("endpoint_check", func(fl validator.FieldLevel) bool {
//var isFieldUnique bool = functions.IsFieldUnique(networkName, "endpoint", node.Endpoint)
isIp := functions.IsIpNet(node.Address)
empty := node.Endpoint == ""
return (empty || isIp)
})
_ = v.RegisterValidation("localaddress_check", func(fl validator.FieldLevel) bool {
//var isFieldUnique bool = functions.IsFieldUnique(networkName, "endpoint", node.Endpoint)
isIp := functions.IsIpNet(node.LocalAddress)
empty := node.LocalAddress == ""
return (empty || isIp )
})
_ = v.RegisterValidation("macaddress_unique", func(fl validator.FieldLevel) bool {
return true
})

_ = v.RegisterValidation("macaddress_valid", func(fl validator.FieldLevel) bool {
_, err := net.ParseMAC(node.MacAddress)
return err == nil
})

_ = v.RegisterValidation("name_valid", func(fl validator.FieldLevel) bool {
isvalid := functions.NameInNodeCharSet(node.Name)
return isvalid
})

_ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
_, err := node.GetNetwork()
return err == nil
})
_ = v.RegisterValidation("pubkey_check", func(fl validator.FieldLevel) bool {
empty := node.PublicKey == ""
isBase64 := functions.IsBase64(node.PublicKey)
return (empty || isBase64)
})
_ = v.RegisterValidation("password_check", func(fl validator.FieldLevel) bool {
empty := node.Password == ""
goodLength := len(node.Password) > 5
return (empty || goodLength)
})

err := v.Struct(node)

if err != nil {
for _, e := range err.(validator.ValidationErrors) {
fmt.Println(e)
}
}
return err
return (empty || isIp)
})
_ = v.RegisterValidation("localaddress_check", func(fl validator.FieldLevel) bool {
//var isFieldUnique bool = functions.IsFieldUnique(networkName, "endpoint", node.Endpoint)
isIp := functions.IsIpNet(node.LocalAddress)
empty := node.LocalAddress == ""
return (empty || isIp)
})
_ = v.RegisterValidation("macaddress_unique", func(fl validator.FieldLevel) bool {
return true
})

_ = v.RegisterValidation("macaddress_valid", func(fl validator.FieldLevel) bool {
_, err := net.ParseMAC(node.MacAddress)
return err == nil
})

_ = v.RegisterValidation("name_valid", func(fl validator.FieldLevel) bool {
isvalid := functions.NameInNodeCharSet(node.Name)
return isvalid
})

_ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
_, err := node.GetNetwork()
return err == nil
})
_ = v.RegisterValidation("pubkey_check", func(fl validator.FieldLevel) bool {
empty := node.PublicKey == ""
isBase64 := functions.IsBase64(node.PublicKey)
return (empty || isBase64)
})
_ = v.RegisterValidation("password_check", func(fl validator.FieldLevel) bool {
empty := node.Password == ""
goodLength := len(node.Password) > 5
return (empty || goodLength)
})

err := v.Struct(node)

if err != nil {
for _, e := range err.(validator.ValidationErrors) {
fmt.Println(e)
}
}
return err
}

func UpdateNode(nodechange models.Node, node models.Node) (models.Node, error) {
Expand All @@ -199,10 +199,10 @@ func UpdateNode(nodechange models.Node, node models.Node) (models.Node, error) {
node.Address = nodechange.Address
notifynetwork = true
}
if nodechange.Address6 != "" {
node.Address6 = nodechange.Address6
notifynetwork = true
}
if nodechange.Address6 != "" {
node.Address6 = nodechange.Address6
notifynetwork = true
}
if nodechange.Name != "" {
node.Name = nodechange.Name
}
Expand Down Expand Up @@ -379,15 +379,15 @@ func CreateNode(node models.Node, networkName string) (models.Node, error) {
if err != nil {
return node, err
}
fmt.Println("Setting node address: " + node.Address)
fmt.Println("Setting node address: " + node.Address)

node.Address6, err = functions.UniqueAddress6(networkName)
if node.Address6 != "" {
node.Address6, err = functions.UniqueAddress6(networkName)
if node.Address6 != "" {
fmt.Println("Setting node ipv6 address: " + node.Address6)
}
if err != nil {
return node, err
}
return node, err
}

//IDK why these aren't a part of "set defaults. Pretty dumb.
//TODO: This is dumb. Consolidate and fix.
Expand Down
Loading

0 comments on commit 0e1a5f5

Please sign in to comment.