Debian Codegraph @

dir/path_test.go

raw
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package dir
15
16 import (
17 "os"
18 "path/filepath"
19 "testing"
20 )
21
22 func mockUserPath() (string, error) {
23 return "/path/", nil
24 }
25
26 func setup() {
27 UserConfigDir = ""
28 UserLibexecDir = ""
29 UserCacheDir = ""
30 }
31
32 func Test_UserConfigDirPath(t *testing.T) {
33 userConfigDir = mockUserPath
34 setup()
35 got := userConfigDirPath()
36 if got != "/path/notation" {
37 t.Fatalf(`UserConfigDirPath() = %q, want "/path/notation"`, got)
38 }
39 }
40
41 func Test_NoHomeVariable(t *testing.T) {
42 t.Setenv("HOME", "")
43 t.Setenv("XDG_CONFIG_HOME", "")
44 t.Setenv("XDG_CACHE_HOME", "")
45 setup()
46 userConfigDir = os.UserConfigDir
47 got := userConfigDirPath()
48 if got != ".notation" {
49 t.Fatalf(`userConfigDirPath() = %q, want ".notation"`, got)
50 }
51 got = userCacheDirPath()
52 want := filepath.Join("."+notation, "cache")
53 if got != want {
54 t.Fatalf(`userCacheDirPath() = %q, want %q`, got, want)
55 }
56 }
57
58 func Test_UserLibexecDirPath(t *testing.T) {
59 userConfigDir = mockUserPath
60 setup()
61 got := userLibexecDirPath()
62 if got != "/path/notation" {
63 t.Fatalf(`UserConfigDirPath() = %q, want "/path/notation"`, got)
64 }
65 }
66
67 func Test_UserCacheDirPath(t *testing.T) {
68 userCacheDir = mockUserPath
69 setup()
70 got := userCacheDirPath()
71 if got != "/path/notation" {
72 t.Fatalf(`UserCacheDirPath() = %q, want "/path/notation"`, got)
73 }
74 }
75
76 func TestLocalKeyPath(t *testing.T) {
77 userConfigDir = mockUserPath
78 setup()
79 _ = userConfigDirPath()
80 _ = userLibexecDirPath()
81 gotKeyPath, gotCertPath := LocalKeyPath("web")
82 if gotKeyPath != "localkeys/web.key" {
83 t.Fatalf(`LocalKeyPath() gotKeyPath = %q, want "localkeys/web.key"`, gotKeyPath)
84 }
85 if gotCertPath != "localkeys/web.crt" {
86 t.Fatalf(`LocalKeyPath() gotCertPath = %q, want "localkeys/web.crt"`, gotCertPath)
87 }
88 }
89
90 func TestX509TrustStoreDir(t *testing.T) {
91 userConfigDir = mockUserPath
92 setup()
93 _ = userConfigDirPath()
94 _ = userLibexecDirPath()
95 if got := X509TrustStoreDir("ca", "web"); got != "truststore/x509/ca/web" {
96 t.Fatalf(`X509TrustStoreDir() = %q, want "truststore/x509/ca/web"`, got)
97 }
98 }