Test_WithContextRenderError locks in M8: a misconfigured context tag must not silently drop context — the failure should leave a visible marker in the log line so operators notice. The error message is also sanitized for control bytes so a tag that wraps attacker-controlled data in its error cannot
(t *testing.T)
| 103 | // because under -shuffle=on this test may run before any other log test, in |
| 104 | // which case the package-global logger.stdlog could still be nil. |
| 105 | func Test_WithContextRenderError(t *testing.T) { |
| 106 | initDefaultLogger() |
| 107 | t.Cleanup(initDefaultLogger) |
| 108 | |
| 109 | // Embed CR/LF in the error to exercise the sanitiser. |
| 110 | templateErr := errors.New("tag\r\nboom") |
| 111 | require.NoError(t, SetContextTemplate(ContextConfig{ |
| 112 | Format: "[${broken}] ", |
| 113 | CustomTags: map[string]ContextTagFunc{ |
| 114 | "broken": func(_ Buffer, _ any, _ *ContextData, _ string) (int, error) { |
| 115 | return 0, templateErr |
| 116 | }, |
| 117 | }, |
| 118 | })) |
| 119 | t.Cleanup(func() { MustSetContextTemplate(ContextConfig{}) }) |
| 120 | |
| 121 | var w byteSliceWriter |
| 122 | SetOutput(&w) |
| 123 | |
| 124 | WithContext(context.Background()).Info("payload") |
| 125 | |
| 126 | out := string(w.b) |
| 127 | require.Contains(t, out, "ctx-render-error", "expected render-error marker, got %q", out) |
| 128 | require.Contains(t, out, "payload", "expected payload to still be emitted, got %q", out) |
| 129 | require.NotContains(t, out, "\r", "CR in error message must be sanitized") |
| 130 | // One trailing newline from log.Output is expected; reject any embedded ones. |
| 131 | require.Equal(t, 1, strings.Count(out, "\n"), "embedded LF in error message must be sanitized; got %q", out) |
| 132 | } |
| 133 | |
| 134 | // Test_NewRetainedContext_TypedNil locks in the reflect-based nil detection |
| 135 | // in newRetainedContext: a typed-nil pointer wrapped in `any` must be treated |
nothing calls this directly
no test coverage detected