|
| 1 | +import { getRandomBytes, isValidSpanId, isValidTraceId, newSpanId, newTraceId } from './ids' |
| 2 | + |
| 3 | +describe('trace and span ids', () => { |
| 4 | + describe('newTraceId', () => { |
| 5 | + it('is 32 lowercase hex characters', () => { |
| 6 | + for (let i = 0; i < 50; i++) { |
| 7 | + expect(newTraceId()).toMatch(/^[0-9a-f]{32}$/) |
| 8 | + } |
| 9 | + }) |
| 10 | + |
| 11 | + it('is never all zeros', () => { |
| 12 | + for (let i = 0; i < 50; i++) { |
| 13 | + expect(newTraceId()).not.toBe('0'.repeat(32)) |
| 14 | + } |
| 15 | + }) |
| 16 | + |
| 17 | + it('does not repeat', () => { |
| 18 | + const ids = new Set(Array.from({ length: 200 }, newTraceId)) |
| 19 | + expect(ids.size).toBe(200) |
| 20 | + }) |
| 21 | + }) |
| 22 | + |
| 23 | + describe('newSpanId', () => { |
| 24 | + it('is 16 lowercase hex characters', () => { |
| 25 | + for (let i = 0; i < 50; i++) { |
| 26 | + expect(newSpanId()).toMatch(/^[0-9a-f]{16}$/) |
| 27 | + } |
| 28 | + }) |
| 29 | + |
| 30 | + it('does not repeat', () => { |
| 31 | + const ids = new Set(Array.from({ length: 200 }, newSpanId)) |
| 32 | + expect(ids.size).toBe(200) |
| 33 | + }) |
| 34 | + }) |
| 35 | + |
| 36 | + describe('getRandomBytes', () => { |
| 37 | + it('returns the requested length', () => { |
| 38 | + expect(getRandomBytes(8)).toHaveLength(8) |
| 39 | + expect(getRandomBytes(16)).toHaveLength(16) |
| 40 | + }) |
| 41 | + |
| 42 | + it('falls back to Math.random when crypto is unavailable', () => { |
| 43 | + const original = Object.getOwnPropertyDescriptor(globalThis, 'crypto') |
| 44 | + // React Native has no global crypto without a polyfill — the fallback path |
| 45 | + // is what keeps span ids working there. |
| 46 | + Object.defineProperty(globalThis, 'crypto', { value: undefined, configurable: true }) |
| 47 | + try { |
| 48 | + expect(newTraceId()).toMatch(/^[0-9a-f]{32}$/) |
| 49 | + expect(newSpanId()).toMatch(/^[0-9a-f]{16}$/) |
| 50 | + } finally { |
| 51 | + if (original) { |
| 52 | + Object.defineProperty(globalThis, 'crypto', original) |
| 53 | + } |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + it('falls back when getRandomValues throws', () => { |
| 58 | + const original = Object.getOwnPropertyDescriptor(globalThis, 'crypto') |
| 59 | + Object.defineProperty(globalThis, 'crypto', { |
| 60 | + value: { |
| 61 | + getRandomValues: () => { |
| 62 | + throw new Error('not allowed') |
| 63 | + }, |
| 64 | + }, |
| 65 | + configurable: true, |
| 66 | + }) |
| 67 | + try { |
| 68 | + expect(newTraceId()).toMatch(/^[0-9a-f]{32}$/) |
| 69 | + } finally { |
| 70 | + if (original) { |
| 71 | + Object.defineProperty(globalThis, 'crypto', original) |
| 72 | + } |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + it('never emits an all-zero id even when the random source is broken', () => { |
| 77 | + const original = Object.getOwnPropertyDescriptor(globalThis, 'crypto') |
| 78 | + Object.defineProperty(globalThis, 'crypto', { |
| 79 | + value: { getRandomValues: (array: Uint8Array) => array.fill(0) }, |
| 80 | + configurable: true, |
| 81 | + }) |
| 82 | + try { |
| 83 | + // The server zeroes ids it can't use, so an all-zero id would be stored |
| 84 | + // and silently orphaned rather than rejected. |
| 85 | + expect(newTraceId()).not.toBe('0'.repeat(32)) |
| 86 | + expect(newSpanId()).not.toBe('0'.repeat(16)) |
| 87 | + } finally { |
| 88 | + if (original) { |
| 89 | + Object.defineProperty(globalThis, 'crypto', original) |
| 90 | + } |
| 91 | + } |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + describe('validation', () => { |
| 96 | + it.each([ |
| 97 | + ['a valid trace id', '4bf92f3577b34da6a3ce929d0e0e4736', true], |
| 98 | + ['an all-zero trace id', '0'.repeat(32), false], |
| 99 | + ['a short trace id', 'abc', false], |
| 100 | + ['uppercase hex', '4BF92F3577B34DA6A3CE929D0E0E4736', false], |
| 101 | + ['a non-hex string', 'zzf92f3577b34da6a3ce929d0e0e4736', false], |
| 102 | + ['a non-string', 12345, false], |
| 103 | + ])('isValidTraceId rejects/accepts %s', (_name, value, expected) => { |
| 104 | + expect(isValidTraceId(value)).toBe(expected) |
| 105 | + }) |
| 106 | + |
| 107 | + it.each([ |
| 108 | + ['a valid span id', '00f067aa0ba902b7', true], |
| 109 | + ['an all-zero span id', '0'.repeat(16), false], |
| 110 | + ['a trace-length id', '4bf92f3577b34da6a3ce929d0e0e4736', false], |
| 111 | + ])('isValidSpanId rejects/accepts %s', (_name, value, expected) => { |
| 112 | + expect(isValidSpanId(value)).toBe(expected) |
| 113 | + }) |
| 114 | + }) |
| 115 | +}) |
0 commit comments