| 1 |
// Copyright 2020-2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package test |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"strconv" |
| 19 |
"strings" |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/nats-io/nats-server/v2/server" |
| 24 |
"github.com/nats-io/nats.go" |
| 25 |
) |
| 26 |
|
| 27 |
func TestAccountCycleService(t *testing.T) { |
| 28 |
conf := createConfFile(t, []byte(` |
| 29 |
accounts { |
| 30 |
A { |
| 31 |
exports [ { service: help } ] |
| 32 |
imports [ { service { subject: help, account: B } } ] |
| 33 |
} |
| 34 |
B { |
| 35 |
exports [ { service: help } ] |
| 36 |
imports [ { service { subject: help, account: A } } ] |
| 37 |
} |
| 38 |
} |
| 39 |
`)) |
| 40 |
|
| 41 |
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) { |
| 42 |
t.Fatalf("Expected an error on cycle service import, got none") |
| 43 |
} |
| 44 |
|
| 45 |
conf = createConfFile(t, []byte(` |
| 46 |
accounts { |
| 47 |
A { |
| 48 |
exports [ { service: * } ] |
| 49 |
imports [ { service { subject: help, account: B } } ] |
| 50 |
} |
| 51 |
B { |
| 52 |
exports [ { service: help } ] |
| 53 |
imports [ { service { subject: *, account: A } } ] |
| 54 |
} |
| 55 |
} |
| 56 |
`)) |
| 57 |
|
| 58 |
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) { |
| 59 |
t.Fatalf("Expected an error on cycle service import, got none") |
| 60 |
} |
| 61 |
|
| 62 |
conf = createConfFile(t, []byte(` |
| 63 |
accounts { |
| 64 |
A { |
| 65 |
exports [ { service: * } ] |
| 66 |
imports [ { service { subject: help, account: B } } ] |
| 67 |
} |
| 68 |
B { |
| 69 |
exports [ { service: help } ] |
| 70 |
imports [ { service { subject: help, account: C } } ] |
| 71 |
} |
| 72 |
C { |
| 73 |
exports [ { service: * } ] |
| 74 |
imports [ { service { subject: *, account: A } } ] |
| 75 |
} |
| 76 |
} |
| 77 |
`)) |
| 78 |
|
| 79 |
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) { |
| 80 |
t.Fatalf("Expected an error on cycle service import, got none") |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func TestAccountCycleStream(t *testing.T) { |
| 85 |
conf := createConfFile(t, []byte(` |
| 86 |
accounts { |
| 87 |
A { |
| 88 |
exports [ { stream: strm } ] |
| 89 |
imports [ { stream { subject: strm, account: B } } ] |
| 90 |
} |
| 91 |
B { |
| 92 |
exports [ { stream: strm } ] |
| 93 |
imports [ { stream { subject: strm, account: A } } ] |
| 94 |
} |
| 95 |
} |
| 96 |
`)) |
| 97 |
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) { |
| 98 |
t.Fatalf("Expected an error on cyclic import, got none") |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func TestAccountCycleStreamWithMapping(t *testing.T) { |
| 103 |
conf := createConfFile(t, []byte(` |
| 104 |
accounts { |
| 105 |
A { |
| 106 |
exports [ { stream: * } ] |
| 107 |
imports [ { stream { subject: bar, account: B } } ] |
| 108 |
} |
| 109 |
B { |
| 110 |
exports [ { stream: bar } ] |
| 111 |
imports [ { stream { subject: foo, account: A }, to: bar } ] |
| 112 |
} |
| 113 |
} |
| 114 |
`)) |
| 115 |
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) { |
| 116 |
t.Fatalf("Expected an error on cyclic import, got none") |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
func TestAccountCycleNonCycleStreamWithMapping(t *testing.T) { |
| 121 |
conf := createConfFile(t, []byte(` |
| 122 |
accounts { |
| 123 |
A { |
| 124 |
exports [ { stream: foo } ] |
| 125 |
imports [ { stream { subject: bar, account: B } } ] |
| 126 |
} |
| 127 |
B { |
| 128 |
exports [ { stream: bar } ] |
| 129 |
imports [ { stream { subject: baz, account: C }, to: bar } ] |
| 130 |
} |
| 131 |
C { |
| 132 |
exports [ { stream: baz } ] |
| 133 |
imports [ { stream { subject: foo, account: A }, to: bar } ] |
| 134 |
} |
| 135 |
} |
| 136 |
`)) |
| 137 |
if _, err := server.ProcessConfigFile(conf); err != nil { |
| 138 |
t.Fatalf("Expected no error but got %s", err) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func TestAccountCycleServiceCycleWithMapping(t *testing.T) { |
| 143 |
conf := createConfFile(t, []byte(` |
| 144 |
accounts { |
| 145 |
A { |
| 146 |
exports [ { service: a } ] |
| 147 |
imports [ { service { subject: b, account: B }, to: a } ] |
| 148 |
} |
| 149 |
B { |
| 150 |
exports [ { service: b } ] |
| 151 |
imports [ { service { subject: a, account: A }, to: b } ] |
| 152 |
} |
| 153 |
} |
| 154 |
`)) |
| 155 |
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) { |
| 156 |
t.Fatalf("Expected an error on cycle service import, got none") |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func TestAccountCycleServiceNonCycle(t *testing.T) { |
| 161 |
conf := createConfFile(t, []byte(` |
| 162 |
accounts { |
| 163 |
A { |
| 164 |
exports [ { service: * } ] |
| 165 |
imports [ { service { subject: help, account: B } } ] |
| 166 |
} |
| 167 |
B { |
| 168 |
exports [ { service: help } ] |
| 169 |
imports [ { service { subject: nohelp, account: C } } ] |
| 170 |
} |
| 171 |
C { |
| 172 |
exports [ { service: * } ] |
| 173 |
imports [ { service { subject: *, account: A } } ] |
| 174 |
} |
| 175 |
} |
| 176 |
`)) |
| 177 |
|
| 178 |
if _, err := server.ProcessConfigFile(conf); err != nil { |
| 179 |
t.Fatalf("Expected no error but got %s", err) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func TestAccountCycleServiceNonCycleChain(t *testing.T) { |
| 184 |
conf := createConfFile(t, []byte(` |
| 185 |
accounts { |
| 186 |
A { |
| 187 |
exports [ { service: help } ] |
| 188 |
imports [ { service { subject: help, account: B } } ] |
| 189 |
} |
| 190 |
B { |
| 191 |
exports [ { service: help } ] |
| 192 |
imports [ { service { subject: help, account: C } } ] |
| 193 |
} |
| 194 |
C { |
| 195 |
exports [ { service: help } ] |
| 196 |
imports [ { service { subject: help, account: D } } ] |
| 197 |
} |
| 198 |
D { |
| 199 |
exports [ { service: help } ] |
| 200 |
} |
| 201 |
} |
| 202 |
`)) |
| 203 |
|
| 204 |
if _, err := server.ProcessConfigFile(conf); err != nil { |
| 205 |
t.Fatalf("Expected no error but got %s", err) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
// bug: https://github.com/nats-io/nats-server/issues/1769 |
| 210 |
func TestServiceImportReplyMatchCycle(t *testing.T) { |
| 211 |
conf := createConfFile(t, []byte(` |
| 212 |
port: -1 |
| 213 |
accounts { |
| 214 |
A { |
| 215 |
users: [{user: d, pass: x}] |
| 216 |
imports [ {service: {account: B, subject: ">" }}] |
| 217 |
} |
| 218 |
B { |
| 219 |
users: [{user: x, pass: x}] |
| 220 |
exports [ { service: ">" } ] |
| 221 |
} |
| 222 |
} |
| 223 |
no_auth_user: d |
| 224 |
`)) |
| 225 |
|
| 226 |
s, opts := RunServerWithConfig(conf) |
| 227 |
defer s.Shutdown() |
| 228 |
|
| 229 |
nc1 := clientConnectToServerWithUP(t, opts, "x", "x") |
| 230 |
defer nc1.Close() |
| 231 |
|
| 232 |
msg := []byte("HELLO") |
| 233 |
_, err := nc1.Subscribe("foo", func(m *nats.Msg) { |
| 234 |
m.Respond(msg) |
| 235 |
}) |
| 236 |
if err != nil { |
| 237 |
t.Fatal(err) |
| 238 |
} |
| 239 |
|
| 240 |
// Ensure the subscription is known by the server we're connected to. |
| 241 |
err = nc1.Flush() |
| 242 |
if err != nil { |
| 243 |
t.Fatal(err) |
| 244 |
} |
| 245 |
checkSubInterest(t, s, "B", "foo", time.Second) |
| 246 |
|
| 247 |
nc2 := clientConnectToServer(t, s) |
| 248 |
defer nc2.Close() |
| 249 |
|
| 250 |
resp, err := nc2.Request("foo", nil, time.Second) |
| 251 |
if err != nil { |
| 252 |
t.Fatalf("Unexpected error: %v", err) |
| 253 |
} |
| 254 |
if resp == nil || string(resp.Data) != string(msg) { |
| 255 |
t.Fatalf("Wrong or empty response") |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
func TestServiceImportReplyMatchCycleMultiHops(t *testing.T) { |
| 260 |
conf := createConfFile(t, []byte(` |
| 261 |
port: -1 |
| 262 |
accounts { |
| 263 |
A { |
| 264 |
users: [{user: d, pass: x}] |
| 265 |
imports [ {service: {account: B, subject: ">" }}] |
| 266 |
} |
| 267 |
B { |
| 268 |
exports [ { service: ">" } ] |
| 269 |
imports [ {service: {account: C, subject: ">" }}] |
| 270 |
} |
| 271 |
C { |
| 272 |
users: [{user: x, pass: x}] |
| 273 |
exports [ { service: ">" } ] |
| 274 |
} |
| 275 |
} |
| 276 |
no_auth_user: d |
| 277 |
`)) |
| 278 |
|
| 279 |
s, opts := RunServerWithConfig(conf) |
| 280 |
defer s.Shutdown() |
| 281 |
|
| 282 |
nc1 := clientConnectToServerWithUP(t, opts, "x", "x") |
| 283 |
defer nc1.Close() |
| 284 |
|
| 285 |
msg := []byte("HELLO") |
| 286 |
_, err := nc1.Subscribe("foo", func(m *nats.Msg) { |
| 287 |
m.Respond(msg) |
| 288 |
}) |
| 289 |
if err != nil { |
| 290 |
t.Fatal(err) |
| 291 |
} |
| 292 |
|
| 293 |
// Ensure the subscription is known by the server we're connected to. |
| 294 |
err = nc1.Flush() |
| 295 |
if err != nil { |
| 296 |
t.Fatal(err) |
| 297 |
} |
| 298 |
checkSubInterest(t, s, "B", "foo", time.Second) |
| 299 |
|
| 300 |
nc2 := clientConnectToServer(t, s) |
| 301 |
defer nc2.Close() |
| 302 |
|
| 303 |
resp, err := nc2.Request("foo", nil, time.Second) |
| 304 |
if err != nil { |
| 305 |
t.Fatalf("Unexpected error: %v", err) |
| 306 |
} |
| 307 |
if resp == nil || string(resp.Data) != string(msg) { |
| 308 |
t.Fatalf("Wrong or empty response") |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
// Go's stack are infinite sans memory, but not call depth. However its good to limit. |
| 313 |
func TestAccountCycleDepthLimit(t *testing.T) { |
| 314 |
var last *server.Account |
| 315 |
chainLen := server.MaxAccountCycleSearchDepth + 1 |
| 316 |
|
| 317 |
// Services |
| 318 |
for i := 1; i <= chainLen; i++ { |
| 319 |
acc := server.NewAccount(fmt.Sprintf("ACC-%d", i)) |
| 320 |
if err := acc.AddServiceExport("*", nil); err != nil { |
| 321 |
t.Fatalf("Error adding service export to '*': %v", err) |
| 322 |
} |
| 323 |
if last != nil { |
| 324 |
err := acc.AddServiceImport(last, "foo", "foo") |
| 325 |
switch i { |
| 326 |
case chainLen: |
| 327 |
if err != server.ErrCycleSearchDepth { |
| 328 |
t.Fatalf("Expected last import to fail with '%v', but got '%v'", server.ErrCycleSearchDepth, err) |
| 329 |
} |
| 330 |
default: |
| 331 |
if err != nil { |
| 332 |
t.Fatalf("Error adding service import to 'foo': %v", err) |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
last = acc |
| 337 |
} |
| 338 |
|
| 339 |
last = nil |
| 340 |
|
| 341 |
// Streams |
| 342 |
for i := 1; i <= chainLen; i++ { |
| 343 |
acc := server.NewAccount(fmt.Sprintf("ACC-%d", i)) |
| 344 |
if err := acc.AddStreamExport("foo", nil); err != nil { |
| 345 |
t.Fatalf("Error adding stream export to '*': %v", err) |
| 346 |
} |
| 347 |
if last != nil { |
| 348 |
err := acc.AddStreamImport(last, "foo", "") |
| 349 |
switch i { |
| 350 |
case chainLen: |
| 351 |
if err != server.ErrCycleSearchDepth { |
| 352 |
t.Fatalf("Expected last import to fail with '%v', but got '%v'", server.ErrCycleSearchDepth, err) |
| 353 |
} |
| 354 |
default: |
| 355 |
if err != nil { |
| 356 |
t.Fatalf("Error adding stream import to 'foo': %v", err) |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
last = acc |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
// Test token and partition subject mapping within an account |
| 365 |
func TestAccountSubjectMapping(t *testing.T) { |
| 366 |
conf := createConfFile(t, []byte(` |
| 367 |
port: -1 |
| 368 |
mappings = { |
| 369 |
"foo.*.*" : "foo.$1.{{wildcard(2)}}.{{partition(10,1,2)}}" |
| 370 |
} |
| 371 |
`)) |
| 372 |
|
| 373 |
s, _ := RunServerWithConfig(conf) |
| 374 |
defer s.Shutdown() |
| 375 |
|
| 376 |
nc1 := clientConnectToServer(t, s) |
| 377 |
defer nc1.Close() |
| 378 |
|
| 379 |
numMessages := 100 |
| 380 |
subjectsReceived := make(chan string) |
| 381 |
|
| 382 |
msg := []byte("HELLO") |
| 383 |
sub1, err := nc1.Subscribe("foo.*.*.*", func(m *nats.Msg) { |
| 384 |
subjectsReceived <- m.Subject |
| 385 |
}) |
| 386 |
if err != nil { |
| 387 |
t.Fatalf("Unexpected error: %v", err) |
| 388 |
} |
| 389 |
sub1.AutoUnsubscribe(numMessages * 2) |
| 390 |
nc1.Flush() |
| 391 |
|
| 392 |
nc2 := clientConnectToServer(t, s) |
| 393 |
defer nc2.Close() |
| 394 |
|
| 395 |
// publish numMessages with an increasing id (should map to partition numbers with the range of 10 partitions) - twice |
| 396 |
for j := 0; j < 2; j++ { |
| 397 |
for i := 0; i < numMessages; i++ { |
| 398 |
err = nc2.Publish(fmt.Sprintf("foo.%d.%d", i, numMessages-i), msg) |
| 399 |
if err != nil { |
| 400 |
t.Fatalf("Unexpected error: %v", err) |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
// verify all the partition numbers are in the expected range |
| 406 |
partitionsReceived := make([]int, numMessages) |
| 407 |
|
| 408 |
for i := 0; i < numMessages; i++ { |
| 409 |
var subject string |
| 410 |
select { |
| 411 |
case subject = <-subjectsReceived: |
| 412 |
case <-time.After(5 * time.Second): |
| 413 |
t.Fatal("Timed out waiting for messages") |
| 414 |
} |
| 415 |
sTokens := strings.Split(subject, ".") |
| 416 |
if err != nil { |
| 417 |
t.Fatalf("Unexpected error: %v", err) |
| 418 |
} |
| 419 |
t1, _ := strconv.Atoi(sTokens[1]) |
| 420 |
t2, _ := strconv.Atoi(sTokens[2]) |
| 421 |
partitionsReceived[i], err = strconv.Atoi(sTokens[3]) |
| 422 |
if err != nil { |
| 423 |
t.Fatalf("Unexpected error: %v", err) |
| 424 |
} |
| 425 |
|
| 426 |
if partitionsReceived[i] > 9 || partitionsReceived[i] < 0 || t1 != i || t2 != numMessages-i { |
| 427 |
t.Fatalf("Error received unexpected %d.%d to partition %d", t1, t2, partitionsReceived[i]) |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
// verify hashing is deterministic by checking it produces the same exact result twice |
| 432 |
for i := 0; i < numMessages; i++ { |
| 433 |
subject := <-subjectsReceived |
| 434 |
partitionNumber, err := strconv.Atoi(strings.Split(subject, ".")[3]) |
| 435 |
if err != nil { |
| 436 |
t.Fatalf("Unexpected error: %v", err) |
| 437 |
} |
| 438 |
if partitionsReceived[i] != partitionNumber { |
| 439 |
t.Fatalf("Error: same id mapped to two different partitions") |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
// test token subject mapping within an account |
| 445 |
// Alice imports from Bob with subject mapping |
| 446 |
func TestAccountImportSubjectMapping(t *testing.T) { |
| 447 |
conf := createConfFile(t, []byte(` |
| 448 |
port: -1 |
| 449 |
accounts { |
| 450 |
A { |
| 451 |
users: [{user: a, pass: x}] |
| 452 |
imports [ {stream: {account: B, subject: "foo.*.*"}, to : "foo.$1.{{wildcard(2)}}"}] |
| 453 |
} |
| 454 |
B { |
| 455 |
users: [{user: b, pass x}] |
| 456 |
exports [ { stream: ">" } ] |
| 457 |
} |
| 458 |
} |
| 459 |
`)) |
| 460 |
|
| 461 |
s, opts := RunServerWithConfig(conf) |
| 462 |
|
| 463 |
defer s.Shutdown() |
| 464 |
ncA := clientConnectToServerWithUP(t, opts, "a", "x") |
| 465 |
defer ncA.Close() |
| 466 |
|
| 467 |
numMessages := 100 |
| 468 |
subjectsReceived := make(chan string) |
| 469 |
|
| 470 |
msg := []byte("HELLO") |
| 471 |
sub1, err := ncA.Subscribe("foo.*.*", func(m *nats.Msg) { |
| 472 |
subjectsReceived <- m.Subject |
| 473 |
}) |
| 474 |
if err != nil { |
| 475 |
t.Fatalf("Unexpected error: %v", err) |
| 476 |
} |
| 477 |
sub1.AutoUnsubscribe(numMessages) |
| 478 |
ncA.Flush() |
| 479 |
|
| 480 |
ncB := clientConnectToServerWithUP(t, opts, "b", "x") |
| 481 |
defer ncB.Close() |
| 482 |
|
| 483 |
// publish numMessages with an increasing id |
| 484 |
|
| 485 |
for i := 0; i < numMessages; i++ { |
| 486 |
err = ncB.Publish(fmt.Sprintf("foo.%d.%d", i, numMessages-i), msg) |
| 487 |
if err != nil { |
| 488 |
t.Fatalf("Unexpected error: %v", err) |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
for i := 0; i < numMessages; i++ { |
| 493 |
var subject string |
| 494 |
select { |
| 495 |
case subject = <-subjectsReceived: |
| 496 |
case <-time.After(1 * time.Second): |
| 497 |
t.Fatal("Timed out waiting for messages") |
| 498 |
} |
| 499 |
sTokens := strings.Split(subject, ".") |
| 500 |
if err != nil { |
| 501 |
t.Fatalf("Unexpected error: %v", err) |
| 502 |
} |
| 503 |
t1, _ := strconv.Atoi(sTokens[1]) |
| 504 |
t2, _ := strconv.Atoi(sTokens[2]) |
| 505 |
|
| 506 |
if t1 != i || t2 != numMessages-i { |
| 507 |
t.Fatalf("Error received unexpected %d.%d", t1, t2) |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
// bug: https://github.com/nats-io/nats-server/issues/1789 |
| 513 |
func TestAccountCycleWithRenaming(t *testing.T) { |
| 514 |
conf := createConfFile(t, []byte(` |
| 515 |
accounts { |
| 516 |
A { |
| 517 |
exports [ { service: * } ] |
| 518 |
imports [ { service { subject: foo, account: B }, to: foo } ] |
| 519 |
} |
| 520 |
B { |
| 521 |
exports [ { service: foo } ] |
| 522 |
imports [ { service { subject: *, account: A }, to: "$1" } ] |
| 523 |
} |
| 524 |
} |
| 525 |
`)) |
| 526 |
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) { |
| 527 |
t.Fatalf("Expected an error on cycle service import, got none") |
| 528 |
} |
| 529 |
|
| 530 |
conf = createConfFile(t, []byte(` |
| 531 |
accounts { |
| 532 |
A { |
| 533 |
exports [ { stream: * } ] |
| 534 |
imports [ { stream { subject: foo, account: B }, to: foo } ] |
| 535 |
} |
| 536 |
B { |
| 537 |
exports [ { stream: foo } ] |
| 538 |
imports [ { stream { subject: *, account: A }, to: "$1" } ] |
| 539 |
} |
| 540 |
} |
| 541 |
`)) |
| 542 |
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) { |
| 543 |
t.Fatalf("Expected an error on cycle service import, got none") |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
// https://github.com/nats-io/nats-server/issues/5752 |
| 548 |
func TestAccountCycleFalsePositiveSubjectMapping(t *testing.T) { |
| 549 |
conf := createConfFile(t, []byte(` |
| 550 |
accounts { |
| 551 |
A { |
| 552 |
exports [ { service: "a.*" } ] |
| 553 |
imports [ { service { subject: "a.*", account: B }, to: "b.*" } ] |
| 554 |
} |
| 555 |
B { |
| 556 |
exports [ { service: "a.*" } ] |
| 557 |
imports [ { service { subject: "a.foo", account: A }, to: "c.foo" } ] |
| 558 |
} |
| 559 |
} |
| 560 |
`)) |
| 561 |
if _, err := server.ProcessConfigFile(conf); err != nil { |
| 562 |
t.Fatalf("Expected no errors on cycle service import, got error") |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
// TestAccountCycleServiceMissedDueToFromMutation reproduces a bug where |
| 567 |
// checkServiceImportsForCycles mutates the `from` loop variable via |
| 568 |
// `from = si.from`, affecting subsequent map iterations non-deterministically. |
| 569 |
// |
| 570 |
// Cycle: E -> A -> D -> E |
| 571 |
// A also has a non-cyclic import from C. When C's map key is iterated first, |
| 572 |
// `from` is narrowed from "*" to "narrow", causing D's import to be skipped |
| 573 |
// (SubjectsCollide("narrow","other") is false), and the cycle is missed. |
| 574 |
func TestAccountCycleServiceMissedDueToFromMutation(t *testing.T) { |
| 575 |
// Run multiple times because Go map iteration order is non-deterministic. |
| 576 |
// The bug only manifests when the non-cyclic import is visited first. |
| 577 |
for i := 0; i < 100; i++ { |
| 578 |
conf := createConfFile(t, []byte(` |
| 579 |
accounts { |
| 580 |
A { |
| 581 |
exports [ { service: * } ] |
| 582 |
imports [ |
| 583 |
{ service { subject: narrow, account: C } } |
| 584 |
{ service { subject: other, account: D } } |
| 585 |
] |
| 586 |
} |
| 587 |
C { |
| 588 |
exports [ { service: narrow } ] |
| 589 |
} |
| 590 |
D { |
| 591 |
exports [ { service: other } ] |
| 592 |
imports [ { service { subject: other, account: E } } ] |
| 593 |
} |
| 594 |
E { |
| 595 |
exports [ { service: * } ] |
| 596 |
imports [ { service { subject: *, account: A } } ] |
| 597 |
} |
| 598 |
} |
| 599 |
`)) |
| 600 |
_, err := server.ProcessConfigFile(conf) |
| 601 |
if err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) { |
| 602 |
t.Fatalf("Iteration %d: Expected cycle detection error for E->A->D->E, got: %v", i, err) |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
// TestAccountCycleStreamMissedDueToToMutation reproduces a bug where the `to` |
| 608 |
// parameter is mutated inside the loop in checkStreamImportsForCycles. |
| 609 |
// Stream imports use a slice (deterministic order), so listing the narrowing |
| 610 |
// import first guarantees the bug triggers every time. |
| 611 |
// |
| 612 |
// Cycle: E → A → D → E (via subject "other") |
| 613 |
// A also imports "narrow" from C (leaf, no cycle) declared first. |
| 614 |
// After processing the first import, `to` is narrowed from "*" to "narrow", |
| 615 |
// causing SubjectsCollide("narrow","other") to return false and miss the cycle. |
| 616 |
func TestAccountCycleStreamMissedDueToToMutation(t *testing.T) { |
| 617 |
conf := createConfFile(t, []byte(` |
| 618 |
accounts { |
| 619 |
A { |
| 620 |
exports [ { stream: * } ] |
| 621 |
imports [ |
| 622 |
{ stream { subject: narrow, account: C }, to: narrow } |
| 623 |
{ stream { subject: other, account: D }, to: other } |
| 624 |
] |
| 625 |
} |
| 626 |
C { |
| 627 |
exports [ { stream: narrow } ] |
| 628 |
} |
| 629 |
D { |
| 630 |
exports [ { stream: other } ] |
| 631 |
imports [ { stream { subject: other, account: E }, to: other } ] |
| 632 |
} |
| 633 |
E { |
| 634 |
exports [ { stream: * } ] |
| 635 |
imports [ { stream { subject: *, account: A } } ] |
| 636 |
} |
| 637 |
} |
| 638 |
`)) |
| 639 |
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) { |
| 640 |
t.Fatalf("Expected cycle detection error for E->A->D->E, got: %v", err) |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
func clientConnectToServer(t *testing.T, s *server.Server) *nats.Conn { |
| 645 |
t.Helper() |
| 646 |
nc, err := nats.Connect(s.ClientURL(), |
| 647 |
nats.Name("JS-TEST"), |
| 648 |
nats.ReconnectWait(5*time.Millisecond), |
| 649 |
nats.MaxReconnects(-1)) |
| 650 |
if err != nil { |
| 651 |
t.Fatalf("Failed to create client: %v", err) |
| 652 |
} |
| 653 |
return nc |
| 654 |
} |
| 655 |
|
| 656 |
func clientConnectToServerWithUP(t *testing.T, opts *server.Options, user, pass string) *nats.Conn { |
| 657 |
curl := fmt.Sprintf("nats://%s:%s@%s:%d", user, pass, opts.Host, opts.Port) |
| 658 |
nc, err := nats.Connect(curl, nats.Name("JS-UP-TEST"), nats.ReconnectWait(5*time.Millisecond), nats.MaxReconnects(-1)) |
| 659 |
if err != nil { |
| 660 |
t.Fatalf("Failed to create client: %v", err) |
| 661 |
} |
| 662 |
return nc |
| 663 |
} |