summaryrefslogtreecommitdiff
path: root/pkg/gitdiff/base85_test.go
blob: 672c4715fdd55efe62242c3aa1d3b16a2ce5f8c0 (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
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
package gitdiff

import (
	"bytes"
	"testing"
)

func TestBase85Decode(t *testing.T) {
	tests := map[string]struct {
		Input  string
		Output []byte
		Err    bool
	}{
		"twoBytes": {
			Input:  "%KiWV",
			Output: []byte{0xCA, 0xFE},
		},
		"fourBytes": {
			Input:  "007GV",
			Output: []byte{0x0, 0x0, 0xCA, 0xFE},
		},
		"sixBytes": {
			Input:  "007GV%KiWV",
			Output: []byte{0x0, 0x0, 0xCA, 0xFE, 0xCA, 0xFE},
		},
		"invalidCharacter": {
			Input: "00'GV",
			Err:   true,
		},
		"underpaddedSequence": {
			Input: "007G",
			Err:   true,
		},
		"dataUnderrun": {
			Input:  "007GV",
			Output: make([]byte, 8),
			Err:    true,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			dst := make([]byte, len(test.Output))
			err := base85Decode(dst, []byte(test.Input))
			if test.Err {
				if err == nil {
					t.Fatalf("expected error decoding base85 data, but got nil")
				}
				return
			}
			if err != nil {
				t.Fatalf("unexpected error decoding base85 data: %v", err)
			}
			for i, b := range test.Output {
				if dst[i] != b {
					t.Errorf("incorrect byte at index %d: expected 0x%X, actual 0x%X", i, b, dst[i])
				}
			}
		})
	}
}

func TestBase85Encode(t *testing.T) {
	tests := map[string]struct {
		Input  []byte
		Output string
	}{
		"zeroBytes": {
			Input:  []byte{},
			Output: "",
		},
		"twoBytes": {
			Input:  []byte{0xCA, 0xFE},
			Output: "%KiWV",
		},
		"fourBytes": {
			Input:  []byte{0x0, 0x0, 0xCA, 0xFE},
			Output: "007GV",
		},
		"sixBytes": {
			Input:  []byte{0x0, 0x0, 0xCA, 0xFE, 0xCA, 0xFE},
			Output: "007GV%KiWV",
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			dst := make([]byte, len(test.Output))
			base85Encode(dst, test.Input)
			for i, b := range test.Output {
				if dst[i] != byte(b) {
					t.Errorf("incorrect character at index %d: expected '%c', actual '%c'", i, b, dst[i])
				}
			}
		})
	}
}

func FuzzBase85Roundtrip(f *testing.F) {
	f.Add([]byte{0x2b, 0x0d})
	f.Add([]byte{0xbc, 0xb4, 0x3f})
	f.Add([]byte{0xfa, 0x62, 0x05, 0x83, 0x24, 0x39, 0xd5, 0x25})
	f.Add([]byte{0x31, 0x59, 0x02, 0xa0, 0x61, 0x12, 0xd9, 0x43, 0xb8, 0x23, 0x1a, 0xb4, 0x02, 0xae, 0xfa, 0xcc, 0x22, 0xad, 0x41, 0xb9, 0xb8})

	f.Fuzz(func(t *testing.T, in []byte) {
		n := len(in)
		dst := make([]byte, base85Len(n))
		out := make([]byte, n)

		base85Encode(dst, in)
		if err := base85Decode(out, dst); err != nil {
			t.Fatalf("unexpected error decoding base85 data: %v", err)
		}
		if !bytes.Equal(in, out) {
			t.Errorf("decoded data differed from input data:\n   input: %x\n  output: %x\nencoding: %s\n", in, out, string(dst))
		}
	})
}