summaryrefslogtreecommitdiff
path: root/internal/base64_test.go
blob: f18bcd7ff1630eb4dfee3bdfac724bef361bc2dc (plain)
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
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package internal

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

	"github.com/stretchr/testify/require"
)

func b64encode(t *testing.T, input []byte) []byte {
	var encoded bytes.Buffer
	enc := base64.NewEncoder(base64.StdEncoding, &encoded)
	_, err := enc.Write(input)
	require.NoError(t, err)
	require.NoError(t, enc.Close())
	return encoded.Bytes()
}

func b64decode(t *testing.T, input []byte) []byte {
	var decoded bytes.Buffer
	dec := NewBase64Decoder(&decoded)
	_, err := dec.Write(input)
	require.NoError(t, err)
	require.NoError(t, dec.Close())
	return decoded.Bytes()
}

func FuzzBase64Decoder(f *testing.F) {
	f.Fuzz(func(t *testing.T, input []byte) {
		encoded := b64encode(t, input)
		decoded := b64decode(t, encoded)
		t.Logf("input b64        = %q", encoded)
		t.Logf("expected decoded = %#v", input)
		t.Logf("actual decoded   = %#v", decoded)
		if !bytes.Equal(input, decoded) {
			t.Fail()
		}
	})
}