]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - file_test.go
nsscash: add "ca" option for files
[nsscash/nsscash.git] / file_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         "io/ioutil"
20         "log"
21         "os"
22         "testing"
23 )
24
25 func TestDeployFile(t *testing.T) {
26         const deploy = "testdata/deploy"
27
28         // Suppress log messages
29         log.SetOutput(ioutil.Discard)
30         defer log.SetOutput(os.Stderr)
31
32         // Setup & cleanup
33         f, err := os.Create(deploy)
34         if err != nil {
35                 t.Fatal(err)
36         }
37         err = f.Close()
38         if err != nil {
39                 t.Fatal(err)
40         }
41         defer os.Remove(deploy)
42
43         tests := []struct {
44                 perm os.FileMode
45                 exp  os.FileMode
46         }{
47                 {
48                         0644,
49                         0444,
50                 },
51                 {
52                         0400,
53                         0400,
54                 },
55                 {
56                         0600,
57                         0400,
58                 },
59                 {
60                         0777,
61                         0555,
62                 },
63                 {
64                         0666,
65                         0444,
66                 },
67                 {
68                         0000,
69                         0000,
70                 },
71         }
72
73         for n, tc := range tests {
74                 err = os.Chmod(deploy, tc.perm)
75                 if err != nil {
76                         t.Fatal(err)
77                 }
78                 err = deployFile(&File{
79                         Type: FileTypePlain,
80                         Url:  "http://example.org",
81                         Path: deploy,
82                         body: []byte("Hello World!"),
83                 })
84                 if err != nil {
85                         t.Fatal(err)
86                 }
87
88                 info, err := os.Stat(deploy)
89                 if err != nil {
90                         t.Fatal(err)
91                 }
92                 perm := info.Mode().Perm()
93                 if perm != tc.exp {
94                         t.Errorf("%d: perm = %v, want %v",
95                                 n, perm, tc.exp)
96                 }
97         }
98 }