1 package adal
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import (
18 "encoding/json"
19 "io/ioutil"
20 "os"
21 "path"
22 "reflect"
23 "runtime"
24 "strings"
25 "testing"
26 )
27
28 const MockTokenJSON string = `{
29 "access_token": "accessToken",
30 "refresh_token": "refreshToken",
31 "expires_in": "1000",
32 "expires_on": "2000",
33 "not_before": "3000",
34 "resource": "resource",
35 "token_type": "type"
36 }`
37
38 var TestToken = Token{
39 AccessToken: "accessToken",
40 RefreshToken: "refreshToken",
41 ExpiresIn: "1000",
42 ExpiresOn: "2000",
43 NotBefore: "3000",
44 Resource: "resource",
45 Type: "type",
46 }
47
48 func writeTestTokenFile(t *testing.T, suffix string, contents string) *os.File {
49 f, err := ioutil.TempFile(os.TempDir(), suffix)
50 if err != nil {
51 t.Fatalf("azure: unexpected error when creating temp file: %v", err)
52 }
53 defer f.Close()
54
55 _, err = f.Write([]byte(contents))
56 if err != nil {
57 t.Fatalf("azure: unexpected error when writing temp test file: %v", err)
58 }
59
60 return f
61 }
62
63 func TestLoadToken(t *testing.T) {
64 f := writeTestTokenFile(t, "testloadtoken", MockTokenJSON)
65 defer os.Remove(f.Name())
66
67 expectedToken := TestToken
68 actualToken, err := LoadToken(f.Name())
69 if err != nil {
70 t.Fatalf("azure: unexpected error loading token from file: %v", err)
71 }
72
73 if *actualToken != expectedToken {
74 t.Fatalf("azure: failed to decode properly expected(%v) actual(%v)", expectedToken, *actualToken)
75 }
76
77
78 err = SaveToken(f.Name(), 0600, *actualToken)
79 if err != nil {
80 t.Fatalf("azure: could not save token after LoadToken: %v", err)
81 }
82 }
83
84 func TestLoadTokenFailsBadPath(t *testing.T) {
85 _, err := LoadToken("/tmp/this_file_should_never_exist_really")
86 expectedSubstring := "failed to open file"
87 if err == nil || !strings.Contains(err.Error(), expectedSubstring) {
88 t.Fatalf("azure: failed to get correct error expected(%s) actual(%s)", expectedSubstring, err.Error())
89 }
90 }
91
92 func TestLoadTokenFailsBadJson(t *testing.T) {
93 gibberishJSON := strings.Replace(MockTokenJSON, "expires_on", ";:\"gibberish", -1)
94 f := writeTestTokenFile(t, "testloadtokenfailsbadjson", gibberishJSON)
95 defer os.Remove(f.Name())
96
97 _, err := LoadToken(f.Name())
98 expectedSubstring := "failed to decode contents of file"
99 if err == nil || !strings.Contains(err.Error(), expectedSubstring) {
100 t.Fatalf("azure: failed to get correct error expected(%s) actual(%s)", expectedSubstring, err.Error())
101 }
102 }
103
104 func token() *Token {
105 var token Token
106 json.Unmarshal([]byte(MockTokenJSON), &token)
107 return &token
108 }
109
110 func TestSaveToken(t *testing.T) {
111 f, err := ioutil.TempFile("", "testloadtoken")
112 if err != nil {
113 t.Fatalf("azure: unexpected error when creating temp file: %v", err)
114 }
115 defer os.Remove(f.Name())
116 f.Close()
117
118 mode := os.ModePerm & 0642
119 err = SaveToken(f.Name(), mode, *token())
120 if err != nil {
121 t.Fatalf("azure: unexpected error saving token to file: %v", err)
122 }
123 fi, err := os.Stat(f.Name())
124 if err != nil {
125 t.Fatalf("azure: stat failed: %v", err)
126 }
127 if runtime.GOOS != "windows" {
128 if perm := fi.Mode().Perm(); perm != mode {
129 t.Fatalf("azure: wrong file perm. got:%s; expected:%s file :%s", perm, mode, f.Name())
130 }
131 }
132
133 var actualToken Token
134 var expectedToken Token
135
136 json.Unmarshal([]byte(MockTokenJSON), &expectedToken)
137
138 contents, err := ioutil.ReadFile(f.Name())
139 if err != nil {
140 t.Fatal("!!")
141 }
142 json.Unmarshal(contents, &actualToken)
143
144 if !reflect.DeepEqual(actualToken, expectedToken) {
145 t.Fatal("azure: token was not serialized correctly")
146 }
147 }
148
149 func TestSaveTokenFailsNoPermission(t *testing.T) {
150 pathWhereWeShouldntHavePermission := "/usr/thiswontwork/atall"
151 if runtime.GOOS == "windows" {
152 pathWhereWeShouldntHavePermission = path.Join(os.Getenv("windir"), "system32\\mytokendir\\mytoken")
153 }
154 err := SaveToken(pathWhereWeShouldntHavePermission, 0644, *token())
155 expectedSubstring := "failed to create directory"
156 if err == nil || !strings.Contains(err.Error(), expectedSubstring) {
157 t.Fatalf("azure: failed to get correct error expected(%s) actual(%v)", expectedSubstring, err)
158 }
159 }
160
161 func TestSaveTokenFailsCantCreate(t *testing.T) {
162 tokenPath := "/usr/thiswontwork"
163 if runtime.GOOS == "windows" {
164 tokenPath = path.Join(os.Getenv("windir"), "system32")
165 }
166 err := SaveToken(tokenPath, 0644, *token())
167 expectedSubstring := "failed to create the temp file to write the token"
168 if err == nil || !strings.Contains(err.Error(), expectedSubstring) {
169 t.Fatalf("azure: failed to get correct error expected(%s) actual(%v)", expectedSubstring, err)
170 }
171 }
172
View as plain text