1package diagnose
2
3import (
4	"context"
5	"os"
6	"strings"
7	"testing"
8)
9
10func TestRaftFolderPerms(t *testing.T) {
11	// Make sure overpermissive permissions are caught
12	err := os.Mkdir("diagnose", 0777)
13	if err != nil {
14		t.Fatal(err)
15	}
16
17	info, _ := os.Stat("diagnose")
18
19	if !IsDir(info) {
20		t.Fatal("directory was reported to not be a directory")
21	}
22
23	// Create a boltDB formatted file and make sure isDB returns true
24	fullDBPath := "diagnose/" + DatabaseFilename
25	_, err = os.Create(fullDBPath)
26	if err != nil {
27		t.Fatal(err)
28	}
29	if !HasDB(fullDBPath) {
30		t.Fatal("well-formatted database path is not accepted by DB check function")
31	}
32
33	hasOnlyOwnerRW, errs := CheckFilePerms(info)
34	if hasOnlyOwnerRW {
35		t.Fatal("folder has more than owner rw")
36	}
37	if len(errs) != 1 && !strings.Contains(errs[0], FileTooPermissiveWarning) {
38		t.Fatalf("wrong error or number of errors or wrong error returned: %v", errs)
39	}
40
41	// Make sure underpermissiveness is caught
42	err = os.Chmod("diagnose", 0100)
43	if err != nil {
44		t.Fatal(err)
45	}
46	info, _ = os.Stat("diagnose")
47	hasOnlyOwnerRW, errs = CheckFilePerms(info)
48	if hasOnlyOwnerRW {
49		t.Fatal("folder should not have owner write")
50	}
51	if len(errs) != 1 || !strings.Contains(errs[0], FilePermissionsMissingWarning) {
52		t.Fatalf("wrong error or number of errors returned: %v", errs)
53	}
54
55	// Make sure actually setting owner rw returns properly
56	err = os.Chmod("diagnose", 0600)
57	if err != nil {
58		t.Fatal(err)
59	}
60	info, _ = os.Stat("diagnose")
61	hasOnlyOwnerRW, errs = CheckFilePerms(info)
62	if errs != nil || !hasOnlyOwnerRW {
63		t.Fatal("folder with correct perms returns error")
64	}
65
66	// Make sure we can clean up the diagnose folder
67	os.Chmod("diagnose", 0777)
68
69	// Clean up test diagnose folder
70	err = os.RemoveAll("diagnose")
71	if err != nil {
72		t.Fatal(err)
73	}
74}
75
76func TestRaftStorageQuorum(t *testing.T) {
77	m := mockStorageBackend{}
78	m.raftServerQuorumType = 0
79	twoVoterCluster := RaftStorageQuorum(context.Background(), m)
80
81	if !strings.Contains(twoVoterCluster, "Please ensure that Vault has access to an odd number of voter nodes.") {
82		t.Fatalf("two voter cluster yielded wrong error: %+s", twoVoterCluster)
83	}
84
85	m.raftServerQuorumType = 1
86	threeVoterCluster := RaftStorageQuorum(context.Background(), m)
87	if !strings.Contains(threeVoterCluster, "Voter quorum exists") {
88		t.Fatalf("three voter cluster yielded incorrect error: %s", threeVoterCluster)
89	}
90
91	m.raftServerQuorumType = 2
92	threeNodeTwoVoterCluster := RaftStorageQuorum(context.Background(), m)
93	if !strings.Contains(threeNodeTwoVoterCluster, "Please ensure that Vault has access to an odd number of voter nodes.") {
94		t.Fatalf("two voter cluster yielded wrong error: %+s", threeNodeTwoVoterCluster)
95	}
96
97	m.raftServerQuorumType = 3
98	errClusterInfo := RaftStorageQuorum(context.Background(), m)
99	if !strings.Contains(errClusterInfo, "error") {
100		t.Fatalf("two voter cluster yielded wrong error: %+s", errClusterInfo)
101	}
102}
103