File: base85_test.go

package info (click to toggle)
golang-github-gitleaks-go-gitdiff 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 800 kB
  • sloc: makefile: 2; sh: 1
file content (60 lines) | stat: -rw-r--r-- 1,168 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
package gitdiff

import (
	"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])
				}
			}
		})
	}
}