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,
210 for _, f := range tests {
215 func runMainTest(t *testing.T, f func(args)) {
224 // NOTE: This is not guaranteed to work according to reflect's
225 // documentation but seems to work reliable for normal
227 fn := runtime.FuncForPC(reflect.ValueOf(f).Pointer())
229 name = name[strings.LastIndex(name, ".")+1:]
231 t.Run(name, func(t *testing.T) {
232 // Preparation & cleanup
233 for _, p := range cleanup {
235 if err != nil && !os.IsNotExist(err) {
238 // Remove the file at the end of this test
239 // run, if it was created
243 var handler func(http.ResponseWriter, *http.Request)
244 ts := httptest.NewServer(http.HandlerFunc(
245 func(w http.ResponseWriter, r *http.Request) {
258 func fetchPasswdCacheFileDoesNotExist(a args) {
260 mustWritePasswdConfig(t, a.url)
262 err := mainFetch(configPath)
263 mustBeErrorWithSubstring(t, err,
264 "file.path \""+passwdPath+"\" must exist")
266 mustNotExist(t, statePath, passwdPath, plainPath, groupPath)
269 func fetchPasswd404(a args) {
271 mustWritePasswdConfig(t, a.url)
272 mustCreate(t, passwdPath)
274 *a.handler = func(w http.ResponseWriter, r *http.Request) {
276 w.WriteHeader(http.StatusNotFound)
279 err := mainFetch(configPath)
280 mustBeErrorWithSubstring(t, err,
283 mustNotExist(t, statePath, plainPath, groupPath)
284 mustBeOld(a.t, passwdPath)
287 func fetchPasswdEmpty(a args) {
289 mustWritePasswdConfig(t, a.url)
290 mustCreate(t, passwdPath)
292 *a.handler = func(w http.ResponseWriter, r *http.Request) {
296 err := mainFetch(configPath)
297 mustBeErrorWithSubstring(t, err,
298 "refusing to use empty passwd file")
300 mustNotExist(t, statePath, plainPath, groupPath)
301 mustBeOld(t, passwdPath)
304 func fetchPasswdInvalid(a args) {
306 mustWritePasswdConfig(t, a.url)
307 mustCreate(t, passwdPath)
309 *a.handler = func(w http.ResponseWriter, r *http.Request) {
310 if r.URL.Path != "/passwd" {
314 fmt.Fprintln(w, "root:x:invalid:0:root:/root:/bin/bash")
317 err := mainFetch(configPath)
318 mustBeErrorWithSubstring(t, err,
319 "invalid uid in line")
321 mustNotExist(t, statePath, plainPath, groupPath)
322 mustBeOld(t, passwdPath)
325 func fetchPasswdLimits(a args) {
327 mustWritePasswdConfig(t, a.url)
328 mustCreate(t, passwdPath)
330 *a.handler = func(w http.ResponseWriter, r *http.Request) {
331 if r.URL.Path != "/passwd" {
335 fmt.Fprint(w, "root:x:0:0:root:/root:/bin/bash")
336 for i := 0; i < 65536; i++ {
342 err := mainFetch(configPath)
343 mustBeErrorWithSubstring(t, err,
344 "passwd too large to serialize")
346 mustNotExist(t, statePath, plainPath, groupPath)
347 mustBeOld(t, passwdPath)
350 func fetchPasswd(a args) {
352 mustWritePasswdConfig(t, a.url)
353 mustCreate(t, passwdPath)
354 mustHaveHash(t, passwdPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
356 t.Log("First fetch, write files")
358 *a.handler = func(w http.ResponseWriter, r *http.Request) {
359 if r.URL.Path != "/passwd" {
363 // No "Last-Modified" header
364 fmt.Fprintln(w, "root:x:0:0:root:/root:/bin/bash")
365 fmt.Fprintln(w, "daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin")
368 err := mainFetch(configPath)
373 mustNotExist(t, plainPath, groupPath)
374 mustBeNew(t, passwdPath, statePath)
375 // The actual content of passwdPath is verified by the NSS tests
376 mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
378 t.Log("Fetch again, no support for Last-Modified")
380 mustMakeOld(t, passwdPath, statePath)
382 err = mainFetch(configPath)
387 mustNotExist(t, plainPath, groupPath)
388 mustBeNew(t, passwdPath, statePath)
389 mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
391 t.Log("Fetch again, support for Last-Modified, but not retrieved yet")
393 mustMakeOld(t, passwdPath, statePath)
395 lastChange := time.Now()
396 *a.handler = func(w http.ResponseWriter, r *http.Request) {
397 if r.URL.Path != "/passwd" {
401 modified := r.Header.Get("If-Modified-Since")
403 x, err := http.ParseTime(modified)
405 t.Fatalf("invalid If-Modified-Since %v",
408 if !x.Before(lastChange) {
409 w.WriteHeader(http.StatusNotModified)
414 w.Header().Add("Last-Modified",
415 lastChange.Format(http.TimeFormat))
416 fmt.Fprintln(w, "root:x:0:0:root:/root:/bin/bash")
417 fmt.Fprintln(w, "daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin")
420 err = mainFetch(configPath)
425 mustNotExist(t, plainPath, groupPath)
426 mustBeNew(t, passwdPath, statePath)
427 mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
429 t.Log("Fetch again, support for Last-Modified")
431 mustMakeOld(t, passwdPath, statePath)
433 err = mainFetch(configPath)
438 mustNotExist(t, plainPath, groupPath)
439 mustBeOld(t, passwdPath)
440 mustBeNew(t, statePath)
441 mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
443 t.Log("Corrupt local passwd cache, fetched again")
445 os.Chmod(passwdPath, 0644) // make writable again
446 mustCreate(t, passwdPath)
447 mustMakeOld(t, passwdPath, statePath)
449 err = mainFetch(configPath)
454 mustNotExist(t, plainPath, groupPath)
455 mustBeNew(t, passwdPath, statePath)
456 mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
459 func fetchPlainEmpty(a args) {
461 mustWriteConfig(t, fmt.Sprintf(`
468 `, statePath, a.url, plainPath))
469 mustCreate(t, plainPath)
471 *a.handler = func(w http.ResponseWriter, r *http.Request) {
475 err := mainFetch(configPath)
476 mustBeErrorWithSubstring(t, err,
477 "refusing to use empty response")
479 mustNotExist(t, statePath, passwdPath, groupPath)
480 mustBeOld(t, plainPath)
483 func fetchPlain(a args) {
485 mustWriteConfig(t, fmt.Sprintf(`
492 `, statePath, a.url, plainPath))
493 mustCreate(t, plainPath)
494 mustHaveHash(t, plainPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
496 *a.handler = func(w http.ResponseWriter, r *http.Request) {
497 if r.URL.Path != "/plain" {
501 fmt.Fprintln(w, "some file")
504 err := mainFetch(configPath)
509 mustNotExist(t, passwdPath, groupPath)
510 mustBeNew(t, plainPath, statePath)
511 mustHaveHash(t, plainPath, "0e08b5e8c10abc3e455b75286ba4a1fbd56e18a5")
513 // Remaining functionality already tested in fetchPasswd()
516 func fetchGroupEmpty(a args) {
518 mustWriteGroupConfig(t, a.url)
519 mustCreate(t, groupPath)
521 *a.handler = func(w http.ResponseWriter, r *http.Request) {
525 err := mainFetch(configPath)
526 mustBeErrorWithSubstring(t, err,
527 "refusing to use empty group file")
529 mustNotExist(t, statePath, passwdPath, plainPath)
530 mustBeOld(t, groupPath)
533 func fetchGroupInvalid(a args) {
535 mustWriteGroupConfig(t, a.url)
536 mustCreate(t, groupPath)
538 *a.handler = func(w http.ResponseWriter, r *http.Request) {
539 if r.URL.Path != "/group" {
543 fmt.Fprintln(w, "root:x::")
546 err := mainFetch(configPath)
547 mustBeErrorWithSubstring(t, err,
548 "invalid gid in line")
550 mustNotExist(t, statePath, passwdPath, plainPath)
551 mustBeOld(t, groupPath)
554 func fetchGroupLimits(a args) {
556 mustWriteGroupConfig(t, a.url)
557 mustCreate(t, groupPath)
559 *a.handler = func(w http.ResponseWriter, r *http.Request) {
560 if r.URL.Path != "/group" {
564 fmt.Fprint(w, "root:x:0:")
565 for i := 0; i < 65536; i++ {
571 err := mainFetch(configPath)
572 mustBeErrorWithSubstring(t, err,
573 "group too large to serialize")
575 mustNotExist(t, statePath, passwdPath, plainPath)
576 mustBeOld(t, groupPath)
579 func fetchGroup(a args) {
581 mustWriteGroupConfig(t, a.url)
582 mustCreate(t, groupPath)
583 mustHaveHash(t, groupPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
585 *a.handler = func(w http.ResponseWriter, r *http.Request) {
586 if r.URL.Path != "/group" {
590 fmt.Fprintln(w, "root:x:0:")
591 fmt.Fprintln(w, "daemon:x:1:andariel,duriel,mephisto,diablo,baal")
594 err := mainFetch(configPath)
599 mustNotExist(t, passwdPath, plainPath)
600 mustBeNew(t, groupPath, statePath)
601 // The actual content of groupPath is verified by the NSS tests
602 mustHaveHash(t, groupPath, "8c27a8403278ba2e392b86d98d4dff1fdefcafdd")
604 // Remaining functionality already tested in fetchPasswd()
607 func fetchNoConfig(a args) {
610 err := mainFetch(configPath)
611 mustBeErrorWithSubstring(t, err,
612 configPath+": no such file or directory")
614 mustNotExist(t, configPath, statePath, passwdPath, plainPath, groupPath)
617 func fetchStateCannotRead(a args) {
619 mustWritePasswdConfig(t, a.url)
621 mustCreate(t, statePath)
622 err := os.Chmod(statePath, 0000)
627 err = mainFetch(configPath)
628 mustBeErrorWithSubstring(t, err,
629 statePath+": permission denied")
631 mustNotExist(t, passwdPath, plainPath, groupPath)
634 func fetchStateInvalid(a args) {
636 mustWriteGroupConfig(t, a.url)
637 mustCreate(t, statePath)
639 err := mainFetch(configPath)
640 mustBeErrorWithSubstring(t, err,
641 "unexpected end of JSON input")
643 mustNotExist(t, groupPath, passwdPath, plainPath)
644 mustBeOld(t, statePath)
647 func fetchStateCannotWrite(a args) {
649 mustWriteGroupConfig(t, a.url)
650 mustCreate(t, groupPath)
651 mustHaveHash(t, groupPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
653 *a.handler = func(w http.ResponseWriter, r *http.Request) {
654 // To prevent mainFetch() from trying to update groupPath
655 // which will also fail
656 w.WriteHeader(http.StatusNotModified)
659 err := os.Chmod("testdata", 0500)
663 defer os.Chmod("testdata", 0755)
665 err = mainFetch(configPath)
666 mustBeErrorWithSubstring(t, err,
669 mustNotExist(t, statePath, passwdPath, plainPath)
670 mustBeOld(t, groupPath)
673 func fetchCannotDeploy(a args) {
675 mustWriteGroupConfig(t, a.url)
676 mustCreate(t, groupPath)
677 mustHaveHash(t, groupPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
679 *a.handler = func(w http.ResponseWriter, r *http.Request) {
680 if r.URL.Path != "/group" {
684 fmt.Fprintln(w, "root:x:0:")
685 fmt.Fprintln(w, "daemon:x:1:andariel,duriel,mephisto,diablo,baal")
688 err := os.Chmod("testdata", 0500)
692 defer os.Chmod("testdata", 0755)
694 err = mainFetch(configPath)
695 mustBeErrorWithSubstring(t, err,
698 mustNotExist(t, statePath, passwdPath, plainPath)
699 mustBeOld(t, groupPath)
702 func fetchSecondFetchFails(a args) {
704 mustWriteConfig(t, fmt.Sprintf(`
716 `, statePath, a.url, passwdPath, groupPath))
717 mustCreate(t, passwdPath)
718 mustCreate(t, groupPath)
719 mustHaveHash(t, passwdPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
720 mustHaveHash(t, groupPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
722 *a.handler = func(w http.ResponseWriter, r *http.Request) {
723 if r.URL.Path == "/passwd" {
724 fmt.Fprintln(w, "root:x:0:0:root:/root:/bin/bash")
726 if r.URL.Path == "/group" {
727 w.WriteHeader(http.StatusNotFound)
731 err := mainFetch(configPath)
732 mustBeErrorWithSubstring(t, err,
735 mustNotExist(t, statePath, plainPath)
736 // Even though passwd was successfully fetched, no files were modified
737 // because the second fetch failed
738 mustBeOld(t, passwdPath, groupPath)