-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
262 lines (203 loc) · 7.83 KB
/
client_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
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
package httpx
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/go-chi/chi"
"github.com/lancer-kit/armory/crypto"
"github.com/lancer-kit/armory/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSXClient_Auth(t *testing.T) {
client := SXClient{}
client.auth = false
assert.Equal(t, false, client.Auth(), "XClient.Auth() must return actual value")
assert.Equal(t, client.auth, client.Auth(), "XClient.Auth() must return actual value")
client.auth = true
assert.Equal(t, true, client.Auth(), "XClient.Auth() must return actual value")
assert.Equal(t, client.auth, client.Auth(), "XClient.Auth() must return actual value")
}
func TestSXClient_OffAuth(t *testing.T) {
client := NewSXClient()
assert.False(t, client.Auth(), "XClient.Auth() disabled by default")
client.auth = true
client.OffAuth()
assert.False(t, client.Auth(), "XClient.Auth() must be disabled")
}
func TestSXClient_OnAuth(t *testing.T) {
client := NewSXClient()
assert.False(t, client.Auth(), "XClient.Auth() disabled by default")
client.OnAuth()
assert.True(t, client.Auth(), "XClient.Auth() must be enabled")
}
func TestSXClient_SetAuth(t *testing.T) {
client := NewSXClient()
const service = "test_service"
kp := crypto.RandomKP()
client = client.SetAuth(service, kp).(*SXClient)
assert.True(t, client.Auth(), "XClient.Auth() must be enabled after XClient.SetAuth()")
assert.Equal(t, kp, client.kp)
assert.Equal(t, kp.Public, client.PublicKey())
}
func TestSXClient_SignRequest(t *testing.T) {
const service = "test_service"
var kp = crypto.RandomKP()
client := NewSXClient().SetAuth(service, kp)
req, err := http.NewRequest(http.MethodGet, "http://example.com/test?user=foo", nil)
assert.Nil(t, err)
req, err = client.SignRequest(req, nil, nil)
assert.Nil(t, err)
assert.NotEmpty(t, req.Header.Get(HeaderBodyHash))
assert.NotEmpty(t, req.Header.Get(HeaderSignature))
assert.NotEmpty(t, req.Header.Get(HeaderService))
assert.Equal(t, service, req.Header.Get(HeaderService))
assert.NotEmpty(t, req.Header.Get(HeaderSigner))
assert.Equal(t, kp.Public.String(), req.Header.Get(HeaderSigner))
ok, err := client.VerifyRequest(req, kp.Public.String())
assert.Nil(t, err)
assert.True(t, ok)
req, err = http.NewRequest(http.MethodPost, "http://example.com/test?user=foo", bytes.NewBuffer([]byte("{}")))
_ = err
req, err = client.SignRequest(req, nil, nil)
assert.Nil(t, err)
assert.NotEmpty(t, req.Header.Get(HeaderBodyHash))
assert.NotEmpty(t, req.Header.Get(HeaderSignature))
assert.NotEmpty(t, req.Header.Get(HeaderService))
assert.Equal(t, service, req.Header.Get(HeaderService))
assert.NotEmpty(t, req.Header.Get(HeaderSigner))
assert.Equal(t, kp.Public.String(), req.Header.Get(HeaderSigner))
ok, err = client.VerifyRequest(req, kp.Public.String())
assert.Nil(t, err)
assert.True(t, ok)
}
func TestSXClient(t *testing.T) {
type fakeData struct {
dat string
}
kp := crypto.RandomKP()
server1, client1 := createFakeService(t, "test server 1", kp)
server2, _ := createFakeService(t, "test server 2", kp)
go func() {
log.Get().Info("Starting test server 1")
if err := http.ListenAndServe(":3030", server1); err != nil {
log.Get().WithError(err).Error("Unable to start test server 1")
}
}()
go func() {
log.Get().Info("Starting test server 2")
if err := http.ListenAndServe(":4040", server2); err != nil {
log.Get().WithError(err).Error("Unable to start test server 1")
}
}()
time.Sleep(5 * time.Second)
data := fakeData{"Fake data"}
sendCorrectRequests(t, client1, 4040, data)
sendCorrectRequests(t, client1, 4040, nil)
sendBadRequests(t, client1, 4040, data)
}
func createFakeService(t *testing.T, name string, kp crypto.KP) (*chi.Mux, SecuredClient) {
assertions := require.New(t)
r := chi.NewRouter()
client := NewSXClient().SetAuth(name, kp)
r.Route("/test", func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
ok, err := client.VerifyRequest(r, kp.Public.String())
assertions.NoErrorf(err, "Wrong auth headers in GET request")
if !ok {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("GET: Wrong request headers"))
log.Get().Infof("Get request to: \"%s\" has failed", name)
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("GET was successful"))
log.Get().Infof("Get request to: \"%s\" was successful", name)
})
r.Get("/bad", func(w http.ResponseWriter, r *http.Request) {
_, err := client.VerifyRequest(r, kp.Public.String())
assertions.Errorf(err, "Error with bad header OK ")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Error evoked, success"))
log.Get().Infof("Get request to: \"%s\", was successful", name)
})
r.Post("/", func(w http.ResponseWriter, r *http.Request) {
ok, err := client.VerifyRequest(r, kp.Public.String())
assertions.NoErrorf(err, "Wrong auth headers in POST request")
if !ok {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("POST: Wrong request headers"))
log.Get().Infof("POST request to: \"%s\" has failed", name)
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("POST was successful"))
log.Get().Infof("POST request to: \"%s\" was successful", name)
})
})
return r, client
}
func sendCorrectRequests(t *testing.T, client SecuredClient, port int, data interface{}) {
assertions := require.New(t)
url := fmt.Sprintf("http://127.0.0.1:%d/test", port)
if data != nil {
log.Get().WithField("Testing client requests with data", data).Info("Happy flow")
} else {
log.Get().Infof("%s Testing client requests without data", "Happy flow")
}
res, err := client.GetJSON(url, nil)
assertions.NoErrorf(err, "Error when trying to send GET request")
resBody, _ := ioutil.ReadAll(res.Body)
log.Get().WithField("GET response: ", string(resBody)).Info("Happy flow")
res, err = client.PostJSON(url, data, nil)
assertions.NoErrorf(err, "Error when trying to send POST request")
resBody, _ = ioutil.ReadAll(res.Body)
log.Get().WithField("POST response: ", string(resBody)).Info("Happy flow")
res, err = client.WithCookies([]*http.Cookie{}).PostJSON(url, data, nil)
assertions.NoErrorf(err, "Error when trying to send POST request")
resBody, _ = ioutil.ReadAll(res.Body)
log.Get().WithField("POST response: ", string(resBody)).Info("Happy flow")
}
func sendBadRequests(t *testing.T, iClient SecuredClient, port int, data interface{}) {
client := iClient.(*SXClient)
assertions := require.New(t)
var body io.Reader = nil
var err error
var rawData []byte
if data != nil {
log.Get().WithField("Testing client requests with data", data).Info("Bad flow")
} else {
log.Get().Infof("%s Testing client requests without data", "Bad flow")
}
if data != nil {
rawData, err = json.Marshal(data)
if err != nil {
return
}
body = bytes.NewBuffer(rawData)
}
url := fmt.Sprintf("http://127.0.0.1:%d/test", port)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d/test/bad", port), body)
assert.Nil(t, err)
req, err = client.SignRequest(req, nil, nil)
assertions.NoErrorf(err, "Error when trying to sign GET request")
req.Header.Set(HeaderSignature, "bad sign")
res, err := client.Do(req)
assertions.NoErrorf(err, "Error when trying to send GET request")
resBody, _ := ioutil.ReadAll(res.Body)
log.Get().WithField("GET response: ", string(resBody)).Info("Bad flow")
req, err = http.NewRequest(http.MethodPost, url, body)
assertions.NoErrorf(err, "Error when trying to create POST request")
req, err = client.SignRequest(req, rawData, nil)
assertions.NoErrorf(err, "Error when trying to sign POST request")
req.Header.Set(HeaderBodyHash, "bad body hash")
res, err = client.Do(req)
assertions.NoErrorf(err, "Error when trying to send POST request")
resBody, _ = ioutil.ReadAll(res.Body)
log.Get().WithField("GET response: ", string(resBody)).Info("Bad flow")
}