1 // Copyright (C) 2019 Simon Ruderich
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Affero 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 Affero General Public License for more details.
13 // You should have received a copy of the GNU Affero General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
35 configPath = "testdata/config.toml"
36 statePath = "testdata/state.json"
37 passwdPath = "testdata/passwd.nsscash"
38 plainPath = "testdata/plain"
39 groupPath = "testdata/group.nsscash"
45 handler *func(http.ResponseWriter, *http.Request)
48 // mustNotExist verifies that all given paths don't exist in the file system.
49 func mustNotExist(t *testing.T, paths ...string) {
50 for _, p := range paths {
53 if !os.IsNotExist(err) {
54 t.Errorf("path %q: unexpected error: %v",
58 t.Errorf("path %q exists", p)
64 // mustHaveHash checks if the given path content has the given SHA-1 string
66 func mustHaveHash(t *testing.T, path string, hash string) {
67 x, err := ioutil.ReadFile(path)
74 y := hex.EncodeToString(h.Sum(nil))
77 t.Errorf("%q has unexpected hash %q", path, y)
81 // mustBeErrorWithSubstring checks if the given error, represented as string,
82 // contains the given substring. This is somewhat ugly but the simplest way to
83 // check for proper errors.
84 func mustBeErrorWithSubstring(t *testing.T, err error, substring string) {
86 t.Errorf("err is nil")
87 } else if !strings.Contains(err.Error(), substring) {
88 t.Errorf("err %q does not contain string %q", err, substring)
92 func mustWriteConfig(t *testing.T, config string) {
93 err := ioutil.WriteFile(configPath, []byte(config), 0644)
99 func mustWritePasswdConfig(t *testing.T, url string) {
100 mustWriteConfig(t, fmt.Sprintf(`
107 `, statePath, url, passwdPath))
110 func mustWriteGroupConfig(t *testing.T, url string) {
111 mustWriteConfig(t, fmt.Sprintf(`
118 `, statePath, url, groupPath))
121 // mustCreate creates a file, truncating it if it exists. It then changes the
122 // modification to be in the past.
123 func mustCreate(t *testing.T, path string) {
124 f, err := os.Create(path)
133 // Change modification time to the past to detect updates to the file
137 // mustMakeOld change the modification time of all paths to be in the past.
138 func mustMakeOld(t *testing.T, paths ...string) {
139 old := time.Now().Add(-2 * time.Hour)
140 for _, p := range paths {
141 err := os.Chtimes(p, old, old)
148 // mustMakeOld verifies that all paths have a modification time in the past,
149 // as set by mustMakeOld().
150 func mustBeOld(t *testing.T, paths ...string) {
151 for _, p := range paths {
159 if now.Sub(mtime) < time.Hour {
160 t.Errorf("%q was recently modified", p)
165 // mustBeNew verifies that all paths have a modification time in the present.
166 func mustBeNew(t *testing.T, paths ...string) {
167 for _, p := range paths {
175 if now.Sub(mtime) > time.Hour {
176 t.Errorf("%q was not recently modified", p)
181 func TestMainFetch(t *testing.T) {
182 // Suppress log messages
183 log.SetOutput(ioutil.Discard)
184 defer log.SetOutput(os.Stderr)
186 tests := []func(args){
187 // Perform most tests with passwd for simplicity
188 fetchPasswdCacheFileDoesNotExist,
194 // Tests for plain and group
203 fetchStateCannotRead,
205 fetchStateCannotWrite,
207 fetchSecondFetchFails,
218 for _, f := range tests {
219 // NOTE: This is not guaranteed to work according to reflect's
220 // documentation but seems to work reliable for normal
222 fn := runtime.FuncForPC(reflect.ValueOf(f).Pointer())
224 name = name[strings.LastIndex(name, ".")+1:]
226 t.Run(name, func(t *testing.T) {
227 // Preparation & cleanup
228 for _, p := range cleanup {
230 if err != nil && !os.IsNotExist(err) {
233 // Remove the file at the end of this test
234 // run, if it was created
238 var handler func(http.ResponseWriter, *http.Request)
239 ts := httptest.NewServer(http.HandlerFunc(
240 func(w http.ResponseWriter, r *http.Request) {
254 func fetchPasswdCacheFileDoesNotExist(a args) {
256 mustWritePasswdConfig(t, a.url)
258 err := mainFetch(configPath)
259 mustBeErrorWithSubstring(t, err,
260 "file.path \""+passwdPath+"\" must exist")
262 mustNotExist(t, statePath, passwdPath, plainPath, groupPath)
265 func fetchPasswd404(a args) {
267 mustWritePasswdConfig(t, a.url)
268 mustCreate(t, passwdPath)
270 *a.handler = func(w http.ResponseWriter, r *http.Request) {
272 w.WriteHeader(http.StatusNotFound)
275 err := mainFetch(configPath)
276 mustBeErrorWithSubstring(t, err,
279 mustNotExist(t, statePath, plainPath, groupPath)
280 mustBeOld(a.t, passwdPath)
283 func fetchPasswdEmpty(a args) {
285 mustWritePasswdConfig(t, a.url)
286 mustCreate(t, passwdPath)
288 *a.handler = func(w http.ResponseWriter, r *http.Request) {
292 err := mainFetch(configPath)
293 mustBeErrorWithSubstring(t, err,
294 "refusing to use empty passwd file")
296 mustNotExist(t, statePath, plainPath, groupPath)
297 mustBeOld(t, passwdPath)
300 func fetchPasswdInvalid(a args) {
302 mustWritePasswdConfig(t, a.url)
303 mustCreate(t, passwdPath)
305 *a.handler = func(w http.ResponseWriter, r *http.Request) {
306 if r.URL.Path != "/passwd" {
310 fmt.Fprintln(w, "root:x:invalid:0:root:/root:/bin/bash")
313 err := mainFetch(configPath)
314 mustBeErrorWithSubstring(t, err,
315 "invalid uid in line")
317 mustNotExist(t, statePath, plainPath, groupPath)
318 mustBeOld(t, passwdPath)
321 func fetchPasswdLimits(a args) {
323 mustWritePasswdConfig(t, a.url)
324 mustCreate(t, passwdPath)
326 *a.handler = func(w http.ResponseWriter, r *http.Request) {
327 if r.URL.Path != "/passwd" {
331 fmt.Fprint(w, "root:x:0:0:root:/root:/bin/bash")
332 for i := 0; i < 65536; i++ {
338 err := mainFetch(configPath)
339 mustBeErrorWithSubstring(t, err,
340 "passwd too large to serialize")
342 mustNotExist(t, statePath, plainPath, groupPath)
343 mustBeOld(t, passwdPath)
346 func fetchPasswd(a args) {
348 mustWritePasswdConfig(t, a.url)
349 mustCreate(t, passwdPath)
350 mustHaveHash(t, passwdPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
352 t.Log("First fetch, write files")
354 *a.handler = func(w http.ResponseWriter, r *http.Request) {
355 if r.URL.Path != "/passwd" {
359 // No "Last-Modified" header
360 fmt.Fprintln(w, "root:x:0:0:root:/root:/bin/bash")
361 fmt.Fprintln(w, "daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin")
364 err := mainFetch(configPath)
369 mustNotExist(t, plainPath, groupPath)
370 mustBeNew(t, passwdPath, statePath)
371 // The actual content of passwdPath is verified by the NSS tests
372 mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
374 t.Log("Fetch again, no support for Last-Modified")
376 mustMakeOld(t, passwdPath, statePath)
378 err = mainFetch(configPath)
383 mustNotExist(t, plainPath, groupPath)
384 mustBeNew(t, passwdPath, statePath)
385 mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
387 t.Log("Fetch again, support for Last-Modified, but not retrieved yet")
389 mustMakeOld(t, passwdPath, statePath)
391 lastChange := time.Now()
392 *a.handler = func(w http.ResponseWriter, r *http.Request) {
393 if r.URL.Path != "/passwd" {
397 modified := r.Header.Get("If-Modified-Since")
399 x, err := http.ParseTime(modified)
401 t.Fatalf("invalid If-Modified-Since %v",
404 if !x.Before(lastChange) {
405 w.WriteHeader(http.StatusNotModified)
410 w.Header().Add("Last-Modified",
411 lastChange.Format(http.TimeFormat))
412 fmt.Fprintln(w, "root:x:0:0:root:/root:/bin/bash")
413 fmt.Fprintln(w, "daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin")
416 err = mainFetch(configPath)
421 mustNotExist(t, plainPath, groupPath)
422 mustBeNew(t, passwdPath, statePath)
423 mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
425 t.Log("Fetch again, support for Last-Modified")
427 mustMakeOld(t, passwdPath, statePath)
429 err = mainFetch(configPath)
434 mustNotExist(t, plainPath, groupPath)
435 mustBeOld(t, passwdPath)
436 mustBeNew(t, statePath)
437 mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
439 t.Log("Corrupt local passwd cache, fetched again")
441 os.Chmod(passwdPath, 0644) // make writable again
442 mustCreate(t, passwdPath)
443 mustMakeOld(t, passwdPath, statePath)
445 err = mainFetch(configPath)
450 mustNotExist(t, plainPath, groupPath)
451 mustBeNew(t, passwdPath, statePath)
452 mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
455 func fetchPlainEmpty(a args) {
457 mustWriteConfig(t, fmt.Sprintf(`
464 `, statePath, a.url, plainPath))
465 mustCreate(t, plainPath)
467 *a.handler = func(w http.ResponseWriter, r *http.Request) {
471 err := mainFetch(configPath)
472 mustBeErrorWithSubstring(t, err,
473 "refusing to use empty response")
475 mustNotExist(t, statePath, passwdPath, groupPath)
476 mustBeOld(t, plainPath)
479 func fetchPlain(a args) {
481 mustWriteConfig(t, fmt.Sprintf(`
488 `, statePath, a.url, plainPath))
489 mustCreate(t, plainPath)
490 mustHaveHash(t, plainPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
492 *a.handler = func(w http.ResponseWriter, r *http.Request) {
493 if r.URL.Path != "/plain" {
497 fmt.Fprintln(w, "some file")
500 err := mainFetch(configPath)
505 mustNotExist(t, passwdPath, groupPath)
506 mustBeNew(t, plainPath, statePath)
507 mustHaveHash(t, plainPath, "0e08b5e8c10abc3e455b75286ba4a1fbd56e18a5")
509 // Remaining functionality already tested in fetchPasswd()
512 func fetchGroupEmpty(a args) {
514 mustWriteGroupConfig(t, a.url)
515 mustCreate(t, groupPath)
517 *a.handler = func(w http.ResponseWriter, r *http.Request) {
521 err := mainFetch(configPath)
522 mustBeErrorWithSubstring(t, err,
523 "refusing to use empty group file")
525 mustNotExist(t, statePath, passwdPath, plainPath)
526 mustBeOld(t, groupPath)
529 func fetchGroupInvalid(a args) {
531 mustWriteGroupConfig(t, a.url)
532 mustCreate(t, groupPath)
534 *a.handler = func(w http.ResponseWriter, r *http.Request) {
535 if r.URL.Path != "/group" {
539 fmt.Fprintln(w, "root:x::")
542 err := mainFetch(configPath)
543 mustBeErrorWithSubstring(t, err,
544 "invalid gid in line")
546 mustNotExist(t, statePath, passwdPath, plainPath)
547 mustBeOld(t, groupPath)
550 func fetchGroupLimits(a args) {
552 mustWriteGroupConfig(t, a.url)
553 mustCreate(t, groupPath)
555 *a.handler = func(w http.ResponseWriter, r *http.Request) {
556 if r.URL.Path != "/group" {
560 fmt.Fprint(w, "root:x:0:")
561 for i := 0; i < 65536; i++ {
567 err := mainFetch(configPath)
568 mustBeErrorWithSubstring(t, err,
569 "group too large to serialize")
571 mustNotExist(t, statePath, passwdPath, plainPath)
572 mustBeOld(t, groupPath)
575 func fetchGroup(a args) {
577 mustWriteGroupConfig(t, a.url)
578 mustCreate(t, groupPath)
579 mustHaveHash(t, groupPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
581 *a.handler = func(w http.ResponseWriter, r *http.Request) {
582 if r.URL.Path != "/group" {
586 fmt.Fprintln(w, "root:x:0:")
587 fmt.Fprintln(w, "daemon:x:1:andariel,duriel,mephisto,diablo,baal")
590 err := mainFetch(configPath)
595 mustNotExist(t, passwdPath, plainPath)
596 mustBeNew(t, groupPath, statePath)
597 // The actual content of groupPath is verified by the NSS tests
598 mustHaveHash(t, groupPath, "8c27a8403278ba2e392b86d98d4dff1fdefcafdd")
600 // Remaining functionality already tested in fetchPasswd()
603 func fetchNoConfig(a args) {
606 err := mainFetch(configPath)
607 mustBeErrorWithSubstring(t, err,
608 configPath+": no such file or directory")
610 mustNotExist(t, configPath, statePath, passwdPath, plainPath, groupPath)
613 func fetchStateCannotRead(a args) {
615 mustWritePasswdConfig(t, a.url)
617 mustCreate(t, statePath)
618 err := os.Chmod(statePath, 0000)
623 err = mainFetch(configPath)
624 mustBeErrorWithSubstring(t, err,
625 statePath+": permission denied")
627 mustNotExist(t, passwdPath, plainPath, groupPath)
630 func fetchStateInvalid(a args) {
632 mustWriteGroupConfig(t, a.url)
633 mustCreate(t, statePath)
635 err := mainFetch(configPath)
636 mustBeErrorWithSubstring(t, err,
637 "unexpected end of JSON input")
639 mustNotExist(t, groupPath, passwdPath, plainPath)
640 mustBeOld(t, statePath)
643 func fetchStateCannotWrite(a args) {
645 mustWriteGroupConfig(t, a.url)
646 mustCreate(t, groupPath)
647 mustHaveHash(t, groupPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
649 *a.handler = func(w http.ResponseWriter, r *http.Request) {
650 // To prevent mainFetch() from trying to update groupPath
651 // which will also fail
652 w.WriteHeader(http.StatusNotModified)
655 err := os.Chmod("testdata", 0500)
659 defer os.Chmod("testdata", 0755)
661 err = mainFetch(configPath)
662 mustBeErrorWithSubstring(t, err,
665 mustNotExist(t, statePath, passwdPath, plainPath)
666 mustBeOld(t, groupPath)
669 func fetchCannotDeploy(a args) {
671 mustWriteGroupConfig(t, a.url)
672 mustCreate(t, groupPath)
673 mustHaveHash(t, groupPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
675 *a.handler = func(w http.ResponseWriter, r *http.Request) {
676 if r.URL.Path != "/group" {
680 fmt.Fprintln(w, "root:x:0:")
681 fmt.Fprintln(w, "daemon:x:1:andariel,duriel,mephisto,diablo,baal")
684 err := os.Chmod("testdata", 0500)
688 defer os.Chmod("testdata", 0755)
690 err = mainFetch(configPath)
691 mustBeErrorWithSubstring(t, err,
694 mustNotExist(t, statePath, passwdPath, plainPath)
695 mustBeOld(t, groupPath)
698 func fetchSecondFetchFails(a args) {
700 mustWriteConfig(t, fmt.Sprintf(`
712 `, statePath, a.url, passwdPath, groupPath))
713 mustCreate(t, passwdPath)
714 mustCreate(t, groupPath)
715 mustHaveHash(t, passwdPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
716 mustHaveHash(t, groupPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
718 *a.handler = func(w http.ResponseWriter, r *http.Request) {
719 if r.URL.Path == "/passwd" {
720 fmt.Fprintln(w, "root:x:0:0:root:/root:/bin/bash")
722 if r.URL.Path == "/group" {
723 w.WriteHeader(http.StatusNotFound)
727 err := mainFetch(configPath)
728 mustBeErrorWithSubstring(t, err,
731 mustNotExist(t, statePath, plainPath)
732 // Even though passwd was successfully fetched, no files were modified
733 // because the second fetch failed
734 mustBeOld(t, passwdPath, groupPath)