-
Notifications
You must be signed in to change notification settings - Fork 11
/
sync_worker_test.go
77 lines (67 loc) · 1.86 KB
/
sync_worker_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
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
// leaderKeys holds arbitrary keys that we use for testing.
var leaderKeys = &enclaveKeys{
NitridingKey: []byte("NitridingTestKey"),
NitridingCert: []byte("NitridingTestCert"),
AppKeys: []byte("AppTestKeys"),
}
func initLeaderKeysCert(t *testing.T) {
t.Helper()
cert, key, err := createCertificate("example.com")
if err != nil {
t.Fatal(err)
}
leaderKeys.setNitridingKeys(key, cert)
}
func TestSuccessfulRegisterWith(t *testing.T) {
e := createEnclave(&defaultCfg)
hasRegistered := false
srv := httptest.NewTLSServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
hasRegistered = true
w.WriteHeader(http.StatusOK)
}),
)
leader, err := url.Parse(srv.URL)
if err != nil {
t.Fatalf("Error creating test server URL: %v", err)
}
worker := &url.URL{
Host: "localhost",
}
err = asWorker(e.setupWorkerPostSync, &dummyAttester{}).registerWith(leader, worker)
if err != nil {
t.Fatalf("Error registering with leader: %v", err)
}
if !hasRegistered {
t.Fatal("Worker did not register with leader.")
}
}
func TestSuccessfulSync(t *testing.T) {
// For key synchronization to be successful, we need actual certificates in
// the leader keys.
initLeaderKeysCert(t)
// Set up the worker.
worker := createEnclave(&defaultCfg)
srv := httptest.NewTLSServer(
asWorker(worker.setupWorkerPostSync, &dummyAttester{}),
)
workerURL, err := url.Parse(srv.URL)
if err != nil {
t.Fatalf("Error creating test server URL: %v", err)
}
if err = asLeader(leaderKeys, &dummyAttester{}).syncWith(workerURL); err != nil {
t.Fatalf("Error syncing with leader: %v", err)
}
// Make sure that the keys were synced correctly.
if !worker.keys.equal(leaderKeys) {
t.Fatalf("Keys differ between worker and leader:\n%v (worker)\n%v (leader)",
leaderKeys, worker.keys)
}
}