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

import (
Koen van Ingen's avatar
Koen van Ingen committed
4
5
	"fmt"
	"testing"
6

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

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

var client *Client

func TestManualSession(t *testing.T) {
22
23
	client = parseStorage(t)

24
25
26
27
28
29
30
	request := "{\"nonce\": 0, \"message\":\"I owe you everything\",\"messageType\":\"STRING\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"

	channel := make(chan *irma.SessionError)
	manualSessionHandler := ManualSessionHandler{t: t, c: channel}

	client.NewManualSession(request, &manualSessionHandler)

31
	test.ClearTestStorage(t)
32
33
34
35
36
37
38

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

func TestManualKeyShareSession(t *testing.T) {
39
40
	client = parseStorage(t)

41
42
43
44
45
46
47
	keyshareRequest := "{\"nonce\": 0, \"message\":\"I owe you everything\",\"messageType\":\"STRING\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"test.test.mijnirma.email\"]}]}"

	channel := make(chan *irma.SessionError)
	manualSessionHandler := ManualSessionHandler{t: t, c: channel}

	client.NewManualSession(keyshareRequest, &manualSessionHandler)

48
	test.ClearTestStorage(t)
49
50
51
52
53
54

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

55
func (sh *ManualSessionHandler) Success(irmaAction irma.Action, result string) {
56
57
	sh.c <- nil
}
58
func (sh *ManualSessionHandler) UnsatisfiableRequest(irmaAction irma.Action, serverName string, missingAttributes irma.AttributeDisjunctionList) {
Koen van Ingen's avatar
Koen van Ingen committed
59
60
	sh.t.Fail()
}
61
62

// Done in irma bridge?
Koen van Ingen's avatar
Koen van Ingen committed
63
func (sh *ManualSessionHandler) StatusUpdate(irmaAction irma.Action, status irma.Status) {}
64
65
66
67
68
69
70
71
func (sh *ManualSessionHandler) RequestPin(remainingAttempts int, ph PinHandler) {
	ph(true, "12345")
}
func (sh *ManualSessionHandler) RequestSignaturePermission(request irma.SignatureRequest, requesterName string, ph PermissionHandler) {
	c := irma.DisclosureChoice{request.Candidates[0]}
	ph(true, &c)
}

72
// These handlers should not be called, fail test if they are called
73
func (sh *ManualSessionHandler) Cancelled(irmaAction irma.Action) {
74
	sh.Failure(irma.ActionUnknown, &irma.SessionError{Err: errors.New("Session was cancelled")})
75
}
76
func (sh *ManualSessionHandler) KeyshareEnrollmentMissing(manager irma.SchemeManagerIdentifier) {
77
	sh.Failure(irma.ActionUnknown, &irma.SessionError{Err: errors.Errorf("Missing keyshare server %s", manager.String())})
Koen van Ingen's avatar
Koen van Ingen committed
78
79
}
func (sh *ManualSessionHandler) RequestIssuancePermission(request irma.IssuanceRequest, issuerName string, ph PermissionHandler) {
80
	sh.Failure(irma.ActionUnknown, &irma.SessionError{Err: errors.New("Unexpected session type")})
Koen van Ingen's avatar
Koen van Ingen committed
81
82
}
func (sh *ManualSessionHandler) RequestSchemeManagerPermission(manager *irma.SchemeManager, callback func(proceed bool)) {
83
	sh.Failure(irma.ActionUnknown, &irma.SessionError{Err: errors.New("Unexpected session type")})
Koen van Ingen's avatar
Koen van Ingen committed
84
85
}
func (sh *ManualSessionHandler) RequestVerificationPermission(request irma.DisclosureRequest, verifierName string, ph PermissionHandler) {
86
	sh.Failure(irma.ActionUnknown, &irma.SessionError{Err: errors.New("Unexpected session type")})
Koen van Ingen's avatar
Koen van Ingen committed
87
}
88
89
func (sh *ManualSessionHandler) Failure(irmaAction irma.Action, err *irma.SessionError) {
	fmt.Println(err.Err)
90
91
92
93
94
95
	select {
	case sh.c <- err:
		// nop
	default:
		sh.t.Fatal(err)
	}
96
}
97
func (sh *ManualSessionHandler) KeyshareBlocked(manager irma.SchemeManagerIdentifier, duration int) {
98
	sh.Failure(irma.ActionUnknown, &irma.SessionError{Err: errors.New("KeyshareBlocked")})
99
}
100
101
func (sh *ManualSessionHandler) KeyshareEnrollmentIncomplete(manager irma.SchemeManagerIdentifier) {
	sh.Failure(irma.ActionUnknown, &irma.SessionError{Err: errors.New("KeyshareEnrollmentIncomplete")})
102
}