aboutsummaryrefslogtreecommitdiff
path: root/pkg/config/game_test.go
blob: b3bf107f825614f39d759a759036436bc0504249 (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
package config

import "testing"

func TestGameValidate(t *testing.T) {
	// Game name
	game := Game{}
	if err := game.validate("invalid game name because of spaces"); err == nil {
		t.Fatal("game name with spaces should not be valid")
	}
	// ChrootPath
	game = Game{ChrootPath: "test_data/non_existant"}
	if err := game.validate("test"); err == nil {
		t.Fatal("non_existant ChrootPath should not be valid")
	}
	// FileMode
	game = Game{
		ChrootPath: "test_data/fake_nethack_directory",
	}
	if err := game.validate("test"); err == nil {
		t.Fatal("Invalid FileMode should not be valid")
	}
	game = Game{
		ChrootPath: "test_data/fake_nethack_directory",
		FileMode:   "abcd",
	}
	if err := game.validate("test"); err == nil {
		t.Fatal("Invalid FileMode should not be valid")
	}
	game = Game{
		ChrootPath: "test_data/fake_nethack_directory",
		FileMode:   "777",
	}
	if err := game.validate("test"); err == nil {
		t.Fatal("Invalid FileMode should not be valid")
	}
	// Commands are mostly tested from command_test.go
	game = Game{
		ChrootPath: "test_data/fake_nethack_directory",
		FileMode:   "0777",
	}
	if err := game.validate("test"); err == nil {
		t.Fatal("Empty Commands list should not be valid")
	}
	game = Game{
		ChrootPath: "test_data/fake_nethack_directory",
		FileMode:   "0777",
		Commands:   []string{"invalid"},
	}
	if err := game.validate("test"); err == nil {
		t.Fatal("Invalid command in Commands should not be valid")
	}
	// Env
	game = Game{
		ChrootPath: "test_data/fake_nethack_directory",
		FileMode:   "0777",
		Commands:   []string{"wait"},
	}
	if err := game.validate("test"); err != nil {
		t.Fatal("Empty env list should be valid")
	}
	game = Game{
		ChrootPath: "test_data/fake_nethack_directory",
		FileMode:   "0777",
		Commands:   []string{"wait"},
		Env: map[string]string{
			"test invalid": "test",
		},
	}
	if err := game.validate("test"); err == nil {
		t.Fatal("Spaces in environnement variable name are invalid")
	}
	game = Game{
		ChrootPath: "test_data/fake_nethack_directory",
		FileMode:   "0777",
		Commands:   []string{"wait"},
		Env: map[string]string{
			"test\000invalid": "test",
		},
	}
	if err := game.validate("test"); err == nil {
		t.Fatal("null character in environnement variable name are invalid")
	}
	game = Game{
		ChrootPath: "test_data/fake_nethack_directory",
		FileMode:   "0777",
		Commands:   []string{"wait"},
		Env: map[string]string{
			"test=invalid": "test",
		},
	}
	if err := game.validate("test"); err == nil {
		t.Fatal("equals symbol in environnement variable name are invalid")
	}
	// Valid Game entry
	game = Game{
		ChrootPath: "test_data/fake_nethack_directory",
		FileMode:   "0777",
		Commands:   []string{"wait"},
		Env: map[string]string{
			"test": "test",
		},
	}
	if err := game.validate("test"); err != nil {
		t.Fatalf("Valid game entry should pass but got error %s", err)
	}
}