| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package e2etest |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"os" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
) |
| 14 |
|
| 15 |
// targetNested tests the following property: |
| 16 |
// > Passing a value of type T directly through an environment variable |
| 17 |
// > should be equivalent to passing it through a nested environment variable. |
| 18 |
func targetNested[T any](t *testing.T, value string) { |
| 19 |
resolver := NewResolver(t, "types_expand.yaml") |
| 20 |
|
| 21 |
// Use os.Setenv so we can check the error and return instead of failing the fuzzing. |
| 22 |
os.Setenv("ENV", "${env:ENV2}") //nolint:usetesting |
| 23 |
defer os.Unsetenv("ENV") |
| 24 |
err := os.Setenv("ENV2", value) //nolint:usetesting |
| 25 |
defer os.Unsetenv("ENV2") |
| 26 |
if err != nil { |
| 27 |
return |
| 28 |
} |
| 29 |
confNested, errResolveNested := resolver.Resolve(context.Background()) |
| 30 |
|
| 31 |
err = os.Setenv("ENV", value) //nolint:usetesting |
| 32 |
if err != nil { |
| 33 |
return |
| 34 |
} |
| 35 |
confSimple, errResolveSimple := resolver.Resolve(context.Background()) |
| 36 |
require.Equal(t, errResolveNested, errResolveSimple) |
| 37 |
if errResolveNested != nil { |
| 38 |
return |
| 39 |
} |
| 40 |
|
| 41 |
var cfgNested targetConfig[T] |
| 42 |
errNested := confNested.Unmarshal(cfgNested) |
| 43 |
|
| 44 |
var cfgSimple targetConfig[T] |
| 45 |
errSimple := confSimple.Unmarshal(cfgSimple) |
| 46 |
|
| 47 |
require.Equal(t, errNested, errSimple) |
| 48 |
if errNested != nil { |
| 49 |
return |
| 50 |
} |
| 51 |
assert.Equal(t, cfgNested, cfgSimple) |
| 52 |
} |
| 53 |
|
| 54 |
// testStrings for fuzzing targets |
| 55 |
var testStrings = []string{ |
| 56 |
"123", |
| 57 |
"opentelemetry", |
| 58 |
"!!str 123", |
| 59 |
"\"0123\"", |
| 60 |
"\"", |
| 61 |
"1111:1111:1111:1111:1111::", |
| 62 |
"{field: value}", |
| 63 |
"0xdeadbeef", |
| 64 |
"0b101", |
| 65 |
"field:", |
| 66 |
"2006-01-02T15:04:05Z07:00", |
| 67 |
} |
| 68 |
|
| 69 |
func FuzzNestedString(f *testing.F) { |
| 70 |
for _, value := range testStrings { |
| 71 |
f.Add(value) |
| 72 |
} |
| 73 |
f.Fuzz(targetNested[string]) |
| 74 |
} |
| 75 |
|
| 76 |
func FuzzNestedInt(f *testing.F) { |
| 77 |
for _, value := range testStrings { |
| 78 |
f.Add(value) |
| 79 |
} |
| 80 |
f.Fuzz(targetNested[int]) |
| 81 |
} |
| 82 |
|
| 83 |
func FuzzNestedMap(f *testing.F) { |
| 84 |
for _, value := range testStrings { |
| 85 |
f.Add(value) |
| 86 |
} |
| 87 |
f.Fuzz(targetNested[map[string]any]) |
| 88 |
} |