Debian Codegraph @

server/subject_fuzz_test.go

raw
1 // Copyright 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 server
15
16 import "testing"
17
18 // FuzzSubjectsCollide performs fuzz testing on the NATS subject collision detection logic.
19 // It verifies the behavior of the SubjectsCollide function which determines if two NATS
20 // subjects/subscriptions could potentially overlap in the NATS pub-sub system.
21 func FuzzSubjectsCollide(f *testing.F) {
22 corpuses := []struct {
23 s1 string
24 s2 string
25 }{
26 // SubjectsCollide true
27 {s1: "", s2: ""},
28 {s1: "a", s2: "a"},
29 {s1: "a.b.c", s2: "a.b.c"},
30 {s1: "$JS.b.c", s2: "$JS.b.c"},
31 {s1: "a.b.c", s2: "a.*.c"},
32 {s1: "a.b.*", s2: "a.*.c"},
33 {s1: "aaa.bbb.ccc", s2: "aaa.bbb.ccc"},
34 {s1: "aaa.*.ccc", s2: "*.bbb.ccc"},
35 {s1: "*", s2: "*"},
36 {s1: "**", s2: "*"},
37 {s1: "", s2: ">"},
38 {s1: ">", s2: ">"},
39 {s1: ">>", s2: ">"},
40 {s1: "a", s2: ">"},
41 {s1: "a.b.c", s2: ">"},
42 {s1: "a.b.c.>", s2: "a.b.>"},
43 {s1: "a.b.c.d.*", s2: "a.b.c.*.e"},
44 {s1: "a.*.*.d.>", s2: "a.bbb.ccc.*.e"},
45
46 // SubjectsCollide false
47 {s1: "a", s2: ""},
48 {s1: "a.b", s2: "b.a"},
49 {s1: "a.bbbbb.*.d", s2: "a.b.>"},
50 {s1: "a.b", s2: "a.b.c"},
51 {s1: "a.b.c", s2: "a.b"},
52 {s1: "a.b", s2: ""},
53 {s1: "a.*.*.d.e.>", s2: "a.bbb.ccc.*.e"},
54 }
55
56 for _, crp := range corpuses {
57 f.Add(crp.s1, crp.s2)
58 }
59
60 f.Fuzz(func(t *testing.T, s1, s2 string) {
61 if !IsValidSubject(s1) {
62 return
63 }
64
65 if !IsValidSubject(s2) {
66 return
67 }
68
69 SubjectsCollide(s1, s2)
70 })
71 }