forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaggageTest.kt
More file actions
1158 lines (1030 loc) · 32.4 KB
/
Copy pathBaggageTest.kt
File metadata and controls
1158 lines (1030 loc) · 32.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package io.sentry
import com.github.javafaker.Faker
import io.sentry.Baggage.MAX_BAGGAGE_LIST_MEMBER_COUNT
import io.sentry.Baggage.MAX_BAGGAGE_STRING_LENGTH
import io.sentry.protocol.SentryId
import io.sentry.protocol.TransactionNameSource
import java.util.UUID
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
class BaggageTest {
lateinit var logger: ILogger
@BeforeTest
fun setup() {
logger = NoOpLogger.getInstance()
}
@Test
fun `can parse single baggage string with white spaces in it`() {
val baggage =
Baggage.fromHeader(
"sentry-userId = alice , sentry-serverNode = DF%2028,sentry-isProduction=false",
false,
logger,
)
assertEquals("alice", baggage.get("sentry-userId"))
assertEquals("DF 28", baggage.get("sentry-serverNode"))
assertEquals("false", baggage.get("sentry-isProduction"))
assertEquals(
"sentry-isProduction=false,sentry-serverNode=DF%2028,sentry-userId=alice",
baggage.toHeaderString(null),
)
}
@Test
fun `retain single third party baggage string`() {
val baggage =
Baggage.fromHeader("userId=alice,serverNode=DF%2028,isProduction=false", true, logger)
assertEquals(
"userId=alice,serverNode=DF%2028,isProduction=false",
baggage.toHeaderString(baggage.getThirdPartyHeader()),
)
}
@Test
fun `sentry values in third party baggage string are removed`() {
val baggage =
Baggage.fromHeader(
"userId=alice,sentry-thirdParty=thirdPartyValue,serverNode=DF%2028,isProduction=false",
true,
logger,
)
assertEquals(
"userId=alice,serverNode=DF%2028,isProduction=false",
baggage.getThirdPartyHeader(),
)
}
@Test
fun `third party headers are dropped if not specified to keep`() {
val baggage =
Baggage.fromHeader("userId=alice,sentry-serverNode=DF%2028,isProduction=false", logger)
assertEquals("sentry-serverNode=DF%2028", baggage.toHeaderString(null))
}
@Test
fun `multiple baggage headers are merged into one, third party headers are dropped`() {
val headerValues =
listOf(
"userId=alice,sentry-serverNode=DF%2028,isProduction=false",
"sentry-environment=production, os=android",
"trace=123456, isSampled=true",
)
val baggage = Baggage.fromHeader(headerValues, logger)
assertEquals(
"sentry-environment=production,sentry-serverNode=DF%2028",
baggage.toHeaderString(null),
)
}
@Test
fun `sentry entries are stripped from third party headers and saved in original order`() {
val headerValues =
listOf(
"userId=alice,sentry-serverNode=DF%2028,isProduction=false",
"sentry-environment=production, os=android",
"trace=123456, isSampled=true",
)
val baggage = Baggage.fromHeader(headerValues, true, logger)
assertEquals(
"userId=alice,isProduction=false,os=android,trace=123456,isSampled=true",
baggage.thirdPartyHeader,
)
}
@Test
fun `when merging incoming and thirdparty baggages, sentry values are appended at the end`() {
val headerValues =
listOf(
"userId=alice,sentry-serverNode=DF%2028,isProduction=false",
"sentry-environment=production, os=android",
"trace=123456, isSampled=true",
)
val baggage = Baggage.fromHeader(headerValues, false, logger)
val thirdPartyBaggage = Baggage.fromHeader(headerValues, true, logger)
assertEquals(
"userId=alice,isProduction=false,os=android,trace=123456,isSampled=true,sentry-environment=production,sentry-serverNode=DF%2028",
baggage.toHeaderString(thirdPartyBaggage.thirdPartyHeader),
)
}
@Test
fun `when merging and thirdparty headers already use up the available space, sentry values are not added`() {
val largeThirdPartyHeaderValue =
Faker.instance()
.random()
.hex(
MAX_BAGGAGE_STRING_LENGTH - 16 - 12 - 1
) // 8192 - "large-value=" - "otherValue=value" - ","
val incomingHeaderValues =
listOf(
"userId=alice,sentry-serverNode=DF%2028,isProduction=false",
"sentry-environment=production, os=android",
"trace=123456, isSampled=true",
)
val thirdPartyHeaderValues = "large-value=$largeThirdPartyHeaderValue, otherValue=value"
val baggage = Baggage.fromHeader(incomingHeaderValues, false, logger)
val thirdPartyBaggage = Baggage.fromHeader(thirdPartyHeaderValues, true, logger)
val headerString = baggage.toHeaderString(thirdPartyBaggage.thirdPartyHeader)
assertEquals(MAX_BAGGAGE_STRING_LENGTH, headerString.length)
assertEquals("large-value=$largeThirdPartyHeaderValue,otherValue=value", headerString)
}
@Test
fun `when merging third party baggage is kept even if it exceeds the allowed max length`() {
val largeThirdPartyHeaderValue = Faker.instance().random().hex(MAX_BAGGAGE_STRING_LENGTH)
val headerValues =
listOf(
"userId=alice,sentry-serverNode=DF%2028,isProduction=false",
"sentry-environment=production, os=android",
"trace=123456, isSampled=true",
)
val baggage = Baggage.fromHeader(headerValues, false, logger)
val thirdPartyBaggage =
Baggage.fromHeader("largeHeader=$largeThirdPartyHeaderValue", true, logger)
val headerString = baggage.toHeaderString(thirdPartyBaggage.thirdPartyHeader)
assertEquals(MAX_BAGGAGE_STRING_LENGTH + 12, headerString.length)
assertEquals("largeHeader=$largeThirdPartyHeaderValue", headerString)
}
@Test
fun `exceeding entry limit causes values to be dropped`() {
val baggage = Baggage(logger)
val expectedItems = mutableListOf<String>()
for (i in 1..100) {
val key = 100 + i
baggage.set("a$key", "$i")
if (i <= MAX_BAGGAGE_LIST_MEMBER_COUNT) {
expectedItems.add("a$key=$i")
}
}
val expectedHeaderString = expectedItems.joinToString(",")
assertEquals(expectedHeaderString, baggage.toHeaderString(null))
}
@Test
fun `if third party header exceeds entry limit, sentry headers are not added`() {
val baggage = Baggage(logger)
baggage.set("sentry-one", "value1")
baggage.set("sentry-two", "value2")
val thirdPartyItems = mutableListOf<String>()
for (i in 1..70) {
val key = 70 + i
thirdPartyItems.add("a$key=$i")
}
val thirdPartyHeaderString = thirdPartyItems.joinToString(",")
assertEquals(thirdPartyHeaderString, baggage.toHeaderString(thirdPartyHeaderString))
}
@Test
fun `if third party header is close to entry limit only some sentry headers are added`() {
val baggage = Baggage(logger)
baggage.set("sentry-one", "value1")
baggage.set("sentry-two", "value2")
val thirdPartyItems = mutableListOf<String>()
for (i in 1..63) {
val key = 63 + i
thirdPartyItems.add("a$key=$i")
}
val thirdPartyHeaderString = thirdPartyItems.joinToString(",")
val expectedHeaderString = "$thirdPartyHeaderString,sentry-one=value1"
assertEquals(expectedHeaderString, baggage.toHeaderString(thirdPartyHeaderString))
}
@Test
fun `keys are encoded and decoded as well`() {
val baggage =
Baggage.fromHeader(
"sentry-user%2Bid=alice,sentry-server%2Bnode=DF%2028,sentry-is%2Bproduction=false",
logger,
)
assertEquals("alice", baggage.get("sentry-user+id"))
assertEquals("DF 28", baggage.get("sentry-server+node"))
assertEquals("false", baggage.get("sentry-is+production"))
assertEquals(
"sentry-is%2Bproduction=false,sentry-server%2Bnode=DF%2028,sentry-user%2Bid=alice",
baggage.toHeaderString(null),
)
}
@Test
fun `can parse multiple baggage strings`() {
val baggage =
Baggage.fromHeader(
listOf("sentry-userId=alice", "sentry-serverNode=DF%2028,sentry-isProduction=false"),
logger,
)
assertEquals("alice", baggage.get("sentry-userId"))
assertEquals("DF 28", baggage.get("sentry-serverNode"))
assertEquals("false", baggage.get("sentry-isProduction"))
assertEquals(
"sentry-isProduction=false,sentry-serverNode=DF%2028,sentry-userId=alice",
baggage.toHeaderString(null),
)
}
@Test
fun `can parse multiple baggage strings with white spaces`() {
val baggage =
Baggage.fromHeader(
listOf(
"sentry-userId = alice",
"sentry-serverNode = DF%2028, sentry-isProduction = false",
),
logger,
)
assertEquals("alice", baggage.get("sentry-userId"))
assertEquals("DF 28", baggage.get("sentry-serverNode"))
assertEquals("false", baggage.get("sentry-isProduction"))
assertEquals(
"sentry-isProduction=false,sentry-serverNode=DF%2028,sentry-userId=alice",
baggage.toHeaderString(null),
)
}
@Test
fun `can parse null baggage string`() {
val nothing: String? = null
val baggage = Baggage.fromHeader(nothing, logger)
assertEquals("", baggage.toHeaderString(null))
}
@Test
fun `can parse blank baggage string`() {
val baggage = Baggage.fromHeader("", logger)
assertEquals("", baggage.toHeaderString(null))
}
@Test
fun `can parse whitespace only baggage string`() {
val baggage = Baggage.fromHeader(" ", logger)
assertEquals("", baggage.toHeaderString(null))
}
@Test
fun `can parse whitespace only baggage strings`() {
val baggage = Baggage.fromHeader(listOf(" ", " "), logger)
assertEquals("", baggage.toHeaderString(null))
}
@Test
fun `single large value is dropped and small values are kept`() {
val largeValue = Faker.instance().random().hex(8193)
val baggage =
Baggage.fromHeader(
"sentry-smallValue=remains,sentry-largeValue=$largeValue,sentry-otherValue=kept",
logger,
)
assertEquals("remains", baggage.get("sentry-smallValue"))
assertNotNull(baggage.get("sentry-largeValue"))
assertEquals("kept", baggage.get("sentry-otherValue"))
assertEquals("sentry-otherValue=kept,sentry-smallValue=remains", baggage.toHeaderString(null))
}
@Test
fun `medium size value can cause small values to be dropped`() {
val mediumValue =
Faker.instance()
.random()
.hex(
MAX_BAGGAGE_STRING_LENGTH - 19 - 22 - 1
) // 8192 - "sentry-mediumValue=" - "sentry-otherValue=kept" - ","
val baggage =
Baggage.fromHeader(
"sentry-mediumValue=$mediumValue,sentry-smallValue=removed,sentry-otherValue=kept",
logger,
)
assertEquals("removed", baggage.get("sentry-smallValue"))
assertEquals(mediumValue, baggage.get("sentry-mediumValue"))
assertEquals("kept", baggage.get("sentry-otherValue"))
val headerString = baggage.toHeaderString(null)
assertEquals(MAX_BAGGAGE_STRING_LENGTH, headerString.length)
assertEquals("sentry-mediumValue=$mediumValue,sentry-otherValue=kept", headerString)
}
@Test
fun `medium size value can cause all values to be dropped`() {
// nothing else will fit after mediumValue as the separator + any key/value would exceed the
// limit
val mediumValue =
Faker.instance()
.random()
.hex(
MAX_BAGGAGE_STRING_LENGTH - 19 - 22
) // 8192 - "sentry-mediumValue=" - "sentry-otherValue=lost"
val baggage =
Baggage.fromHeader(
"sentry-mediumValue=$mediumValue,sentry-smallValue=stripped,sentry-otherValue=lost",
logger,
)
assertEquals("stripped", baggage.get("sentry-smallValue"))
assertEquals(mediumValue, baggage.get("sentry-mediumValue"))
assertEquals("lost", baggage.get("sentry-otherValue"))
val headerString = baggage.toHeaderString(null)
assertEquals(8170, headerString.length)
assertEquals("sentry-mediumValue=$mediumValue", headerString)
}
@Test
fun `null value is omitted from header string`() {
val baggage = Baggage(logger)
baggage.traceId = null
baggage.publicKey = null
baggage.release = null
baggage.environment = null
baggage.transaction = null
baggage.userId = null
assertEquals("", baggage.toHeaderString(null))
}
@Test
fun `can set values from trace context`() {
val baggage = Baggage(logger)
val publicKey = Dsn(dsnString).publicKey
val traceId = SentryId().toString()
val userId = UUID.randomUUID().toString()
baggage.setTraceId(traceId)
baggage.setPublicKey(publicKey)
baggage.setRelease("1.0-rc.1")
baggage.setEnvironment("production")
baggage.setTransaction("TX")
baggage.setUserId(userId)
baggage.setSampleRate((1.0 / 3.0))
baggage.setSampled("true")
assertEquals(
"sentry-environment=production,sentry-public_key=$publicKey,sentry-release=1.0-rc.1,sentry-sample_rate=0.3333333333333333,sentry-sampled=true,sentry-trace_id=$traceId,sentry-transaction=TX,sentry-user_id=$userId",
baggage.toHeaderString(null),
)
}
@Test
fun `duplicate entries are lost`() {
val baggage = Baggage.fromHeader("sentry-duplicate=a,sentry-duplicate=b", logger)
assertEquals("sentry-duplicate=b", baggage.toHeaderString(null))
}
@Test
fun `setting a value multiple times only keeps the last`() {
val baggage = Baggage.fromHeader("", logger)
baggage.traceId = "a"
baggage.traceId = "b"
baggage.traceId = "c"
assertEquals("sentry-trace_id=c", baggage.toHeaderString(null))
}
@Test
fun `setting values on frozen baggage has no effect`() {
val baggage =
Baggage.fromHeader("sentry-trace_id=a,sentry-transaction=sentryTransaction", logger)
baggage.freeze()
baggage.traceId = "b"
baggage.traceId = "c"
baggage.transaction = "newTransaction"
baggage.environment = "production"
assertEquals(
"sentry-trace_id=a,sentry-transaction=sentryTransaction",
baggage.toHeaderString(null),
)
}
@Test
fun `if header contains sentry values baggage is marked as shouldFreeze`() {
val baggage =
Baggage.fromHeader("sentry-trace_id=a,sentry-transaction=sentryTransaction", logger)
assertTrue(baggage.isShouldFreeze)
}
@Test
fun `if header does not contain sentry values baggage is not marked as shouldFreeze`() {
val baggage = Baggage.fromHeader("a=b", logger)
assertFalse(baggage.isShouldFreeze)
}
@Test
fun `value may contain = sign`() {
val baggage = Baggage(logger)
baggage.setTransaction("a=b")
assertEquals("sentry-transaction=a%3Db", baggage.toHeaderString(null))
}
@Test
fun `corrupted string does not throw out`() {
val baggage = Baggage.fromHeader("a", logger)
assertEquals("", baggage.toHeaderString(null))
}
@Test
fun `corrupted string does not throw out 2`() {
val baggage = Baggage.fromHeader("a=b=", logger)
assertEquals("", baggage.toHeaderString(null))
}
@Test
fun `corrupted string can be parsed partially`() {
val baggage = Baggage.fromHeader("sentry-a=value,sentry-b", logger)
assertEquals("sentry-a=value", baggage.toHeaderString(null))
}
@Test
fun `baggage value encoding`() {
// keep baggage-octet: %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
val values =
percentEncodedValues().also {
/* some characters have been commented out
* as per baggage specification we are allowed to encode these
* and java.net.URLEncoder encodes them by default
*/
// it["!"] = "!" // instead of %21
// it["#"] = "#" // instead of %23
// it["$"] = "$" // instead of %24
// it["%"] = "%" // instead of %25
// it["&"] = "&" // instead of %26
// it["'"] = "'" // instead of %27
// it["("] = "(" // instead of %28
// it[")"] = ")" // instead of %29
it["*"] = "*" // instead of %2A
// it["+"] = "+" // instead of %2B
it["-"] = "-" // instead of %2D
it["."] = "." // instead of %2E
// it["/"] = "/" // instead of %2F
it["0"] = "0" // instead of %30
it["1"] = "1" // instead of %31
it["2"] = "2" // instead of %32
it["3"] = "3" // instead of %33
it["4"] = "4" // instead of %34
it["5"] = "5" // instead of %35
it["6"] = "6" // instead of %36
it["7"] = "7" // instead of %37
it["8"] = "8" // instead of %38
it["9"] = "9" // instead of %39
// it[":"] = ":" // instead of %3A
// it["<"] = "<" // instead of %3C
// it["="] = "=" // instead of %3D
// it[">"] = ">" // instead of %3E
// it["?"] = "?" // instead of %3F
// it["@"] = "@" // instead of %40
it["A"] = "A" // instead of %41
it["B"] = "B" // instead of %42
it["C"] = "C" // instead of %43
it["D"] = "D" // instead of %44
it["E"] = "E" // instead of %45
it["F"] = "F" // instead of %46
it["G"] = "G" // instead of %47
it["H"] = "H" // instead of %48
it["I"] = "I" // instead of %49
it["J"] = "J" // instead of %4A
it["K"] = "K" // instead of %4B
it["L"] = "L" // instead of %4C
it["M"] = "M" // instead of %4D
it["N"] = "N" // instead of %4E
it["O"] = "O" // instead of %4F
it["P"] = "P" // instead of %50
it["Q"] = "Q" // instead of %51
it["R"] = "R" // instead of %52
it["S"] = "S" // instead of %53
it["T"] = "T" // instead of %54
it["U"] = "U" // instead of %55
it["V"] = "V" // instead of %56
it["W"] = "W" // instead of %57
it["X"] = "X" // instead of %58
it["Y"] = "Y" // instead of %59
it["Z"] = "Z" // instead of %5A
// it["["] = "[" // instead of %5B
// it["]"] = "]" // instead of %5D
// it["^"] = "^" // instead of %5E
it["_"] = "_" // instead of %5F
// it["`"] = "`" // instead of %60
it["a"] = "a" // instead of %61
it["b"] = "b" // instead of %62
it["c"] = "c" // instead of %63
it["d"] = "d" // instead of %64
it["e"] = "e" // instead of %65
it["f"] = "f" // instead of %66
it["g"] = "g" // instead of %67
it["h"] = "h" // instead of %68
it["i"] = "i" // instead of %69
it["j"] = "j" // instead of %6A
it["k"] = "k" // instead of %6B
it["l"] = "l" // instead of %6C
it["m"] = "m" // instead of %6D
it["n"] = "n" // instead of %6E
it["o"] = "o" // instead of %6F
it["p"] = "p" // instead of %70
it["q"] = "q" // instead of %71
it["r"] = "r" // instead of %72
it["s"] = "s" // instead of %73
it["t"] = "t" // instead of %74
it["u"] = "u" // instead of %75
it["v"] = "v" // instead of %76
it["w"] = "w" // instead of %77
it["x"] = "x" // instead of %78
it["y"] = "y" // instead of %79
it["z"] = "z" // instead of %7A
}
val failures = mutableListOf<String>()
values.forEach { key, value ->
val baggage = Baggage(logger)
baggage.setTransaction(key)
val headerString = baggage.toHeaderString(null)
if ("sentry-transaction=$value" != headerString) {
failures.add("$key should be $value but was >$headerString<")
}
val decodedBaggage = Baggage.fromHeader(headerString, logger)
decodedBaggage.get("sentry-transaction")
}
assertTrue(failures.joinToString("\n")) { failures.isEmpty() }
}
@Test
fun `all characters defined as valid for keys can be used`() {
val baggage = Baggage(logger)
val key = "sentry-" + validTokenCharacters().joinToString("")
baggage.set(key, "value")
val reparsedBaggage = Baggage.fromHeader(baggage.toHeaderString(null), logger)
assertEquals("value", reparsedBaggage.get(key))
}
@Test
fun `baggage key replaces invalid characters`() {
val baggage = Baggage(logger)
baggage.set(invalidTokenCharacters().joinToString(""), "value")
assertEquals(
"%22%28%29%2C%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%7B%7D=value",
baggage.toHeaderString(null),
)
}
@Test
fun `can skip logger for header from single string`() {
val baggage = Baggage.fromHeader("sentry-trace_id=a,sentry-transaction=sentryTransaction")
assertEquals(
"sentry-trace_id=a,sentry-transaction=sentryTransaction",
baggage.toHeaderString(null),
)
}
@Test
fun `can skip logger for header from list of strings`() {
val baggage =
Baggage.fromHeader(listOf("sentry-trace_id=a", "sentry-transaction=sentryTransaction"))
assertEquals(
"sentry-trace_id=a,sentry-transaction=sentryTransaction",
baggage.toHeaderString(null),
)
}
@Test
fun `unknown returns sentry- prefixed keys that are not known and passes them on to TraceContext`() {
val baggage =
Baggage.fromHeader(
listOf(
"sentry-trace_id=${SentryId()},sentry-public_key=b, sentry-replay_id=${SentryId()}",
"sentry-transaction=sentryTransaction, sentry-anewkey=abc",
)
)
val unknown = baggage.unknown
assertEquals(1, unknown.size)
assertEquals("abc", unknown["anewkey"])
val traceContext = baggage.toTraceContext()!!
assertEquals(1, traceContext.unknown!!.size)
assertEquals("abc", traceContext.unknown!!["anewkey"])
}
@Test
fun `header with sentry values is marked for freezing`() {
val baggage = Baggage.fromHeader("sentry-trace_id=a,sentry-transaction=sentryTransaction")
assertTrue(baggage.isShouldFreeze)
}
@Test
fun `header with sentry sample rand only is not marked for freezing`() {
val baggage = Baggage.fromHeader("sentry-sample_rand=0.3")
assertFalse(baggage.isShouldFreeze)
}
@Test
fun `header without sentry values is not marked for freezing`() {
val baggage = Baggage.fromHeader("a=b,c=d")
assertFalse(baggage.isShouldFreeze)
}
@Test
fun `sets values from traces sampling decision`() {
val baggage = Baggage.fromHeader("a=b,c=d")
baggage.setValuesFromSamplingDecision(TracesSamplingDecision(true, 0.021, 0.025))
assertEquals("true", baggage.sampled)
assertEquals(0.021, baggage.sampleRate!!, 0.0001)
assertEquals(0.025, baggage.sampleRand!!, 0.0001)
}
@Test
fun `handles null traces sampling decision`() {
val baggage = Baggage.fromHeader("a=b,c=d")
baggage.setValuesFromSamplingDecision(null)
}
@Test
fun `sets values from traces sampling decision only if non null`() {
val baggage = Baggage.fromHeader("a=b,c=d")
baggage.setValuesFromSamplingDecision(TracesSamplingDecision(true, 0.021, 0.025))
baggage.setValuesFromSamplingDecision(TracesSamplingDecision(false, null, null))
assertEquals("false", baggage.sampled)
assertEquals(0.021, baggage.sampleRate!!, 0.0001)
assertEquals(0.025, baggage.sampleRand!!, 0.0001)
}
@Test
fun `replaces only sample rate if already frozen`() {
val baggage = Baggage.fromHeader("a=b,c=d")
baggage.setValuesFromSamplingDecision(TracesSamplingDecision(true, 0.021, 0.025))
baggage.freeze()
baggage.setValuesFromSamplingDecision(TracesSamplingDecision(false, 0.121, 0.125))
assertEquals("true", baggage.sampled)
assertEquals(0.121, baggage.sampleRate!!, 0.0001)
assertEquals(0.025, baggage.sampleRand!!, 0.0001)
}
@Test
fun `sample rate can be retrieved as double`() {
val baggage = Baggage.fromHeader("a=b,c=d")
baggage.sampleRate = 0.1
assertEquals(0.1, baggage.sampleRate!!, 0.0001)
}
@Test
fun `sample rand can be retrieved as double`() {
val baggage = Baggage.fromHeader("a=b,c=d")
baggage.sampleRand = 0.1
assertEquals(0.1, baggage.sampleRand!!, 0.0001)
}
@Test
fun `null sample rand returns null double`() {
val baggage = Baggage.fromHeader("a=b,c=d")
baggage.sampleRand = null
assertNull(baggage.sampleRand)
}
@Test
fun `null sample rate returns null double`() {
val baggage = Baggage.fromHeader("a=b,c=d")
baggage.sampleRate = null
assertNull(baggage.sampleRate)
}
@Test
fun `setValuesFromScope falls back to DSN org id when explicit orgId is empty`() {
val options =
SentryOptions().apply {
dsn = "https://key@o123.ingest.sentry.io/456"
orgId = ""
}
val scope = Scope(options)
val baggage = Baggage(logger)
baggage.setValuesFromScope(scope, options)
assertEquals("123", baggage.orgId)
}
@Test
fun `setValuesFromScope falls back to DSN org id when explicit orgId is whitespace`() {
val options =
SentryOptions().apply {
dsn = "https://key@o123.ingest.sentry.io/456"
orgId = " "
}
val scope = Scope(options)
val baggage = Baggage(logger)
baggage.setValuesFromScope(scope, options)
assertEquals("123", baggage.orgId)
}
@Test
fun `setValuesFromTransaction falls back to DSN org id when explicit orgId is empty`() {
val options =
SentryOptions().apply {
dsn = "https://key@o123.ingest.sentry.io/456"
orgId = ""
}
val baggage = Baggage(logger)
baggage.setValuesFromTransaction(
SentryId(),
SentryId(),
options,
TracesSamplingDecision(true, 1.0),
"test-transaction",
TransactionNameSource.CUSTOM,
)
assertEquals("123", baggage.orgId)
}
@Test
fun `fromEvent falls back to DSN org id when explicit orgId is empty`() {
val options =
SentryOptions().apply {
dsn = "https://key@o123.ingest.sentry.io/456"
orgId = ""
}
val event = SentryEvent()
event.contexts.setTrace(SpanContext("test-op"))
val baggage = Baggage.fromEvent(event, "test-transaction", options)
assertEquals("123", baggage.orgId)
}
@Test
fun `baggage header does not include org id when both explicit and DSN org ids are empty`() {
val options =
SentryOptions().apply {
dsn = "https://key@sentry.io/456"
orgId = ""
release = "1.0.0"
}
val scope = Scope(options)
val baggage = Baggage(logger)
baggage.setValuesFromScope(scope, options)
val headerString = baggage.toHeaderString(null)
// Should not contain sentry-org_id if both explicit and DSN org ids are null/empty
assertFalse(headerString.contains("sentry-org_id"))
}
/**
* token = 1*tchar tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / "^" / "_" /
* "`" / "|" / "~" / DIGIT / ALPHA ; any VCHAR, except delimiters
*/
private fun validTokenCharacters() =
mutableListOf(
"!",
"#",
"$",
"%",
"&",
"'",
"*",
"+",
"-",
".",
"^",
"_",
"`",
"|",
"~",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
)
private fun invalidTokenCharacters() =
mutableListOf(
"\"",
"(",
")",
",",
"/",
":",
";",
"<",
"=",
">",
"?",
"@",
"[",
"\\",
"]",
"{",
"}",
)
private fun percentEncodedValues() =
mutableMapOf(
" " to "%20",
"!" to "%21",
"\"" to "%22",
"#" to "%23",
"$" to "%24",
"%" to "%25",
"&" to "%26",
"'" to "%27",
"(" to "%28",
")" to "%29",
"*" to "%2A",
"+" to "%2B",
"," to "%2C",
"-" to "%2D",
"." to "%2E",
"/" to "%2F",
"0" to "%30",
"1" to "%31",
"2" to "%32",
"3" to "%33",
"4" to "%34",
"5" to "%35",
"6" to "%36",
"7" to "%37",
"8" to "%38",
"9" to "%39",
":" to "%3A",
";" to "%3B",
"<" to "%3C",
"=" to "%3D",
">" to "%3E",
"?" to "%3F",
"@" to "%40",
"A" to "%41",
"B" to "%42",
"C" to "%43",
"D" to "%44",
"E" to "%45",
"F" to "%46",
"G" to "%47",
"H" to "%48",
"I" to "%49",
"J" to "%4A",
"K" to "%4B",
"L" to "%4C",
"M" to "%4D",
"N" to "%4E",
"O" to "%4F",
"P" to "%50",
"Q" to "%51",
"R" to "%52",
"S" to "%53",
"T" to "%54",
"U" to "%55",
"V" to "%56",
"W" to "%57",
"X" to "%58",
"Y" to "%59",
"Z" to "%5A",
"[" to "%5B",
"\\" to "%5C",
"]" to "%5D",
"^" to "%5E",
"_" to "%5F",
"`" to "%60",
"a" to "%61",
"b" to "%62",
"c" to "%63",