-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers_test.go
84 lines (68 loc) · 2.41 KB
/
helpers_test.go
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
package xenstore
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestValidPath(t *testing.T) {
invalid := []string{"/vm/", "/vm//tools", "vo/", "/\x00ot"}
for _, path := range invalid {
if ValidPath(path) {
t.Fatalf("Should have been invalid: %s", path)
}
}
valid := []string{"/vm", "/vm/tools", "vm/test", "/vm\x00"}
for _, path := range valid {
if !ValidPath(path) {
t.Fatalf("Should have been valid: %s", path)
}
}
}
func TestValidWatchPath(t *testing.T) {
invalid := []string{"/vm/", "/vm//tools", "vo/", "/\x00ot"}
for _, path := range invalid {
if ValidWatchPath(path) {
t.Fatalf("Should have been invalid: %s", path)
}
}
valid := []string{"/vm", "/vm/tools", "vm/test", "/vm\x00", "introduceDomain", "releaseDomain\x00"}
for _, path := range valid {
if !ValidWatchPath(path) {
t.Fatalf("Should have been valid: %s", path)
}
}
}
func TestValidPermissions(t *testing.T) {
if ValidPermissions("w0w", "b", "0") {
t.Fatalf("Invalid arguments not correctly detected")
}
if !ValidPermissions("w0", "b15", "r1", "n100") {
t.Fatalf("Valid arguments incorrectly rejected")
}
}
func TestUnixSocketPath(t *testing.T) {
os.Setenv("XENSTORED_PATH", "/example/xensocket")
os.Setenv("XENSTORED_RUNDIR", "")
assert.Equal(t, "/example/xensocket", UnixSocketPath(),
"Should be equal to XENSTORED_PATH if XENSTORED_PATH is set")
os.Setenv("XENSTORED_PATH", "")
os.Setenv("XENSTORED_RUNDIR", "/tmp/xenstored/")
assert.Equal(t, "/tmp/xenstored/socket", UnixSocketPath(),
"Should be equal to XENSTORED_RUNDIR + 'socket' if XENSTORED_RUNDIR is set")
os.Setenv("XENSTORED_PATH", "")
os.Setenv("XENSTORED_RUNDIR", "")
assert.Equal(t, "/var/run/xenstored/socket", UnixSocketPath(),
"Should have a default if neither env variable is set")
}
func TestPathJoin(t *testing.T) {
assert.Equal(t, "/tools/vm", JoinXenStorePath("/tools", "vm"),
"Should preserve leading slash when joining")
assert.Equal(t, "tools/vm", JoinXenStorePath("tools", "vm"),
"Should preserve lack of leading slash when joining")
assert.Equal(t, "/local/domain/0/name", JoinXenStorePath("/local", "domain", "0", "name"),
"Should behave the same for larger lists of elements")
assert.Equal(t, "/tools/vm", JoinXenStorePath("/", "tools", "vm"),
"Should preserve a separate leading slash")
assert.Equal(t, "/", JoinXenStorePath("/"),
"Should preserve a leading slash even if it is the only element")
}