]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - file_test.go
nsscash: test deployFile() sets permissions properly
[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
31         // Setup & cleanup
32         f, err := os.Create(deploy)
33         if err != nil {
34                 t.Fatal(err)
35         }
36         err = f.Close()
37         if err != nil {
38                 t.Fatal(err)
39         }
40         defer os.Remove(deploy)
41
42         tests := []struct {
43                 perm os.FileMode
44                 exp  os.FileMode
45         }{
46                 {
47                         0644,
48                         0644,
49                 },
50                 {
51                         0400,
52                         0400,
53                 },
54                 {
55                         0600,
56                         0600,
57                 },
58                 {
59                         0777,
60                         0777,
61                 },
62                 {
63                         0666,
64                         0666,
65                 },
66                 {
67                         0000,
68                         0000,
69                 },
70         }
71
72         for n, tc := range tests {
73                 err = os.Chmod(deploy, tc.perm)
74                 if err != nil {
75                         t.Fatal(err)
76                 }
77                 err = deployFile(&File{
78                         Type: FileTypePlain,
79                         Url:  "http://example.org",
80                         Path: deploy,
81                         body: []byte("Hello World!"),
82                 })
83                 if err != nil {
84                         t.Fatal(err)
85                 }
86
87                 info, err := os.Stat(deploy)
88                 if err != nil {
89                         t.Fatal(err)
90                 }
91                 perm := info.Mode().Perm()
92                 if perm != tc.exp {
93                         t.Errorf("%d: perm = %v, want %v",
94                                 n, perm, tc.exp)
95                 }
96         }
97 }