manual_session_test.go 8.97 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
	switch h.action {
	case irma.ActionDisclosing:
		verifyasRequest := &irma.DisclosureRequest{}
41
42
		err := json.Unmarshal([]byte(verifyAs), verifyasRequest)
		require.NoError(t, err)
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
		return irma.ProofList(result.VerificationResult).Verify(client.Configuration, verifyasRequest)
	case irma.ActionSigning:
		var verifyasRequest *irma.SignatureRequest
		if verifyAs != "" {
			verifyasRequest = &irma.SignatureRequest{}
			err := json.Unmarshal([]byte(verifyAs), verifyasRequest)
			require.NoError(t, err)
		}

		if corrupt {
			// Interesting: modifying C results in INVALID_CRYPTO; modifying A or an attribute results in INVALID_TIMESTAMP
			i := result.SignatureResult.Signature[0].(*gabi.ProofD).C
			i.Add(i, big.NewInt(16))
		}
		return result.SignatureResult.Verify(client.Configuration, verifyasRequest)
	default:
		return nil
60
	}
61
}
62

63
64
65
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)
66

67
	result := manualSessionHelper(t, nil, ms, request, request, false)
68
69
	require.Equal(t, irma.ProofStatusValid, result.Status)
	require.Equal(t, irma.AttributeProofStatusPresent, result.Attributes[0].Status)
70

71
72
73
	result = manualSessionHelper(t, nil, ms, request, "", false)
	require.Equal(t, irma.ProofStatusValid, result.Status)
	require.Equal(t, irma.AttributeProofStatusExtra, result.Attributes[0].Status)
74
75
76
77
78
79

	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) {
80
81
	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\"]}]}"
82
83
	ms := createManualSessionHandler(t, nil)
	result := manualSessionHelper(t, nil, ms, request, invalidRequest, false)
84

85
	require.Equal(t, irma.ProofStatusUnmatchedRequest, result.Status)
86

87
	test.ClearTestStorage(t)
88
89
}

90
91
// Test if proof verification fails with status 'MISSING_ATTRIBUTES' if we provide it with a non-matching signature request
func TestManualSessionInvalidRequest(t *testing.T) {
92
93
	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\"]}]}"
94
95
	ms := createManualSessionHandler(t, nil)
	result := manualSessionHelper(t, nil, ms, request, invalidRequest, false)
96

97
	require.Equal(t, irma.ProofStatusMissingAttributes, result.Status)
98
	// First attribute result is MISSING, because it is in the request but not disclosed
99
	require.Equal(t, irma.AttributeProofStatusMissing, result.Attributes[0].Status)
100
	// Second attribute result is EXTRA, since it is disclosed, but not matching the sigrequest
101
	require.Equal(t, irma.AttributeProofStatusExtra, result.Attributes[1].Status)
102

103
104
105
106
107
	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) {
108
109
	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\"}}]}"
110
111
	ms := createManualSessionHandler(t, nil)
	result := manualSessionHelper(t, nil, ms, request, invalidRequest, false)
112

113
114
	require.Equal(t, irma.ProofStatusMissingAttributes, result.Status)
	require.Equal(t, irma.AttributeProofStatusInvalidValue, result.Attributes[0].Status)
115
116
117
118
119

	test.ClearTestStorage(t)
}

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

123
	result := manualSessionHelper(t, nil, ms, request, request, false)
124
	require.Equal(t, irma.ProofStatusValid, result.Status)
125

126
127
	result = manualSessionHelper(t, nil, ms, request, "", false)
	require.Equal(t, irma.ProofStatusValid, result.Status)
128

129
	test.ClearTestStorage(t)
130
131
132
}

func TestManualSessionMultiProof(t *testing.T) {
133
	client := parseStorage(t)
134

135
	// First, we need to issue an extra credential (BSN)
136
137
	jwtcontents := getIssuanceJwt("testip", true, "")
	sessionHelper(t, jwtcontents, "issue", client)
138
139

	// Request to sign with both BSN and StudentID
140
	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\"]}]}"
141

142
	ms := createManualSessionHandler(t, client)
143

144
	result := manualSessionHelper(t, client, ms, request, request, false)
145
146
147
	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)
148

149
150
151
152
	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)
153

154
	test.ClearTestStorage(t)
155
156
}

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

162
	require.Equal(t, irma.ProofStatusInvalidCrypto, result.Status)
163

164
	test.ClearTestStorage(t)
165
}
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

func TestManualDisclosureSession(t *testing.T) {
	request := "{\"nonce\": 0, \"context\": 0, \"type\": \"disclosing\", \"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
	ms := createManualSessionHandler(t, nil)
	result := manualSessionHelper(t, nil, ms, request, request, false)

	require.Equal(t, irma.AttributeProofStatusPresent, result.Attributes[0].Status)
	require.Equal(t, "456", result.Attributes[0].Value["en"])
	require.Equal(t, irma.ProofStatusValid, result.Status)

	test.ClearTestStorage(t)
}

// Test if proof verification fails with status 'MISSING_ATTRIBUTES' if we provide it with a non-matching disclosure request
func TestManualDisclosureSessionInvalidRequest(t *testing.T) {
	request := "{\"nonce\": 0, \"context\": 0, \"type\": \"disclosing\", \"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
	invalidRequest := "{\"nonce\": 0, \"context\": 0, \"type\": \"disclosing\", \"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.university\"]}]}"
	ms := createManualSessionHandler(t, nil)
	result := manualSessionHelper(t, nil, ms, request, invalidRequest, false)

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

	test.ClearTestStorage(t)
}