Debian Codegraph @

otelconf/v0.3.0/fuzz_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelconf
5
6 import (
7 "context"
8 "encoding/json"
9 "os"
10 "path/filepath"
11 "slices"
12 "testing"
13 "time"
14
15 "github.com/stretchr/testify/require"
16 )
17
18 func FuzzJSON(f *testing.F) {
19 b, err := os.ReadFile(filepath.Join("..", "testdata", "v0.3.json"))
20 require.NoError(f, err)
21 f.Add(b)
22
23 f.Fuzz(func(t *testing.T, data []byte) {
24 t.Log("JSON:\n" + string(data))
25
26 var cfg OpenTelemetryConfiguration
27 err := json.Unmarshal(data, &cfg)
28 if err != nil {
29 return
30 }
31
32 sdk, err := NewSDK(WithOpenTelemetryConfiguration(cfg))
33 if err != nil {
34 return
35 }
36
37 ctx, cancel := context.WithTimeout(t.Context(), time.Millisecond)
38 defer cancel()
39 _ = sdk.Shutdown(ctx)
40 })
41 }
42
43 func FuzzYAML(f *testing.F) {
44 b, err := os.ReadFile(filepath.Join("..", "testdata", "v0.3.yaml"))
45 require.NoError(f, err)
46 f.Add(b)
47
48 f.Fuzz(func(t *testing.T, data []byte) {
49 t.Log("YAML:\n" + string(data))
50
51 cfg, err := ParseYAML(data)
52 if err != nil {
53 return
54 }
55
56 sdk, err := NewSDK(WithOpenTelemetryConfiguration(*cfg))
57 if err != nil {
58 return
59 }
60
61 ctx, cancel := context.WithTimeout(t.Context(), time.Millisecond)
62 defer cancel()
63 _ = sdk.Shutdown(ctx)
64 })
65 }
66
67 func FuzzYAMLWithEnvVars(f *testing.F) {
68 b, err := os.ReadFile(filepath.Join("..", "testdata", "v0.3-env-var.yaml"))
69 require.NoError(f, err)
70
71 // Add example values for fuzzing - YAML data and all env var values.
72 f.Add(b, "false", "4096", "test_string", "true", "42", "3.14", "0xFF", "invalid", "dynamic_key", "replaced_value", "value\\nwith\\tescape")
73
74 f.Fuzz(func(t *testing.T, data []byte, otelSDKDisabled, otelAttrValueLengthLimit, stringValue, boolValue, intValue, floatValue, hexValue, invalidMapValue, envVarInKey, replaceMe, valueWithEscape string) {
75 t.Log("YAML with env vars:\n" + string(data))
76
77 // Helper function to check if environment variable value is valid.
78 isValidEnvValue := func(value string) bool {
79 // Environment variable values cannot contain null bytes.
80 return !slices.Contains([]byte(value), 0)
81 }
82
83 // Set environment variables used in the test YAML with fuzzed values.
84 // Skip if any value contains invalid characters.
85 envVars := map[string]string{
86 "OTEL_SDK_DISABLED": otelSDKDisabled,
87 "OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT": otelAttrValueLengthLimit,
88 "STRING_VALUE": stringValue,
89 "BOOL_VALUE": boolValue,
90 "INT_VALUE": intValue,
91 "FLOAT_VALUE": floatValue,
92 "HEX_VALUE": hexValue,
93 "INVALID_MAP_VALUE": invalidMapValue,
94 "ENV_VAR_IN_KEY": envVarInKey,
95 "REPLACE_ME": replaceMe,
96 "VALUE_WITH_ESCAPE": valueWithEscape,
97 }
98
99 for key, value := range envVars {
100 if !isValidEnvValue(value) {
101 t.Skipf("Skipping test due to invalid env var value for %s", key)
102 }
103 t.Setenv(key, value)
104 }
105
106 cfg, err := ParseYAML(data)
107 if err != nil {
108 return
109 }
110
111 sdk, err := NewSDK(WithOpenTelemetryConfiguration(*cfg))
112 if err != nil {
113 return
114 }
115
116 ctx, cancel := context.WithTimeout(t.Context(), time.Millisecond)
117 defer cancel()
118 _ = sdk.Shutdown(ctx)
119 })
120 }