This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
clientInitiatedSessionDestruction_test.go
192 lines (169 loc) · 5.68 KB
/
clientInitiatedSessionDestruction_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
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
package test
import (
"context"
"testing"
"time"
webwire "github.com/qbeon/webwire-go"
webwireClient "github.com/qbeon/webwire-go/client"
)
// TestClientInitiatedSessionDestruction verifies
// client-initiated session destruction
func TestClientInitiatedSessionDestruction(t *testing.T) {
sessionCreationCallbackCalled := NewPending(1, 1*time.Second, true)
sessionDestructionCallbackCalled := NewPending(1, 1*time.Second, true)
var createdSession *webwire.Session
expectedCredentials := webwire.Payload{
Encoding: webwire.EncodingUtf8,
Data: []byte("secret_credentials"),
}
placeholderMessage := webwire.Payload{
Encoding: webwire.EncodingUtf8,
Data: []byte("nothinginteresting"),
}
currentStep := 1
// Initialize webwire server
_, addr := setupServer(
t,
webwire.ServerOptions{
SessionsEnabled: true,
Hooks: webwire.Hooks{
OnRequest: func(ctx context.Context) (webwire.Payload, error) {
// Extract request message and requesting client from the context
msg := ctx.Value(webwire.Msg).(webwire.Message)
// On step 2 - verify session creation and correctness
if currentStep == 2 {
compareSessions(t, createdSession, msg.Client.Session)
if string(msg.Payload.Data) != msg.Client.Session.Key {
t.Errorf(
"Clients session key doesn't match: "+
"client: '%s' | server: '%s'",
string(msg.Payload.Data),
msg.Client.Session.Key,
)
}
return webwire.Payload{}, nil
}
// On step 4 - verify session destruction
if currentStep == 4 {
if msg.Client.Session != nil {
t.Errorf(
"Expected the session to be destroyed, got: %v",
msg.Client.Session,
)
}
return webwire.Payload{}, nil
}
// On step 1 - authenticate and create a new session
if err := msg.Client.CreateSession(nil); err != nil {
return webwire.Payload{}, err
}
// Return the key of the newly created session
return webwire.Payload{
Data: []byte(msg.Client.Session.Key),
}, nil
},
// Define dummy hooks to enable sessions on this server
OnSessionCreated: func(_ *webwire.Client) error { return nil },
OnSessionLookup: func(_ string) (*webwire.Session, error) { return nil, nil },
OnSessionClosed: func(_ *webwire.Client) error { return nil },
},
},
)
// Initialize client
client := webwireClient.NewClient(
addr,
webwireClient.Options{
Hooks: webwireClient.Hooks{
OnSessionCreated: func(_ *webwire.Session) {
// Mark the client-side session creation callback as executed
sessionCreationCallbackCalled.Done()
},
OnSessionClosed: func() {
// Ensure this callback is called during the
if currentStep != 3 {
t.Errorf(
"Client-side session destruction callback "+
"called at wrong step (%d)",
currentStep,
)
}
sessionDestructionCallbackCalled.Done()
},
},
DefaultRequestTimeout: 2 * time.Second,
},
)
/*****************************************************************\
Step 1 - Session Creation
\*****************************************************************/
if err := client.Connect(); err != nil {
t.Fatalf("Couldn't connect: %s", err)
}
// Send authentication request
authReqReply, err := client.Request("login", expectedCredentials)
if err != nil {
t.Fatalf("Authentication request failed: %s", err)
}
tmp := client.Session()
createdSession = &tmp
// Verify reply
if createdSession.Key != string(authReqReply.Data) {
t.Fatalf(
"Unexpected session key: %s | %s",
createdSession.Key,
string(authReqReply.Data),
)
}
// Wait for the client-side session creation callback to be executed
if err := sessionCreationCallbackCalled.Wait(); err != nil {
t.Fatal("Session creation callback not called")
}
// Ensure the session was locally created
currentSessionAfterCreation := client.Session()
if currentSessionAfterCreation.Key == "" {
t.Fatalf(
"Expected session on client-side, got none: %v",
currentSessionAfterCreation,
)
}
/*****************************************************************\
Step 2 - Session Creation Verification
\*****************************************************************/
currentStep = 2
// Send a test-request to verify the session creation on the server
if _, err := client.Request(
"verify-session-created",
webwire.Payload{Data: []byte(client.Session().Key)},
); err != nil {
t.Fatalf("Session creation verification request failed: %s", err)
}
/*****************************************************************\
Step 3 - Client-Side Session Destruction
\*****************************************************************/
currentStep = 3
// Request session destruction
if err := client.CloseSession(); err != nil {
t.Fatalf("Failed closing session on the client: %s", err)
}
// Wait for the client-side session destruction callback to be called
if err := sessionDestructionCallbackCalled.Wait(); err != nil {
t.Fatal("Session destruction callback not called")
}
/*****************************************************************\
Step 4 - Destruction Verification
\*****************************************************************/
currentStep = 4
// Ensure the session is destroyed locally as well
currentSessionAfterDestruction := client.Session()
if currentSessionAfterDestruction.Key != "" {
t.Fatalf(
"Expected session to be destroyed on the client as well, "+
"but still got: %v",
currentSessionAfterDestruction,
)
}
// Send a test-request to verify the session was destroyed on the server
if _, err := client.Request("test-request", placeholderMessage); err != nil {
t.Fatalf("Session destruction verification request failed: %s", err)
}
}