-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
99 lines (84 loc) · 2.96 KB
/
server_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
// A test that uses a mock.
package main_test
import (
"encoding/json"
"errors"
"fmt"
. "github.com/CaliOpen/gofido"
"github.com/CaliOpen/gofido/store"
"github.com/gin-gonic/gin"
"github.com/golang/mock/gomock"
"github.com/tstranex/u2f"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestRegisterRequest(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mock := store.NewMockStoreInterface(ctrl)
user := gomock.Any()
appId := "https://localhost:123456"
trustedFacets := []string{appId}
challenge, err := u2f.NewChallenge(appId, trustedFacets)
registrations := &[]u2f.Registration{}
response := u2f.NewWebRegisterRequest(challenge, *registrations)
mock.EXPECT().NewChallenge(user).Return(*challenge, nil)
mock.EXPECT().GetRegistrations(user).Return(*registrations, nil)
s := FidoServer{}
s.Store = mock
gin.SetMode(gin.TestMode)
r := gin.Default()
register_url := fmt.Sprintf("/api/%s/register", user)
r.GET(register_url, s.RegisterRequest)
req, err := http.NewRequest(http.MethodGet, register_url, nil)
if err != nil {
t.Fatalf("Couldn't create request: %v\n", err)
}
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("Expected to get status %d but instead got %d\n", http.StatusOK, w.Code)
}
body, _ := ioutil.ReadAll(w.Result().Body)
expected := &u2f.WebRegisterRequest{}
_ = json.Unmarshal(body, expected)
if expected.AppID != response.AppID {
t.Errorf("Expected %s for appId %s", expected.AppID, response.AppID)
}
request := expected.RegisterRequests[0]
if request.Version != "U2F_V2" {
t.Errorf("Invalid request version %s", request.Version)
}
if request.Challenge != store.EncodeBase64(challenge.Challenge) {
t.Errorf("Invalid challenge response")
}
}
func TestRegisterRequestKO(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mock := store.NewMockStoreInterface(ctrl)
user := gomock.Any()
appId := "https://localhost:123456"
trustedFacets := []string{appId}
challenge, err := u2f.NewChallenge(appId, trustedFacets)
registrations := &[]u2f.Registration{}
mock.EXPECT().NewChallenge(user).Return(*challenge, nil)
mock.EXPECT().GetRegistrations(user).Return(*registrations, errors.New("test"))
s := FidoServer{}
s.Store = mock
gin.SetMode(gin.TestMode)
r := gin.Default()
register_url := fmt.Sprintf("/api/%s/register", user)
r.GET(register_url, s.RegisterRequest)
req, err := http.NewRequest(http.MethodGet, register_url, nil)
if err != nil {
t.Fatalf("Couldn't create request: %v\n", err)
}
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusInternalServerError {
t.Fatalf("Expected to get status %d but instead got %d\n", http.StatusInternalServerError, w.Code)
}
}