summaryrefslogtreecommitdiff
path: root/internal/discovery/discovery_test.go
blob: 08c5ef8e8cc9cafd37e4b2317f0f153c012c6fff (plain)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package discovery

import (
	"context"
	"net/http"
	"reflect"
	"testing"
	"time"

	"codeberg.org/eduVPN/eduvpn-common/internal/test"
	discotypes "codeberg.org/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
	_, fresh, err := d.Servers(context.Background())
	if !fresh {
		t.Fatalf("Did not obtain the server list fresh")
	}
	if err != nil {
		t.Fatalf("Failed getting servers: %v", err)
	}

	// force expired
	d.ServerList.Timestamp = time.Now().Add(-1 * time.Hour)

	// override the last server with a keyword list
	// and make sure a new fetch doesn't copy over the keyword list
	// to the last entry
	d.ServerList.List[len(d.ServerList.List)-1] = Server{
		Server: discotypes.Server{
			BaseURL: "https://example.org/",
			DisplayName: map[string]string{
				"en": "example",
			},
			Type: "institute_access",
		},
		KeywordList: map[string]string{
			"en": "test bla",
		},
		SupportContact: []string{"mailto:test@example.org"},
	}
	// conditional requests: this should not be fetched fresh
	_, fresh, err = d.Servers(context.Background())
	if fresh {
		t.Fatalf("Obtained the server list fresh with conditional requests")
	}
	if err != nil {
		t.Fatalf("Failed getting servers after inserting a mock entry: %v", err)
	}
	// mock conditional requests
	d.ServerList.UpdateHeader = time.Time{}
	s1, fresh, err := d.Servers(context.Background())
	if !fresh {
		t.Fatalf("Did not obtain the server list fresh with mocked conditional request and mocked entry")
	}
	if err != nil {
		t.Fatalf("Failed getting servers after inserting a mock entry and mocking conditional request: %v", err)
	}
	if kws := s1.List[len(s1.List)-1].KeywordList; kws != nil {
		t.Fatalf("KeywordList is not nil when getting a fresh server list after inserting a mock entry: %v", kws)
	}

	// Shutdown the server
	s.Close()
	// Test if we get the same cached copy
	s2, fresh, err := d.Servers(context.Background())
	// We should not get an error as the timestamp is not expired
	if fresh {
		t.Fatalf("The server list was obtained fresh")
	}
	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
	// we should return the previous with an error
	d.ServerList.Timestamp = time.Now().Add(-1 * time.Hour)

	s3, fresh, err := d.Servers(context.Background())
	if fresh {
		t.Fatalf("Server list was gotten fresh")
	}
	// 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
	_, fresh, err := d.Organizations(context.Background())
	if !fresh {
		t.Fatalf("The organization list was not obtained fresh")
	}
	if err != nil {
		t.Fatalf("Failed getting organizations: %v", err)
	}
	if !fresh {
		t.Fatalf("Did not get a fresh organization list")
	}

	// force expired
	d.OrganizationList.Timestamp = time.Now().Add(-4 * time.Hour)

	// override the last organization with a keyword list
	// and make sure a new fetch doesn't copy over the keyword list
	// to the last entry
	d.OrganizationList.List[len(d.OrganizationList.List)-1] = Organization{
		Organization: discotypes.Organization{
			DisplayName: map[string]string{
				"en": "example",
			},
			OrgID: "example_orgid",
		},
		SecureInternetHome: "example.org",
		KeywordList: map[string]string{
			"en": "test bla",
		},
	}

	_, fresh, err = d.Organizations(context.Background())
	if fresh {
		t.Fatalf("Obtained the organization list fresh with conditional requests")
	}
	if err != nil {
		t.Fatalf("Failed getting organizations after inserting a mock entry: %v", err)
	}
	// mock conditional requests
	d.OrganizationList.UpdateHeader = time.Time{}
	s1, fresh, err := d.Organizations(context.Background())
	if !fresh {
		t.Fatalf("Did not obtain the organization list fresh after inserting a mock entry, faking expiry and mocking conditional request")
	}
	if err != nil {
		t.Fatalf("Failed getting organizations after inserting a mock entry and faking conditional request: %v", err)
	}
	if kws := s1.List[len(s1.List)-1].KeywordList; kws != nil {
		t.Fatalf("KeywordList is not nil when getting a fresh organization list after inserting a mock entry: %v", kws)
	}

	// 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, fresh, err := d.Organizations(context.Background())
	if fresh {
		t.Fatalf("The organization list is freshly obtained")
	}
	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"}},
				// 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'")
	}
}