]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/files_test.go
tests: use filetest.CreateFifo() in config tests
[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         "runtime"
23         "testing"
24
25         "ruderich.org/simon/safcm"
26         ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
27         "ruderich.org/simon/safcm/testutil"
28 )
29
30 func chmod(name string, perm int) {
31         err := os.Chmod(name, FullPermToFileMode(perm))
32         if err != nil {
33                 panic(err)
34         }
35 }
36
37 func TestLoadFiles(t *testing.T) {
38         cwd, err := os.Getwd()
39         if err != nil {
40                 t.Fatal(err)
41         }
42         defer os.Chdir(cwd)
43
44         err = os.Chdir("../testdata/project")
45         if err != nil {
46                 t.Fatal(err)
47         }
48
49         // Regular users cannot create sticky files
50         skipInvalidSticky := os.Getuid() != 0 &&
51                 (runtime.GOOS == "freebsd" || runtime.GOOS == "openbsd")
52
53         chmod("files-invalid-perm-dir/files", 0500)
54         defer chmod("files-invalid-perm-dir/files", 0700)
55         chmod("files-invalid-perm-dir/files/etc/", 0755)
56         chmod("files-invalid-perm-dir/files/etc/resolv.conf", 0644)
57         chmod("files-invalid-perm-dir-setgid/files", 0755)
58         chmod("files-invalid-perm-dir-setgid/files/etc/", 02755)
59         chmod("files-invalid-perm-dir-setgid/files/etc/resolv.conf", 0644)
60         chmod("files-invalid-perm-file/files", 0755)
61         chmod("files-invalid-perm-file/files/etc/", 0755)
62         chmod("files-invalid-perm-file/files/etc/resolv.conf", 0600)
63         chmod("files-invalid-perm-file-executable/files", 0755)
64         chmod("files-invalid-perm-file-executable/files/etc", 0755)
65         chmod("files-invalid-perm-file-executable/files/etc/rc.local", 0750)
66         if !skipInvalidSticky {
67                 chmod("files-invalid-perm-file-sticky/files", 0755)
68                 chmod("files-invalid-perm-file-sticky/files/etc", 0755)
69                 chmod("files-invalid-perm-file-sticky/files/etc/resolv.conf", 01644)
70         }
71
72         ft.CreateFifo("files-invalid-type/files/invalid", 0644)
73         defer os.Remove("files-invalid-type/files/invalid")
74
75         const errMsg = `
76
77 The actual permissions and user/group of files and directories are not used
78 (except for +x on files). 0644/0755 and current remote user/group is used per
79 default. Apply different file permissions via permissions.yaml. To prevent
80 confusion files must be manually chmodded 0644/0755 and directories 0755 or
81 via "safcm fixperms".
82 `
83
84         tests := []struct {
85                 group  string
86                 skip   bool
87                 exp    map[string]*safcm.File
88                 expErr error
89         }{
90
91                 {
92                         "empty",
93                         false,
94                         nil,
95                         nil,
96                 },
97
98                 {
99                         "group",
100                         false,
101                         map[string]*safcm.File{
102                                 "/": {
103                                         Path: "/",
104                                         Mode: fs.ModeDir | 0755,
105                                         Uid:  -1,
106                                         Gid:  -1,
107                                 },
108                                 "/etc": {
109                                         Path: "/etc",
110                                         Mode: fs.ModeDir | 0755,
111                                         Uid:  -1,
112                                         Gid:  -1,
113                                 },
114                                 "/etc/.hidden": {
115                                         Path: "/etc/.hidden",
116                                         Mode: 0644,
117                                         Uid:  -1,
118                                         Gid:  -1,
119                                         Data: []byte("..."),
120                                 },
121                                 "/etc/motd": {
122                                         Path: "/etc/motd",
123                                         Mode: 0644,
124                                         Uid:  -1,
125                                         Gid:  -1,
126                                         Data: []byte(`Welcome to
127 {{- if .IsHost "host1.example.org"}} Host ONE
128 {{- else if "host2"}} Host TWO
129 {{- end}}
130
131 {{if .InGroup "detected_linux"}}
132 This is GNU/Linux host
133 {{end}}
134 {{if .InGroup "detected_freebsd"}}
135 This is FreeBSD host
136 {{end}}
137
138 {{if .InGroup "all"}}
139 all
140 {{end}}
141 {{if .InGroup "host1.example.org"}}
142 host1.example.org
143 {{end}}
144 {{if .InGroup "host2"}}
145 host2
146 {{end}}
147 {{if .InGroup "host3.example.net"}}
148 host3.example.net
149 {{end}}
150 `),
151                                 },
152                                 "/etc/rc.local": {
153                                         Path: "/etc/rc.local",
154                                         Mode: 0755,
155                                         Uid:  -1,
156                                         Gid:  -1,
157                                         Data: []byte("#!/bin/sh\n"),
158                                 },
159                                 "/etc/resolv.conf": {
160                                         Path: "/etc/resolv.conf",
161                                         Mode: 0644,
162                                         Uid:  -1,
163                                         Gid:  -1,
164                                         Data: []byte("nameserver ::1\n"),
165                                 },
166                                 "/etc/test": {
167                                         Path: "/etc/test",
168                                         Mode: fs.ModeSymlink | 0777,
169                                         Uid:  -1,
170                                         Gid:  -1,
171                                         Data: []byte("doesnt-exist"),
172                                 },
173                         },
174                         nil,
175                 },
176
177                 {
178                         "files-invalid-type",
179                         false,
180                         nil,
181                         fmt.Errorf("files-invalid-type: \"files-invalid-type/files/invalid\": file type not supported"),
182                 },
183                 {
184                         "files-invalid-perm-dir",
185                         false,
186                         nil,
187                         fmt.Errorf("files-invalid-perm-dir: \"files-invalid-perm-dir/files\": invalid permissions 0500" + errMsg),
188                 },
189                 {
190                         "files-invalid-perm-dir-setgid",
191                         false,
192                         nil,
193                         fmt.Errorf("files-invalid-perm-dir-setgid: \"files-invalid-perm-dir-setgid/files/etc\": invalid permissions 02755" + errMsg),
194                 },
195                 {
196                         "files-invalid-perm-file",
197                         false,
198                         nil,
199                         fmt.Errorf("files-invalid-perm-file: \"files-invalid-perm-file/files/etc/resolv.conf\": invalid permissions 0600" + errMsg),
200                 },
201                 {
202                         "files-invalid-perm-file-executable",
203                         false,
204                         nil,
205                         fmt.Errorf("files-invalid-perm-file-executable: \"files-invalid-perm-file-executable/files/etc/rc.local\": invalid permissions 0750" + errMsg),
206                 },
207                 {
208                         "files-invalid-perm-file-sticky",
209                         skipInvalidSticky,
210                         nil,
211                         fmt.Errorf("files-invalid-perm-file-sticky: \"files-invalid-perm-file-sticky/files/etc/resolv.conf\": invalid permissions 01644" + errMsg),
212                 },
213         }
214
215         for _, tc := range tests {
216                 t.Run(tc.group, func(t *testing.T) {
217                         if tc.skip {
218                                 t.SkipNow()
219                         }
220
221                         res, err := LoadFiles(tc.group)
222                         testutil.AssertEqual(t, "res", res, tc.exp)
223                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
224                 })
225         }
226 }