manual_session_test.go 6.96 KB
Newer Older
1
2
3
package irmaclient

import (
4
	"encoding/json"
Koen van Ingen's avatar
Koen van Ingen committed
5
6
	"fmt"
	"testing"
7

8
	"github.com/go-errors/errors"
9
	"github.com/privacybydesign/irmago"
10
	"github.com/privacybydesign/irmago/internal/test"
11
12
13
14
15
)

type ManualSessionHandler struct {
	permissionHandler PermissionHandler
	pinHandler        PinHandler
Koen van Ingen's avatar
Koen van Ingen committed
16
17
	t                 *testing.T
	c                 chan *irma.SessionError
18
	sigRequest        *irma.SignatureRequest
19
20
21
22
}

var client *Client

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
// Issue BSN credential using sessionHelper
func issue(t *testing.T, ms ManualSessionHandler) {
	name := "testip"

	jwtcontents := getIssuanceJwt(name)
	sessionHandlerHelper(t, jwtcontents, "issue", client, &ms)
}

// Flip one bit in the proof string if invalidate is set to true
var invalidate bool

func corruptProofString(proof string) string {
	if invalidate {
		proofBytes := []byte(proof)

		flipLoc := 15
		if proofBytes[flipLoc] == 0x33 {
			proofBytes[flipLoc] = 0x32
		} else {
			proofBytes[flipLoc] = 0x33
		}
		return string(proofBytes)
	}
	return proof
}

49
func TestManualSession(t *testing.T) {
50
51
	invalidate = false
	channel := make(chan *irma.SessionError)
52
53
	client = parseStorage(t)

54
55
56
57
	request := "{\"nonce\": 0, \"context\": 0, \"message\":\"I owe you everything\",\"messageType\":\"STRING\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
	sigRequestJSON := []byte(request)
	sigRequest := &irma.SignatureRequest{}
	json.Unmarshal(sigRequestJSON, sigRequest)
58

59
60
61
62
63
	ms := ManualSessionHandler{
		t:          t,
		c:          channel,
		sigRequest: sigRequest,
	}
64

65
	client.NewManualSession(request, &ms)
66

67
	test.ClearTestStorage(t)
68
69
70
71
72
73
74

	if err := <-channel; err != nil {
		t.Fatal(*err)
	}
}

func TestManualKeyShareSession(t *testing.T) {
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
	invalidate = false
	channel := make(chan *irma.SessionError)

	keyshareRequestString := "{\"nonce\": 0, \"context\": 0, \"message\":\"I owe you everything\",\"messageType\":\"STRING\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"test.test.mijnirma.email\"]}]}"
	keyshareRequestJSON := []byte(keyshareRequestString)
	keyshareRequest := &irma.SignatureRequest{}
	json.Unmarshal(keyshareRequestJSON, keyshareRequest)

	manualSessionHandler := ManualSessionHandler{
		t:          t,
		c:          channel,
		sigRequest: keyshareRequest,
	}

	client = parseStorage(t)

	client.NewManualSession(keyshareRequestString, &manualSessionHandler)

	teardown(t)

	if err := <-channel; err != nil {
		t.Fatal(*err)
	}
}

func TestManualSessionMultiProof(t *testing.T) {
	invalidate = false
102
103
	client = parseStorage(t)

104
105
106
107
108
109
110
111
112
113
	// First, we need to issue an extra credential (BSN)
	is := ManualSessionHandler{t: t, c: make(chan *irma.SessionError)}
	go issue(t, is)
	if err := <-is.c; err != nil {
		fmt.Println("Error during initial issueing!")
		t.Fatal(*err)
	}

	// Request to sign with both BSN and StudentID
	request := "{\"nonce\": 0, \"context\": 0, \"message\":\"I owe you everything\",\"messageType\":\"STRING\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]},{\"label\":\"BSN\",\"attributes\":[\"irma-demo.MijnOverheid.root.BSN\"]}]}"
114
115

	channel := make(chan *irma.SessionError)
116
117
118
119
120
121
122
123
124
	sigRequestJSON := []byte(request)
	sigRequest := &irma.SignatureRequest{}
	json.Unmarshal(sigRequestJSON, sigRequest)

	ms := ManualSessionHandler{
		t:          t,
		c:          channel,
		sigRequest: sigRequest,
	}
125

126
	client.NewManualSession(request, &ms)
127

128
	test.ClearTestStorage(t)
129
130
131
132
133
134

	if err := <-channel; err != nil {
		t.Fatal(*err)
	}
}

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
func TestManualSessionInvalidProof(t *testing.T) {
	invalidate = true
	channel := make(chan *irma.SessionError)
	client = parseStorage(t)

	request := "{\"nonce\": 0, \"context\": 0, \"message\":\"I owe you everything\",\"messageType\":\"STRING\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
	sigRequestJSON := []byte(request)
	sigRequest := &irma.SignatureRequest{}
	json.Unmarshal(sigRequestJSON, sigRequest)

	ms := ManualSessionHandler{
		t:          t,
		c:          channel,
		sigRequest: sigRequest,
	}

	client.NewManualSession(request, &ms)

	teardown(t)

	if err := <-channel; err.ErrorType != "Proof does not verify" {
		t.Fatal(*err)
	}
}

160
func (sh *ManualSessionHandler) Success(irmaAction irma.Action, result string) {
161
162
163
164
165
166
167
168
169
170
171
172
	switch irmaAction {
	case irma.ActionSigning:
		// Make proof corrupt if we want to test invalid proofs
		result = corruptProofString(result)

		if !verifySig(client, result, sh.sigRequest) {
			sh.c <- &irma.SessionError{
				ErrorType: irma.ErrorType("Proof does not verify"),
			}
			return
		}
	}
173
174
	sh.c <- nil
}
175
176
177
178
179
180
181
func (sh *ManualSessionHandler) UnsatisfiableRequest(irmaAction irma.Action, missingAttributes irma.AttributeDisjunctionList) {
	// This function is called from main thread, which blocks go channel, so need go routine here
	go func() {
		sh.c <- &irma.SessionError{
			ErrorType: irma.ErrorType("UnsatisfiableRequest"),
		}
	}()
Koen van Ingen's avatar
Koen van Ingen committed
182
}
183

Koen van Ingen's avatar
Koen van Ingen committed
184
func (sh *ManualSessionHandler) StatusUpdate(irmaAction irma.Action, status irma.Status) {}
185

186
187
188
189
func (sh *ManualSessionHandler) RequestPin(remainingAttempts int, ph PinHandler) {
	ph(true, "12345")
}
func (sh *ManualSessionHandler) RequestSignaturePermission(request irma.SignatureRequest, requesterName string, ph PermissionHandler) {
190
191
192
193
194
	var attributes []*irma.AttributeIdentifier
	for _, cand := range request.Candidates {
		attributes = append(attributes, cand[0])
	}
	c := irma.DisclosureChoice{attributes}
195
196
	ph(true, &c)
}
197
198
199
func (sh *ManualSessionHandler) RequestIssuancePermission(request irma.IssuanceRequest, issuerName string, ph PermissionHandler) {
	ph(true, nil)
}
200

201
// These handlers should not be called, fail test if they are called
202
func (sh *ManualSessionHandler) Cancelled(irmaAction irma.Action) {
203
	sh.Failure(irma.ActionUnknown, &irma.SessionError{Err: errors.New("Session was cancelled")})
204
}
205
func (sh *ManualSessionHandler) KeyshareEnrollmentMissing(manager irma.SchemeManagerIdentifier) {
206
	sh.Failure(irma.ActionUnknown, &irma.SessionError{Err: errors.Errorf("Missing keyshare server %s", manager.String())})
Koen van Ingen's avatar
Koen van Ingen committed
207
208
}
func (sh *ManualSessionHandler) RequestSchemeManagerPermission(manager *irma.SchemeManager, callback func(proceed bool)) {
209
	sh.Failure(irma.ActionUnknown, &irma.SessionError{Err: errors.New("Unexpected session type")})
Koen van Ingen's avatar
Koen van Ingen committed
210
211
}
func (sh *ManualSessionHandler) RequestVerificationPermission(request irma.DisclosureRequest, verifierName string, ph PermissionHandler) {
212
	sh.Failure(irma.ActionUnknown, &irma.SessionError{Err: errors.New("Unexpected session type")})
Koen van Ingen's avatar
Koen van Ingen committed
213
}
214
215
func (sh *ManualSessionHandler) Failure(irmaAction irma.Action, err *irma.SessionError) {
	fmt.Println(err.Err)
216
217
218
219
220
221
	select {
	case sh.c <- err:
		// nop
	default:
		sh.t.Fatal(err)
	}
222
}
223
func (sh *ManualSessionHandler) KeyshareBlocked(manager irma.SchemeManagerIdentifier, duration int) {
224
	sh.Failure(irma.ActionUnknown, &irma.SessionError{Err: errors.New("KeyshareBlocked")})
225
}
226
227
func (sh *ManualSessionHandler) KeyshareEnrollmentIncomplete(manager irma.SchemeManagerIdentifier) {
	sh.Failure(irma.ActionUnknown, &irma.SessionError{Err: errors.New("KeyshareEnrollmentIncomplete")})
228
}