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