2023-11-05 19:03:58 +01:00
|
|
|
package conf
|
|
|
|
|
2023-12-22 22:49:47 +01:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
2023-11-05 19:03:58 +01:00
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
func getExampleConfiguration() *DaemonConfiguration {
|
2023-12-22 22:49:47 +01:00
|
|
|
discovery := PUBLIC_IP_DISCOVERY
|
|
|
|
advertiseRoutes := false
|
|
|
|
advertiseDefaultRoute := false
|
|
|
|
endpoint := "abc.com:123"
|
|
|
|
nodeType := CLIENT_ROLE
|
|
|
|
keepAliveWg := 0
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
return &DaemonConfiguration{
|
2023-12-22 22:49:47 +01:00
|
|
|
CertificatePath: "../../../cert/cert.pem",
|
|
|
|
PrivateKeyPath: "../../../cert/priv.pem",
|
|
|
|
CaCertificatePath: "../../../cert/cacert.pem",
|
2023-11-05 19:03:58 +01:00
|
|
|
SkipCertVerification: true,
|
2023-12-22 22:49:47 +01:00
|
|
|
GrpcPort: 25,
|
|
|
|
Timeout: 5,
|
|
|
|
StubWg: false,
|
2024-01-05 13:59:13 +01:00
|
|
|
SyncInterval: 2,
|
|
|
|
Heartbeat: 2,
|
2023-12-22 22:49:47 +01:00
|
|
|
ClusterSize: 64,
|
|
|
|
InterClusterChance: 0.15,
|
2024-01-05 13:59:13 +01:00
|
|
|
Branch: 3,
|
|
|
|
PullInterval: 0,
|
2023-12-22 22:49:47 +01:00
|
|
|
InfectionCount: 2,
|
|
|
|
BaseConfiguration: WgConfiguration{
|
|
|
|
IPDiscovery: &discovery,
|
|
|
|
AdvertiseRoutes: &advertiseRoutes,
|
|
|
|
AdvertiseDefaultRoute: &advertiseDefaultRoute,
|
|
|
|
Endpoint: &endpoint,
|
|
|
|
Role: &nodeType,
|
|
|
|
KeepAliveWg: &keepAliveWg,
|
|
|
|
},
|
2023-11-05 19:03:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurationCertificatePathEmpty(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
|
|
|
conf.CertificatePath = ""
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
err := ValidateDaemonConfiguration(conf)
|
2023-11-05 19:03:58 +01:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurationPrivateKeyPathEmpty(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
|
|
|
conf.PrivateKeyPath = ""
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
err := ValidateDaemonConfiguration(conf)
|
2023-11-05 19:03:58 +01:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurationCaCertificatePathEmpty(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
|
|
|
conf.CaCertificatePath = ""
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
err := ValidateDaemonConfiguration(conf)
|
2023-11-05 19:03:58 +01:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurationGrpcPortEmpty(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
2023-12-10 20:21:54 +01:00
|
|
|
conf.GrpcPort = 0
|
2023-11-05 19:03:58 +01:00
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
err := ValidateDaemonConfiguration(conf)
|
2023-11-05 19:03:58 +01:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-22 22:49:47 +01:00
|
|
|
func TestIPDiscoveryNotSet(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
|
|
|
ipDiscovery := IPDiscovery("djdsjdskd")
|
|
|
|
conf.BaseConfiguration.IPDiscovery = &ipDiscovery
|
|
|
|
|
|
|
|
err := ValidateDaemonConfiguration(conf)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdvertiseRoutesNotSet(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
|
|
|
conf.BaseConfiguration.AdvertiseRoutes = nil
|
|
|
|
|
|
|
|
err := ValidateDaemonConfiguration(conf)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdvertiseDefaultRouteNotSet(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
|
|
|
conf.BaseConfiguration.AdvertiseDefaultRoute = nil
|
|
|
|
|
|
|
|
err := ValidateDaemonConfiguration(conf)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeepAliveWgNegative(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
|
|
|
keepAliveWg := -1
|
|
|
|
conf.BaseConfiguration.KeepAliveWg = &keepAliveWg
|
|
|
|
|
|
|
|
err := ValidateDaemonConfiguration(conf)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoleTypeNotValid(t *testing.T) {
|
2023-11-05 19:03:58 +01:00
|
|
|
conf := getExampleConfiguration()
|
2023-12-22 22:49:47 +01:00
|
|
|
role := NodeType("bruhhh")
|
|
|
|
conf.BaseConfiguration.Role = &role
|
2023-11-05 19:03:58 +01:00
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
err := ValidateDaemonConfiguration(conf)
|
2023-11-05 19:03:58 +01:00
|
|
|
|
2023-12-22 22:49:47 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoleTypeNotSpecified(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
|
|
|
conf.BaseConfiguration.Role = nil
|
|
|
|
|
|
|
|
err := ValidateDaemonConfiguration(conf)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`invalid role type`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBranchRateZero(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
2024-01-05 13:59:13 +01:00
|
|
|
conf.Branch = 0
|
2023-12-22 22:49:47 +01:00
|
|
|
|
|
|
|
err := ValidateDaemonConfiguration(conf)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-31 13:44:50 +01:00
|
|
|
func TestsyncTimeZero(t *testing.T) {
|
2023-12-22 22:49:47 +01:00
|
|
|
conf := getExampleConfiguration()
|
2024-01-05 13:59:13 +01:00
|
|
|
conf.SyncInterval = 0
|
2023-12-22 22:49:47 +01:00
|
|
|
|
|
|
|
err := ValidateDaemonConfiguration(conf)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeepAliveTimeZero(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
2024-01-05 13:59:13 +01:00
|
|
|
conf.Heartbeat = 0
|
2023-12-22 22:49:47 +01:00
|
|
|
err := ValidateDaemonConfiguration(conf)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClusterSizeZero(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
|
|
|
conf.ClusterSize = 0
|
|
|
|
err := ValidateDaemonConfiguration(conf)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInterClusterChanceZero(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
|
|
|
conf.InterClusterChance = 0
|
|
|
|
|
|
|
|
err := ValidateDaemonConfiguration(conf)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInfectionCountOne(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
|
|
|
conf.InfectionCount = 0
|
|
|
|
|
|
|
|
err := ValidateDaemonConfiguration(conf)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-31 13:17:16 +01:00
|
|
|
func TestPullTimeNegative(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
2024-01-05 13:59:13 +01:00
|
|
|
conf.PullInterval = -1
|
2023-12-31 13:17:16 +01:00
|
|
|
|
|
|
|
err := ValidateDaemonConfiguration(conf)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(`error should be thrown`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-22 22:49:47 +01:00
|
|
|
func TestValidConfiguration(t *testing.T) {
|
|
|
|
conf := getExampleConfiguration()
|
|
|
|
err := ValidateDaemonConfiguration(conf)
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|