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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
package discovery
import (
"context"
"net/http"
"reflect"
"testing"
"time"
"github.com/eduvpn/eduvpn-common/internal/test"
discotypes "github.com/eduvpn/eduvpn-common/types/discovery"
)
// TestServers tests whether or not we can obtain discovery servers
// It setups up a file server using the 'test_files' directory
func TestServers(t *testing.T) {
handler := http.FileServer(http.Dir("test_files"))
s := test.NewServer(handler, nil)
DiscoURL = s.URL
c, err := s.Client()
if err != nil {
t.Fatalf("Failed to get HTTP test client: %v", err)
}
d := &Discovery{httpClient: c}
// get servers
s1, err := d.Servers(context.Background())
if err != nil {
t.Fatalf("Failed getting servers: %v", err)
}
// Shutdown the server
s.Close()
// Test if we get the same cached copy
s2, err := d.Servers(context.Background())
// We should not get an error as the timestamp is not expired
if err != nil {
t.Fatalf("Got a servers error after shutting down server: %v", err)
}
if s1 != s2 {
t.Fatalf("Servers copies not equal after shutting down file server")
}
// Force expired, 1 hour in the past
d.ServerList.Timestamp = time.Now().Add(-1 * time.Hour)
s3, err := d.Servers(context.Background())
// Now we expect an error with the cached copy
if err == nil {
t.Fatalf("Got a servers nil error after shutting down file server and expired")
}
if s1 != s3 {
t.Fatalf("Servers copies not equal after shutting down file server and expired")
}
}
// TestOrganizations tests whether or not we can obtain discovery organizations
// It setups up a file server using the 'test_files' directory
func TestOrganizations(t *testing.T) {
handler := http.FileServer(http.Dir("test_files"))
s := test.NewServer(handler, nil)
DiscoURL = s.URL
c, err := s.Client()
if err != nil {
t.Fatalf("Failed to get HTTP test client: %v", err)
}
d := &Discovery{httpClient: c}
// get servers
s1, err := d.Organizations(context.Background())
if err != nil {
t.Fatalf("Failed getting organizations: %v", err)
}
// Shutdown the server
s.Close()
// Test if we get the same cached copy
// We should not get an error as the timestamp is not zero
s2, err := d.Organizations(context.Background())
if err != nil {
t.Fatalf("Got an organizations error after shutting down file server: %v", err)
}
if s1 != s2 {
t.Fatalf("Organizations copies not equal after shutting down file server")
}
}
// TestSecureLocationList tests the function for getting a list of secure internet servers
func TestSecureLocationList(t *testing.T) {
d := Discovery{
ServerList: Servers{
Version: 1,
List: []Server{
// institute access server, this should not be found
{Server: discotypes.Server{Type: "institute_access"}, CountryCode: ""},
// secure internet servers, these should be found
{Server: discotypes.Server{Type: "secure_internet"}, CountryCode: "b"},
{Server: discotypes.Server{Type: "secure_internet"}, CountryCode: "c"},
// Unexpected type, this should not be found
{Server: discotypes.Server{Type: "test"}, CountryCode: "d"},
},
},
}
cc := d.SecureLocationList()
want := []string{"b", "c"}
if !reflect.DeepEqual(cc, want) {
t.Fatalf("Secure location list is not equal. Got: %v, Want: %v", cc, want)
}
}
// TestServerByURL tests the function for getting a server by the Base URL and type
func TestServerByURL(t *testing.T) {
d := Discovery{
ServerList: Servers{
Version: 1,
List: []Server{
// institute access server
{Server: discotypes.Server{BaseURL: "a", Type: "institute_access"}},
// secure internet servers
{Server: discotypes.Server{BaseURL: "b", Type: "secure_internet"}},
// Unexpected type, this should not be found
{Server: discotypes.Server{BaseURL: "d", Type: "test"}},
},
},
}
// Institute Access: Can be found
_, err := d.ServerByURL("a", "institute_access")
if err != nil {
t.Fatalf("Got error: %v, when getting a server by url with parameters 'a' and 'institute_access'", err)
}
// Institute Access: Cannot be found
_, err = d.ServerByURL("b", "institute_access")
if err == nil {
t.Fatal("Got no error, when getting a non-existing server by url with parameters 'b' and 'institute_access'")
}
// Secure Internet: Can be found
_, err = d.ServerByURL("b", "secure_internet")
if err != nil {
t.Fatalf("Got error: %v, when getting a server by url with parameters 'b' and 'secure_internet'", err)
}
// Secure Internet: Cannot be found because of invalid type
_, err = d.ServerByURL("d", "secure_internet")
if err == nil {
t.Fatal("Got no error, when getting a non-existing server by url with parameters 'd' and 'secure_internet'")
}
}
// TestServerByCountryCode tests the function for getting a server by the country code
func TestServerByCountryCode(t *testing.T) {
s1 := Server{Server: discotypes.Server{Type: "secure_internet"}, CountryCode: "a"}
d := Discovery{
ServerList: Servers{
Version: 1,
List: []Server{
// secure internet server
s1,
// Unexpected types, these should not be found
{Server: discotypes.Server{Type: "institute_access"}, CountryCode: "b"},
{Server: discotypes.Server{Type: "test"}, CountryCode: "c"},
},
},
}
// Institute Access: Can be found
s, err := d.ServerByCountryCode("a")
if err != nil {
t.Fatalf("Got error: %v, when getting a server by country code 'a'", err)
}
if s.CountryCode != s1.CountryCode || s.Type != s1.Type {
t.Fatalf("Server with country code 'a' not equal, Got: %v, Want: %v", s, s1)
}
// Others: Cannot be found
_, err = d.ServerByCountryCode("b")
if err == nil {
t.Fatal("Got no error when getting a server by country code 'b'")
}
_, err = d.ServerByCountryCode("c")
if err == nil {
t.Fatal("Got no error when getting a server by country code 'c'")
}
}
// TestOrgByID tests the function for getting an organization by ID
func TestOrgByID(t *testing.T) {
o1 := discotypes.Organization{OrgID: "a"}
d := Discovery{
OrganizationList: Organizations{
Version: 1,
List: []Organization{
{Organization: o1},
},
},
}
o, err := d.orgByID("a")
if err != nil {
t.Fatal("Got an error when getting an organization with ID: 'a'")
}
if o.OrgID != o1.OrgID {
t.Fatalf("Organizations not equal, Got: %v, Want: %v", o, o1)
}
_, err = d.orgByID("b")
if err == nil {
t.Fatal("Got no error when searching for non-existing organization 'b'")
}
}
// TestSecureHomeArgs tests the function for getting an organization and matching secure internet server by organization ID
func TestSecureHomeArgs(t *testing.T) {
o1 := Organization{Organization: discotypes.Organization{OrgID: "id"}, SecureInternetHome: "a"}
s1 := discotypes.Server{BaseURL: "a", Type: "secure_internet"}
d := Discovery{
OrganizationList: Organizations{
Version: 1,
List: []Organization{
{Organization: discotypes.Organization{OrgID: "id2"}, SecureInternetHome: "c"},
o1,
},
},
ServerList: Servers{
Version: 1,
List: []Server{
{Server: s1},
{Server: discotypes.Server{BaseURL: "b"}},
},
},
}
// Args found
o, s, err := d.SecureHomeArgs("id")
if err != nil {
t.Fatalf("Got error: %v, when getting secure home arguments with ID: 'id'", err)
}
if o.OrgID != o1.OrgID || o.SecureInternetHome != o1.SecureInternetHome {
t.Fatalf("Organizations not equal for secure home arguments, Got: %v, Want: %v", o, o1)
}
if s.BaseURL != s1.BaseURL {
t.Fatalf("Servers not equal for secure home arguments, Got: %v, Want: %v", s, s1)
}
// Args not found because no matching secure internet server
_, _, err = d.SecureHomeArgs("id2")
if err == nil {
t.Fatal("Got no error, when getting non-matching secure home arguments with ID: 'id2'")
}
// Args not found because no organization
_, _, err = d.SecureHomeArgs("id3")
if err == nil {
t.Fatal("Got no error, when getting non-existing secure home arguments with ID: 'id3'")
}
}
|