(t *testing.T)
| 9 | ) |
| 10 | |
| 11 | func TestHexStringToTraceID(t *testing.T) { |
| 12 | tc := []struct { |
| 13 | id string |
| 14 | expected []byte |
| 15 | expectError error |
| 16 | }{ |
| 17 | { |
| 18 | id: "12", |
| 19 | expected: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12}, |
| 20 | }, |
| 21 | { |
| 22 | id: "1234567890abcdef", // 64 bit |
| 23 | expected: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}, |
| 24 | }, |
| 25 | { |
| 26 | id: "1234567890abcdef1234567890abcdef", // 128 bit |
| 27 | expected: []byte{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}, |
| 28 | }, |
| 29 | { |
| 30 | id: "121234567890abcdef1234567890abcdef", // value too long |
| 31 | expected: []byte{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}, |
| 32 | expectError: errors.New("trace IDs can't be larger than 128 bits"), |
| 33 | }, |
| 34 | { |
| 35 | id: "234567890abcdef", // odd length |
| 36 | expected: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}, |
| 37 | }, |
| 38 | { |
| 39 | id: "1234567890abcdef ", // trailing space |
| 40 | expected: nil, |
| 41 | expectError: errors.New("trace IDs can only contain hex characters: invalid character ' ' at position 17"), |
| 42 | }, |
| 43 | } |
| 44 | |
| 45 | for _, tt := range tc { |
| 46 | t.Run(tt.id, func(t *testing.T) { |
| 47 | actual, err := HexStringToTraceID(tt.id) |
| 48 | |
| 49 | if tt.expectError != nil { |
| 50 | assert.Equal(t, tt.expectError, err) |
| 51 | assert.Nil(t, actual) |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | assert.NoError(t, err) |
| 56 | assert.Equal(t, tt.expected, actual) |
| 57 | }) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestTraceIDToHexString(t *testing.T) { |
| 62 | tc := []struct { |
nothing calls this directly
no test coverage detected