summaryrefslogtreecommitdiff
path: root/internal/fusefrontend_reverse/rfs_test.go
blob: eccaab9384eb9b0839528dc0724539ae564fea98 (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
119
120
121
122
123
124
package fusefrontend_reverse

import (
	"testing"

	"github.com/rfjakob/gocryptfs/internal/configfile"
	"github.com/rfjakob/gocryptfs/internal/nametransform"
)

func TestShouldDetectDirIV(t *testing.T) {
	var rfs ReverseFS
	ftype := rfs.getFileType("some/path/" + nametransform.DirIVFilename)
	if ftype != typeDiriv {
		t.Errorf("Expecting %d, got %d\n", typeDiriv, ftype)
	}
}

func TestShouldDetectNameFile(t *testing.T) {
	var rfs ReverseFS
	ftype := rfs.getFileType("dir1/dir2/gocryptfs.longname.URrM8kgxTKYMgCk4hKk7RO9Lcfr30XQof4L_5bD9Iro=" + nametransform.LongNameSuffix)
	if ftype != typeName {
		t.Errorf("Expecting %d, got %d\n", typeName, ftype)
	}
}

func TestShouldDetectConfigFile(t *testing.T) {
	var rfs ReverseFS
	ftype := rfs.getFileType(configfile.ConfDefaultName)
	if ftype != typeConfig {
		t.Errorf("Expecting %d, got %d\n", typeConfig, ftype)
	}
}

func TestShouldDetectRegularFile(t *testing.T) {
	var rfs ReverseFS
	ftype := rfs.getFileType("documents/text_file.txt")
	if ftype != typeRegular {
		t.Errorf("Expecting %d, got %d\n", typeRegular, ftype)
	}
}

// Note: For path exclusion, see also the integration tests in
// tests/reverse/exclude_test.go
func TestShouldNotCallIgnoreParserForTranslatedConfig(t *testing.T) {
	rfs, ignorerMock := createRFSWithMocks()

	ftype, excluded, _, err := rfs.getFileInfo(configfile.ConfDefaultName)
	if err != nil {
		t.Errorf("Unexpected error %q\n", err)
	}
	if ftype != typeConfig {
		t.Errorf("Wrong file type, expecting %d, got %d\n", typeConfig, ftype)
	}
	if excluded {
		t.Error("Should not exclude translated config")
	}
	if ignorerMock.calledWith != "" {
		t.Error("Should not call IgnoreParser for translated config")
	}
}

func TestShouldCheckIfParentIsExcludedForDirIV(t *testing.T) {
	rfs, ignorerMock := createRFSWithMocks()
	path := "dir"
	ignorerMock.toExclude = "mockdecrypt_dir"
	dirIV := path + "/" + nametransform.DirIVFilename

	ftype, excluded, _, err := rfs.getFileInfo(dirIV)
	if err != nil {
		t.Errorf("Unexpected error %q\n", err)
	}
	if ftype != typeDiriv {
		t.Errorf("Wrong file type, expecting %d, got %d\n", typeDiriv, ftype)
	}
	if !excluded {
		t.Error("Should have excluded DirIV based on parent")
	}
	if ignorerMock.calledWith != "mockdecrypt_dir" {
		t.Errorf("Should have checked parent dir, checked %q", ignorerMock.calledWith)
	}
}

func TestShouldCheckIfParentIsExcludedForLongName(t *testing.T) {
	rfs, ignorerMock := createRFSWithMocks()
	path := "parent"
	ignorerMock.toExclude = "mockdecrypt_parent"
	dirIV := path + "/" + "gocryptfs.longname.fake.name"

	ftype, excluded, _, err := rfs.getFileInfo(dirIV)
	if err != nil {
		t.Errorf("Unexpected error %q\n", err)
	}
	if ftype != typeName {
		t.Errorf("Wrong file type, expecting %d, got %d\n", typeName, ftype)
	}
	if !excluded {
		t.Error("Should have excluded LongName based on parent")
	}
	if ignorerMock.calledWith != "mockdecrypt_parent" {
		t.Errorf("Should have checked parent dir, checked %q", ignorerMock.calledWith)
	}
}

func TestShouldDecryptPathAndReturnTrueForExcludedPath(t *testing.T) {
	rfs, ignorerMock := createRFSWithMocks()
	ignorerMock.toExclude = "mockdecrypt_file.txt"

	ftype, excluded, pPath, err := rfs.getFileInfo("file.txt")
	if err != nil {
		t.Errorf("Unexpected error %q\n", err)
	}
	if ftype != typeRegular {
		t.Errorf("Wrong file type, expecting %d, got %d\n", typeRegular, ftype)
	}
	if !excluded {
		t.Error("Should have excluded")
	}
	if pPath != "mockdecrypt_file.txt" {
		t.Errorf("Wrong pPath returned, got %q\n", pPath)
	}
	if ignorerMock.calledWith != "mockdecrypt_file.txt" {
		t.Error("Didn't call IgnoreParser with decrypted path")
	}
}