aboutsummaryrefslogtreecommitdiffhomepage
path: root/vfs/unfold_test.go
blob: 59e5204178aeb4c56d0719cb8dbad22ec8c593ee (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
package vfs_test

import (
	"errors"
	"reflect"
	"slices"
	"strings"
	"syscall"
	"testing"

	"git.gensokyo.uk/security/hakurei/vfs"
)

func TestUnfold(t *testing.T) {
	testCases := []struct {
		name    string
		sample  string
		target  string
		wantErr error

		want         *vfs.MountInfoNode
		wantCollectF func(n *vfs.MountInfoNode) []*vfs.MountInfoNode
		wantCollectN []string
	}{
		{
			"no match",
			sampleMountinfoBase,
			"/mnt",
			syscall.ESTALE, nil, nil, nil,
		},
		{
			"cover",
			`33 1 0:33 / / rw,relatime shared:1 - tmpfs impure rw,size=16777216k,mode=755
37 33 0:32 / /proc rw,nosuid,nodev,noexec,relatime shared:41 - proc proc rw
551 33 0:121 / /mnt rw,relatime shared:666 - tmpfs tmpfs rw
595 551 0:123 / /mnt rw,relatime shared:990 - tmpfs tmpfs rw
611 595 0:142 / /mnt/etc rw,relatime shared:1112 - tmpfs tmpfs rw
625 644 0:142 /passwd /mnt/etc/passwd rw,relatime shared:1112 - tmpfs tmpfs rw
641 625 0:33 /etc/passwd /mnt/etc/passwd rw,relatime shared:1 - tmpfs impure rw,size=16777216k,mode=755
644 611 0:33 /etc/passwd /mnt/etc/passwd rw,relatime shared:1 - tmpfs impure rw,size=16777216k,mode=755
`, "/mnt", nil,
			mn(595, 551, 0, 123, "/", "/mnt", "rw,relatime", o("shared:990"), "tmpfs", "tmpfs", "rw", false,
				mn(611, 595, 0, 142, "/", "/mnt/etc", "rw,relatime", o("shared:1112"), "tmpfs", "tmpfs", "rw", false,
					mn(644, 611, 0, 33, "/etc/passwd", "/mnt/etc/passwd", "rw,relatime", o("shared:1"), "tmpfs", "impure", "rw,size=16777216k,mode=755", true,
						mn(625, 644, 0, 142, "/passwd", "/mnt/etc/passwd", "rw,relatime", o("shared:1112"), "tmpfs", "tmpfs", "rw", true,
							mn(641, 625, 0, 33, "/etc/passwd", "/mnt/etc/passwd", "rw,relatime", o("shared:1"), "tmpfs", "impure", "rw,size=16777216k,mode=755", false,
								nil, nil), nil), nil), nil), nil), func(n *vfs.MountInfoNode) []*vfs.MountInfoNode {
				return []*vfs.MountInfoNode{n, n.FirstChild, n.FirstChild.FirstChild.FirstChild.FirstChild}
			}, []string{"/mnt", "/mnt/etc", "/mnt/etc/passwd"},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			d := vfs.NewMountInfoDecoder(strings.NewReader(tc.sample))
			got, err := d.Unfold(tc.target)

			if !errors.Is(err, tc.wantErr) {
				t.Errorf("Unfold: error = %v, wantErr %v",
					err, tc.wantErr)
			}

			if !reflect.DeepEqual(got, tc.want) {
				t.Errorf("Unfold:\ngot  %s\nwant %s",
					mustMarshal(got), mustMarshal(tc.want))
			}

			if err == nil && tc.wantCollectF != nil {
				t.Run("collective", func(t *testing.T) {
					wantCollect := tc.wantCollectF(got)
					gotCollect := slices.Collect(got.Collective())
					if !reflect.DeepEqual(gotCollect, wantCollect) {
						t.Errorf("Collective: \ngot  %#v\nwant %#v",
							gotCollect, wantCollect)
					}
					t.Run("target", func(t *testing.T) {
						gotCollectN := slices.Collect[string](func(yield func(v string) bool) {
							for _, cur := range gotCollect {
								if !yield(cur.Clean) {
									return
								}
							}
						})
						if !reflect.DeepEqual(gotCollectN, tc.wantCollectN) {
							t.Errorf("Collective: got %q, want %q",
								gotCollectN, tc.wantCollectN)
						}
					})
				})
			}
		})
	}
}