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