Debian Codegraph @

otelconf/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 "testing"
12 "time"
13
14 "github.com/stretchr/testify/require"
15 )
16
17 func FuzzJSON(f *testing.F) {
18 b, err := os.ReadFile(filepath.Join("testdata", "v1.0.0.json"))
19 require.NoError(f, err)
20 f.Add(b)
21
22 f.Fuzz(func(t *testing.T, data []byte) {
23 t.Log("JSON:\n" + string(data))
24
25 var cfg OpenTelemetryConfiguration
26 err := json.Unmarshal(data, &cfg)
27 if err != nil {
28 return
29 }
30
31 sdk, err := NewSDK(WithOpenTelemetryConfiguration(cfg))
32 if err != nil {
33 return
34 }
35
36 ctx, cancel := context.WithTimeout(t.Context(), time.Millisecond)
37 defer cancel()
38 _ = sdk.Shutdown(ctx)
39 })
40 }
41
42 func FuzzYAML(f *testing.F) {
43 b, err := os.ReadFile(filepath.Join("testdata", "v1.0.0.yaml"))
44 require.NoError(f, err)
45 f.Add(b)
46
47 f.Fuzz(func(t *testing.T, data []byte) {
48 t.Log("YAML:\n" + string(data))
49
50 cfg, err := ParseYAML(data)
51 if err != nil {
52 return
53 }
54
55 sdk, err := NewSDK(WithOpenTelemetryConfiguration(*cfg))
56 if err != nil {
57 return
58 }
59
60 ctx, cancel := context.WithTimeout(t.Context(), time.Millisecond)
61 defer cancel()
62 _ = sdk.Shutdown(ctx)
63 })
64 }