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

import (
4
	"encoding/json"
5
	"math/big"
Sietse Ringers's avatar
Sietse Ringers committed
6
7
	"testing"

8
	"github.com/mhe/gabi"
9
	"github.com/privacybydesign/irmago"
Koen van Ingen's avatar
Koen van Ingen committed
10
	"github.com/privacybydesign/irmago/internal/test"
11
	"github.com/stretchr/testify/require"
12
13
)

14
15
16
// Create a ManualTestHandler for unit tests
func createManualSessionHandler(t *testing.T, client *Client) *ManualTestHandler {
	return &ManualTestHandler{
17
18
19
20
21
		TestHandler: TestHandler{
			t:      t,
			c:      make(chan *SessionResult),
			client: client,
		},
22
23
24
	}
}

25
func manualSessionHelper(t *testing.T, client *Client, h *ManualTestHandler, request string, verifyAs string, corrupt bool) *irma.VerificationResult {
26
27
28
29
	init := client == nil
	if init {
		client = parseStorage(t)
	}
30

31
	client.NewSession(request, h)
32

33
34
35
	result := <-h.c
	if result.Err != nil {
		require.NoError(t, result.Err)
36
37
	}

38
39
40
41
42
43
44
	var verifyasRequest *irma.SignatureRequest
	if verifyAs != "" {
		verifyasRequest = &irma.SignatureRequest{}
		err := json.Unmarshal([]byte(verifyAs), verifyasRequest)
		require.NoError(t, err)
	}

45
46
47
48
	if corrupt {
		// Interesting: modifying C results in INVALID_CRYPTO; modifying A or an attribute results in INVALID_TIMESTAMP
		i := result.Result.Signature[0].(*gabi.ProofD).C
		i.Add(i, big.NewInt(16))
49
	}
50
	return result.Result.Verify(client.Configuration, verifyasRequest)
51
}
52

53
54
55
func TestManualSession(t *testing.T) {
	request := "{\"nonce\": 42, \"context\": 1337, \"type\": \"signing\", \"message\":\"I owe you everything\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
	ms := createManualSessionHandler(t, nil)
56

57
	result := manualSessionHelper(t, nil, ms, request, request, false)
58
59
	require.Equal(t, irma.ProofStatusValid, result.Status)
	require.Equal(t, irma.AttributeProofStatusPresent, result.Attributes[0].Status)
60

61
62
63
	result = manualSessionHelper(t, nil, ms, request, "", false)
	require.Equal(t, irma.ProofStatusValid, result.Status)
	require.Equal(t, irma.AttributeProofStatusExtra, result.Attributes[0].Status)
64
65
66
67
68
69

	test.ClearTestStorage(t)
}

// Test if proof verification fails with status 'ERROR_CRYPTO' if we verify it with an invalid nonce
func TestManualSessionInvalidNonce(t *testing.T) {
70
71
	request := "{\"nonce\": 0, \"context\": 0, \"type\": \"signing\", \"message\":\"I owe you everything\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
	invalidRequest := "{\"nonce\": 1, \"context\": 0, \"type\": \"signing\", \"message\":\"I owe you everything\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
72
73
	ms := createManualSessionHandler(t, nil)
	result := manualSessionHelper(t, nil, ms, request, invalidRequest, false)
74

75
	require.Equal(t, irma.ProofStatusUnmatchedRequest, result.Status)
76

77
	test.ClearTestStorage(t)
78
79
}

80
81
// Test if proof verification fails with status 'MISSING_ATTRIBUTES' if we provide it with a non-matching signature request
func TestManualSessionInvalidRequest(t *testing.T) {
82
83
	request := "{\"nonce\": 0, \"context\": 0, \"type\": \"signing\", \"message\":\"I owe you everything\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
	invalidRequest := "{\"nonce\": 0, \"context\": 0, \"type\": \"signing\", \"message\":\"I owe you everything\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.university\"]}]}"
84
85
	ms := createManualSessionHandler(t, nil)
	result := manualSessionHelper(t, nil, ms, request, invalidRequest, false)
86

87
	require.Equal(t, irma.ProofStatusMissingAttributes, result.Status)
88
	// First attribute result is MISSING, because it is in the request but not disclosed
89
	require.Equal(t, irma.AttributeProofStatusMissing, result.Attributes[0].Status)
90
	// Second attribute result is EXTRA, since it is disclosed, but not matching the sigrequest
91
	require.Equal(t, irma.AttributeProofStatusExtra, result.Attributes[1].Status)
92

93
94
95
96
97
	test.ClearTestStorage(t)
}

// Test if proof verification fails with status 'MISSING_ATTRIBUTES' if we provide it with invalid attribute values
func TestManualSessionInvalidAttributeValue(t *testing.T) {
98
99
	request := "{\"nonce\": 0, \"context\": 0, \"type\": \"signing\", \"message\":\"I owe you everything\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":{\"irma-demo.RU.studentCard.studentID\": \"456\"}}]}"
	invalidRequest := "{\"nonce\": 0, \"context\": 0, \"type\": \"signing\", \"message\":\"I owe you everything\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":{\"irma-demo.RU.studentCard.studentID\": \"123\"}}]}"
100
101
	ms := createManualSessionHandler(t, nil)
	result := manualSessionHelper(t, nil, ms, request, invalidRequest, false)
102

103
104
	require.Equal(t, irma.ProofStatusMissingAttributes, result.Status)
	require.Equal(t, irma.AttributeProofStatusInvalidValue, result.Attributes[0].Status)
105
106
107
108
109

	test.ClearTestStorage(t)
}

func TestManualKeyShareSession(t *testing.T) {
110
	request := "{\"nonce\": 0, \"context\": 0, \"type\": \"signing\", \"message\":\"I owe you everything\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"test.test.mijnirma.email\"]}]}"
111
	ms := createManualSessionHandler(t, nil)
112

113
	result := manualSessionHelper(t, nil, ms, request, request, false)
114
	require.Equal(t, irma.ProofStatusValid, result.Status)
115

116
117
	result = manualSessionHelper(t, nil, ms, request, "", false)
	require.Equal(t, irma.ProofStatusValid, result.Status)
118

119
	test.ClearTestStorage(t)
120
121
122
}

func TestManualSessionMultiProof(t *testing.T) {
123
	client := parseStorage(t)
124

125
	// First, we need to issue an extra credential (BSN)
126
127
	jwtcontents := getIssuanceJwt("testip", true, "")
	sessionHelper(t, jwtcontents, "issue", client)
128
129

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

132
	ms := createManualSessionHandler(t, client)
133

134
	result := manualSessionHelper(t, client, ms, request, request, false)
135
136
137
	require.Equal(t, irma.ProofStatusValid, result.Status)
	require.Equal(t, irma.AttributeProofStatusPresent, result.Attributes[0].Status)
	require.Equal(t, irma.AttributeProofStatusPresent, result.Attributes[1].Status)
138

139
140
141
142
	result = manualSessionHelper(t, client, ms, request, "", false)
	require.Equal(t, irma.ProofStatusValid, result.Status)
	require.Equal(t, irma.AttributeProofStatusExtra, result.Attributes[0].Status)
	require.Equal(t, irma.AttributeProofStatusExtra, result.Attributes[1].Status)
143

144
	test.ClearTestStorage(t)
145
146
}

147
func TestManualSessionInvalidProof(t *testing.T) {
148
	request := "{\"nonce\": 0, \"context\": 0, \"type\": \"signing\", \"message\":\"I owe you everything\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
149
150
	ms := createManualSessionHandler(t, nil)
	result := manualSessionHelper(t, nil, ms, request, request, true)
151

152
	require.Equal(t, irma.ProofStatusInvalidCrypto, result.Status)
153

154
	test.ClearTestStorage(t)
155
}