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/>.
27 ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
28 "ruderich.org/simon/safcm/testutil"
31 func TestHandle(t *testing.T) {
32 cwd, err := os.Getwd()
38 err = os.RemoveAll("testdata")
42 err = os.Mkdir("testdata", 0700)
47 // Set umask to test mode for new files
48 umask := syscall.Umask(027)
49 defer syscall.Umask(umask)
53 Mode: fs.ModeDir | 0700,
55 _, uid, _, gid := ft.CurrentUserAndGroup()
57 symlinkExists := "open file: too many levels of symbolic links"
58 if runtime.GOOS == "freebsd" {
59 // EMLINK instead of ELOOP
60 symlinkExists = "open file: too many links"
74 // TODO: Add tests for chown and run them only as root
86 fmt.Errorf("\"file\": file does not exist, use -create"),
100 Data: []byte("line\n"),
104 fmt.Sprintf(`"file": created file (%d/%d -rw-r-----)`, uid, gid),
105 `"file": added line "line"`,
110 "missing: create (empty line)",
119 fmt.Errorf("empty line"),
122 "missing: create (newline)",
131 fmt.Errorf("line must not contain newlines: \"line\\n\""),
140 ft.CreateFile("file", "line\n", 0641)
147 Data: []byte("line\n"),
160 ft.CreateFile("file", "existing\n", 0641)
167 Data: []byte("existing\nline\n"),
171 `"file": added line "line"`,
176 "exists: append (no newline in file)",
181 ft.CreateFile("file", "existing", 0641)
188 Data: []byte("existing\nline\n"),
192 `"file": added missing trailing newline`,
193 `"file": added line "line"`,
198 "exists: append (only trailing newline I)",
203 ft.CreateFile("file", "first\nline", 0641)
210 Data: []byte("first\nline\n"),
214 `"file": added missing trailing newline`,
219 "exists: append (only trailing newline II)",
224 ft.CreateFile("file", "first\nline", 0641)
231 Data: []byte("first\nline\n"),
235 `"file": added missing trailing newline`,
240 "exists: append (partial line)",
245 ft.CreateFile("file", "# line with spaces\n", 0641)
252 Data: []byte("# line with spaces\nline with spaces\n"),
256 `"file": added line "line with spaces"`,
267 ft.CreateSymlink("file", "target")
273 Mode: fs.ModeSymlink | 0777,
274 Data: []byte("target"),
278 fmt.Errorf(symlinkExists),
286 ft.CreateFifo("file", 0640)
292 Mode: fs.ModeNamedPipe | 0640,
296 fmt.Errorf("\"file\" is not a regular file but p---------"),
300 for _, tc := range tests {
301 t.Run(tc.name, func(t *testing.T) {
302 // Create separate test directory for each test case
303 path := filepath.Join(cwd, "testdata", tc.name)
304 err = os.Mkdir(path, 0700)
313 if tc.prepare != nil {
317 changes, err := handle(tc.path, tc.line, tc.create)
318 testutil.AssertEqual(t, "changes",
319 changes, tc.expChanges)
320 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
322 files, err := ft.WalkDir(path)
326 testutil.AssertEqual(t, "files", files, tc.expFiles)
331 err = os.RemoveAll(filepath.Join(cwd, "testdata"))