forked from samuong/alpaca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyring_darwin_test.go
103 lines (94 loc) · 2.83 KB
/
keyring_darwin_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2019, 2021 The Alpaca Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/require"
)
func fakeExecCommand(env []string) func(string, ...string) *exec.Cmd {
return func(name string, arg ...string) *exec.Cmd {
arg = append([]string{"-test.run=TestMockDefaults", "--", name}, arg...)
cmd := exec.Command(os.Args[0], arg...)
cmd.Env = append(env, "ALPACA_WANT_MOCK_DEFAULTS=1")
return cmd
}
}
func TestMockDefaults(t *testing.T) {
if os.Getenv("ALPACA_WANT_MOCK_DEFAULTS") != "1" {
return
}
args := os.Args
for i := 0; i < len(args); i++ {
if args[i] == "--" {
args = args[i+1:]
break
}
}
if len(args) == len(os.Args) {
fmt.Println("no command")
os.Exit(2)
} else if cmd := args[0]; cmd != "defaults" {
fmt.Printf("%s: command not found\n", cmd)
os.Exit(127)
} else if len(args) != 4 || args[1] != "read" {
fmt.Println("usage: defaults read <domain> <key>")
os.Exit(255)
}
domain, key := args[2], args[3]
if os.Getenv("DOMAIN_EXISTS") != "1" {
fmt.Printf("Domain %s does not exist\n", domain)
os.Exit(1)
} else if key == "UseKeychain" && os.Getenv("USE_KEYCHAIN") == "1" {
fmt.Println(1)
os.Exit(0)
} else if key == "UserPrincipal" {
switch os.Getenv("USER_PRINCIPAL") {
case "1":
fmt.Println("malory")
os.Exit(0)
case "2":
fmt.Println("malory@ISIS")
os.Exit(0)
}
}
fmt.Printf("The domain/default pair of (%s, %s) does not exist\n", domain, key)
os.Exit(1)
}
func TestNoMADNotConfigured(t *testing.T) {
env := []string{"DOMAIN_EXISTS=0"}
k := &keyring{execCommand: fakeExecCommand(env)}
_, err := k.getCredentials()
require.Error(t, err)
}
func TestNoMADNotUsingKeychain(t *testing.T) {
env := []string{"DOMAIN_EXISTS=1", "USE_KEYCHAIN=0"}
k := &keyring{execCommand: fakeExecCommand(env)}
_, err := k.getCredentials()
require.Error(t, err)
}
func TestNoMADNoUserPrincipal(t *testing.T) {
env := []string{"DOMAIN_EXISTS=1", "USE_KEYCHAIN=1", "USER_PRINCIPAL=0"}
k := &keyring{execCommand: fakeExecCommand(env)}
_, err := k.getCredentials()
require.Error(t, err)
}
func TestNoMADInvalidUserPrincipal(t *testing.T) {
env := []string{"DOMAIN_EXISTS=1", "USE_KEYCHAIN=1", "USER_PRINCIPAL=1"}
k := &keyring{execCommand: fakeExecCommand(env)}
_, err := k.getCredentials()
require.Error(t, err)
}