| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package consistent |
| 5 |
|
| 6 |
import ( |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
) |
| 13 |
|
| 14 |
func testName(in string) string { |
| 15 |
x := strings.NewReplacer(":", "_", ";", "_").Replace(in) |
| 16 |
if len(x) > 32 { |
| 17 |
return "" |
| 18 |
} |
| 19 |
return x |
| 20 |
} |
| 21 |
|
| 22 |
func TestNewTraceState(t *testing.T) { |
| 23 |
otts := newTraceState() |
| 24 |
require.False(t, otts.hasPValue()) |
| 25 |
require.False(t, otts.hasRValue()) |
| 26 |
require.Empty(t, otts.serialize()) |
| 27 |
} |
| 28 |
|
| 29 |
func TestTraceStatePRValueSerialize(t *testing.T) { |
| 30 |
otts := newTraceState() |
| 31 |
otts.pvalue = 3 |
| 32 |
otts.rvalue = 4 |
| 33 |
otts.unknown = []string{"a:b", "c:d"} |
| 34 |
require.True(t, otts.hasPValue()) |
| 35 |
require.True(t, otts.hasRValue()) |
| 36 |
require.Equal(t, "p:3;r:4;a:b;c:d", otts.serialize()) |
| 37 |
} |
| 38 |
|
| 39 |
func TestTraceStateSerializeOverflow(t *testing.T) { |
| 40 |
long := "x:" + strings.Repeat(".", 254) |
| 41 |
otts := newTraceState() |
| 42 |
otts.unknown = []string{long} |
| 43 |
// this drops the extra key, sorry! |
| 44 |
require.Equal(t, long, otts.serialize()) |
| 45 |
otts.pvalue = 1 |
| 46 |
require.Equal(t, "p:1", otts.serialize()) |
| 47 |
} |
| 48 |
|
| 49 |
func TestParseTraceStateUnsampled(t *testing.T) { |
| 50 |
type testCase struct { |
| 51 |
in string |
| 52 |
rval uint8 |
| 53 |
expectErr error |
| 54 |
} |
| 55 |
const notset = 255 |
| 56 |
for _, test := range []testCase{ |
| 57 |
// All are unsampled tests, i.e., `sampled` is not set in traceparent. |
| 58 |
{"r:2", 2, nil}, |
| 59 |
{"r:1;", notset, strconv.ErrSyntax}, |
| 60 |
{"r:1", 1, nil}, |
| 61 |
{"r:1=p:2", notset, strconv.ErrSyntax}, |
| 62 |
{"r:1;p:2=s:3", notset, strconv.ErrSyntax}, |
| 63 |
{":1;p:2=s:3", notset, strconv.ErrSyntax}, |
| 64 |
{":;p:2=s:3", notset, strconv.ErrSyntax}, |
| 65 |
{":;:", notset, strconv.ErrSyntax}, |
| 66 |
{":", notset, strconv.ErrSyntax}, |
| 67 |
{"", notset, nil}, |
| 68 |
{"r:;p=1", notset, strconv.ErrSyntax}, |
| 69 |
{"r:1", 1, nil}, |
| 70 |
{"r:10", 10, nil}, |
| 71 |
{"r:33", 33, nil}, |
| 72 |
{"r:61", 61, nil}, |
| 73 |
{"r:62", 62, nil}, // max r-value |
| 74 |
{"r:63", notset, strconv.ErrRange}, // out-of-range |
| 75 |
{"r:100", notset, strconv.ErrRange}, // out-of-range |
| 76 |
{"r:100001", notset, strconv.ErrRange}, // out-of-range |
| 77 |
{"p:64", notset, strconv.ErrRange}, |
| 78 |
{"p:100", notset, strconv.ErrRange}, |
| 79 |
{"r:1a", notset, strconv.ErrSyntax}, // not-hexadecimal |
| 80 |
{"p:-1", notset, strconv.ErrSyntax}, // non-negative |
| 81 |
|
| 82 |
// Inconsistent trace state: any p-value when unsampled |
| 83 |
{"p:4;r:2", 2, errTraceStateInconsistent}, |
| 84 |
{"p:1;r:2", 2, errTraceStateInconsistent}, |
| 85 |
} { |
| 86 |
t.Run(testName(test.in), func(t *testing.T) { |
| 87 |
// Note: passing isSampled=false as stated above. |
| 88 |
otts, err := parseOTelTraceState(test.in, false) |
| 89 |
|
| 90 |
require.False(t, otts.hasPValue(), "should have no p-value") |
| 91 |
|
| 92 |
if test.expectErr != nil { |
| 93 |
require.ErrorIs(t, err, test.expectErr, "not expecting %v", err) |
| 94 |
} |
| 95 |
if test.rval != notset { |
| 96 |
require.True(t, otts.hasRValue()) |
| 97 |
require.Equal(t, test.rval, otts.rvalue) |
| 98 |
} else { |
| 99 |
require.False(t, otts.hasRValue(), "should have no r-value") |
| 100 |
} |
| 101 |
require.Equal(t, []string(nil), otts.unknown) |
| 102 |
|
| 103 |
if test.expectErr == nil { |
| 104 |
// Require serialize to round-trip |
| 105 |
otts2, err := parseOTelTraceState(otts.serialize(), false) |
| 106 |
require.NoError(t, err) |
| 107 |
require.Equal(t, otts, otts2) |
| 108 |
} |
| 109 |
}) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func TestParseTraceStateSampled(t *testing.T) { |
| 114 |
type testCase struct { |
| 115 |
in string |
| 116 |
rval, pval uint8 |
| 117 |
expectErr error |
| 118 |
} |
| 119 |
const notset = 255 |
| 120 |
for _, test := range []testCase{ |
| 121 |
// All are sampled tests, i.e., `sampled` is set in traceparent. |
| 122 |
{"r:2;p:2", 2, 2, nil}, |
| 123 |
{"r:2;p:1", 2, 1, nil}, |
| 124 |
{"r:2;p:0", 2, 0, nil}, |
| 125 |
|
| 126 |
{"r:1;p:1", 1, 1, nil}, |
| 127 |
{"r:1;p:0", 1, 0, nil}, |
| 128 |
|
| 129 |
{"r:0;p:0", 0, 0, nil}, |
| 130 |
|
| 131 |
{"r:62;p:0", 62, 0, nil}, |
| 132 |
{"r:62;p:62", 62, 62, nil}, |
| 133 |
|
| 134 |
// The important special case: |
| 135 |
{"r:0;p:63", 0, 63, nil}, |
| 136 |
{"r:2;p:63", 2, 63, nil}, |
| 137 |
{"r:62;p:63", 62, 63, nil}, |
| 138 |
|
| 139 |
// Inconsistent p causes unset p-value. |
| 140 |
{"r:2;p:3", 2, notset, errTraceStateInconsistent}, |
| 141 |
{"r:2;p:4", 2, notset, errTraceStateInconsistent}, |
| 142 |
{"r:2;p:62", 2, notset, errTraceStateInconsistent}, |
| 143 |
{"r:0;p:1", 0, notset, errTraceStateInconsistent}, |
| 144 |
{"r:1;p:2", 1, notset, errTraceStateInconsistent}, |
| 145 |
{"r:61;p:62", 61, notset, errTraceStateInconsistent}, |
| 146 |
|
| 147 |
// Inconsistent r causes unset p-value and r-value. |
| 148 |
{"r:63;p:2", notset, notset, strconv.ErrRange}, |
| 149 |
{"r:120;p:2", notset, notset, strconv.ErrRange}, |
| 150 |
{"r:ab;p:2", notset, notset, strconv.ErrSyntax}, |
| 151 |
|
| 152 |
// Syntax is tested before range errors |
| 153 |
{"r:ab;p:77", notset, notset, strconv.ErrSyntax}, |
| 154 |
|
| 155 |
// p without r (when sampled) |
| 156 |
{"p:1", notset, 1, nil}, |
| 157 |
{"p:62", notset, 62, nil}, |
| 158 |
{"p:63", notset, 63, nil}, |
| 159 |
|
| 160 |
// r without p (when sampled) |
| 161 |
{"r:2", 2, notset, nil}, |
| 162 |
{"r:62", 62, notset, nil}, |
| 163 |
{"r:0", 0, notset, nil}, |
| 164 |
} { |
| 165 |
t.Run(testName(test.in), func(t *testing.T) { |
| 166 |
// Note: passing isSampled=true as stated above. |
| 167 |
otts, err := parseOTelTraceState(test.in, true) |
| 168 |
|
| 169 |
if test.expectErr != nil { |
| 170 |
require.ErrorIs(t, err, test.expectErr, "not expecting %v", err) |
| 171 |
} else { |
| 172 |
require.NoError(t, err) |
| 173 |
} |
| 174 |
if test.pval != notset { |
| 175 |
require.True(t, otts.hasPValue()) |
| 176 |
require.Equal(t, test.pval, otts.pvalue) |
| 177 |
} else { |
| 178 |
require.False(t, otts.hasPValue(), "should have no p-value") |
| 179 |
} |
| 180 |
if test.rval != notset { |
| 181 |
require.True(t, otts.hasRValue()) |
| 182 |
require.Equal(t, test.rval, otts.rvalue) |
| 183 |
} else { |
| 184 |
require.False(t, otts.hasRValue(), "should have no r-value") |
| 185 |
} |
| 186 |
require.Equal(t, []string(nil), otts.unknown) |
| 187 |
|
| 188 |
if test.expectErr == nil { |
| 189 |
// Require serialize to round-trip |
| 190 |
otts2, err := parseOTelTraceState(otts.serialize(), true) |
| 191 |
require.NoError(t, err) |
| 192 |
require.Equal(t, otts, otts2) |
| 193 |
} |
| 194 |
}) |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
func TestParseTraceStateExtra(t *testing.T) { |
| 199 |
type testCase struct { |
| 200 |
in string |
| 201 |
rval, pval uint8 |
| 202 |
sampled bool |
| 203 |
extra []string |
| 204 |
expectErr error |
| 205 |
} |
| 206 |
const notset = 255 |
| 207 |
for _, test := range []testCase{ |
| 208 |
// one field |
| 209 |
{"e100:1", notset, notset, false, []string{"e100:1"}, nil}, |
| 210 |
|
| 211 |
// two fields |
| 212 |
{"e1:1;e2:2", notset, notset, false, []string{"e1:1", "e2:2"}, nil}, |
| 213 |
{"e1:1;e2:2", notset, notset, false, []string{"e1:1", "e2:2"}, nil}, |
| 214 |
|
| 215 |
// one extra key, three ways |
| 216 |
{"r:2;p:2;extra:stuff", 2, 2, true, []string{"extra:stuff"}, nil}, |
| 217 |
{"extra:stuff;r:2;p:2", 2, 2, true, []string{"extra:stuff"}, nil}, |
| 218 |
{"p:2;extra:stuff;r:2", 2, 2, true, []string{"extra:stuff"}, nil}, |
| 219 |
|
| 220 |
// extra with inconsistent p with and without sampling |
| 221 |
{"r:3;extra:stuff;p:4", 3, notset, true, []string{"extra:stuff"}, errTraceStateInconsistent}, |
| 222 |
{"extra:stuff;r:3;p:2", 3, notset, false, []string{"extra:stuff"}, errTraceStateInconsistent}, |
| 223 |
|
| 224 |
// two extra fields |
| 225 |
{"e100:100;r:2;p:1;e101:101", 2, 1, true, []string{"e100:100", "e101:101"}, nil}, |
| 226 |
{"r:2;p:1;e100:100;e101:101", 2, 1, true, []string{"e100:100", "e101:101"}, nil}, |
| 227 |
{"e100:100;e101:101;r:2;p:1", 2, 1, true, []string{"e100:100", "e101:101"}, nil}, |
| 228 |
|
| 229 |
// parse error prevents capturing unrecognized keys |
| 230 |
{"1:1;u:V", notset, notset, true, nil, strconv.ErrSyntax}, |
| 231 |
{"X:1;u:V", notset, notset, true, nil, strconv.ErrSyntax}, |
| 232 |
{"x:1;u:V", notset, notset, true, []string{"x:1", "u:V"}, nil}, |
| 233 |
|
| 234 |
// no trailing ; |
| 235 |
{"x:1;", notset, notset, true, nil, strconv.ErrSyntax}, |
| 236 |
|
| 237 |
// empty key |
| 238 |
{"x:", notset, notset, true, []string{"x:"}, nil}, |
| 239 |
|
| 240 |
// charset test |
| 241 |
{"x:0X1FFF;y:.-_-.;z:", notset, notset, true, []string{"x:0X1FFF", "y:.-_-.", "z:"}, nil}, |
| 242 |
{"x1y2z3:1-2-3;y1:y_1;xy:-;r:50", 50, notset, true, []string{"x1y2z3:1-2-3", "y1:y_1", "xy:-"}, nil}, |
| 243 |
|
| 244 |
// size exceeded |
| 245 |
{"x:" + strings.Repeat("_", 255), notset, notset, false, nil, strconv.ErrSyntax}, |
| 246 |
{"x:" + strings.Repeat("_", 254), notset, notset, false, []string{"x:" + strings.Repeat("_", 254)}, nil}, |
| 247 |
} { |
| 248 |
t.Run(testName(test.in), func(t *testing.T) { |
| 249 |
// Note: These tests are independent of sampling state, |
| 250 |
// so both are tested. |
| 251 |
otts, err := parseOTelTraceState(test.in, test.sampled) |
| 252 |
|
| 253 |
if test.expectErr != nil { |
| 254 |
require.ErrorIs(t, err, test.expectErr, "not expecting %v", err) |
| 255 |
} else { |
| 256 |
require.NoError(t, err) |
| 257 |
} |
| 258 |
if test.pval != notset { |
| 259 |
require.True(t, otts.hasPValue()) |
| 260 |
require.Equal(t, test.pval, otts.pvalue) |
| 261 |
} else { |
| 262 |
require.False(t, otts.hasPValue(), "should have no p-value") |
| 263 |
} |
| 264 |
if test.rval != notset { |
| 265 |
require.True(t, otts.hasRValue()) |
| 266 |
require.Equal(t, test.rval, otts.rvalue) |
| 267 |
} else { |
| 268 |
require.False(t, otts.hasRValue(), "should have no r-value") |
| 269 |
} |
| 270 |
require.Equal(t, test.extra, otts.unknown) |
| 271 |
|
| 272 |
// on success w/o r-value or p-value, serialize() should not modify |
| 273 |
if !otts.hasRValue() && !otts.hasPValue() && test.expectErr == nil { |
| 274 |
require.Equal(t, test.in, otts.serialize()) |
| 275 |
} |
| 276 |
}) |
| 277 |
} |
| 278 |
} |