-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_test.go
More file actions
200 lines (192 loc) · 4.58 KB
/
setup_test.go
File metadata and controls
200 lines (192 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (c) 2025-2026. TxnLab Inc.
* All Rights reserved.
*/
package main
import (
"strings"
"testing"
"github.com/coredns/caddy"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNfdParse(t *testing.T) {
tests := []struct {
name string
input string
expectError bool
errorContains string
validate func(t *testing.T, cfg *nfdPluginConfig)
}{
{
name: "valid minimal config",
input: `nfd {
node https://mainnet-api.4160.nodely.dev
}`,
expectError: false,
validate: func(t *testing.T, cfg *nfdPluginConfig) {
assert.Equal(t, "https://mainnet-api.4160.nodely.dev", cfg.nodeUrl)
// Defaults
assert.Equal(t, uint64(defRegId), cfg.registryID)
assert.Equal(t, defAlgoXyzIp, cfg.algoXyzIp)
assert.Equal(t, defCacheMinutes, cfg.cacheMins)
},
},
{
name: "valid full config",
input: `nfd {
node https://mainnet-api.4160.nodely.dev
token mytoken123
registryid 12345678
algoxyzip 192.168.1.100
cachemins 10
}`,
expectError: false,
validate: func(t *testing.T, cfg *nfdPluginConfig) {
assert.Equal(t, "https://mainnet-api.4160.nodely.dev", cfg.nodeUrl)
assert.Equal(t, "mytoken123", cfg.token)
assert.Equal(t, uint64(12345678), cfg.registryID)
assert.Equal(t, "192.168.1.100", cfg.algoXyzIp)
assert.Equal(t, 10, cfg.cacheMins)
},
},
{
name: "missing node",
input: `nfd {
}`,
expectError: true,
errorContains: "no node",
},
{
name: "node with no value",
input: `nfd {
node
}`,
expectError: true,
errorContains: "invalid node; no value",
},
{
name: "node with multiple values",
input: `nfd {
node https://example1.com https://example2.com
}`,
expectError: true,
errorContains: "invalid node; multiple values",
},
{
name: "token with no value",
input: `nfd {
node https://mainnet-api.4160.nodely.dev
token
}`,
expectError: true,
errorContains: "invalid token; no value",
},
{
name: "token with multiple values",
input: `nfd {
node https://mainnet-api.4160.nodely.dev
token tok1 tok2
}`,
expectError: true,
errorContains: "invalid token; multiple values",
},
{
name: "invalid algoxyzip - not IPv4",
input: `nfd {
node https://mainnet-api.4160.nodely.dev
algoxyzip 2001:db8::1
}`,
expectError: true,
errorContains: "not a valid IPv4 address",
},
{
name: "invalid algoxyzip - not an IP",
input: `nfd {
node https://mainnet-api.4160.nodely.dev
algoxyzip notanip
}`,
expectError: true,
errorContains: "not a valid IPv4 address",
},
{
name: "algoxyzip with no value",
input: `nfd {
node https://mainnet-api.4160.nodely.dev
algoxyzip
}`,
expectError: true,
errorContains: "invalid algoxyzip; no value",
},
{
name: "invalid registryid - not a number",
input: `nfd {
node https://mainnet-api.4160.nodely.dev
registryid notanumber
}`,
expectError: true,
errorContains: "invalid integer value for registry id",
},
{
name: "registryid with no value",
input: `nfd {
node https://mainnet-api.4160.nodely.dev
registryid
}`,
expectError: true,
errorContains: "invalid registryid; no value",
},
{
name: "invalid cachemins - not a number",
input: `nfd {
node https://mainnet-api.4160.nodely.dev
cachemins notanumber
}`,
expectError: true,
errorContains: "invalid integer value for cache minutes",
},
{
name: "cachemins with no value",
input: `nfd {
node https://mainnet-api.4160.nodely.dev
cachemins
}`,
expectError: true,
errorContains: "invalid cachemins; no value",
},
{
name: "unknown directive",
input: `nfd {
node https://mainnet-api.4160.nodely.dev
unknowndirective somevalue
}`,
expectError: true,
errorContains: "unknown value",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := caddy.NewTestController("dns", tt.input)
cfg, err := nfdParse(c)
if tt.expectError {
require.Error(t, err)
if tt.errorContains != "" {
assert.True(t, strings.Contains(err.Error(), tt.errorContains),
"expected error containing %q, got %q", tt.errorContains, err.Error())
}
return
}
require.NoError(t, err)
require.NotNil(t, cfg)
if tt.validate != nil {
tt.validate(t, cfg)
}
})
}
}
func TestDefaultValues(t *testing.T) {
// Verify default constants
assert.Equal(t, uint64(760937186), uint64(defRegId))
assert.Equal(t, "34.8.101.7", defAlgoXyzIp)
assert.Equal(t, 5, defCacheMinutes)
}