|
1 | 1 | package tracing |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "crypto/rand" |
| 6 | + "errors" |
4 | 7 | "testing" |
5 | 8 |
|
6 | 9 | "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "go.opentelemetry.io/otel/trace" |
| 12 | + |
| 13 | + "github.com/github/gh-aw-mcpg/internal/config" |
7 | 14 | ) |
8 | 15 |
|
9 | 16 | func TestMergeOTLPHeaders(t *testing.T) { |
@@ -50,3 +57,61 @@ func TestMergeOTLPHeaders(t *testing.T) { |
50 | 57 | }) |
51 | 58 | } |
52 | 59 | } |
| 60 | + |
| 61 | +// TestGenerateRandomSpanID verifies that generateRandomSpanID produces a valid, |
| 62 | +// non-zero 8-byte span ID and that successive calls produce distinct values. |
| 63 | +func TestGenerateRandomSpanID(t *testing.T) { |
| 64 | + t.Parallel() |
| 65 | + |
| 66 | + id, err := generateRandomSpanID() |
| 67 | + require.NoError(t, err, "generateRandomSpanID should not fail with a healthy rand.Reader") |
| 68 | + assert.NotEqual(t, trace.SpanID{}, id, "generated span ID should be non-zero") |
| 69 | + |
| 70 | + id2, err := generateRandomSpanID() |
| 71 | + require.NoError(t, err) |
| 72 | + assert.NotEqual(t, id, id2, "successive calls should produce distinct span IDs") |
| 73 | +} |
| 74 | + |
| 75 | +// errorReader is an io.Reader that always returns the configured error. |
| 76 | +type errorReader struct{ err error } |
| 77 | + |
| 78 | +func (r *errorReader) Read(_ []byte) (int, error) { return 0, r.err } |
| 79 | + |
| 80 | +// TestGenerateRandomSpanID_Error verifies that generateRandomSpanID propagates |
| 81 | +// errors from the underlying random source and returns a zero span ID. |
| 82 | +// Must NOT run in parallel: temporarily replaces the global crypto/rand.Reader. |
| 83 | +func TestGenerateRandomSpanID_Error(t *testing.T) { |
| 84 | + syntheticErr := errors.New("synthetic entropy failure") |
| 85 | + |
| 86 | + origReader := rand.Reader |
| 87 | + rand.Reader = &errorReader{err: syntheticErr} |
| 88 | + defer func() { rand.Reader = origReader }() |
| 89 | + |
| 90 | + id, err := generateRandomSpanID() |
| 91 | + |
| 92 | + assert.Equal(t, trace.SpanID{}, id, "span ID should be zero on error") |
| 93 | + require.Error(t, err, "should return an error when the random source fails") |
| 94 | + assert.ErrorIs(t, err, syntheticErr, "error should wrap the underlying source error") |
| 95 | + assert.Contains(t, err.Error(), "failed to generate random span ID") |
| 96 | +} |
| 97 | + |
| 98 | +// TestParentContext_RandomSpanIDFailure verifies that ParentContext returns the |
| 99 | +// original context unchanged when generateRandomSpanID fails due to a broken |
| 100 | +// random source. This covers the genErr != nil branch (lines 322–325 of provider.go). |
| 101 | +// Must NOT run in parallel: temporarily replaces the global crypto/rand.Reader. |
| 102 | +func TestParentContext_RandomSpanIDFailure(t *testing.T) { |
| 103 | + ctx := context.Background() |
| 104 | + cfg := &config.TracingConfig{ |
| 105 | + TraceID: "4bf92f3577b34da6a3ce929d0e0e4736", // valid 32-char hex (non-zero) |
| 106 | + // SpanID intentionally absent so ParentContext must generate one |
| 107 | + } |
| 108 | + |
| 109 | + origReader := rand.Reader |
| 110 | + rand.Reader = &errorReader{err: errors.New("entropy unavailable")} |
| 111 | + defer func() { rand.Reader = origReader }() |
| 112 | + |
| 113 | + parentCtx := ParentContext(ctx, cfg) |
| 114 | + |
| 115 | + assert.Equal(t, ctx, parentCtx, |
| 116 | + "ParentContext must return the original context when random span ID generation fails") |
| 117 | +} |
0 commit comments