]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - main_test.go
38499da197bb6fd0be3276e48d11527215a30a8c
[nsscash/nsscash.git] / main_test.go
1 // Copyright (C) 2019  Simon Ruderich
2 //
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.
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 Affero General Public License for more details.
12 //
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/>.
15
16 package main
17
18 import (
19         "crypto/sha1"
20         "crypto/tls"
21         "encoding/hex"
22         "fmt"
23         "io/ioutil"
24         "log"
25         "net/http"
26         "net/http/httptest"
27         "os"
28         "reflect"
29         "runtime"
30         "strings"
31         "testing"
32         "time"
33 )
34
35 const (
36         configPath = "testdata/config.toml"
37         statePath  = "testdata/state.json"
38         passwdPath = "testdata/passwd.nsscash"
39         plainPath  = "testdata/plain"
40         groupPath  = "testdata/group.nsscash"
41         tlsCAPath   = "testdata/ca.crt"
42         tlsCertPath = "testdata/server.crt"
43         tlsKeyPath  = "testdata/server.key"
44         tlsCA2Path  = "testdata/ca2.crt"
45 )
46
47 type args struct {
48         t       *testing.T
49         url     string
50         handler *func(http.ResponseWriter, *http.Request)
51 }
52
53 // mustNotExist verifies that all given paths don't exist in the file system.
54 func mustNotExist(t *testing.T, paths ...string) {
55         for _, p := range paths {
56                 f, err := os.Open(p)
57                 if err != nil {
58                         if !os.IsNotExist(err) {
59                                 t.Errorf("path %q: unexpected error: %v",
60                                         p, err)
61                         }
62                 } else {
63                         t.Errorf("path %q exists", p)
64                         f.Close()
65                 }
66         }
67 }
68
69 // mustHaveHash checks if the given path content has the given SHA-1 string
70 // (in hex).
71 func mustHaveHash(t *testing.T, path string, hash string) {
72         x, err := ioutil.ReadFile(path)
73         if err != nil {
74                 t.Fatal(err)
75         }
76
77         h := sha1.New()
78         h.Write(x)
79         y := hex.EncodeToString(h.Sum(nil))
80
81         if y != hash {
82                 t.Errorf("%q has unexpected hash %q", path, y)
83         }
84 }
85
86 // mustBeErrorWithSubstring checks if the given error, represented as string,
87 // contains the given substring. This is somewhat ugly but the simplest way to
88 // check for proper errors.
89 func mustBeErrorWithSubstring(t *testing.T, err error, substring string) {
90         if err == nil {
91                 t.Errorf("err is nil")
92         } else if !strings.Contains(err.Error(), substring) {
93                 t.Errorf("err %q does not contain string %q", err, substring)
94         }
95 }
96
97 func mustWriteConfig(t *testing.T, config string) {
98         err := ioutil.WriteFile(configPath, []byte(config), 0644)
99         if err != nil {
100                 t.Fatal(err)
101         }
102 }
103
104 func mustWritePasswdConfig(t *testing.T, url string) {
105         mustWriteConfig(t, fmt.Sprintf(`
106 statepath = "%[1]s"
107
108 [[file]]
109 type = "passwd"
110 url = "%[2]s/passwd"
111 path = "%[3]s"
112 ca = "%[4]s"
113 `, statePath, url, passwdPath, tlsCAPath))
114 }
115
116 func mustWriteGroupConfig(t *testing.T, url string) {
117         mustWriteConfig(t, fmt.Sprintf(`
118 statepath = "%[1]s"
119
120 [[file]]
121 type = "group"
122 url = "%[2]s/group"
123 path = "%[3]s"
124 ca = "%[4]s"
125 `, statePath, url, groupPath, tlsCAPath))
126 }
127
128 // mustCreate creates a file, truncating it if it exists. It then changes the
129 // modification to be in the past.
130 func mustCreate(t *testing.T, path string) {
131         f, err := os.Create(path)
132         if err != nil {
133                 t.Fatal(err)
134         }
135         err = f.Close()
136         if err != nil {
137                 t.Fatal(err)
138         }
139
140         // Change modification time to the past to detect updates to the file
141         mustMakeOld(t, path)
142 }
143
144 // mustMakeOld change the modification time of all paths to be in the past.
145 func mustMakeOld(t *testing.T, paths ...string) {
146         old := time.Now().Add(-2 * time.Hour)
147         for _, p := range paths {
148                 err := os.Chtimes(p, old, old)
149                 if err != nil {
150                         t.Fatal(err)
151                 }
152         }
153 }
154
155 // mustMakeOld verifies that all paths have a modification time in the past,
156 // as set by mustMakeOld().
157 func mustBeOld(t *testing.T, paths ...string) {
158         for _, p := range paths {
159                 i, err := os.Stat(p)
160                 if err != nil {
161                         t.Fatal(err)
162                 }
163
164                 mtime := i.ModTime()
165                 now := time.Now()
166                 if now.Sub(mtime) < time.Hour {
167                         t.Errorf("%q was recently modified", p)
168                 }
169         }
170 }
171
172 // mustBeNew verifies that all paths have a modification time in the present.
173 func mustBeNew(t *testing.T, paths ...string) {
174         for _, p := range paths {
175                 i, err := os.Stat(p)
176                 if err != nil {
177                         t.Fatal(err)
178                 }
179
180                 mtime := i.ModTime()
181                 now := time.Now()
182                 if now.Sub(mtime) > time.Hour {
183                         t.Errorf("%q was not recently modified", p)
184                 }
185         }
186 }
187
188 func TestMainFetch(t *testing.T) {
189         // Suppress log messages
190         log.SetOutput(ioutil.Discard)
191         defer log.SetOutput(os.Stderr)
192
193         tests := []func(args){
194                 // Perform most tests with passwd for simplicity
195                 fetchPasswdCacheFileDoesNotExist,
196                 fetchPasswd404,
197                 fetchPasswdEmpty,
198                 fetchPasswdInvalid,
199                 fetchPasswdLimits,
200                 fetchPasswd,
201                 // Tests for plain and group
202                 fetchPlainEmpty,
203                 fetchPlain,
204                 fetchGroupEmpty,
205                 fetchGroupInvalid,
206                 fetchGroupLimits,
207                 fetchGroup,
208                 // Special tests
209                 fetchNoConfig,
210                 fetchStateCannotRead,
211                 fetchStateInvalid,
212                 fetchStateCannotWrite,
213                 fetchCannotDeploy,
214                 fetchSecondFetchFails,
215         }
216
217         // HTTP tests
218
219         for _, f := range tests {
220                 runMainTest(t, f, nil)
221         }
222
223         // HTTPS tests
224
225         tests = append(tests, fetchInvalidCA)
226
227         cert, err := tls.LoadX509KeyPair(tlsCertPath, tlsKeyPath)
228         if err != nil {
229                 t.Fatal(err)
230         }
231         tls := &tls.Config{
232                 Certificates: []tls.Certificate{cert},
233         }
234
235         for _, f := range tests {
236                 runMainTest(t, f, tls)
237         }
238 }
239
240 func runMainTest(t *testing.T, f func(args), tls *tls.Config) {
241         cleanup := []string{
242                 configPath,
243                 statePath,
244                 passwdPath,
245                 plainPath,
246                 groupPath,
247         }
248
249         // NOTE: This is not guaranteed to work according to reflect's
250         // documentation but seems to work reliable for normal functions.
251         fn := runtime.FuncForPC(reflect.ValueOf(f).Pointer())
252         name := fn.Name()
253         name = name[strings.LastIndex(name, ".")+1:]
254         if tls != nil {
255                 name = "tls" + name
256         }
257
258         t.Run(name, func(t *testing.T) {
259                 // Preparation & cleanup
260                 for _, p := range cleanup {
261                         err := os.Remove(p)
262                         if err != nil && !os.IsNotExist(err) {
263                                 t.Fatal(err)
264                         }
265                         // Remove the file at the end of this test run, if it
266                         // was created
267                         defer os.Remove(p)
268                 }
269
270                 var handler func(http.ResponseWriter, *http.Request)
271                 ts := httptest.NewUnstartedServer(http.HandlerFunc(
272                         func(w http.ResponseWriter, r *http.Request) {
273                                 handler(w, r)
274                         }))
275                 if tls == nil {
276                         ts.Start()
277                 } else {
278                         ts.TLS = tls
279                         ts.StartTLS()
280                 }
281                 defer ts.Close()
282
283                 f(args{
284                         t:       t,
285                         url:     ts.URL,
286                         handler: &handler,
287                 })
288         })
289 }
290
291 func fetchPasswdCacheFileDoesNotExist(a args) {
292         t := a.t
293         mustWritePasswdConfig(t, a.url)
294
295         err := mainFetch(configPath)
296         mustBeErrorWithSubstring(t, err,
297                 "file.path \""+passwdPath+"\" must exist")
298
299         mustNotExist(t, statePath, passwdPath, plainPath, groupPath)
300 }
301
302 func fetchPasswd404(a args) {
303         t := a.t
304         mustWritePasswdConfig(t, a.url)
305         mustCreate(t, passwdPath)
306
307         *a.handler = func(w http.ResponseWriter, r *http.Request) {
308                 // 404
309                 w.WriteHeader(http.StatusNotFound)
310         }
311
312         err := mainFetch(configPath)
313         mustBeErrorWithSubstring(t, err,
314                 "status code 404")
315
316         mustNotExist(t, statePath, plainPath, groupPath)
317         mustBeOld(a.t, passwdPath)
318 }
319
320 func fetchPasswdEmpty(a args) {
321         t := a.t
322         mustWritePasswdConfig(t, a.url)
323         mustCreate(t, passwdPath)
324
325         *a.handler = func(w http.ResponseWriter, r *http.Request) {
326                 // Empty response
327         }
328
329         err := mainFetch(configPath)
330         mustBeErrorWithSubstring(t, err,
331                 "refusing to use empty passwd file")
332
333         mustNotExist(t, statePath, plainPath, groupPath)
334         mustBeOld(t, passwdPath)
335 }
336
337 func fetchPasswdInvalid(a args) {
338         t := a.t
339         mustWritePasswdConfig(t, a.url)
340         mustCreate(t, passwdPath)
341
342         *a.handler = func(w http.ResponseWriter, r *http.Request) {
343                 if r.URL.Path != "/passwd" {
344                         return
345                 }
346
347                 fmt.Fprintln(w, "root:x:invalid:0:root:/root:/bin/bash")
348         }
349
350         err := mainFetch(configPath)
351         mustBeErrorWithSubstring(t, err,
352                 "invalid uid in line")
353
354         mustNotExist(t, statePath, plainPath, groupPath)
355         mustBeOld(t, passwdPath)
356 }
357
358 func fetchPasswdLimits(a args) {
359         t := a.t
360         mustWritePasswdConfig(t, a.url)
361         mustCreate(t, passwdPath)
362
363         *a.handler = func(w http.ResponseWriter, r *http.Request) {
364                 if r.URL.Path != "/passwd" {
365                         return
366                 }
367
368                 fmt.Fprint(w, "root:x:0:0:root:/root:/bin/bash")
369                 for i := 0; i < 65536; i++ {
370                         fmt.Fprint(w, "x")
371                 }
372                 fmt.Fprint(w, "\n")
373         }
374
375         err := mainFetch(configPath)
376         mustBeErrorWithSubstring(t, err,
377                 "passwd too large to serialize")
378
379         mustNotExist(t, statePath, plainPath, groupPath)
380         mustBeOld(t, passwdPath)
381 }
382
383 func fetchPasswd(a args) {
384         t := a.t
385         mustWritePasswdConfig(t, a.url)
386         mustCreate(t, passwdPath)
387         mustHaveHash(t, passwdPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
388
389         t.Log("First fetch, write files")
390
391         *a.handler = func(w http.ResponseWriter, r *http.Request) {
392                 if r.URL.Path != "/passwd" {
393                         return
394                 }
395
396                 // No "Last-Modified" header
397                 fmt.Fprintln(w, "root:x:0:0:root:/root:/bin/bash")
398                 fmt.Fprintln(w, "daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin")
399         }
400
401         err := mainFetch(configPath)
402         if err != nil {
403                 t.Error(err)
404         }
405
406         mustNotExist(t, plainPath, groupPath)
407         mustBeNew(t, passwdPath, statePath)
408         // The actual content of passwdPath is verified by the NSS tests
409         mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
410
411         t.Log("Fetch again, no support for Last-Modified")
412
413         mustMakeOld(t, passwdPath, statePath)
414
415         err = mainFetch(configPath)
416         if err != nil {
417                 t.Error(err)
418         }
419
420         mustNotExist(t, plainPath, groupPath)
421         mustBeNew(t, passwdPath, statePath)
422         mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
423
424         t.Log("Fetch again, support for Last-Modified, but not retrieved yet")
425
426         mustMakeOld(t, passwdPath, statePath)
427
428         lastChange := time.Now()
429         *a.handler = func(w http.ResponseWriter, r *http.Request) {
430                 if r.URL.Path != "/passwd" {
431                         return
432                 }
433
434                 modified := r.Header.Get("If-Modified-Since")
435                 if modified != "" {
436                         x, err := http.ParseTime(modified)
437                         if err != nil {
438                                 t.Fatalf("invalid If-Modified-Since %v",
439                                         modified)
440                         }
441                         if !x.Before(lastChange) {
442                                 w.WriteHeader(http.StatusNotModified)
443                                 return
444                         }
445                 }
446
447                 w.Header().Add("Last-Modified",
448                         lastChange.Format(http.TimeFormat))
449                 fmt.Fprintln(w, "root:x:0:0:root:/root:/bin/bash")
450                 fmt.Fprintln(w, "daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin")
451         }
452
453         err = mainFetch(configPath)
454         if err != nil {
455                 t.Error(err)
456         }
457
458         mustNotExist(t, plainPath, groupPath)
459         mustBeNew(t, passwdPath, statePath)
460         mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
461
462         t.Log("Fetch again, support for Last-Modified")
463
464         mustMakeOld(t, passwdPath, statePath)
465
466         err = mainFetch(configPath)
467         if err != nil {
468                 t.Error(err)
469         }
470
471         mustNotExist(t, plainPath, groupPath)
472         mustBeOld(t, passwdPath)
473         mustBeNew(t, statePath)
474         mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
475
476         t.Log("Corrupt local passwd cache, fetched again")
477
478         os.Chmod(passwdPath, 0644) // make writable again
479         mustCreate(t, passwdPath)
480         mustMakeOld(t, passwdPath, statePath)
481
482         err = mainFetch(configPath)
483         if err != nil {
484                 t.Error(err)
485         }
486
487         mustNotExist(t, plainPath, groupPath)
488         mustBeNew(t, passwdPath, statePath)
489         mustHaveHash(t, passwdPath, "bbb7db67469b111200400e2470346d5515d64c23")
490 }
491
492 func fetchPlainEmpty(a args) {
493         t := a.t
494         mustWriteConfig(t, fmt.Sprintf(`
495 statepath = "%[1]s"
496
497 [[file]]
498 type = "plain"
499 url = "%[2]s/plain"
500 path = "%[3]s"
501 ca = "%[4]s"
502 `, statePath, a.url, plainPath, tlsCAPath))
503         mustCreate(t, plainPath)
504
505         *a.handler = func(w http.ResponseWriter, r *http.Request) {
506                 // Empty response
507         }
508
509         err := mainFetch(configPath)
510         mustBeErrorWithSubstring(t, err,
511                 "refusing to use empty response")
512
513         mustNotExist(t, statePath, passwdPath, groupPath)
514         mustBeOld(t, plainPath)
515 }
516
517 func fetchPlain(a args) {
518         t := a.t
519         mustWriteConfig(t, fmt.Sprintf(`
520 statepath = "%[1]s"
521
522 [[file]]
523 type = "plain"
524 url = "%[2]s/plain"
525 path = "%[3]s"
526 ca = "%[4]s"
527 `, statePath, a.url, plainPath, tlsCAPath))
528         mustCreate(t, plainPath)
529         mustHaveHash(t, plainPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
530
531         *a.handler = func(w http.ResponseWriter, r *http.Request) {
532                 if r.URL.Path != "/plain" {
533                         return
534                 }
535
536                 fmt.Fprintln(w, "some file")
537         }
538
539         err := mainFetch(configPath)
540         if err != nil {
541                 t.Error(err)
542         }
543
544         mustNotExist(t, passwdPath, groupPath)
545         mustBeNew(t, plainPath, statePath)
546         mustHaveHash(t, plainPath, "0e08b5e8c10abc3e455b75286ba4a1fbd56e18a5")
547
548         // Remaining functionality already tested in fetchPasswd()
549 }
550
551 func fetchGroupEmpty(a args) {
552         t := a.t
553         mustWriteGroupConfig(t, a.url)
554         mustCreate(t, groupPath)
555
556         *a.handler = func(w http.ResponseWriter, r *http.Request) {
557                 // Empty response
558         }
559
560         err := mainFetch(configPath)
561         mustBeErrorWithSubstring(t, err,
562                 "refusing to use empty group file")
563
564         mustNotExist(t, statePath, passwdPath, plainPath)
565         mustBeOld(t, groupPath)
566 }
567
568 func fetchGroupInvalid(a args) {
569         t := a.t
570         mustWriteGroupConfig(t, a.url)
571         mustCreate(t, groupPath)
572
573         *a.handler = func(w http.ResponseWriter, r *http.Request) {
574                 if r.URL.Path != "/group" {
575                         return
576                 }
577
578                 fmt.Fprintln(w, "root:x::")
579         }
580
581         err := mainFetch(configPath)
582         mustBeErrorWithSubstring(t, err,
583                 "invalid gid in line")
584
585         mustNotExist(t, statePath, passwdPath, plainPath)
586         mustBeOld(t, groupPath)
587 }
588
589 func fetchGroupLimits(a args) {
590         t := a.t
591         mustWriteGroupConfig(t, a.url)
592         mustCreate(t, groupPath)
593
594         *a.handler = func(w http.ResponseWriter, r *http.Request) {
595                 if r.URL.Path != "/group" {
596                         return
597                 }
598
599                 fmt.Fprint(w, "root:x:0:")
600                 for i := 0; i < 65536; i++ {
601                         fmt.Fprint(w, "x")
602                 }
603                 fmt.Fprint(w, "\n")
604         }
605
606         err := mainFetch(configPath)
607         mustBeErrorWithSubstring(t, err,
608                 "group too large to serialize")
609
610         mustNotExist(t, statePath, passwdPath, plainPath)
611         mustBeOld(t, groupPath)
612 }
613
614 func fetchGroup(a args) {
615         t := a.t
616         mustWriteGroupConfig(t, a.url)
617         mustCreate(t, groupPath)
618         mustHaveHash(t, groupPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
619
620         *a.handler = func(w http.ResponseWriter, r *http.Request) {
621                 if r.URL.Path != "/group" {
622                         return
623                 }
624
625                 fmt.Fprintln(w, "root:x:0:")
626                 fmt.Fprintln(w, "daemon:x:1:andariel,duriel,mephisto,diablo,baal")
627         }
628
629         err := mainFetch(configPath)
630         if err != nil {
631                 t.Error(err)
632         }
633
634         mustNotExist(t, passwdPath, plainPath)
635         mustBeNew(t, groupPath, statePath)
636         // The actual content of groupPath is verified by the NSS tests
637         mustHaveHash(t, groupPath, "8c27a8403278ba2e392b86d98d4dff1fdefcafdd")
638
639         // Remaining functionality already tested in fetchPasswd()
640 }
641
642 func fetchNoConfig(a args) {
643         t := a.t
644
645         err := mainFetch(configPath)
646         mustBeErrorWithSubstring(t, err,
647                 configPath+": no such file or directory")
648
649         mustNotExist(t, configPath, statePath, passwdPath, plainPath, groupPath)
650 }
651
652 func fetchStateCannotRead(a args) {
653         t := a.t
654         mustWritePasswdConfig(t, a.url)
655
656         mustCreate(t, statePath)
657         err := os.Chmod(statePath, 0000)
658         if err != nil {
659                 t.Fatal(err)
660         }
661
662         err = mainFetch(configPath)
663         mustBeErrorWithSubstring(t, err,
664                 statePath+": permission denied")
665
666         mustNotExist(t, passwdPath, plainPath, groupPath)
667 }
668
669 func fetchStateInvalid(a args) {
670         t := a.t
671         mustWriteGroupConfig(t, a.url)
672         mustCreate(t, statePath)
673
674         err := mainFetch(configPath)
675         mustBeErrorWithSubstring(t, err,
676                 "unexpected end of JSON input")
677
678         mustNotExist(t, groupPath, passwdPath, plainPath)
679         mustBeOld(t, statePath)
680 }
681
682 func fetchStateCannotWrite(a args) {
683         t := a.t
684         mustWriteGroupConfig(t, a.url)
685         mustCreate(t, groupPath)
686         mustHaveHash(t, groupPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
687
688         *a.handler = func(w http.ResponseWriter, r *http.Request) {
689                 // To prevent mainFetch() from trying to update groupPath
690                 // which will also fail
691                 w.WriteHeader(http.StatusNotModified)
692         }
693
694         err := os.Chmod("testdata", 0500)
695         if err != nil {
696                 t.Fatal(err)
697         }
698         defer os.Chmod("testdata", 0755)
699
700         err = mainFetch(configPath)
701         mustBeErrorWithSubstring(t, err,
702                 "permission denied")
703
704         mustNotExist(t, statePath, passwdPath, plainPath)
705         mustBeOld(t, groupPath)
706 }
707
708 func fetchCannotDeploy(a args) {
709         t := a.t
710         mustWriteGroupConfig(t, a.url)
711         mustCreate(t, groupPath)
712         mustHaveHash(t, groupPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
713
714         *a.handler = func(w http.ResponseWriter, r *http.Request) {
715                 if r.URL.Path != "/group" {
716                         return
717                 }
718
719                 fmt.Fprintln(w, "root:x:0:")
720                 fmt.Fprintln(w, "daemon:x:1:andariel,duriel,mephisto,diablo,baal")
721         }
722
723         err := os.Chmod("testdata", 0500)
724         if err != nil {
725                 t.Fatal(err)
726         }
727         defer os.Chmod("testdata", 0755)
728
729         err = mainFetch(configPath)
730         mustBeErrorWithSubstring(t, err,
731                 "permission denied")
732
733         mustNotExist(t, statePath, passwdPath, plainPath)
734         mustBeOld(t, groupPath)
735 }
736
737 func fetchSecondFetchFails(a args) {
738         t := a.t
739         mustWriteConfig(t, fmt.Sprintf(`
740 statepath = "%[1]s"
741
742 [[file]]
743 type = "passwd"
744 url = "%[2]s/passwd"
745 path = "%[3]s"
746 ca = "%[5]s"
747
748 [[file]]
749 type = "group"
750 url = "%[2]s/group"
751 path = "%[4]s"
752 ca = "%[5]s"
753 `, statePath, a.url, passwdPath, groupPath, tlsCAPath))
754         mustCreate(t, passwdPath)
755         mustCreate(t, groupPath)
756         mustHaveHash(t, passwdPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
757         mustHaveHash(t, groupPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
758
759         *a.handler = func(w http.ResponseWriter, r *http.Request) {
760                 if r.URL.Path == "/passwd" {
761                         fmt.Fprintln(w, "root:x:0:0:root:/root:/bin/bash")
762                 }
763                 if r.URL.Path == "/group" {
764                         w.WriteHeader(http.StatusNotFound)
765                 }
766         }
767
768         err := mainFetch(configPath)
769         mustBeErrorWithSubstring(t, err,
770                 "status code 404")
771
772         mustNotExist(t, statePath, plainPath)
773         // Even though passwd was successfully fetched, no files were modified
774         // because the second fetch failed
775         mustBeOld(t, passwdPath, groupPath)
776 }
777
778 func fetchInvalidCA(a args) {
779         t := a.t
780
781         // System CA
782
783         mustWriteConfig(t, fmt.Sprintf(`
784 statepath = "%[1]s"
785
786 [[file]]
787 type = "passwd"
788 url = "%[2]s/passwd"
789 path = "%[3]s"
790 `, statePath, a.url, passwdPath))
791         mustCreate(t, passwdPath)
792         mustHaveHash(t, passwdPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
793
794         *a.handler = func(w http.ResponseWriter, r *http.Request) {
795                 if r.URL.Path == "/passwd" {
796                         fmt.Fprintln(w, "root:x:0:0:root:/root:/bin/bash")
797                 }
798         }
799
800         err := mainFetch(configPath)
801         mustBeErrorWithSubstring(t, err,
802                 "x509: certificate signed by unknown authority")
803
804         mustNotExist(t, statePath, plainPath, groupPath)
805         mustBeOld(t, passwdPath)
806
807         // Invalid CA
808
809         mustWriteConfig(t, fmt.Sprintf(`
810 statepath = "%[1]s"
811
812 [[file]]
813 type = "passwd"
814 url = "%[2]s/passwd"
815 path = "%[3]s"
816 ca = "%[4]s"
817 `, statePath, a.url, passwdPath, tlsCA2Path))
818         mustCreate(t, passwdPath)
819         mustHaveHash(t, passwdPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
820
821         *a.handler = func(w http.ResponseWriter, r *http.Request) {
822                 if r.URL.Path == "/passwd" {
823                         fmt.Fprintln(w, "root:x:0:0:root:/root:/bin/bash")
824                 }
825         }
826
827         err = mainFetch(configPath)
828         mustBeErrorWithSubstring(t, err,
829                 "x509: certificate signed by unknown authority")
830
831         mustNotExist(t, statePath, plainPath, groupPath)
832         mustBeOld(t, passwdPath)
833 }