]> ruderich.org/simon Gitweb - nsscash/nsscash.git/commitdiff
nsscash: test deployFile() sets permissions properly
authorSimon Ruderich <simon@ruderich.org>
Mon, 17 Jun 2019 19:07:19 +0000 (21:07 +0200)
committerSimon Ruderich <simon@ruderich.org>
Mon, 17 Jun 2019 19:07:19 +0000 (21:07 +0200)
file_test.go [new file with mode: 0644]
testdata/.gitignore [new file with mode: 0644]

diff --git a/file_test.go b/file_test.go
new file mode 100644 (file)
index 0000000..59cb169
--- /dev/null
@@ -0,0 +1,97 @@
+// Copyright (C) 2019  Simon Ruderich
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+package main
+
+import (
+       "io/ioutil"
+       "log"
+       "os"
+       "testing"
+)
+
+func TestDeployFile(t *testing.T) {
+       const deploy = "testdata/deploy"
+
+       // Suppress log messages
+       log.SetOutput(ioutil.Discard)
+
+       // Setup & cleanup
+       f, err := os.Create(deploy)
+       if err != nil {
+               t.Fatal(err)
+       }
+       err = f.Close()
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer os.Remove(deploy)
+
+       tests := []struct {
+               perm os.FileMode
+               exp  os.FileMode
+       }{
+               {
+                       0644,
+                       0644,
+               },
+               {
+                       0400,
+                       0400,
+               },
+               {
+                       0600,
+                       0600,
+               },
+               {
+                       0777,
+                       0777,
+               },
+               {
+                       0666,
+                       0666,
+               },
+               {
+                       0000,
+                       0000,
+               },
+       }
+
+       for n, tc := range tests {
+               err = os.Chmod(deploy, tc.perm)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               err = deployFile(&File{
+                       Type: FileTypePlain,
+                       Url:  "http://example.org",
+                       Path: deploy,
+                       body: []byte("Hello World!"),
+               })
+               if err != nil {
+                       t.Fatal(err)
+               }
+
+               info, err := os.Stat(deploy)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               perm := info.Mode().Perm()
+               if perm != tc.exp {
+                       t.Errorf("%d: perm = %v, want %v",
+                               n, perm, tc.exp)
+               }
+       }
+}
diff --git a/testdata/.gitignore b/testdata/.gitignore
new file mode 100644 (file)
index 0000000..e69de29