requestor_test.go 5.29 KB
Newer Older
1 2 3 4 5 6 7 8
package sessiontest

import (
	"encoding/json"
	"testing"

	"github.com/privacybydesign/irmago"
	"github.com/privacybydesign/irmago/internal/test"
Sietse Ringers's avatar
Sietse Ringers committed
9
	"github.com/privacybydesign/irmago/server"
10 11 12
	"github.com/stretchr/testify/require"
)

Sietse Ringers's avatar
Sietse Ringers committed
13
func requestorSessionHelper(t *testing.T, request irma.SessionRequest) *server.SessionResult {
Sietse Ringers's avatar
Sietse Ringers committed
14 15
	StartIrmaServer(t)
	defer StopIrmaServer()
16

17
	client, _ := parseStorage(t)
18 19
	defer test.ClearTestStorage(t)

20
	clientChan := make(chan *SessionResult)
Sietse Ringers's avatar
Sietse Ringers committed
21
	serverChan := make(chan *server.SessionResult)
22

23
	qr, token, err := irmaServer.StartSession(request, func(result *server.SessionResult) {
24 25 26 27
		serverChan <- result
	})
	require.NoError(t, err)

28
	h := TestHandler{t, clientChan, client, nil}
29 30 31 32 33 34 35 36 37
	j, err := json.Marshal(qr)
	require.NoError(t, err)
	client.NewSession(string(j), h)
	clientResult := <-clientChan
	if clientResult != nil {
		require.NoError(t, clientResult.Err)
	}

	serverResult := <-serverChan
38

39 40 41 42
	require.Equal(t, token, serverResult.Token)
	return serverResult
}

Sietse Ringers's avatar
Sietse Ringers committed
43
func TestRequestorSignatureSession(t *testing.T) {
44
	id := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
Sietse Ringers's avatar
Sietse Ringers committed
45
	serverResult := requestorSessionHelper(t, &irma.SignatureRequest{
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
		Message: "message",
		DisclosureRequest: irma.DisclosureRequest{
			BaseRequest: irma.BaseRequest{Type: irma.ActionSigning},
			Content: irma.AttributeDisjunctionList([]*irma.AttributeDisjunction{{
				Label:      "foo",
				Attributes: []irma.AttributeTypeIdentifier{id},
			}}),
		},
	})

	require.Nil(t, serverResult.Err)
	require.Equal(t, irma.ProofStatusValid, serverResult.ProofStatus)
	require.NotEmpty(t, serverResult.Disclosed)
	require.Equal(t, id, serverResult.Disclosed[0].Identifier)
	require.Equal(t, "456", serverResult.Disclosed[0].Value["en"])
}

Sietse Ringers's avatar
Sietse Ringers committed
63
func TestRequestorDisclosureSession(t *testing.T) {
64
	id := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
65
	request := &irma.DisclosureRequest{
66 67 68 69 70
		BaseRequest: irma.BaseRequest{Type: irma.ActionDisclosing},
		Content: irma.AttributeDisjunctionList([]*irma.AttributeDisjunction{{
			Label:      "foo",
			Attributes: []irma.AttributeTypeIdentifier{id},
		}}),
71 72 73
	}
	serverResult := testRequestorDisclosure(t, request)
	require.Len(t, serverResult.Disclosed, 1)
74 75
	require.Equal(t, id, serverResult.Disclosed[0].Identifier)
	require.Equal(t, "456", serverResult.Disclosed[0].Value["en"])
76
}
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
func TestRequestorDisclosureMultipleAttrs(t *testing.T) {
	request := &irma.DisclosureRequest{
		BaseRequest: irma.BaseRequest{Type: irma.ActionDisclosing},
		Content: irma.AttributeDisjunctionList([]*irma.AttributeDisjunction{{
			Label:      "foo",
			Attributes: []irma.AttributeTypeIdentifier{irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")},
		}, {
			Label:      "bar",
			Attributes: []irma.AttributeTypeIdentifier{irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.level")},
		}}),
	}
	serverResult := testRequestorDisclosure(t, request)
	require.Len(t, serverResult.Disclosed, 2)
}

func testRequestorDisclosure(t *testing.T, request *irma.DisclosureRequest) *server.SessionResult {
	serverResult := requestorSessionHelper(t, request)
	require.Nil(t, serverResult.Err)
	require.Equal(t, irma.ProofStatusValid, serverResult.ProofStatus)
	return serverResult
98 99
}

Sietse Ringers's avatar
Sietse Ringers committed
100
func TestRequestorIssuanceSession(t *testing.T) {
101 102 103
	testRequestorIssuance(t, false)
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
func TestRequestorCombinedSessionMultipleAttributes(t *testing.T) {
	var ir irma.IssuanceRequest
	require.NoError(t, irma.UnmarshalValidate([]byte(`{
		"type":"issuing",
		"credentials": [
			{
				"credential":"irma-demo.MijnOverheid.root",
				"attributes" : {
					"BSN":"12345"
				}
			}
		],
		"disclose" : [
			{
				"label":"Initialen",
				"attributes":["irma-demo.RU.studentCard.studentCardNumber"]
			},
			{
				"label":"Achternaam",
				"attributes" : ["irma-demo.RU.studentCard.studentID"]
			},
			{
				"label":"Geboortedatum",
				"attributes":["irma-demo.RU.studentCard.university"]
			}
		]
	}`), &ir))

	require.Equal(t, server.StatusDone, requestorSessionHelper(t, &ir).Status)
}

135
func testRequestorIssuance(t *testing.T, keyshare bool) {
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	attrid := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
	request := &irma.IssuanceRequest{
		BaseRequest: irma.BaseRequest{Type: irma.ActionIssuing},
	}
	request.Credentials = []*irma.CredentialRequest{{
		CredentialTypeID: irma.NewCredentialTypeIdentifier("irma-demo.RU.studentCard"),
		Attributes: map[string]string{
			"university":        "Radboud",
			"studentCardNumber": "31415927",
			"studentID":         "s1234567",
			"level":             "42",
		},
	}, {
		CredentialTypeID: irma.NewCredentialTypeIdentifier("irma-demo.MijnOverheid.root"),
		Attributes: map[string]string{
			"BSN": "299792458",
		},
	}}
154 155 156 157 158 159
	if keyshare {
		request.Credentials = append(request.Credentials, &irma.CredentialRequest{
			CredentialTypeID: irma.NewCredentialTypeIdentifier("test.test.mijnirma"),
			Attributes:       map[string]string{"email": "testusername"},
		})
	}
160 161 162 163 164
	request.Disclose = []*irma.AttributeDisjunction{{
		Label:      "foo",
		Attributes: []irma.AttributeTypeIdentifier{attrid},
	}}

Sietse Ringers's avatar
Sietse Ringers committed
165
	result := requestorSessionHelper(t, request)
166 167 168 169 170 171
	require.Nil(t, result.Err)
	require.Equal(t, irma.ProofStatusValid, result.ProofStatus)
	require.NotEmpty(t, result.Disclosed)
	require.Equal(t, attrid, result.Disclosed[0].Identifier)
	require.Equal(t, "456", result.Disclosed[0].Value["en"])
}