github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/w3ctrace/parent_test.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2020 3 4 package w3ctrace_test 5 6 import ( 7 "testing" 8 9 "github.com/instana/go-sensor/w3ctrace" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestParseParent(t *testing.T) { 15 examples := map[string]struct { 16 Header string 17 Expected w3ctrace.Parent 18 }{ 19 "v0, valid sampled": { 20 Header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01", 21 Expected: w3ctrace.Parent{ 22 Version: w3ctrace.Version_0, 23 TraceID: "4bf92f3577b34da6a3ce929d0e0e4736", 24 ParentID: "00f067aa0ba902b7", 25 Flags: w3ctrace.Flags{ 26 Sampled: true, 27 }, 28 }, 29 }, 30 "v0, valid not sampled": { 31 Header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00", 32 Expected: w3ctrace.Parent{ 33 Version: w3ctrace.Version_0, 34 TraceID: "4bf92f3577b34da6a3ce929d0e0e4736", 35 ParentID: "00f067aa0ba902b7", 36 }, 37 }, 38 "future": { 39 Header: "fe-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-hello future", 40 Expected: w3ctrace.Parent{ 41 Version: w3ctrace.Version_0, 42 TraceID: "4bf92f3577b34da6a3ce929d0e0e4736", 43 ParentID: "00f067aa0ba902b7", 44 Flags: w3ctrace.Flags{ 45 Sampled: true, 46 }, 47 }, 48 }, 49 } 50 51 for name, example := range examples { 52 t.Run(name, func(t *testing.T) { 53 st, err := w3ctrace.ParseParent(example.Header) 54 require.NoError(t, err) 55 assert.Equal(t, example.Expected, st) 56 }) 57 } 58 } 59 60 func TestParseParent_Malformed(t *testing.T) { 61 examples := map[string]string{ 62 "invalid version": "ff-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01", 63 "malformed version": "xx-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01", 64 "v0, no version separator": "00@4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01", 65 "v0, no trace id separator": "00-4bf92f3577b34da6a3ce929d0e0e4736@00f067aa0ba902b7-01", 66 "v0, no parent id separator": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7@01", 67 "v0, malformed flags": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-xx", 68 "future, no flags separator": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01@hello future", 69 "trace id all zeroes": "00-00000000000000000000000000000001-0000000000000000-01", 70 "parent id all zeroes": "00-00000000000000000000000000000000-0000000000000001-01", 71 "trace id and parent id all zeroes": "00-00000000000000000000000000000000-0000000000000000-01", 72 "uppercase in trace id": "00-CAPAAAA4Af92f3577b34da6a3ce92736-00f067aa0ba902b7-01", 73 "uppercase in parent id": "00-aaaaaaa4Af92f3577b34da6a3ce92736-CAP067aa0ba902b7-01", 74 "non-hex chars in trace id": "00-zzzzzzz4Af92f3577b34da6a3ce92736-00f067aa0ba902b7-01", 75 "non-hex chars in parent id": "00-aaaaaaa4Af92f3577b34da6a3ce92736-zzf067aa0ba902b7-01", 76 } 77 78 for name, header := range examples { 79 t.Run(name, func(t *testing.T) { 80 _, err := w3ctrace.ParseParent(header) 81 assert.Equal(t, w3ctrace.ErrContextCorrupted, err) 82 }) 83 } 84 } 85 86 func TestParent_String(t *testing.T) { 87 examples := map[string]struct { 88 Parent w3ctrace.Parent 89 Expected string 90 }{ 91 "v0, valid sampled": { 92 Parent: w3ctrace.Parent{ 93 Version: w3ctrace.Version_0, 94 TraceID: "1234", 95 ParentID: "56789", 96 Flags: w3ctrace.Flags{ 97 Sampled: true, 98 }, 99 }, 100 Expected: "00-00000000000000000000000000001234-0000000000056789-01", 101 }, 102 "v0, valid not sampled": { 103 Parent: w3ctrace.Parent{ 104 Version: w3ctrace.Version_0, 105 TraceID: "4bf92f3577b34da6a3ce929d0e0e4736", 106 ParentID: "00f067aa0ba902b7", 107 }, 108 Expected: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00", 109 }, 110 "future": { 111 Parent: w3ctrace.Parent{ 112 Version: w3ctrace.Version(uint8(w3ctrace.Version_Max) + 1), 113 TraceID: "4bf92f3577b34da6a3ce929d0e0e4736", 114 ParentID: "00f067aa0ba902b7", 115 Flags: w3ctrace.Flags{ 116 Sampled: true, 117 }, 118 }, 119 Expected: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01", 120 }, 121 } 122 123 for name, example := range examples { 124 t.Run(name, func(t *testing.T) { 125 assert.Equal(t, example.Expected, example.Parent.String()) 126 }) 127 } 128 }