1 // Copyright (C) 2021 Simon Ruderich
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.
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.
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/>.
25 "ruderich.org/simon/safcm"
26 ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
27 "ruderich.org/simon/safcm/testutil"
30 func chmod(name string, perm int) {
31 err := os.Chmod(name, FullPermToFileMode(perm))
37 func TestLoadFiles(t *testing.T) {
38 cwd, err := os.Getwd()
44 err = os.Chdir("../testdata/project")
49 // Regular users cannot create sticky files
50 skipInvalidSticky := os.Getuid() != 0 &&
51 (runtime.GOOS == "freebsd" || runtime.GOOS == "openbsd")
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)
72 ft.CreateFifo("files-invalid-type/files/invalid", 0644)
73 defer os.Remove("files-invalid-type/files/invalid")
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
87 exp map[string]*safcm.File
101 map[string]*safcm.File{
104 Mode: fs.ModeDir | 0755,
110 Mode: fs.ModeDir | 0755,
115 Path: "/etc/.hidden",
126 Data: []byte(`Welcome to
127 {{- if .IsHost "host1.example.org"}} Host ONE
128 {{- else if "host2"}} Host TWO
131 {{if .InGroup "detected_linux"}}
132 This is GNU/Linux host
134 {{if .InGroup "detected_freebsd"}}
138 {{if .InGroup "all"}}
141 {{if .InGroup "host1.example.org"}}
144 {{if .InGroup "host2"}}
147 {{if .InGroup "host3.example.net"}}
153 Path: "/etc/rc.local",
157 Data: []byte("#!/bin/sh\n"),
159 "/etc/resolv.conf": {
160 Path: "/etc/resolv.conf",
164 Data: []byte("nameserver ::1\n"),
168 Mode: fs.ModeSymlink | 0777,
171 Data: []byte("doesnt-exist"),
178 "files-invalid-type",
181 fmt.Errorf("files-invalid-type: \"files-invalid-type/files/invalid\": file type not supported"),
184 "files-invalid-perm-dir",
187 fmt.Errorf("files-invalid-perm-dir: \"files-invalid-perm-dir/files\": invalid permissions 0500" + errMsg),
190 "files-invalid-perm-dir-setgid",
193 fmt.Errorf("files-invalid-perm-dir-setgid: \"files-invalid-perm-dir-setgid/files/etc\": invalid permissions 02755" + errMsg),
196 "files-invalid-perm-file",
199 fmt.Errorf("files-invalid-perm-file: \"files-invalid-perm-file/files/etc/resolv.conf\": invalid permissions 0600" + errMsg),
202 "files-invalid-perm-file-executable",
205 fmt.Errorf("files-invalid-perm-file-executable: \"files-invalid-perm-file-executable/files/etc/rc.local\": invalid permissions 0750" + errMsg),
208 "files-invalid-perm-file-sticky",
211 fmt.Errorf("files-invalid-perm-file-sticky: \"files-invalid-perm-file-sticky/files/etc/resolv.conf\": invalid permissions 01644" + errMsg),
215 for _, tc := range tests {
216 t.Run(tc.group, func(t *testing.T) {
221 res, err := LoadFiles(tc.group)
222 testutil.AssertEqual(t, "res", res, tc.exp)
223 testutil.AssertErrorEqual(t, "err", err, tc.expErr)