| 1 |
package conf |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"reflect" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
) |
| 14 |
|
| 15 |
// Test to make sure we get what we expect. |
| 16 |
|
| 17 |
func test(t *testing.T, data string, ex map[string]any) { |
| 18 |
t.Helper() |
| 19 |
m, err := Parse(data) |
| 20 |
if err != nil { |
| 21 |
t.Fatalf("Received err: %v\n", err) |
| 22 |
} |
| 23 |
if m == nil { |
| 24 |
t.Fatal("Received nil map") |
| 25 |
} |
| 26 |
|
| 27 |
if !reflect.DeepEqual(m, ex) { |
| 28 |
t.Fatalf("Not Equal:\nReceived: '%+v'\nExpected: '%+v'\n", m, ex) |
| 29 |
} |
| 30 |
|
| 31 |
// Also test ParseWithChecks |
| 32 |
n, err := ParseWithChecks(data) |
| 33 |
if err != nil { |
| 34 |
t.Fatalf("Received err: %v\n", err) |
| 35 |
} |
| 36 |
if n == nil { |
| 37 |
t.Fatal("Received nil map") |
| 38 |
} |
| 39 |
// Compare with the original results. |
| 40 |
a, err := json.Marshal(m) |
| 41 |
if err != nil { |
| 42 |
t.Fatal(err) |
| 43 |
} |
| 44 |
b, err := json.Marshal(n) |
| 45 |
if err != nil { |
| 46 |
t.Fatal(err) |
| 47 |
} |
| 48 |
if !bytes.Equal(b, a) { |
| 49 |
t.Fatalf("Not Equal:\nReceived: '%+v'\nExpected: '%+v'\n", string(a), string(b)) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestSimpleTopLevel(t *testing.T) { |
| 54 |
ex := map[string]any{ |
| 55 |
"foo": "1", |
| 56 |
"bar": float64(2.2), |
| 57 |
"baz": true, |
| 58 |
"boo": int64(22), |
| 59 |
} |
| 60 |
test(t, "foo='1'; bar=2.2; baz=true; boo=22", ex) |
| 61 |
} |
| 62 |
|
| 63 |
func TestBools(t *testing.T) { |
| 64 |
ex := map[string]any{ |
| 65 |
"foo": true, |
| 66 |
} |
| 67 |
test(t, "foo=true", ex) |
| 68 |
test(t, "foo=TRUE", ex) |
| 69 |
test(t, "foo=true", ex) |
| 70 |
test(t, "foo=yes", ex) |
| 71 |
test(t, "foo=on", ex) |
| 72 |
} |
| 73 |
|
| 74 |
var varSample = ` |
| 75 |
index = 22 |
| 76 |
foo = $index |
| 77 |
` |
| 78 |
|
| 79 |
func TestSimpleVariable(t *testing.T) { |
| 80 |
ex := map[string]any{ |
| 81 |
"index": int64(22), |
| 82 |
"foo": int64(22), |
| 83 |
} |
| 84 |
test(t, varSample, ex) |
| 85 |
} |
| 86 |
|
| 87 |
var varNestedSample = ` |
| 88 |
index = 22 |
| 89 |
nest { |
| 90 |
index = 11 |
| 91 |
foo = $index |
| 92 |
} |
| 93 |
bar = $index |
| 94 |
` |
| 95 |
|
| 96 |
func TestNestedVariable(t *testing.T) { |
| 97 |
ex := map[string]any{ |
| 98 |
"index": int64(22), |
| 99 |
"nest": map[string]any{ |
| 100 |
"index": int64(11), |
| 101 |
"foo": int64(11), |
| 102 |
}, |
| 103 |
"bar": int64(22), |
| 104 |
} |
| 105 |
test(t, varNestedSample, ex) |
| 106 |
} |
| 107 |
|
| 108 |
func TestMissingVariable(t *testing.T) { |
| 109 |
_, err := Parse("foo=$index") |
| 110 |
if err == nil { |
| 111 |
t.Fatalf("Expected an error for a missing variable, got none") |
| 112 |
} |
| 113 |
if !strings.HasPrefix(err.Error(), "variable reference") { |
| 114 |
t.Fatalf("Wanted a variable reference err, got %q\n", err) |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func TestEnvVariable(t *testing.T) { |
| 119 |
ex := map[string]any{ |
| 120 |
"foo": int64(22), |
| 121 |
} |
| 122 |
evar := "__UNIQ22__" |
| 123 |
os.Setenv(evar, "22") |
| 124 |
defer os.Unsetenv(evar) |
| 125 |
test(t, fmt.Sprintf("foo = $%s", evar), ex) |
| 126 |
} |
| 127 |
|
| 128 |
func TestEnvVariableString(t *testing.T) { |
| 129 |
ex := map[string]any{ |
| 130 |
"foo": "xyz", |
| 131 |
} |
| 132 |
evar := "__UNIQ22__" |
| 133 |
os.Setenv(evar, "xyz") |
| 134 |
defer os.Unsetenv(evar) |
| 135 |
test(t, fmt.Sprintf("foo = $%s", evar), ex) |
| 136 |
} |
| 137 |
|
| 138 |
func TestEnvVariableStringStartingWithNumber(t *testing.T) { |
| 139 |
evar := "__UNIQ22__" |
| 140 |
os.Setenv(evar, "3xyz") |
| 141 |
defer os.Unsetenv(evar) |
| 142 |
|
| 143 |
_, err := Parse("foo = $%s") |
| 144 |
if err == nil { |
| 145 |
t.Fatalf("Expected err not being able to process string: %v\n", err) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestEnvVariableStringStartingWithNumberAndSizeUnit(t *testing.T) { |
| 150 |
ex := map[string]any{ |
| 151 |
"foo": "3Gyz", |
| 152 |
} |
| 153 |
evar := "__UNIQ22__" |
| 154 |
os.Setenv(evar, "3Gyz") |
| 155 |
defer os.Unsetenv(evar) |
| 156 |
test(t, fmt.Sprintf("foo = $%s", evar), ex) |
| 157 |
} |
| 158 |
|
| 159 |
func TestEnvVariableStringStartingWithNumberUsingQuotes(t *testing.T) { |
| 160 |
ex := map[string]any{ |
| 161 |
"foo": "3xyz", |
| 162 |
} |
| 163 |
evar := "__UNIQ22__" |
| 164 |
os.Setenv(evar, "'3xyz'") |
| 165 |
defer os.Unsetenv(evar) |
| 166 |
test(t, fmt.Sprintf("foo = $%s", evar), ex) |
| 167 |
} |
| 168 |
|
| 169 |
func TestBcryptVariable(t *testing.T) { |
| 170 |
ex := map[string]any{ |
| 171 |
"password": "$2a$11$ooo", |
| 172 |
} |
| 173 |
test(t, "password: $2a$11$ooo", ex) |
| 174 |
} |
| 175 |
|
| 176 |
var easynum = ` |
| 177 |
k = 8k |
| 178 |
kb = 4kb |
| 179 |
ki = 3ki |
| 180 |
kib = 4ki |
| 181 |
m = 1m |
| 182 |
mb = 2MB |
| 183 |
mi = 2Mi |
| 184 |
mib = 64MiB |
| 185 |
g = 2g |
| 186 |
gb = 22GB |
| 187 |
gi = 22Gi |
| 188 |
gib = 22GiB |
| 189 |
tb = 22TB |
| 190 |
ti = 22Ti |
| 191 |
tib = 22TiB |
| 192 |
pb = 22PB |
| 193 |
pi = 22Pi |
| 194 |
pib = 22PiB |
| 195 |
` |
| 196 |
|
| 197 |
func TestConvenientNumbers(t *testing.T) { |
| 198 |
ex := map[string]any{ |
| 199 |
"k": int64(8 * 1000), |
| 200 |
"kb": int64(4 * 1024), |
| 201 |
"ki": int64(3 * 1024), |
| 202 |
"kib": int64(4 * 1024), |
| 203 |
"m": int64(1000 * 1000), |
| 204 |
"mb": int64(2 * 1024 * 1024), |
| 205 |
"mi": int64(2 * 1024 * 1024), |
| 206 |
"mib": int64(64 * 1024 * 1024), |
| 207 |
"g": int64(2 * 1000 * 1000 * 1000), |
| 208 |
"gb": int64(22 * 1024 * 1024 * 1024), |
| 209 |
"gi": int64(22 * 1024 * 1024 * 1024), |
| 210 |
"gib": int64(22 * 1024 * 1024 * 1024), |
| 211 |
"tb": int64(22 * 1024 * 1024 * 1024 * 1024), |
| 212 |
"ti": int64(22 * 1024 * 1024 * 1024 * 1024), |
| 213 |
"tib": int64(22 * 1024 * 1024 * 1024 * 1024), |
| 214 |
"pb": int64(22 * 1024 * 1024 * 1024 * 1024 * 1024), |
| 215 |
"pi": int64(22 * 1024 * 1024 * 1024 * 1024 * 1024), |
| 216 |
"pib": int64(22 * 1024 * 1024 * 1024 * 1024 * 1024), |
| 217 |
} |
| 218 |
test(t, easynum, ex) |
| 219 |
} |
| 220 |
|
| 221 |
func TestParseFileWithChecksDigestPreservesConfigKeyUsedAsVariable(t *testing.T) { |
| 222 |
confFile := filepath.Join(t.TempDir(), "nats.conf") |
| 223 |
if err := os.WriteFile(confFile, []byte(` |
| 224 |
port = 4222 |
| 225 |
monitor_port = $port |
| 226 |
`), 0666); err != nil { |
| 227 |
t.Fatal(err) |
| 228 |
} |
| 229 |
|
| 230 |
m, _, err := ParseFileWithChecksDigest(confFile) |
| 231 |
if err != nil { |
| 232 |
t.Fatal(err) |
| 233 |
} |
| 234 |
if _, ok := m["port"]; !ok { |
| 235 |
t.Fatal("expected config key used as a variable reference to be preserved") |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
var sample1 = ` |
| 240 |
foo { |
| 241 |
host { |
| 242 |
ip = '127.0.0.1' |
| 243 |
port = 4242 |
| 244 |
} |
| 245 |
servers = [ "a.com", "b.com", "c.com"] |
| 246 |
} |
| 247 |
` |
| 248 |
|
| 249 |
func TestSample1(t *testing.T) { |
| 250 |
ex := map[string]any{ |
| 251 |
"foo": map[string]any{ |
| 252 |
"host": map[string]any{ |
| 253 |
"ip": "127.0.0.1", |
| 254 |
"port": int64(4242), |
| 255 |
}, |
| 256 |
"servers": []any{"a.com", "b.com", "c.com"}, |
| 257 |
}, |
| 258 |
} |
| 259 |
test(t, sample1, ex) |
| 260 |
} |
| 261 |
|
| 262 |
var cluster = ` |
| 263 |
cluster { |
| 264 |
port: 4244 |
| 265 |
|
| 266 |
authorization { |
| 267 |
user: route_user |
| 268 |
password: top_secret |
| 269 |
timeout: 1 |
| 270 |
} |
| 271 |
|
| 272 |
# Routes are actively solicited and connected to from this server. |
| 273 |
# Other servers can connect to us if they supply the correct credentials |
| 274 |
# in their routes definitions from above. |
| 275 |
|
| 276 |
// Test both styles of comments |
| 277 |
|
| 278 |
routes = [ |
| 279 |
nats-route://foo:[email protected]:4245 |
| 280 |
nats-route://foo:[email protected]:4246 |
| 281 |
] |
| 282 |
} |
| 283 |
` |
| 284 |
|
| 285 |
func TestSample2(t *testing.T) { |
| 286 |
ex := map[string]any{ |
| 287 |
"cluster": map[string]any{ |
| 288 |
"port": int64(4244), |
| 289 |
"authorization": map[string]any{ |
| 290 |
"user": "route_user", |
| 291 |
"password": "top_secret", |
| 292 |
"timeout": int64(1), |
| 293 |
}, |
| 294 |
"routes": []any{ |
| 295 |
"nats-route://foo:[email protected]:4245", |
| 296 |
"nats-route://foo:[email protected]:4246", |
| 297 |
}, |
| 298 |
}, |
| 299 |
} |
| 300 |
|
| 301 |
test(t, cluster, ex) |
| 302 |
} |
| 303 |
|
| 304 |
var sample3 = ` |
| 305 |
foo { |
| 306 |
expr = '(true == "false")' |
| 307 |
text = 'This is a multi-line |
| 308 |
text block.' |
| 309 |
} |
| 310 |
` |
| 311 |
|
| 312 |
func TestSample3(t *testing.T) { |
| 313 |
ex := map[string]any{ |
| 314 |
"foo": map[string]any{ |
| 315 |
"expr": "(true == \"false\")", |
| 316 |
"text": "This is a multi-line\ntext block.", |
| 317 |
}, |
| 318 |
} |
| 319 |
test(t, sample3, ex) |
| 320 |
} |
| 321 |
|
| 322 |
var sample4 = ` |
| 323 |
array [ |
| 324 |
{ abc: 123 } |
| 325 |
{ xyz: "word" } |
| 326 |
] |
| 327 |
` |
| 328 |
|
| 329 |
func TestSample4(t *testing.T) { |
| 330 |
ex := map[string]any{ |
| 331 |
"array": []any{ |
| 332 |
map[string]any{"abc": int64(123)}, |
| 333 |
map[string]any{"xyz": "word"}, |
| 334 |
}, |
| 335 |
} |
| 336 |
test(t, sample4, ex) |
| 337 |
} |
| 338 |
|
| 339 |
var sample5 = ` |
| 340 |
now = 2016-05-04T18:53:41Z |
| 341 |
gmt = false |
| 342 |
|
| 343 |
` |
| 344 |
|
| 345 |
func TestSample5(t *testing.T) { |
| 346 |
dt, _ := time.Parse("2006-01-02T15:04:05Z", "2016-05-04T18:53:41Z") |
| 347 |
ex := map[string]any{ |
| 348 |
"now": dt, |
| 349 |
"gmt": false, |
| 350 |
} |
| 351 |
test(t, sample5, ex) |
| 352 |
} |
| 353 |
|
| 354 |
func TestIncludes(t *testing.T) { |
| 355 |
ex := map[string]any{ |
| 356 |
"listen": "127.0.0.1:4222", |
| 357 |
"authorization": map[string]any{ |
| 358 |
"ALICE_PASS": "$2a$10$UHR6GhotWhpLsKtVP0/i6.Nh9.fuY73cWjLoJjb2sKT8KISBcUW5q", |
| 359 |
"BOB_PASS": "$2a$11$dZM98SpGeI7dCFFGSpt.JObQcix8YHml4TBUZoge9R1uxnMIln5ly", |
| 360 |
"users": []any{ |
| 361 |
map[string]any{ |
| 362 |
"user": "alice", |
| 363 |
"password": "$2a$10$UHR6GhotWhpLsKtVP0/i6.Nh9.fuY73cWjLoJjb2sKT8KISBcUW5q"}, |
| 364 |
map[string]any{ |
| 365 |
"user": "bob", |
| 366 |
"password": "$2a$11$dZM98SpGeI7dCFFGSpt.JObQcix8YHml4TBUZoge9R1uxnMIln5ly"}, |
| 367 |
}, |
| 368 |
"timeout": float64(0.5), |
| 369 |
}, |
| 370 |
} |
| 371 |
|
| 372 |
m, err := ParseFile("simple.conf") |
| 373 |
if err != nil { |
| 374 |
t.Fatalf("Received err: %v\n", err) |
| 375 |
} |
| 376 |
if m == nil { |
| 377 |
t.Fatal("Received nil map") |
| 378 |
} |
| 379 |
|
| 380 |
if !reflect.DeepEqual(m, ex) { |
| 381 |
t.Fatalf("Not Equal:\nReceived: '%+v'\nExpected: '%+v'\n", m, ex) |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
var varIncludedVariablesSample = ` |
| 386 |
authorization { |
| 387 |
|
| 388 |
include "./includes/passwords.conf" |
| 389 |
|
| 390 |
CAROL_PASS: foo |
| 391 |
|
| 392 |
users = [ |
| 393 |
{user: alice, password: $ALICE_PASS} |
| 394 |
{user: bob, password: $BOB_PASS} |
| 395 |
{user: carol, password: $CAROL_PASS} |
| 396 |
] |
| 397 |
} |
| 398 |
` |
| 399 |
|
| 400 |
func TestIncludeVariablesWithChecks(t *testing.T) { |
| 401 |
p, err := parse(varIncludedVariablesSample, "", true) |
| 402 |
if err != nil { |
| 403 |
t.Fatalf("Received err: %v\n", err) |
| 404 |
} |
| 405 |
key := "authorization" |
| 406 |
m, ok := p.mapping[key] |
| 407 |
if !ok { |
| 408 |
t.Errorf("Expected %q to be in the config", key) |
| 409 |
} |
| 410 |
expectKeyVal := func(t *testing.T, m any, expectedKey string, expectedVal string, expectedLine, expectedPos int) { |
| 411 |
t.Helper() |
| 412 |
tk := m.(*token) |
| 413 |
v := tk.Value() |
| 414 |
vv := v.(map[string]any) |
| 415 |
value, ok := vv[expectedKey] |
| 416 |
if !ok { |
| 417 |
t.Errorf("Expected key %q", expectedKey) |
| 418 |
} |
| 419 |
tk, ok = value.(*token) |
| 420 |
if !ok { |
| 421 |
t.Fatalf("Expected token %v", value) |
| 422 |
} |
| 423 |
if tk.Line() != expectedLine { |
| 424 |
t.Errorf("Expected token to be at line %d, got: %d", expectedLine, tk.Line()) |
| 425 |
} |
| 426 |
if tk.Position() != expectedPos { |
| 427 |
t.Errorf("Expected token to be at position %d, got: %d", expectedPos, tk.Position()) |
| 428 |
} |
| 429 |
v = tk.Value() |
| 430 |
if v != expectedVal { |
| 431 |
t.Errorf("Expected %q, got: %s", expectedVal, v) |
| 432 |
} |
| 433 |
} |
| 434 |
expectKeyVal(t, m, "ALICE_PASS", "$2a$10$UHR6GhotWhpLsKtVP0/i6.Nh9.fuY73cWjLoJjb2sKT8KISBcUW5q", 2, 1) |
| 435 |
expectKeyVal(t, m, "BOB_PASS", "$2a$11$dZM98SpGeI7dCFFGSpt.JObQcix8YHml4TBUZoge9R1uxnMIln5ly", 3, 1) |
| 436 |
expectKeyVal(t, m, "CAROL_PASS", "foo", 6, 3) |
| 437 |
} |
| 438 |
|
| 439 |
func TestParserNoInfiniteLoop(t *testing.T) { |
| 440 |
for _, test := range []string{`A@@Føøøø?˛ø:{øøøø˙˙`, `include "9/�`} { |
| 441 |
if _, err := Parse(test); err == nil { |
| 442 |
t.Fatal("expected an error") |
| 443 |
} else if !strings.Contains(err.Error(), "Unexpected EOF") { |
| 444 |
t.Fatal("expected unexpected eof error") |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
func TestParseWithNoValuesAreInvalid(t *testing.T) { |
| 450 |
for _, test := range []struct { |
| 451 |
name string |
| 452 |
conf string |
| 453 |
err string |
| 454 |
}{ |
| 455 |
{ |
| 456 |
"invalid key without values", |
| 457 |
`aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`, |
| 458 |
"config is invalid (:1:41)", |
| 459 |
}, |
| 460 |
{ |
| 461 |
"invalid untrimmed key without values", |
| 462 |
` aaaaaaaaaaaaaaaaaaaaaaaaaaa`, |
| 463 |
"config is invalid (:1:41)", |
| 464 |
}, |
| 465 |
{ |
| 466 |
"invalid untrimmed key without values", |
| 467 |
` aaaaaaaaaaaaaaaaaaaaaaaaaaa `, |
| 468 |
"config is invalid (:1:41)", |
| 469 |
}, |
| 470 |
{ |
| 471 |
"invalid keys after comments", |
| 472 |
` |
| 473 |
# with comments and no spaces to create key values |
| 474 |
# is also an invalid config. |
| 475 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| 476 |
`, |
| 477 |
"config is invalid (:5:25)", |
| 478 |
}, |
| 479 |
{ |
| 480 |
"comma separated without values are invalid", |
| 481 |
` |
| 482 |
a,a,a,a,a,a,a,a,a,a,a |
| 483 |
`, |
| 484 |
"config is invalid (:3:25)", |
| 485 |
}, |
| 486 |
} { |
| 487 |
t.Run(test.name, func(t *testing.T) { |
| 488 |
if _, err := parse(test.conf, "", true); err == nil { |
| 489 |
t.Error("expected an error") |
| 490 |
} else if !strings.Contains(err.Error(), test.err) { |
| 491 |
t.Errorf("expected invalid conf error, got: %v", err) |
| 492 |
} |
| 493 |
}) |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
func TestParseWithNoValuesEmptyConfigsAreValid(t *testing.T) { |
| 498 |
for _, test := range []struct { |
| 499 |
name string |
| 500 |
conf string |
| 501 |
}{ |
| 502 |
{ |
| 503 |
"empty conf", |
| 504 |
"", |
| 505 |
}, |
| 506 |
{ |
| 507 |
"empty conf with line breaks", |
| 508 |
` |
| 509 |
|
| 510 |
|
| 511 |
`, |
| 512 |
}, |
| 513 |
{ |
| 514 |
"just comments with no values", |
| 515 |
` |
| 516 |
# just comments with no values |
| 517 |
# is still valid. |
| 518 |
`, |
| 519 |
}, |
| 520 |
} { |
| 521 |
t.Run(test.name, func(t *testing.T) { |
| 522 |
if _, err := parse(test.conf, "", true); err != nil { |
| 523 |
t.Errorf("unexpected error: %v", err) |
| 524 |
} |
| 525 |
}) |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
func TestParseWithTrailingBracketsAreValid(t *testing.T) { |
| 530 |
for _, test := range []struct { |
| 531 |
name string |
| 532 |
conf string |
| 533 |
}{ |
| 534 |
{ |
| 535 |
"empty conf", |
| 536 |
"{}", |
| 537 |
}, |
| 538 |
{ |
| 539 |
"just comments with no values", |
| 540 |
` |
| 541 |
{ |
| 542 |
# comments in the body |
| 543 |
} |
| 544 |
`, |
| 545 |
}, |
| 546 |
{ |
| 547 |
// trailing brackets accidentally can become keys, |
| 548 |
// this is valid since needed to support JSON like configs.. |
| 549 |
"trailing brackets after config", |
| 550 |
` |
| 551 |
accounts { users = [{}]} |
| 552 |
} |
| 553 |
`, |
| 554 |
}, |
| 555 |
{ |
| 556 |
"wrapped in brackets", |
| 557 |
`{ |
| 558 |
accounts { users = [{}]} |
| 559 |
} |
| 560 |
`, |
| 561 |
}, |
| 562 |
} { |
| 563 |
t.Run(test.name, func(t *testing.T) { |
| 564 |
if _, err := parse(test.conf, "", true); err != nil { |
| 565 |
t.Errorf("unexpected error: %v", err) |
| 566 |
} |
| 567 |
}) |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
func TestParseWithNoValuesIncludes(t *testing.T) { |
| 572 |
for _, test := range []struct { |
| 573 |
input string |
| 574 |
includes map[string]string |
| 575 |
err string |
| 576 |
linepos string |
| 577 |
}{ |
| 578 |
{ |
| 579 |
`# includes |
| 580 |
accounts { |
| 581 |
foo { include 'foo.conf'} |
| 582 |
bar { users = [{user = "bar"}] } |
| 583 |
quux { include 'quux.conf'} |
| 584 |
} |
| 585 |
`, |
| 586 |
map[string]string{ |
| 587 |
"foo.conf": ``, |
| 588 |
"quux.conf": `?????????????`, |
| 589 |
}, |
| 590 |
"error parsing include file 'quux.conf', config is invalid", |
| 591 |
"quux.conf:1:1", |
| 592 |
}, |
| 593 |
{ |
| 594 |
`# includes |
| 595 |
accounts { |
| 596 |
foo { include 'foo.conf'} |
| 597 |
bar { include 'bar.conf'} |
| 598 |
quux { include 'quux.conf'} |
| 599 |
} |
| 600 |
`, |
| 601 |
map[string]string{ |
| 602 |
"foo.conf": ``, // Empty configs are ok |
| 603 |
"bar.conf": `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA`, |
| 604 |
"quux.conf": ` |
| 605 |
# just some comments, |
| 606 |
# and no key values also ok. |
| 607 |
`, |
| 608 |
}, |
| 609 |
"error parsing include file 'bar.conf', config is invalid", |
| 610 |
"bar.conf:1:34", |
| 611 |
}, |
| 612 |
} { |
| 613 |
t.Run("", func(t *testing.T) { |
| 614 |
sdir := t.TempDir() |
| 615 |
f, err := os.CreateTemp(sdir, "nats.conf-") |
| 616 |
if err != nil { |
| 617 |
t.Fatal(err) |
| 618 |
} |
| 619 |
if err := os.WriteFile(f.Name(), []byte(test.input), 066); err != nil { |
| 620 |
t.Error(err) |
| 621 |
} |
| 622 |
if test.includes != nil { |
| 623 |
for includeFile, contents := range test.includes { |
| 624 |
inf, err := os.Create(filepath.Join(sdir, includeFile)) |
| 625 |
if err != nil { |
| 626 |
t.Fatal(err) |
| 627 |
} |
| 628 |
if err := os.WriteFile(inf.Name(), []byte(contents), 066); err != nil { |
| 629 |
t.Error(err) |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
if _, err := parse(test.input, f.Name(), true); err == nil { |
| 634 |
t.Error("expected an error") |
| 635 |
} else if !strings.Contains(err.Error(), test.err) || !strings.Contains(err.Error(), test.linepos) { |
| 636 |
t.Errorf("expected invalid conf error, got: %v", err) |
| 637 |
} |
| 638 |
}) |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
func TestJSONParseCompat(t *testing.T) { |
| 643 |
for _, test := range []struct { |
| 644 |
name string |
| 645 |
input string |
| 646 |
includes map[string]string |
| 647 |
expected map[string]any |
| 648 |
}{ |
| 649 |
{ |
| 650 |
"JSON with nested blocks", |
| 651 |
` |
| 652 |
{ |
| 653 |
"http_port": 8227, |
| 654 |
"port": 4227, |
| 655 |
"write_deadline": "1h", |
| 656 |
"cluster": { |
| 657 |
"port": 6222, |
| 658 |
"routes": [ |
| 659 |
"nats://127.0.0.1:4222", |
| 660 |
"nats://127.0.0.1:4223", |
| 661 |
"nats://127.0.0.1:4224" |
| 662 |
] |
| 663 |
} |
| 664 |
} |
| 665 |
`, |
| 666 |
nil, |
| 667 |
map[string]any{ |
| 668 |
"http_port": int64(8227), |
| 669 |
"port": int64(4227), |
| 670 |
"write_deadline": "1h", |
| 671 |
"cluster": map[string]any{ |
| 672 |
"port": int64(6222), |
| 673 |
"routes": []any{ |
| 674 |
"nats://127.0.0.1:4222", |
| 675 |
"nats://127.0.0.1:4223", |
| 676 |
"nats://127.0.0.1:4224", |
| 677 |
}, |
| 678 |
}, |
| 679 |
}, |
| 680 |
}, |
| 681 |
{ |
| 682 |
"JSON with nested blocks", |
| 683 |
`{ |
| 684 |
"jetstream": { |
| 685 |
"store_dir": "/tmp/nats" |
| 686 |
"max_mem": 1000000, |
| 687 |
}, |
| 688 |
"port": 4222, |
| 689 |
"server_name": "nats1" |
| 690 |
} |
| 691 |
`, |
| 692 |
nil, |
| 693 |
map[string]any{ |
| 694 |
"jetstream": map[string]any{ |
| 695 |
"store_dir": "/tmp/nats", |
| 696 |
"max_mem": int64(1_000_000), |
| 697 |
}, |
| 698 |
"port": int64(4222), |
| 699 |
"server_name": "nats1", |
| 700 |
}, |
| 701 |
}, |
| 702 |
{ |
| 703 |
"JSON empty object in one line", |
| 704 |
`{}`, |
| 705 |
nil, |
| 706 |
map[string]any{}, |
| 707 |
}, |
| 708 |
{ |
| 709 |
"JSON empty object with line breaks", |
| 710 |
` |
| 711 |
{ |
| 712 |
} |
| 713 |
`, |
| 714 |
nil, |
| 715 |
map[string]any{}, |
| 716 |
}, |
| 717 |
{ |
| 718 |
"JSON includes", |
| 719 |
` |
| 720 |
accounts { |
| 721 |
foo { include 'foo.json' } |
| 722 |
bar { include 'bar.json' } |
| 723 |
quux { include 'quux.json' } |
| 724 |
} |
| 725 |
`, |
| 726 |
map[string]string{ |
| 727 |
"foo.json": `{ "users": [ {"user": "foo"} ] }`, |
| 728 |
"bar.json": `{ |
| 729 |
"users": [ {"user": "bar"} ] |
| 730 |
}`, |
| 731 |
"quux.json": `{}`, |
| 732 |
}, |
| 733 |
map[string]any{ |
| 734 |
"accounts": map[string]any{ |
| 735 |
"foo": map[string]any{ |
| 736 |
"users": []any{ |
| 737 |
map[string]any{ |
| 738 |
"user": "foo", |
| 739 |
}, |
| 740 |
}, |
| 741 |
}, |
| 742 |
"bar": map[string]any{ |
| 743 |
"users": []any{ |
| 744 |
map[string]any{ |
| 745 |
"user": "bar", |
| 746 |
}, |
| 747 |
}, |
| 748 |
}, |
| 749 |
"quux": map[string]any{}, |
| 750 |
}, |
| 751 |
}, |
| 752 |
}, |
| 753 |
} { |
| 754 |
t.Run(test.name, func(t *testing.T) { |
| 755 |
sdir := t.TempDir() |
| 756 |
f, err := os.CreateTemp(sdir, "nats.conf-") |
| 757 |
if err != nil { |
| 758 |
t.Fatal(err) |
| 759 |
} |
| 760 |
if err := os.WriteFile(f.Name(), []byte(test.input), 066); err != nil { |
| 761 |
t.Error(err) |
| 762 |
} |
| 763 |
if test.includes != nil { |
| 764 |
for includeFile, contents := range test.includes { |
| 765 |
inf, err := os.Create(filepath.Join(sdir, includeFile)) |
| 766 |
if err != nil { |
| 767 |
t.Fatal(err) |
| 768 |
} |
| 769 |
if err := os.WriteFile(inf.Name(), []byte(contents), 066); err != nil { |
| 770 |
t.Error(err) |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
m, err := ParseFile(f.Name()) |
| 775 |
if err != nil { |
| 776 |
t.Fatalf("Unexpected error: %v", err) |
| 777 |
} |
| 778 |
if !reflect.DeepEqual(m, test.expected) { |
| 779 |
t.Fatalf("Not Equal:\nReceived: '%+v'\nExpected: '%+v'\n", m, test.expected) |
| 780 |
} |
| 781 |
}) |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
func TestBlocks(t *testing.T) { |
| 786 |
for _, test := range []struct { |
| 787 |
name string |
| 788 |
input string |
| 789 |
expected map[string]any |
| 790 |
err string |
| 791 |
linepos string |
| 792 |
}{ |
| 793 |
{ |
| 794 |
"inline block", |
| 795 |
`{ listen: 0.0.0.0:4222 }`, |
| 796 |
map[string]any{ |
| 797 |
"listen": "0.0.0.0:4222", |
| 798 |
}, |
| 799 |
"", "", |
| 800 |
}, |
| 801 |
{ |
| 802 |
"newline block", |
| 803 |
`{ |
| 804 |
listen: 0.0.0.0:4222 |
| 805 |
}`, |
| 806 |
map[string]any{ |
| 807 |
"listen": "0.0.0.0:4222", |
| 808 |
}, |
| 809 |
"", "", |
| 810 |
}, |
| 811 |
{ |
| 812 |
"newline block with trailing comment", |
| 813 |
` |
| 814 |
{ |
| 815 |
listen: 0.0.0.0:4222 |
| 816 |
} |
| 817 |
# wibble |
| 818 |
`, |
| 819 |
map[string]any{ |
| 820 |
"listen": "0.0.0.0:4222", |
| 821 |
}, |
| 822 |
"", "", |
| 823 |
}, |
| 824 |
{ |
| 825 |
"nested newline blocks with trailing comment", |
| 826 |
` |
| 827 |
{ |
| 828 |
{ |
| 829 |
listen: 0.0.0.0:4222 // random comment |
| 830 |
} |
| 831 |
# wibble1 |
| 832 |
} |
| 833 |
# wibble2 |
| 834 |
`, |
| 835 |
map[string]any{ |
| 836 |
"listen": "0.0.0.0:4222", |
| 837 |
}, |
| 838 |
"", "", |
| 839 |
}, |
| 840 |
{ |
| 841 |
"top line values in block scope", |
| 842 |
` |
| 843 |
{ |
| 844 |
"debug": False |
| 845 |
"prof_port": 8221 |
| 846 |
"server_name": "aws-useast2-natscj1-1" |
| 847 |
} |
| 848 |
`, |
| 849 |
map[string]any{ |
| 850 |
"debug": false, |
| 851 |
"prof_port": int64(8221), |
| 852 |
"server_name": "aws-useast2-natscj1-1", |
| 853 |
}, |
| 854 |
"", "", |
| 855 |
}, |
| 856 |
{ |
| 857 |
"comment in block scope after value parse", |
| 858 |
` |
| 859 |
{ |
| 860 |
"debug": False |
| 861 |
"server_name": "gcp-asianortheast3-natscj1-1" |
| 862 |
|
| 863 |
# Profile port specification. |
| 864 |
"prof_port": 8221 |
| 865 |
} |
| 866 |
`, |
| 867 |
map[string]any{ |
| 868 |
"debug": false, |
| 869 |
"prof_port": int64(8221), |
| 870 |
"server_name": "gcp-asianortheast3-natscj1-1", |
| 871 |
}, |
| 872 |
"", "", |
| 873 |
}, |
| 874 |
} { |
| 875 |
t.Run(test.name, func(t *testing.T) { |
| 876 |
f, err := os.CreateTemp(t.TempDir(), "nats.conf-") |
| 877 |
if err != nil { |
| 878 |
t.Fatal(err) |
| 879 |
} |
| 880 |
if err := os.WriteFile(f.Name(), []byte(test.input), 066); err != nil { |
| 881 |
t.Error(err) |
| 882 |
} |
| 883 |
if m, err := ParseFile(f.Name()); err == nil { |
| 884 |
if !reflect.DeepEqual(m, test.expected) { |
| 885 |
t.Fatalf("Not Equal:\nReceived: '%+v'\nExpected: '%+v'\n", m, test.expected) |
| 886 |
} |
| 887 |
} else if !strings.Contains(err.Error(), test.err) || !strings.Contains(err.Error(), test.linepos) { |
| 888 |
t.Errorf("expected invalid conf error, got: %v", err) |
| 889 |
} else if err != nil { |
| 890 |
t.Error(err) |
| 891 |
} |
| 892 |
}) |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
func TestParseDigest(t *testing.T) { |
| 897 |
for _, test := range []struct { |
| 898 |
input string |
| 899 |
includes map[string]string |
| 900 |
digest string |
| 901 |
}{ |
| 902 |
{ |
| 903 |
`foo = bar`, |
| 904 |
nil, |
| 905 |
"sha256:226e49e13d16e5e8aa0d62e58cd63361bf097d3e2b2444aa3044334628a2e8de", |
| 906 |
}, |
| 907 |
{ |
| 908 |
`# Comments and whitespace have no effect |
| 909 |
foo = bar |
| 910 |
`, |
| 911 |
nil, |
| 912 |
"sha256:226e49e13d16e5e8aa0d62e58cd63361bf097d3e2b2444aa3044334628a2e8de", |
| 913 |
}, |
| 914 |
{ |
| 915 |
`# Syntax changes have no effect |
| 916 |
'foo': 'bar' |
| 917 |
`, |
| 918 |
nil, |
| 919 |
"sha256:226e49e13d16e5e8aa0d62e58cd63361bf097d3e2b2444aa3044334628a2e8de", |
| 920 |
}, |
| 921 |
{ |
| 922 |
`# Syntax changes have no effect |
| 923 |
{ 'foo': 'bar' } |
| 924 |
`, |
| 925 |
nil, |
| 926 |
"sha256:226e49e13d16e5e8aa0d62e58cd63361bf097d3e2b2444aa3044334628a2e8de", |
| 927 |
}, |
| 928 |
{ |
| 929 |
`# substitutions |
| 930 |
BAR_USERS = { users = [ {user = "bar"} ]} |
| 931 |
hello = 'world' |
| 932 |
accounts { |
| 933 |
QUUX_USERS = [ { user: quux }] |
| 934 |
bar = $BAR_USERS |
| 935 |
quux = { users = $QUUX_USERS } |
| 936 |
} |
| 937 |
very { nested { env { VAR = 'NESTED', quux = $VAR }}} |
| 938 |
`, |
| 939 |
nil, |
| 940 |
"sha256:bddb282343249c26d3edcef9bfaa9d2711505fc67210380e871405ba394a9186", |
| 941 |
}, |
| 942 |
{ |
| 943 |
`# substitutions, same as previous one without env vars. |
| 944 |
hello = 'world' |
| 945 |
accounts { |
| 946 |
bar = { users = [ { user = "bar" } ]} |
| 947 |
quux = { users = [ { user: quux } ]} |
| 948 |
} |
| 949 |
very { nested { env { quux = 'NESTED' }}} |
| 950 |
`, |
| 951 |
nil, |
| 952 |
"sha256:34f8faf3f269fe7509edc4742f20c8c4a7ad51fe21f8b361764314b533ac3ab5", |
| 953 |
}, |
| 954 |
{ |
| 955 |
`# substitutions |
| 956 |
BAR_USERS = { users = [ {user = "foo"} ]} |
| 957 |
bar = $BAR_USERS |
| 958 |
accounts { |
| 959 |
users = $BAR_USERS |
| 960 |
} |
| 961 |
`, |
| 962 |
nil, |
| 963 |
"sha256:b0e2ba0b8ec2cb75681b5c5d61789cda0c9942c2f9fe16cdb7f6703364485360", |
| 964 |
}, |
| 965 |
{ |
| 966 |
`# substitutions |
| 967 |
bar = { users = [ {user = "foo"} ]} |
| 968 |
accounts { |
| 969 |
users = { users = [ {user = "foo"} ]} |
| 970 |
} |
| 971 |
`, |
| 972 |
nil, |
| 973 |
"sha256:f5d943b4ed22b80c6199203f8a7eaa8eb68ef7b2d46ef6b1b26f05e21f8beb13", |
| 974 |
}, |
| 975 |
{ |
| 976 |
`# includes |
| 977 |
accounts { |
| 978 |
foo { include 'foo.conf'} |
| 979 |
bar { users = [{user = "bar"}] } |
| 980 |
quux { include 'quux.conf'} |
| 981 |
} |
| 982 |
`, |
| 983 |
map[string]string{ |
| 984 |
"foo.conf": ` users = [{user = "foo"}]`, |
| 985 |
"quux.conf": ` users = [{user = "quux"}]`, |
| 986 |
}, |
| 987 |
"sha256:e72d70c91b64b0f880f86decb95ec2600cbdcf8bdcd2355fce5ebc54a84a77e9", |
| 988 |
}, |
| 989 |
{ |
| 990 |
`# includes |
| 991 |
accounts { |
| 992 |
foo { include 'foo.conf'} |
| 993 |
bar { include 'bar.conf'} |
| 994 |
quux { include 'quux.conf'} |
| 995 |
} |
| 996 |
`, |
| 997 |
map[string]string{ |
| 998 |
"foo.conf": ` users = [{user = "foo"}]`, |
| 999 |
"bar.conf": ` users = [{user = "bar"}]`, |
| 1000 |
"quux.conf": ` users = [{user = "quux"}]`, |
| 1001 |
}, |
| 1002 |
"sha256:e72d70c91b64b0f880f86decb95ec2600cbdcf8bdcd2355fce5ebc54a84a77e9", |
| 1003 |
}, |
| 1004 |
} { |
| 1005 |
t.Run("", func(t *testing.T) { |
| 1006 |
sdir := t.TempDir() |
| 1007 |
f, err := os.CreateTemp(sdir, "nats.conf-") |
| 1008 |
if err != nil { |
| 1009 |
t.Fatal(err) |
| 1010 |
} |
| 1011 |
if err := os.WriteFile(f.Name(), []byte(test.input), 066); err != nil { |
| 1012 |
t.Error(err) |
| 1013 |
} |
| 1014 |
if test.includes != nil { |
| 1015 |
for includeFile, contents := range test.includes { |
| 1016 |
inf, err := os.Create(filepath.Join(sdir, includeFile)) |
| 1017 |
if err != nil { |
| 1018 |
t.Fatal(err) |
| 1019 |
} |
| 1020 |
if err := os.WriteFile(inf.Name(), []byte(contents), 066); err != nil { |
| 1021 |
t.Error(err) |
| 1022 |
} |
| 1023 |
} |
| 1024 |
} |
| 1025 |
_, digest, err := ParseFileWithChecksDigest(f.Name()) |
| 1026 |
if err != nil { |
| 1027 |
t.Fatal(err) |
| 1028 |
} |
| 1029 |
if digest != test.digest { |
| 1030 |
t.Errorf("\ngot: %s\nexpected: %s", digest, test.digest) |
| 1031 |
} |
| 1032 |
}) |
| 1033 |
} |
| 1034 |
} |