manual_session_test.go 7.09 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

	if err := <-channel; err != nil {
68
	  test.ClearTestStorage(t)
69
70
		t.Fatal(*err)
	}
71
72

	test.ClearTestStorage(t)
73
74
75
}

func TestManualKeyShareSession(t *testing.T) {
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
	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)

	if err := <-channel; err != nil {
95
	  test.ClearTestStorage(t)
96
97
		t.Fatal(*err)
	}
98
	test.ClearTestStorage(t)
99
100
101
102
}

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

105
106
107
108
109
110
111
112
113
114
	// 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\"]}]}"
115
116

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

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

127
	client.NewManualSession(request, &ms)
128
129

	if err := <-channel; err != nil {
130
	  test.ClearTestStorage(t)
131
132
		t.Fatal(*err)
	}
133
	test.ClearTestStorage(t)
134
135
}

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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)

	if err := <-channel; err.ErrorType != "Proof does not verify" {
155
    test.ClearTestStorage(t)
156
157
		t.Fatal(*err)
	}
158
	test.ClearTestStorage(t)
159
160
}

161
func (sh *ManualSessionHandler) Success(irmaAction irma.Action, result string) {
162
163
164
165
166
167
168
169
170
171
172
173
	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
		}
	}
174
175
	sh.c <- nil
}
176
177
178
179
180
181
182
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
183
}
184

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

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

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