]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/files_test.go
tests: use subtests
[safcm/safcm.git] / cmd / safcm / config / files_test.go
1 // Copyright (C) 2021  Simon Ruderich
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package config
17
18 import (
19         "fmt"
20         "io/fs"
21         "os"
22         "reflect"
23         "syscall"
24         "testing"
25
26         "github.com/google/go-cmp/cmp"
27
28         "ruderich.org/simon/safcm"
29 )
30
31 func chmod(name string, perm int) {
32         err := os.Chmod(name, FullPermToFileMode(perm))
33         if err != nil {
34                 panic(err)
35         }
36 }
37
38 func TestLoadFiles(t *testing.T) {
39         cwd, err := os.Getwd()
40         if err != nil {
41                 t.Fatal(err)
42         }
43         defer os.Chdir(cwd)
44
45         err = os.Chdir("../testdata/project")
46         if err != nil {
47                 t.Fatal(err)
48         }
49
50         chmod("files-invalid-perm-dir/files", 0500)
51         defer chmod("files-invalid-perm-dir/files", 0700)
52         chmod("files-invalid-perm-dir/files/etc/", 0755)
53         chmod("files-invalid-perm-dir/files/etc/resolv.conf", 0644)
54         chmod("files-invalid-perm-dir-setgid/files", 0755)
55         chmod("files-invalid-perm-dir-setgid/files/etc/", 02755)
56         chmod("files-invalid-perm-dir-setgid/files/etc/resolv.conf", 0644)
57         chmod("files-invalid-perm-file/files", 0755)
58         chmod("files-invalid-perm-file/files/etc/", 0755)
59         chmod("files-invalid-perm-file/files/etc/resolv.conf", 0600)
60         chmod("files-invalid-perm-file-executable/files", 0755)
61         chmod("files-invalid-perm-file-executable/files/etc", 0755)
62         chmod("files-invalid-perm-file-executable/files/etc/rc.local", 0750)
63         chmod("files-invalid-perm-file-sticky/files", 0755)
64         chmod("files-invalid-perm-file-sticky/files/etc", 0755)
65         chmod("files-invalid-perm-file-sticky/files/etc/resolv.conf", 01644)
66
67         err = syscall.Mkfifo("files-invalid-type/files/invalid", 0644)
68         if err != nil {
69                 t.Fatal(err)
70         }
71         defer os.Remove("files-invalid-type/files/invalid")
72
73         const errMsg = `
74 The actual permissions and user/group of files and directories are not used
75 (except for +x on files). 0644/0755 and current remote user/group is used per
76 default. Apply different file permissions via permissions.yaml. To prevent
77 confusion files must be manually chmodded 0644/0755 and directories 0755 or
78 via "safcm fixperms".
79 `
80
81         tests := []struct {
82                 group  string
83                 exp    map[string]*safcm.File
84                 expErr error
85         }{
86
87                 {
88                         "empty",
89                         nil,
90                         nil,
91                 },
92
93                 {
94                         "group",
95                         map[string]*safcm.File{
96                                 "/": {
97                                         Path: "/",
98                                         Mode: fs.ModeDir | 0755,
99                                         Uid:  -1,
100                                         Gid:  -1,
101                                 },
102                                 "/etc": {
103                                         Path: "/etc",
104                                         Mode: fs.ModeDir | 0755,
105                                         Uid:  -1,
106                                         Gid:  -1,
107                                 },
108                                 "/etc/.hidden": {
109                                         Path: "/etc/.hidden",
110                                         Mode: 0644,
111                                         Uid:  -1,
112                                         Gid:  -1,
113                                         Data: []byte("..."),
114                                 },
115                                 "/etc/motd": {
116                                         Path: "/etc/motd",
117                                         Mode: 0644,
118                                         Uid:  -1,
119                                         Gid:  -1,
120                                         Data: []byte(`Welcome to
121 {{- if .IsHost "host1.example.org"}} Host ONE
122 {{- else if "host2"}} Host TWO
123 {{- end}}
124
125 {{if .InGroup "detected_linux"}}
126 This is GNU/Linux host
127 {{end}}
128 {{if .InGroup "detected_freebsd"}}
129 This is FreeBSD host
130 {{end}}
131 `),
132                                 },
133                                 "/etc/rc.local": {
134                                         Path: "/etc/rc.local",
135                                         Mode: 0755,
136                                         Uid:  -1,
137                                         Gid:  -1,
138                                         Data: []byte("#!/bin/sh\n"),
139                                 },
140                                 "/etc/resolv.conf": {
141                                         Path: "/etc/resolv.conf",
142                                         Mode: 0644,
143                                         Uid:  -1,
144                                         Gid:  -1,
145                                         Data: []byte("nameserver ::1\n"),
146                                 },
147                                 "/etc/test": {
148                                         Path: "/etc/test",
149                                         Mode: fs.ModeSymlink | 0777,
150                                         Uid:  -1,
151                                         Gid:  -1,
152                                         Data: []byte("doesnt-exist"),
153                                 },
154                         },
155                         nil,
156                 },
157
158                 {
159                         "files-invalid-type",
160                         nil,
161                         fmt.Errorf("files-invalid-type: \"files-invalid-type/files/invalid\": file type not supported"),
162                 },
163                 {
164                         "files-invalid-perm-dir",
165                         nil,
166                         fmt.Errorf("files-invalid-perm-dir: \"files-invalid-perm-dir/files\": invalid permissions 0500" + errMsg),
167                 },
168                 {
169                         "files-invalid-perm-dir-setgid",
170                         nil,
171                         fmt.Errorf("files-invalid-perm-dir-setgid: \"files-invalid-perm-dir-setgid/files/etc\": invalid permissions 02755" + errMsg),
172                 },
173                 {
174                         "files-invalid-perm-file",
175                         nil,
176                         fmt.Errorf("files-invalid-perm-file: \"files-invalid-perm-file/files/etc/resolv.conf\": invalid permissions 0600" + errMsg),
177                 },
178                 {
179                         "files-invalid-perm-file-executable",
180                         nil,
181                         fmt.Errorf("files-invalid-perm-file-executable: \"files-invalid-perm-file-executable/files/etc/rc.local\": invalid permissions 0750" + errMsg),
182                 },
183                 {
184                         "files-invalid-perm-file-sticky",
185                         nil,
186                         fmt.Errorf("files-invalid-perm-file-sticky: \"files-invalid-perm-file-sticky/files/etc/resolv.conf\": invalid permissions 01644" + errMsg),
187                 },
188         }
189
190         for _, tc := range tests {
191                 t.Run(tc.group, func(t *testing.T) {
192                 res, err := LoadFiles(tc.group)
193
194                 if !reflect.DeepEqual(tc.exp, res) {
195                         t.Errorf("res: %s",
196                                 cmp.Diff(tc.exp, res))
197                 }
198                 // Ugly but the simplest way to compare errors (including nil)
199                 if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
200                         t.Errorf("err = %#v, want %#v",
201                                 err, tc.expErr)
202                 }
203                 })
204         }
205 }