|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package ottlfuncs |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 12 | + |
| 13 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" |
| 14 | +) |
| 15 | + |
| 16 | +func TestBase64Encode(t *testing.T) { |
| 17 | + type testCase struct { |
| 18 | + name string |
| 19 | + value any |
| 20 | + variant string |
| 21 | + want any |
| 22 | + expectedError string |
| 23 | + } |
| 24 | + tests := []testCase{ |
| 25 | + { |
| 26 | + name: "convert string to base64 (default variant)", |
| 27 | + value: "test string", |
| 28 | + want: "dGVzdCBzdHJpbmc=", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "convert string with newline to base64 (default variant)", |
| 32 | + value: "test string\n", |
| 33 | + want: "dGVzdCBzdHJpbmcK", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "convert Value to base64 (default variant)", |
| 37 | + value: pcommon.NewValueStr("test string"), |
| 38 | + want: "dGVzdCBzdHJpbmc=", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "base64 variant explicit", |
| 42 | + value: "test string", |
| 43 | + variant: "base64", |
| 44 | + want: "dGVzdCBzdHJpbmc=", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "base64 with url-safe sensitive characters", |
| 48 | + value: "data+values/items", |
| 49 | + variant: "base64", |
| 50 | + want: "ZGF0YSt2YWx1ZXMvaXRlbXM=", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "base64-raw with url-safe sensitive characters", |
| 54 | + value: "data+values/items", |
| 55 | + variant: "base64-raw", |
| 56 | + want: "ZGF0YSt2YWx1ZXMvaXRlbXM", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "base64-url with url-safe sensitive characters", |
| 60 | + value: "data+values/items", |
| 61 | + variant: "base64-url", |
| 62 | + want: "ZGF0YSt2YWx1ZXMvaXRlbXM=", |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "base64-raw-url with url-safe sensitive characters", |
| 66 | + value: "data+values/items", |
| 67 | + variant: "base64-raw-url", |
| 68 | + want: "ZGF0YSt2YWx1ZXMvaXRlbXM", |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "unsupported type int", |
| 72 | + value: 10, |
| 73 | + expectedError: "expected string but got int", |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "unsupported type []byte", |
| 77 | + value: []byte{0x00, 0x01, 0x02, 0xFF}, |
| 78 | + expectedError: "expected string but got []uint8", |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "unsupported type valueType invalid", |
| 82 | + value: pcommon.NewValueEmpty(), |
| 83 | + expectedError: "expected string but got Empty", |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "unsupported variant", |
| 87 | + value: "test string", |
| 88 | + variant: "invalid-variant", |
| 89 | + expectedError: "unsupported base64 variant: invalid-variant", |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "empty string", |
| 93 | + value: "", |
| 94 | + want: "", |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + for _, tt := range tests { |
| 99 | + t.Run(tt.name, func(t *testing.T) { |
| 100 | + args := &Base64EncodeArguments[any]{ |
| 101 | + Target: &ottl.StandardStringGetter[any]{ |
| 102 | + Getter: func(context.Context, any) (any, error) { |
| 103 | + return tt.value, nil |
| 104 | + }, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + if tt.variant != "" { |
| 109 | + args.Variant = ottl.NewTestingOptional[ottl.StringGetter[any]](&ottl.StandardStringGetter[any]{ |
| 110 | + Getter: func(context.Context, any) (any, error) { |
| 111 | + return tt.variant, nil |
| 112 | + }, |
| 113 | + }) |
| 114 | + } |
| 115 | + |
| 116 | + expressionFunc, err := createBase64EncodeFunction[any](ottl.FunctionContext{}, args) |
| 117 | + require.NoError(t, err) |
| 118 | + |
| 119 | + result, err := expressionFunc(nil, nil) |
| 120 | + if tt.expectedError != "" { |
| 121 | + require.ErrorContains(t, err, tt.expectedError) |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + require.NoError(t, err) |
| 126 | + require.Equal(t, tt.want, result) |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
0 commit comments