aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/testsuite/fs_test.go
blob: 4eea866d49e073db55d48bfdf3f4db47356fc740 (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
package testsuite_test

import (
	"bytes"
	"errors"
	"fmt"
	"io/fs"
	"testing"
	"testing/fstest"

	"hakurei.app/internal/testsuite"
)

func TestCompare(t *testing.T) {
	var (
		fsPasswdSample = "u0_a20:x:65534:65534:Hakurei:/var/lib/persist/module/hakurei/u0/a20:/run/current-system/sw/bin/zsh"
		fsGroupSample  = "hakurei:x:65534:"
	)

	testCases := []struct {
		name string

		sample  fstest.MapFS
		want    *testsuite.FS
		wantOut string
		wantErr error
	}{
		{"skip", fstest.MapFS{}, &testsuite.FS{}, "[ OK ] s .\x00", nil},
		{"simple pass", fstest.MapFS{".hakurei": {Mode: 0x800001ed}},
			&testsuite.FS{Dir: map[string]*testsuite.FS{".hakurei": {Mode: 0x800001ed}}},
			"[ OK ] s .hakurei\x00[ OK ] d .\x00", nil},
		{"bad length", fstest.MapFS{".hakurei": {Mode: 0x800001ed}},
			&testsuite.FS{Dir: make(map[string]*testsuite.FS)},
			"[FAIL] d .: \".hakurei/\"\x00", testsuite.ErrFSBadLength},
		{"top level bad mode", fstest.MapFS{".hakurei": {Mode: 0x800001ed}},
			&testsuite.FS{Dir: map[string]*testsuite.FS{".hakurei": {Mode: 0xdeadbeef}}},
			"[FAIL] m .hakurei: 020000000755, want 033653337357\x00", testsuite.ErrFSBadMode},
		{"invalid entry condition", fstest.MapFS{"test": {Data: []byte{'0'}, Mode: 0644}},
			&testsuite.FS{Dir: map[string]*testsuite.FS{"test": {Dir: make(map[string]*testsuite.FS)}}},
			"[FAIL] d .: \"test\"\x00", testsuite.ErrFSInvalidEnt},
		{"nonexistent", fstest.MapFS{"test": {Data: []byte{'0'}, Mode: 0644}},
			&testsuite.FS{Dir: map[string]*testsuite.FS{".test": {}}},
			"[FAIL] d .: \"test\"\x00", fs.ErrNotExist},
		{"file", fstest.MapFS{"etc": {Mode: 0x800001c0},
			"etc/passwd": {Data: []byte(fsPasswdSample), Mode: 0644},
			"etc/group":  {Data: []byte(fsGroupSample), Mode: 0644},
		}, &testsuite.FS{Dir: map[string]*testsuite.FS{"etc": {Mode: 0x800001c0, Dir: map[string]*testsuite.FS{
			"passwd": {Mode: 0x1a4, Data: &fsPasswdSample},
			"group":  {Mode: 0x1a4, Data: &fsGroupSample},
		}}}}, "[ OK ] f etc/group\x00[ OK ] f etc/passwd\x00[ OK ] d etc\x00[ OK ] d .\x00", nil},
		{"file differ", fstest.MapFS{"etc": {Mode: 0x800001c0},
			"etc/passwd": {Data: []byte(fsPasswdSample), Mode: 0644},
			"etc/group":  {Data: []byte(fsGroupSample), Mode: 0644},
		}, &testsuite.FS{Dir: map[string]*testsuite.FS{"etc": {Mode: 0x800001c0, Dir: map[string]*testsuite.FS{
			"passwd": {Mode: 0x1a4, Data: &fsGroupSample},
			"group":  {Mode: 0x1a4, Data: &fsGroupSample},
		}}}}, "[ OK ] f etc/group\x00[FAIL] f etc/passwd\n\t got: u0_a20:x:65534:65534:Hakurei:/var/lib/persist/module/hakurei/u0/a20:/run/current-system/sw/bin/zsh\n\twant: hakurei:x:65534:\x00", testsuite.ErrFSBadData},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			var buf bytes.Buffer

			err := tc.want.Compare(
				func(format string, a ...any) {
					_, _ = fmt.Fprintf(&buf, format+"\x00", a...)
				},
				".", tc.sample,
			)
			if !errors.Is(err, tc.wantErr) {
				t.Errorf(
					"Compare: error = %v; wantErr %v",
					err, tc.wantErr,
				)
			}

			if buf.String() != tc.wantOut {
				t.Errorf(
					"Compare: output %q; want %q",
					&buf, tc.wantOut,
				)
			}
		})
	}
}