From 348c8b83453aabeda5fb8acccb23244621a057d6 Mon Sep 17 00:00:00 2001
From: Simon Ruderich <simon@ruderich.org>
Date: Mon, 17 Jun 2019 21:07:19 +0200
Subject: [PATCH] nsscash: test deployFile() sets permissions properly

---
 file_test.go        | 97 +++++++++++++++++++++++++++++++++++++++++++++
 testdata/.gitignore |  0
 2 files changed, 97 insertions(+)
 create mode 100644 file_test.go
 create mode 100644 testdata/.gitignore

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 <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
index 0000000..e69de29
-- 
2.49.0