From: Simon Ruderich Date: Mon, 17 Jun 2019 19:07:19 +0000 (+0200) Subject: nsscash: test deployFile() sets permissions properly X-Git-Tag: 0.1~44 X-Git-Url: https://ruderich.org/simon/gitweb/?p=nsscash%2Fnsscash.git;a=commitdiff_plain;h=348c8b83453aabeda5fb8acccb23244621a057d6 nsscash: test deployFile() sets permissions properly --- diff --git a/file_test.go b/file_test.go new file mode 100644 index 0000000..59cb169 --- /dev/null +++ b/file_test.go @@ -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 . + +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 index 0000000..e69de29