| 1 |
// Copyright 2023-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 ( |
| 17 |
"errors" |
| 18 |
"fmt" |
| 19 |
"math" |
| 20 |
"reflect" |
| 21 |
"slices" |
| 22 |
"testing" |
| 23 |
) |
| 24 |
|
| 25 |
func TestPlaceHolderIndex(t *testing.T) { |
| 26 |
testString := "$1" |
| 27 |
transformType, indexes, nbPartitions, _, err := indexPlaceHolders(testString) |
| 28 |
var position int32 |
| 29 |
|
| 30 |
if err != nil || transformType != Wildcard || len(indexes) != 1 || indexes[0] != 1 || nbPartitions != -1 { |
| 31 |
t.Fatalf("Error parsing %s", testString) |
| 32 |
} |
| 33 |
|
| 34 |
testString = "{{partition(10,1,2,3)}}" |
| 35 |
|
| 36 |
transformType, indexes, nbPartitions, _, err = indexPlaceHolders(testString) |
| 37 |
|
| 38 |
if err != nil || transformType != Partition || !reflect.DeepEqual(indexes, []int{1, 2, 3}) || nbPartitions != 10 { |
| 39 |
t.Fatalf("Error parsing %s", testString) |
| 40 |
} |
| 41 |
|
| 42 |
testString = "{{ Partition (10,1,2,3) }}" |
| 43 |
|
| 44 |
transformType, indexes, nbPartitions, _, err = indexPlaceHolders(testString) |
| 45 |
|
| 46 |
if err != nil || transformType != Partition || !reflect.DeepEqual(indexes, []int{1, 2, 3}) || nbPartitions != 10 { |
| 47 |
t.Fatalf("Error parsing %s", testString) |
| 48 |
} |
| 49 |
|
| 50 |
testString = "{{wildcard(2)}}" |
| 51 |
transformType, indexes, nbPartitions, _, err = indexPlaceHolders(testString) |
| 52 |
|
| 53 |
if err != nil || transformType != Wildcard || len(indexes) != 1 || indexes[0] != 2 || nbPartitions != -1 { |
| 54 |
t.Fatalf("Error parsing %s", testString) |
| 55 |
} |
| 56 |
|
| 57 |
testString = "{{SplitFromLeft(2,1)}}" |
| 58 |
transformType, indexes, position, _, err = indexPlaceHolders(testString) |
| 59 |
|
| 60 |
if err != nil || transformType != SplitFromLeft || len(indexes) != 1 || indexes[0] != 2 || position != 1 { |
| 61 |
t.Fatalf("Error parsing %s", testString) |
| 62 |
} |
| 63 |
|
| 64 |
testString = "{{SplitFromRight(3,2)}}" |
| 65 |
transformType, indexes, position, _, err = indexPlaceHolders(testString) |
| 66 |
|
| 67 |
if err != nil || transformType != SplitFromRight || len(indexes) != 1 || indexes[0] != 3 || position != 2 { |
| 68 |
t.Fatalf("Error parsing %s", testString) |
| 69 |
} |
| 70 |
|
| 71 |
testString = "{{SliceFromLeft(2,2)}}" |
| 72 |
transformType, indexes, sliceSize, _, err := indexPlaceHolders(testString) |
| 73 |
|
| 74 |
if err != nil || transformType != SliceFromLeft || len(indexes) != 1 || indexes[0] != 2 || sliceSize != 2 { |
| 75 |
t.Fatalf("Error parsing %s", testString) |
| 76 |
} |
| 77 |
|
| 78 |
testString = "{{Left(3,2)}}" |
| 79 |
transformType, indexes, position, _, err = indexPlaceHolders(testString) |
| 80 |
|
| 81 |
if err != nil || transformType != Left || len(indexes) != 1 || indexes[0] != 3 || position != 2 { |
| 82 |
t.Fatalf("Error parsing %s", testString) |
| 83 |
} |
| 84 |
|
| 85 |
testString = "{{Right(3,2)}}" |
| 86 |
transformType, indexes, position, _, err = indexPlaceHolders(testString) |
| 87 |
|
| 88 |
if err != nil || transformType != Right || len(indexes) != 1 || indexes[0] != 3 || position != 2 { |
| 89 |
t.Fatalf("Error parsing %s", testString) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestSubjectTransformHelpers(t *testing.T) { |
| 94 |
equals := func(a, b []string) bool { |
| 95 |
if len(a) != len(b) { |
| 96 |
return false |
| 97 |
} |
| 98 |
for i, v := range a { |
| 99 |
if v != b[i] { |
| 100 |
return false |
| 101 |
} |
| 102 |
} |
| 103 |
return true |
| 104 |
} |
| 105 |
|
| 106 |
filter, placeHolders := transformUntokenize("bar") |
| 107 |
if filter != "bar" || len(placeHolders) != 0 { |
| 108 |
t.Fatalf("transformUntokenize for not returning expected result") |
| 109 |
} |
| 110 |
|
| 111 |
filter, placeHolders = transformUntokenize("foo.$2.$1") |
| 112 |
if filter != "foo.*.*" || !equals(placeHolders, []string{"$2", "$1"}) { |
| 113 |
t.Fatalf("transformUntokenize for not returning expected result") |
| 114 |
} |
| 115 |
|
| 116 |
filter, placeHolders = transformUntokenize("foo.{{wildcard(2)}}.{{wildcard(1)}}") |
| 117 |
if filter != "foo.*.*" || !equals(placeHolders, []string{"{{wildcard(2)}}", "{{wildcard(1)}}"}) { |
| 118 |
t.Fatalf("transformUntokenize for not returning expected result") |
| 119 |
} |
| 120 |
|
| 121 |
newReversibleTransform := func(src, dest string) *subjectTransform { |
| 122 |
tr, err := NewSubjectTransformStrict(src, dest) |
| 123 |
if err != nil { |
| 124 |
t.Fatalf("Error getting reversible transform: %s to %s", src, dest) |
| 125 |
} |
| 126 |
return tr |
| 127 |
} |
| 128 |
|
| 129 |
tr := newReversibleTransform("foo.*.*", "bar.$2.{{Wildcard(1)}}") |
| 130 |
subject := "foo.b.a" |
| 131 |
transformed := tr.TransformSubject(subject) |
| 132 |
reverse := tr.reverse() |
| 133 |
if reverse.TransformSubject(transformed) != subject { |
| 134 |
t.Fatal("Reversed transform subject not matching") |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func TestSubjectTransforms(t *testing.T) { |
| 139 |
shouldErr := func(src, dest string, strict bool) { |
| 140 |
t.Helper() |
| 141 |
if _, err := NewSubjectTransformWithStrict(src, dest, strict); err != ErrBadSubject && !errors.Is(err, ErrInvalidMappingDestination) { |
| 142 |
t.Fatalf("Did not get an error for src=%q and dest=%q", src, dest) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// Must be valid subjects. |
| 147 |
shouldErr("foo..", "bar", false) |
| 148 |
|
| 149 |
// Wildcards are allowed in src, but must be matched by token placements on the other side. |
| 150 |
// e.g. foo.* -> bar.$1. |
| 151 |
// Need to have as many pwcs as placements on other side |
| 152 |
|
| 153 |
shouldErr("foo.*", "bar.*", false) |
| 154 |
shouldErr("foo.*", "bar.$2", false) // Bad pwc token identifier |
| 155 |
shouldErr("foo.*", "bar.$1.>", false) // fwcs have to match. |
| 156 |
shouldErr("foo.>", "bar.baz", false) // fwcs have to match. |
| 157 |
shouldErr("foo.*.*", "bar.$2", true) // Must place all pwcs. |
| 158 |
shouldErr("foo.*", "foo.$foo", true) // invalid $ value |
| 159 |
shouldErr("foo.*", "bar.{{Partition(2,1)}}", true) // can only use Wildcard function (and old-style $x) in import transform |
| 160 |
shouldErr("foo.*", "foo.{{wildcard(2)}}", false) // Mapping function being passed an out of range wildcard index |
| 161 |
shouldErr("foo.*", "foo.{{unimplemented(1)}}", false) // Mapping trying to use an unknown mapping function |
| 162 |
shouldErr("foo.*", "foo.{{partition()}}", false) // Not enough arguments passed to the mapping function |
| 163 |
shouldErr("foo.*", "foo.{{random()}}", false) // Not enough arguments passed to the random function |
| 164 |
shouldErr("foo.*", "foo.{{wildcard(foo)}}", false) // Invalid argument passed to the mapping function |
| 165 |
shouldErr("foo.*", "foo.{{wildcard()}}", false) // Not enough arguments passed to the mapping function |
| 166 |
shouldErr("foo.*", "foo.{{wildcard(1,2)}}", false) // Too many arguments passed to the mapping function |
| 167 |
shouldErr("foo.*", "foo.{{ wildcard5) }}", false) // Bad mapping function |
| 168 |
shouldErr("foo.*", "foo.{{splitLeft(2,2}}", false) // arg out of range |
| 169 |
shouldErr("foo", "bla.{{wildcard(1)}}", false) // arg out of range with no wildcard in the source |
| 170 |
|
| 171 |
shouldErr("foo.*", fmt.Sprintf("foo.{{partition(%d)}}", math.MaxInt32+1), false) // Larger than int32 |
| 172 |
shouldErr("foo.*", fmt.Sprintf("foo.{{random(%d)}}", math.MaxInt32+1), false) // Larger than int32 |
| 173 |
|
| 174 |
shouldBeOK := func(src, dest string, strict bool) *subjectTransform { |
| 175 |
t.Helper() |
| 176 |
tr, err := NewSubjectTransformWithStrict(src, dest, strict) |
| 177 |
if err != nil { |
| 178 |
t.Fatalf("Got an error %v for src=%q and dest=%q", err, src, dest) |
| 179 |
} |
| 180 |
return tr |
| 181 |
} |
| 182 |
|
| 183 |
shouldBeOK("foo.*", "bar.{{Wildcard(1)}}", true) |
| 184 |
|
| 185 |
shouldBeOK("foo.*.*", "bar.$2", false) // don't have to use all pwcs. |
| 186 |
shouldBeOK("foo.*.*", "bar.{{wildcard(1)}}", false) // don't have to use all pwcs. |
| 187 |
shouldBeOK("foo.*.*", "bar.{{partition(1)}}", false) |
| 188 |
shouldBeOK("foo.*.*", "bar.{{random(5)}}", false) |
| 189 |
shouldBeOK("foo", "bar", false) |
| 190 |
shouldBeOK("foo.*.bar.*.baz", "req.$2.$1", false) |
| 191 |
shouldBeOK("baz.>", "mybaz.>", false) |
| 192 |
shouldBeOK("*", "{{splitfromleft(1,1)}}", false) |
| 193 |
shouldBeOK("", "prefix.>", false) |
| 194 |
shouldBeOK("*.*", "{{partition(10,1,2)}}", false) |
| 195 |
shouldBeOK("foo.*.*", "foo.{{wildcard(1)}}.{{wildcard(2)}}.{{partition(5,1,2)}}", false) |
| 196 |
|
| 197 |
shouldBeOK("foo.*", fmt.Sprintf("foo.{{partition(%d)}}", math.MaxInt32), false) // Exactly int32 |
| 198 |
shouldBeOK("foo.*", fmt.Sprintf("foo.{{random(%d)}}", math.MaxInt32), false) // Exactly int32 |
| 199 |
shouldBeOK("foo.bar", fmt.Sprintf("foo.{{random(%d)}}", math.MaxInt32), false) // Exactly int32 |
| 200 |
|
| 201 |
shouldMatch := func(src, dest, sample string, expected ...string) { |
| 202 |
t.Helper() |
| 203 |
tr := shouldBeOK(src, dest, false) |
| 204 |
if tr != nil { |
| 205 |
s, err := tr.Match(sample) |
| 206 |
if err != nil { |
| 207 |
t.Fatalf("Got an error %v when expecting a match for %q to %q", err, sample, expected) |
| 208 |
} |
| 209 |
if !slices.Contains(expected, s) { |
| 210 |
t.Fatalf("Dest does not match what was expected. Got %q, expected %q", s, expected) |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
shouldMatch("", "prefix.>", "foo", "prefix.foo") |
| 216 |
shouldMatch("foo", "", "foo", "foo") |
| 217 |
shouldMatch("foo", "bar", "foo", "bar") |
| 218 |
shouldMatch("foo.*.bar.*.baz", "req.$2.$1", "foo.A.bar.B.baz", "req.B.A") |
| 219 |
shouldMatch("foo.*.bar.*.baz", "req.{{wildcard(2)}}.{{wildcard(1)}}", "foo.A.bar.B.baz", "req.B.A") |
| 220 |
shouldMatch("baz.>", "my.pre.>", "baz.1.2.3", "my.pre.1.2.3") |
| 221 |
shouldMatch("baz.>", "foo.bar.>", "baz.1.2.3", "foo.bar.1.2.3") |
| 222 |
shouldMatch("*", "foo.bar.$1", "foo", "foo.bar.foo") |
| 223 |
shouldMatch("*", "{{splitfromleft(1,3)}}", "12345", "123.45") |
| 224 |
shouldMatch("*", "{{SplitFromRight(1,3)}}", "12345", "12.345") |
| 225 |
shouldMatch("*", "{{SliceFromLeft(1,3)}}", "1234567890", "123.456.789.0") |
| 226 |
shouldMatch("*", "{{SliceFromRight(1,3)}}", "1234567890", "1.234.567.890") |
| 227 |
shouldMatch("*", "{{split(1,-)}}", "-abc-def--ghi-", "abc.def.ghi") |
| 228 |
shouldMatch("*", "{{split(1,-)}}", "abc-def--ghi-", "abc.def.ghi") |
| 229 |
shouldMatch("*.*", "{{split(2,-)}}.{{splitfromleft(1,2)}}", "foo.-abc-def--ghij-", "abc.def.ghij.fo.o") // combo + checks split for multiple instance of deliminator and deliminator being at the start or end |
| 230 |
shouldMatch("*", "{{right(1,1)}}", "1234", "4") |
| 231 |
shouldMatch("*", "{{right(1,3)}}", "1234", "234") |
| 232 |
shouldMatch("*", "{{right(1,6)}}", "1234", "1234") |
| 233 |
shouldMatch("*", "{{left(1,1)}}", "1234", "1") |
| 234 |
shouldMatch("*", "{{left(1,3)}}", "1234", "123") |
| 235 |
shouldMatch("*", "{{left(1,6)}}", "1234", "1234") |
| 236 |
shouldMatch("*", "bar.{{partition(0)}}", "baz", "bar.0") |
| 237 |
shouldMatch("*", "bar.{{partition(10, 0)}}", "foo", "bar.3") |
| 238 |
shouldMatch("*.*", "bar.{{partition(10)}}", "foo.bar", "bar.6") |
| 239 |
shouldMatch("*", "bar.{{partition(10)}}", "foo", "bar.3") |
| 240 |
shouldMatch("*", "bar.{{partition(10)}}", "baz", "bar.0") |
| 241 |
shouldMatch("*", "bar.{{partition(10)}}", "qux", "bar.9") |
| 242 |
shouldMatch("*", "bar.{{random(0)}}", "qux", "bar.0") |
| 243 |
for range 100 { |
| 244 |
shouldMatch("*", "bar.{{random(6)}}", "qux", "bar.0", "bar.1", "bar.2", "bar.3", "bar.4", "bar.5") |
| 245 |
} |
| 246 |
shouldBeOK("foo.bar", "baz.{{partition(10)}}", false) |
| 247 |
shouldMatch("foo.bar", "baz.{{partition(10)}}", "foo.bar", "baz.6") |
| 248 |
shouldMatch("foo.baz", "qux.{{partition(10)}}", "foo.baz", "qux.4") |
| 249 |
shouldMatch("test.subject", "result.{{partition(5)}}", "test.subject", "result.0") |
| 250 |
} |
| 251 |
|
| 252 |
func TestSubjectTransformDoesntPanicTransformingMissingToken(t *testing.T) { |
| 253 |
defer func() { |
| 254 |
p := recover() |
| 255 |
require_True(t, p == nil) |
| 256 |
}() |
| 257 |
|
| 258 |
tr, err := NewSubjectTransform("foo.*", "one.two.{{wildcard(1)}}") |
| 259 |
require_NoError(t, err) |
| 260 |
require_Equal(t, tr.TransformTokenizedSubject([]string{"foo"}), "one.two.") |
| 261 |
} |