aboutsummaryrefslogtreecommitdiff
path: root/tests/example_filesystems/example_test_helpers.go
blob: 615e4c16f1573f35fdd7ecafeea312b062ee67de (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
package example_filesystems

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"github.com/rfjakob/gocryptfs/tests/test_helpers"
)

// checkExampleFS - verify that "dir" contains the expected test files
func checkExampleFS(t *testing.T, dir string, rw bool) {
	// Read regular file
	statusFile := filepath.Join(dir, "status.txt")
	contentBytes, err := ioutil.ReadFile(statusFile)
	if err != nil {
		t.Fatal(err)
	}
	content := string(contentBytes)
	if content != statusTxtContent {
		t.Errorf("Unexpected content: %s\n", content)
	}
	// Read relative symlink
	symlink := filepath.Join(dir, "rel")
	target, err := os.Readlink(symlink)
	if err != nil {
		t.Fatal(err)
	}
	if target != "status.txt" {
		t.Errorf("Unexpected link target: %s\n", target)
	}
	// Read absolute symlink
	symlink = filepath.Join(dir, "abs")
	target, err = os.Readlink(symlink)
	if err != nil {
		t.Fatal(err)
	}
	if target != "/a/b/c/d" {
		t.Errorf("Unexpected link target: %s\n", target)
	}

	if rw {
		// Test directory operations
		test_helpers.TestRename(t, dir)
		test_helpers.TestMkdirRmdir(t, dir)
	}
}

// checkExampleFSLongnames - verify that "dir" contains the expected test files
// plus the long file name test file.
// Also tests simple directory operations.
func checkExampleFSLongnames(t *testing.T, dir string) {
	checkExampleFSrw(t, dir, true)
}

// checkExampleFSrw is like checkExampleFSLongnames but gives the caller the
// choice if he wants to run tests that write to the FS.
func checkExampleFSrw(t *testing.T, dir string, rw bool) {
	// regular tests
	checkExampleFS(t, dir, rw)
	// long name test file
	longname := "longname_255_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
		"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
		"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
		"xxxxxxxxxxxxxxxxxxxxxxxx"
	contentBytes, err := ioutil.ReadFile(filepath.Join(dir, longname))
	if err != nil {
		t.Fatal(err)
	}
	content := string(contentBytes)
	if content != statusTxtContent {
		t.Errorf("longname_255: unexpected content: %s\n", content)
	}
}