]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/files_test.go
README: multiple improvements
[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         "syscall"
24         "testing"
25
26         "ruderich.org/simon/safcm"
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         err = syscall.Mkfifo("files-invalid-type/files/invalid", 0644)
73         if err != nil {
74                 t.Fatal(err)
75         }
76         defer os.Remove("files-invalid-type/files/invalid")
77
78         const errMsg = `
79
80 The actual permissions and user/group of files and directories are not used
81 (except for +x on files). 0644/0755 and current remote user/group is used per
82 default. Apply different file permissions via permissions.yaml. To prevent
83 confusion files must be manually chmodded 0644/0755 and directories 0755 or
84 via "safcm fixperms".
85 `
86
87         tests := []struct {
88                 group  string
89                 skip   bool
90                 exp    map[string]*safcm.File
91                 expErr error
92         }{
93
94                 {
95                         "empty",
96                         false,
97                         nil,
98                         nil,
99                 },
100
101                 {
102                         "group",
103                         false,
104                         map[string]*safcm.File{
105                                 "/": {
106                                         Path: "/",
107                                         Mode: fs.ModeDir | 0755,
108                                         Uid:  -1,
109                                         Gid:  -1,
110                                 },
111                                 "/etc": {
112                                         Path: "/etc",
113                                         Mode: fs.ModeDir | 0755,
114                                         Uid:  -1,
115                                         Gid:  -1,
116                                 },
117                                 "/etc/.hidden": {
118                                         Path: "/etc/.hidden",
119                                         Mode: 0644,
120                                         Uid:  -1,
121                                         Gid:  -1,
122                                         Data: []byte("..."),
123                                 },
124                                 "/etc/motd": {
125                                         Path: "/etc/motd",
126                                         Mode: 0644,
127                                         Uid:  -1,
128                                         Gid:  -1,
129                                         Data: []byte(`Welcome to
130 {{- if .IsHost "host1.example.org"}} Host ONE
131 {{- else if "host2"}} Host TWO
132 {{- end}}
133
134 {{if .InGroup "detected_linux"}}
135 This is GNU/Linux host
136 {{end}}
137 {{if .InGroup "detected_freebsd"}}
138 This is FreeBSD host
139 {{end}}
140
141 {{if .InGroup "all"}}
142 all
143 {{end}}
144 {{if .InGroup "host1.example.org"}}
145 host1.example.org
146 {{end}}
147 {{if .InGroup "host2"}}
148 host2
149 {{end}}
150 {{if .InGroup "host3.example.net"}}
151 host3.example.net
152 {{end}}
153 `),
154                                 },
155                                 "/etc/rc.local": {
156                                         Path: "/etc/rc.local",
157                                         Mode: 0755,
158                                         Uid:  -1,
159                                         Gid:  -1,
160                                         Data: []byte("#!/bin/sh\n"),
161                                 },
162                                 "/etc/resolv.conf": {
163                                         Path: "/etc/resolv.conf",
164                                         Mode: 0644,
165                                         Uid:  -1,
166                                         Gid:  -1,
167                                         Data: []byte("nameserver ::1\n"),
168                                 },
169                                 "/etc/test": {
170                                         Path: "/etc/test",
171                                         Mode: fs.ModeSymlink | 0777,
172                                         Uid:  -1,
173                                         Gid:  -1,
174                                         Data: []byte("doesnt-exist"),
175                                 },
176                         },
177                         nil,
178                 },
179
180                 {
181                         "files-invalid-type",
182                         false,
183                         nil,
184                         fmt.Errorf("files-invalid-type: \"files-invalid-type/files/invalid\": file type not supported"),
185                 },
186                 {
187                         "files-invalid-perm-dir",
188                         false,
189                         nil,
190                         fmt.Errorf("files-invalid-perm-dir: \"files-invalid-perm-dir/files\": invalid permissions 0500" + errMsg),
191                 },
192                 {
193                         "files-invalid-perm-dir-setgid",
194                         false,
195                         nil,
196                         fmt.Errorf("files-invalid-perm-dir-setgid: \"files-invalid-perm-dir-setgid/files/etc\": invalid permissions 02755" + errMsg),
197                 },
198                 {
199                         "files-invalid-perm-file",
200                         false,
201                         nil,
202                         fmt.Errorf("files-invalid-perm-file: \"files-invalid-perm-file/files/etc/resolv.conf\": invalid permissions 0600" + errMsg),
203                 },
204                 {
205                         "files-invalid-perm-file-executable",
206                         false,
207                         nil,
208                         fmt.Errorf("files-invalid-perm-file-executable: \"files-invalid-perm-file-executable/files/etc/rc.local\": invalid permissions 0750" + errMsg),
209                 },
210                 {
211                         "files-invalid-perm-file-sticky",
212                         skipInvalidSticky,
213                         nil,
214                         fmt.Errorf("files-invalid-perm-file-sticky: \"files-invalid-perm-file-sticky/files/etc/resolv.conf\": invalid permissions 01644" + errMsg),
215                 },
216         }
217
218         for _, tc := range tests {
219                 t.Run(tc.group, func(t *testing.T) {
220                         if tc.skip {
221                                 t.SkipNow()
222                         }
223
224                         res, err := LoadFiles(tc.group)
225                         testutil.AssertEqual(t, "res", res, tc.exp)
226                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
227                 })
228         }
229 }