| 1 |
// Copyright 2020-2023 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 parser |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"math" |
| 19 |
"reflect" |
| 20 |
"strconv" |
| 21 |
"strings" |
| 22 |
"testing" |
| 23 |
) |
| 24 |
|
| 25 |
func TestParseNum(t *testing.T) { |
| 26 |
tests := []struct { |
| 27 |
name string |
| 28 |
given string |
| 29 |
expected uint64 |
| 30 |
}{ |
| 31 |
{ |
| 32 |
name: "parse 191817", |
| 33 |
given: "191817", |
| 34 |
expected: 191817, |
| 35 |
}, |
| 36 |
{ |
| 37 |
name: "parse 0", |
| 38 |
given: "0", |
| 39 |
expected: 0, |
| 40 |
}, |
| 41 |
{ |
| 42 |
name: "parse 00", |
| 43 |
given: "00", |
| 44 |
expected: 0, |
| 45 |
}, |
| 46 |
{ |
| 47 |
name: "empty string", |
| 48 |
given: "", |
| 49 |
expected: 0, |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "negative number", |
| 53 |
given: "-123", |
| 54 |
expected: 0, |
| 55 |
}, |
| 56 |
{ |
| 57 |
name: "not a number", |
| 58 |
given: "abc", |
| 59 |
expected: 0, |
| 60 |
}, |
| 61 |
{ |
| 62 |
name: "max uit64", |
| 63 |
given: strconv.FormatUint(math.MaxUint64, 10), |
| 64 |
expected: math.MaxUint64, |
| 65 |
}, |
| 66 |
{ |
| 67 |
name: "max uit64 + 2, overflow", |
| 68 |
given: "18446744073709551617", |
| 69 |
expected: 1, |
| 70 |
}, |
| 71 |
} |
| 72 |
|
| 73 |
for _, test := range tests { |
| 74 |
t.Run(test.name, func(t *testing.T) { |
| 75 |
res := ParseNum(test.given) |
| 76 |
if res != test.expected { |
| 77 |
t.Fatalf("Invalid result; want: %d; got: %d", test.expected, res) |
| 78 |
} |
| 79 |
}) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func FuzzParseNum(f *testing.F) { |
| 84 |
testcases := []string{"191817", " ", "-123", "abc"} |
| 85 |
for _, tc := range testcases { |
| 86 |
f.Add(tc) |
| 87 |
} |
| 88 |
|
| 89 |
f.Fuzz(func(t *testing.T, given string) { |
| 90 |
given = strings.TrimLeft(given, "+") |
| 91 |
res := ParseNum(given) |
| 92 |
parsed, err := strconv.ParseUint(given, 10, 64) |
| 93 |
if err != nil && !errors.Is(err, strconv.ErrRange) { |
| 94 |
if res != 0 { |
| 95 |
t.Errorf("given: %s; expected: -1; got: %d; err: %v", given, res, err) |
| 96 |
} |
| 97 |
} else if err == nil && res != parsed { |
| 98 |
t.Errorf("given: %s; expected: %d; got: %d", given, parsed, res) |
| 99 |
} |
| 100 |
}) |
| 101 |
} |
| 102 |
|
| 103 |
func TestGetMetadataFields(t *testing.T) { |
| 104 |
tests := []struct { |
| 105 |
name string |
| 106 |
subject string |
| 107 |
expected []string |
| 108 |
withError error |
| 109 |
}{ |
| 110 |
{ |
| 111 |
name: "parse v2 tokens", |
| 112 |
subject: "$JS.ACK.domain.hash-123.stream.cons.100.200.150.123456789.100.token", |
| 113 |
expected: []string{"$JS", "ACK", "domain", "hash-123", "stream", "cons", "100", "200", "150", "123456789", "100", "token"}, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "parse v2 tokens with underscore domain", |
| 117 |
subject: "$JS.ACK._.hash-123.stream.cons.100.200.150.123456789.100.token", |
| 118 |
expected: []string{"$JS", "ACK", "", "hash-123", "stream", "cons", "100", "200", "150", "123456789", "100", "token"}, |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "parse v1 tokens", |
| 122 |
subject: "$JS.ACK.stream.cons.100.200.150.123456789.100", |
| 123 |
expected: []string{"$JS", "ACK", "", "", "stream", "cons", "100", "200", "150", "123456789", "100"}, |
| 124 |
}, |
| 125 |
{ |
| 126 |
name: "invalid start of subject", |
| 127 |
subject: "$ABC.123.stream.cons.100.200.150.123456789.100", |
| 128 |
withError: ErrInvalidSubjectFormat, |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "invalid subject length (10)", |
| 132 |
subject: "$JS.ACK.stream.cons.100.200.150.123456789.100.ABC", |
| 133 |
withError: ErrInvalidSubjectFormat, |
| 134 |
}, |
| 135 |
{ |
| 136 |
name: "invalid subject length (5)", |
| 137 |
subject: "$JS.ACK.stream.cons.100", |
| 138 |
withError: ErrInvalidSubjectFormat, |
| 139 |
}, |
| 140 |
{ |
| 141 |
name: "invalid subject ", |
| 142 |
subject: "", |
| 143 |
withError: ErrInvalidSubjectFormat, |
| 144 |
}, |
| 145 |
} |
| 146 |
|
| 147 |
for _, test := range tests { |
| 148 |
t.Run(test.name, func(t *testing.T) { |
| 149 |
res, err := GetMetadataFields(test.subject) |
| 150 |
if test.withError != nil { |
| 151 |
if err == nil || !errors.Is(err, test.withError) { |
| 152 |
t.Fatalf("Expected error: %v; got: %v", test.withError, err) |
| 153 |
} |
| 154 |
return |
| 155 |
} |
| 156 |
if !reflect.DeepEqual(test.expected, res) { |
| 157 |
t.Fatalf("Invalid result; want: %v; got: %v", test.expected, res) |
| 158 |
} |
| 159 |
}) |
| 160 |
} |
| 161 |
} |