File: decoder_test.go

package info (click to toggle)
golang-github-dop251-base64dec 0.0~git20231022.c6c9f9a-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 80 kB
  • sloc: makefile: 2
file content (149 lines) | stat: -rw-r--r-- 3,701 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package base64dec

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"testing"
)

func TestDecodeBase64(t *testing.T) {
	f := func(t *testing.T, input, expectedResult string, expectedError error) {
		decoded := make([]byte, base64.RawStdEncoding.DecodedLen(len(input)))
		n, err := DecodeBase64(decoded, input)
		if err != expectedError {
			t.Fatal(err)
		}
		encoded := base64.StdEncoding.EncodeToString(decoded[:n])
		if encoded != expectedResult {
			t.Fatal(encoded)
		}
	}

	t.Run("empty input", func(t *testing.T) {
		f(t, "", "", nil)
	})

	t.Run("newlines only", func(t *testing.T) {
		f(t, "\r\n", "", nil)
	})

	t.Run("correct padding", func(t *testing.T) {
		f(t, "Z9CA-w==", "Z9CA+w==", nil)
	})

	t.Run("correct padding split by newline", func(t *testing.T) {
		f(t, "Z9CA-w=\n=", "Z9CA+w==", nil)
	})

	t.Run("correct padding with concatenation", func(t *testing.T) {
		f(t, "Z9CA-w==Z9CA-w==", "Z9CA+w==", base64.CorruptInputError(8))
	})

	t.Run("trailing newline", func(t *testing.T) {
		f(t, "Z9CA+wZ9CA-w\n", "Z9CA+wZ9CA+w", nil)
	})

	t.Run("trailing newline with padding", func(t *testing.T) {
		f(t, "Z9CA+wZ9CA-www==\n", "Z9CA+wZ9CA+www==", nil)
	})

	t.Run("garbage after newline", func(t *testing.T) {
		f(t, "Z9CA+wZ9CA-www==\n?", "Z9CA+wZ9CA+www==", base64.CorruptInputError(17))
	})

	t.Run("no padding", func(t *testing.T) {
		f(t, "Z9CA-w", "Z9CA+w==", nil)
	})

	t.Run("no padding, garbage at the end", func(t *testing.T) {
		f(t, "Z9CA-w???", "Z9CA+w==", base64.CorruptInputError(6))
	})

	t.Run("not enough padding", func(t *testing.T) {
		f(t, "Z9CA-w=", "Z9CA+w==", base64.CorruptInputError(6))
	})

	t.Run("incorrect padding", func(t *testing.T) {
		f(t, "Z9CA====", "Z9CA", base64.CorruptInputError(4))
	})

	t.Run("incorrect padding with extra base64", func(t *testing.T) {
		f(t, "Z9CA-w=Z9CA-w=", "Z9CA+w==", base64.CorruptInputError(6))
	})

	t.Run("incorrect padding with garbage", func(t *testing.T) {
		f(t, "Z9CA-w=???", "Z9CA+w==", base64.CorruptInputError(6))
	})

}

func FuzzDecodeBase64(f *testing.F) {
	f.Add([]byte{})
	f.Add([]byte("\x14\xfb\x9c\x03\xd9\x7e"))
	f.Add([]byte("\x14\xfb\x9c\x03\xd9"))
	f.Add([]byte("\x14\xfb\x9c\x03"))

	f.Fuzz(func(t *testing.T, b []byte) {
		encoded := base64.StdEncoding.EncodeToString(b)
		decoded := make([]byte, len(b))
		n, err := DecodeBase64(decoded, encoded)
		if err != nil {
			t.Fatalf("%v: %v", b, err)
		}
		if !bytes.Equal(decoded[:n], b) {
			t.Fatal(b)
		}

	})
}

func FuzzDecodeBase64String(f *testing.F) {
	f.Add("Z9CA-w")
	f.Add("=\n=")
	f.Add("=")
	f.Add("====")

	f.Fuzz(func(t *testing.T, s string) {
		decoded := make([]byte, base64.RawStdEncoding.DecodedLen(len(s)))
		_, _ = DecodeBase64(decoded, s) // should not panic
	})
}

func BenchmarkDecodeBase64(b *testing.B) {
	sizes := []int{2, 4, 8, 64, 8192}
	dst := make([]byte, 8192)
	benchFunc := func(b *testing.B, benchSize int) {
		data := base64.StdEncoding.EncodeToString(make([]byte, benchSize))
		b.SetBytes(int64(len(data)))
		b.ResetTimer()
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			DecodeBase64(dst, data)
		}
	}
	for _, size := range sizes {
		b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
			benchFunc(b, size)
		})
	}
}

func BenchmarkDecodeBase64Std(b *testing.B) {
	sizes := []int{2, 4, 8, 64, 8192}
	dst := make([]byte, 8192)
	benchFunc := func(b *testing.B, benchSize int) {
		data := base64.StdEncoding.EncodeToString(make([]byte, benchSize))
		b.SetBytes(int64(len(data)))
		b.ResetTimer()
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			base64.StdEncoding.Decode(dst, []byte(data))
		}
	}
	for _, size := range sizes {
		b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
			benchFunc(b, size)
		})
	}
}