|
| 1 | +package base64 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/go-faker/faker/v4" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/ARM-software/golang-utils/utils/commonerrors" |
| 13 | + "github.com/ARM-software/golang-utils/utils/commonerrors/errortest" |
| 14 | +) |
| 15 | + |
| 16 | +func TestIsBase64Encoded(t *testing.T) { |
| 17 | + random := faker.Sentence() |
| 18 | + base641 := base64.RawURLEncoding.EncodeToString([]byte(random)) |
| 19 | + base642 := base64.RawStdEncoding.EncodeToString([]byte(random)) |
| 20 | + base643 := base64.URLEncoding.EncodeToString([]byte(random)) |
| 21 | + base644 := base64.StdEncoding.EncodeToString([]byte(random)) |
| 22 | + tests := []struct { |
| 23 | + input string |
| 24 | + expected bool |
| 25 | + }{ |
| 26 | + {"U29tZSBkYXRh", true}, // "Some data" |
| 27 | + {"SGVsbG8gd29ybGQ=", true}, // "Hello world" |
| 28 | + {"U29tZSBkYXRh===", false}, |
| 29 | + {"", false}, // Empty string |
| 30 | + {"NotBase64", false}, // Plain text |
| 31 | + {"!@#$%^&*", false}, // Non-Base64 characters |
| 32 | + {"U29tZSBkYXRh\n", true}, // Line break |
| 33 | + {"V2l0aCB3aGl0ZXNwYWNl", true}, // "With whitespace" (valid if stripped) |
| 34 | + {base641, true}, |
| 35 | + {base642, true}, |
| 36 | + {base643, true}, |
| 37 | + {base644, true}, |
| 38 | + {"U29tZSBkYXRh=", true}, |
| 39 | + {"U29tZSBkYXRh==", true}, |
| 40 | + } |
| 41 | + |
| 42 | + for i := range tests { |
| 43 | + test := tests[i] |
| 44 | + t.Run(test.input, func(t *testing.T) { |
| 45 | + if test.expected { |
| 46 | + assert.True(t, IsEncoded(test.input)) |
| 47 | + } else { |
| 48 | + assert.False(t, IsEncoded(test.input)) |
| 49 | + } |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestDecodeIfBase64(t *testing.T) { |
| 55 | + random := faker.Sentence() |
| 56 | + base641 := base64.RawURLEncoding.EncodeToString([]byte(random)) |
| 57 | + base642 := base64.RawStdEncoding.EncodeToString([]byte(random)) |
| 58 | + base643 := base64.URLEncoding.EncodeToString([]byte(random)) |
| 59 | + base644 := base64.StdEncoding.EncodeToString([]byte(random)) |
| 60 | + |
| 61 | + tests := []struct { |
| 62 | + input string |
| 63 | + expected string |
| 64 | + errors bool |
| 65 | + }{ |
| 66 | + {input: "U29tZSBkYXRh", expected: "Some data"}, |
| 67 | + {input: "SGVsbG8gd29ybGQ=", expected: "Hello world"}, |
| 68 | + {input: "VGVzdCBzdHJpbmc=", expected: "Test string"}, |
| 69 | + {input: "MTIzNDU2", expected: "123456"}, |
| 70 | + {input: base641, expected: random}, |
| 71 | + {input: base642, expected: random}, |
| 72 | + {input: base643, expected: random}, |
| 73 | + {input: base644, expected: random}, |
| 74 | + |
| 75 | + {input: "NotBase64", expected: "NotBase64", errors: true}, |
| 76 | + {input: "Invalid===", expected: "Invalid===", errors: true}, |
| 77 | + {input: "", expected: "", errors: true}, |
| 78 | + {input: "!@#$%^&*", expected: "!@#$%^&*", errors: true}, |
| 79 | + |
| 80 | + {input: "U29tZSBkYXRh\n", expected: "Some data"}, // newline is not part of valid base64 |
| 81 | + {input: "U29tZSBkYXRh=", expected: "Some data"}, // valid with single padding |
| 82 | + {input: "U29tZSBkYXRh==", expected: "Some data"}, // valid with double padding |
| 83 | + } |
| 84 | + |
| 85 | + for i := range tests { |
| 86 | + test := tests[i] |
| 87 | + t.Run(test.input, func(t *testing.T) { |
| 88 | + result, err := DecodeString(context.Background(), test.input) |
| 89 | + assert.Equal(t, test.expected, DecodeIfEncoded(context.Background(), test.input)) |
| 90 | + if test.errors { |
| 91 | + errortest.AssertError(t, err, commonerrors.ErrMarshalling, commonerrors.ErrInvalid, commonerrors.ErrEmpty) |
| 92 | + } else { |
| 93 | + require.NoError(t, err) |
| 94 | + assert.Equal(t, test.expected, result) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | + |
| 99 | + t.Run("cancellation", func(t *testing.T) { |
| 100 | + ctx, cancel := context.WithCancel(context.Background()) |
| 101 | + cancel() |
| 102 | + _, err := DecodeString(ctx, random) |
| 103 | + errortest.AssertError(t, err, commonerrors.ErrCancelled) |
| 104 | + assert.Equal(t, random, DecodeIfEncoded(ctx, random)) |
| 105 | + |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +func TestDecodeRecursively(t *testing.T) { |
| 110 | + randomText := faker.Paragraph() |
| 111 | + random, err := faker.RandomInt(1, 10, 1) |
| 112 | + require.NoError(t, err) |
| 113 | + |
| 114 | + encodedText := randomText |
| 115 | + for i := 0; i < random[0]; i++ { |
| 116 | + encodedText = EncodeString(encodedText) |
| 117 | + } |
| 118 | + |
| 119 | + assert.NotEqual(t, randomText, encodedText) |
| 120 | + assert.Equal(t, randomText, DecodeRecursively(context.Background(), encodedText)) |
| 121 | +} |
0 commit comments