| 1 | package base64dec |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/base64" |
| 6 | "fmt" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestDecodeBase64(t *testing.T) { |
| 11 | f := func(t *testing.T, input, expectedResult string, expectedError error) { |
| 12 | decoded := make([]byte, base64.RawStdEncoding.DecodedLen(len(input))) |
| 13 | n, err := DecodeBase64(decoded, input) |
| 14 | if err != expectedError { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | encoded := base64.StdEncoding.EncodeToString(decoded[:n]) |
| 18 | if encoded != expectedResult { |
| 19 | t.Fatal(encoded) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | t.Run("empty input", func(t *testing.T) { |
| 24 | f(t, "", "", nil) |
| 25 | }) |
| 26 | |
| 27 | t.Run("newlines only", func(t *testing.T) { |
| 28 | f(t, "\r\n", "", nil) |
| 29 | }) |
| 30 | |
| 31 | t.Run("correct padding", func(t *testing.T) { |
| 32 | f(t, "Z9CA-w==", "Z9CA+w==", nil) |
| 33 | }) |
| 34 | |
| 35 | t.Run("correct padding split by newline", func(t *testing.T) { |
| 36 | f(t, "Z9CA-w=\n=", "Z9CA+w==", nil) |
| 37 | }) |
| 38 | |
| 39 | t.Run("correct padding with concatenation", func(t *testing.T) { |
| 40 | f(t, "Z9CA-w==Z9CA-w==", "Z9CA+w==", base64.CorruptInputError(8)) |
| 41 | }) |
| 42 | |
| 43 | t.Run("trailing newline", func(t *testing.T) { |
| 44 | f(t, "Z9CA+wZ9CA-w\n", "Z9CA+wZ9CA+w", nil) |
| 45 | }) |
| 46 | |
| 47 | t.Run("trailing newline with padding", func(t *testing.T) { |
| 48 | f(t, "Z9CA+wZ9CA-www==\n", "Z9CA+wZ9CA+www==", nil) |
| 49 | }) |
| 50 | |
| 51 | t.Run("garbage after newline", func(t *testing.T) { |
| 52 | f(t, "Z9CA+wZ9CA-www==\n?", "Z9CA+wZ9CA+www==", base64.CorruptInputError(17)) |
| 53 | }) |
| 54 | |
| 55 | t.Run("no padding", func(t *testing.T) { |
| 56 | f(t, "Z9CA-w", "Z9CA+w==", nil) |
| 57 | }) |
| 58 | |
| 59 | t.Run("no padding, garbage at the end", func(t *testing.T) { |
| 60 | f(t, "Z9CA-w???", "Z9CA+w==", base64.CorruptInputError(6)) |
| 61 | }) |
| 62 | |
| 63 | t.Run("not enough padding", func(t *testing.T) { |
| 64 | f(t, "Z9CA-w=", "Z9CA+w==", base64.CorruptInputError(6)) |
| 65 | }) |
| 66 | |
| 67 | t.Run("incorrect padding", func(t *testing.T) { |
| 68 | f(t, "Z9CA====", "Z9CA", base64.CorruptInputError(4)) |
| 69 | }) |
| 70 | |
| 71 | t.Run("incorrect padding with extra base64", func(t *testing.T) { |
| 72 | f(t, "Z9CA-w=Z9CA-w=", "Z9CA+w==", base64.CorruptInputError(6)) |
| 73 | }) |
| 74 | |
| 75 | t.Run("incorrect padding with garbage", func(t *testing.T) { |
| 76 | f(t, "Z9CA-w=???", "Z9CA+w==", base64.CorruptInputError(6)) |
| 77 | }) |
| 78 | |
| 79 | } |
| 80 | |
| 81 | func FuzzDecodeBase64(f *testing.F) { |
| 82 | f.Add([]byte{}) |
| 83 | f.Add([]byte("\x14\xfb\x9c\x03\xd9\x7e")) |
| 84 | f.Add([]byte("\x14\xfb\x9c\x03\xd9")) |
| 85 | f.Add([]byte("\x14\xfb\x9c\x03")) |
| 86 | |
| 87 | f.Fuzz(func(t *testing.T, b []byte) { |
| 88 | encoded := base64.StdEncoding.EncodeToString(b) |
| 89 | decoded := make([]byte, len(b)) |
| 90 | n, err := DecodeBase64(decoded, encoded) |
| 91 | if err != nil { |
| 92 | t.Fatalf("%v: %v", b, err) |
| 93 | } |
| 94 | if !bytes.Equal(decoded[:n], b) { |
| 95 | t.Fatal(b) |
| 96 | } |
| 97 | |
| 98 | }) |
| 99 | } |
| 100 | |
| 101 | func FuzzDecodeBase64String(f *testing.F) { |
| 102 | f.Add("Z9CA-w") |
| 103 | f.Add("=\n=") |
| 104 | f.Add("=") |
| 105 | f.Add("====") |
| 106 | |
| 107 | f.Fuzz(func(t *testing.T, s string) { |
| 108 | decoded := make([]byte, base64.RawStdEncoding.DecodedLen(len(s))) |
| 109 | _, _ = DecodeBase64(decoded, s) // should not panic |
| 110 | }) |
| 111 | } |
| 112 | |
| 113 | func BenchmarkDecodeBase64(b *testing.B) { |
| 114 | sizes := []int{2, 4, 8, 64, 8192} |
| 115 | dst := make([]byte, 8192) |
| 116 | benchFunc := func(b *testing.B, benchSize int) { |
| 117 | data := base64.StdEncoding.EncodeToString(make([]byte, benchSize)) |
| 118 | b.SetBytes(int64(len(data))) |
| 119 | b.ResetTimer() |
| 120 | b.ReportAllocs() |
| 121 | for i := 0; i < b.N; i++ { |
| 122 | DecodeBase64(dst, data) |
| 123 | } |
| 124 | } |
| 125 | for _, size := range sizes { |
| 126 | b.Run(fmt.Sprintf("%d", size), func(b *testing.B) { |
| 127 | benchFunc(b, size) |
| 128 | }) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func BenchmarkDecodeBase64Std(b *testing.B) { |
| 133 | sizes := []int{2, 4, 8, 64, 8192} |
| 134 | dst := make([]byte, 8192) |
| 135 | benchFunc := func(b *testing.B, benchSize int) { |
| 136 | data := base64.StdEncoding.EncodeToString(make([]byte, benchSize)) |
| 137 | b.SetBytes(int64(len(data))) |
| 138 | b.ResetTimer() |
| 139 | b.ReportAllocs() |
| 140 | for i := 0; i < b.N; i++ { |
| 141 | base64.StdEncoding.Decode(dst, []byte(data)) |
| 142 | } |
| 143 | } |
| 144 | for _, size := range sizes { |
| 145 | b.Run(fmt.Sprintf("%d", size), func(b *testing.B) { |
| 146 | benchFunc(b, size) |
| 147 | }) |
| 148 | } |
| 149 | } |