requestor_test.go 5.41 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"
9
	"github.com/privacybydesign/irmago/irmaclient"
Sietse Ringers's avatar
Sietse Ringers committed
10
	"github.com/privacybydesign/irmago/server"
11 12 13
	"github.com/stretchr/testify/require"
)

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

18 19 20 21
	if client == nil {
		client = parseStorage(t)
		defer test.ClearTestStorage(t)
	}
22

23
	clientChan := make(chan *SessionResult)
Sietse Ringers's avatar
Sietse Ringers committed
24
	serverChan := make(chan *server.SessionResult)
25

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

31
	h := TestHandler{t, clientChan, client, nil}
32 33 34 35 36 37 38 39 40
	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
41

42 43 44 45
	require.Equal(t, token, serverResult.Token)
	return serverResult
}

46 47 48 49 50 51 52 53 54 55 56
// Check that nonexistent IRMA identifiers in the session request fail the session
func TestRequestorInvalidRequest(t *testing.T) {
	StartIrmaServer(t)
	defer StopIrmaServer()
	_, _, err := irmaServer.StartSession(irma.NewDisclosureRequest(
		irma.NewAttributeTypeIdentifier("irma-demo.RU.foo.bar"),
		irma.NewAttributeTypeIdentifier("irma-demo.baz.qux.abc"),
	), nil)
	require.Error(t, err)
}

Sietse Ringers's avatar
Sietse Ringers committed
57
func TestRequestorSignatureSession(t *testing.T) {
58
	id := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
59
	serverResult := requestorSessionHelper(t, irma.NewSignatureRequest("message", id), nil)
60 61 62 63

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

Sietse Ringers's avatar
Sietse Ringers committed
68
func TestRequestorDisclosureSession(t *testing.T) {
69
	id := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
70
	request := irma.NewDisclosureRequest(id)
71 72
	serverResult := testRequestorDisclosure(t, request)
	require.Len(t, serverResult.Disclosed, 1)
73 74
	require.Equal(t, id, serverResult.Disclosed[0][0].Identifier)
	require.Equal(t, "456", serverResult.Disclosed[0][0].Value["en"])
75
}
76

77
func TestRequestorDisclosureMultipleAttrs(t *testing.T) {
78 79 80 81
	request := irma.NewDisclosureRequest(
		irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID"),
		irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.level"),
	)
82 83 84 85 86
	serverResult := testRequestorDisclosure(t, request)
	require.Len(t, serverResult.Disclosed, 2)
}

func testRequestorDisclosure(t *testing.T, request *irma.DisclosureRequest) *server.SessionResult {
87
	serverResult := requestorSessionHelper(t, request, nil)
88 89 90
	require.Nil(t, serverResult.Err)
	require.Equal(t, irma.ProofStatusValid, serverResult.ProofStatus)
	return serverResult
91 92
}

Sietse Ringers's avatar
Sietse Ringers committed
93
func TestRequestorIssuanceSession(t *testing.T) {
94 95 96 97
	testRequestorIssuance(t, false)
}

func testRequestorIssuance(t *testing.T, keyshare bool) {
98
	attrid := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
99
	request := irma.NewIssuanceRequest([]*irma.CredentialRequest{{
100 101 102 103 104 105 106 107 108 109 110 111
		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",
		},
112
	}}, attrid)
113 114 115 116 117 118
	if keyshare {
		request.Credentials = append(request.Credentials, &irma.CredentialRequest{
			CredentialTypeID: irma.NewCredentialTypeIdentifier("test.test.mijnirma"),
			Attributes:       map[string]string{"email": "testusername"},
		})
	}
119

120
	result := requestorSessionHelper(t, request, nil)
121 122 123
	require.Nil(t, result.Err)
	require.Equal(t, irma.ProofStatusValid, result.ProofStatus)
	require.NotEmpty(t, result.Disclosed)
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	require.Equal(t, attrid, result.Disclosed[0][0].Identifier)
	require.Equal(t, "456", result.Disclosed[0][0].Value["en"])
}

func TestConDisCon(t *testing.T) {
	client := parseStorage(t)
	ir := getMultipleIssuanceRequest()
	ir.Credentials = append(ir.Credentials, &irma.CredentialRequest{
		Validity:         ir.Credentials[0].Validity,
		CredentialTypeID: irma.NewCredentialTypeIdentifier("irma-demo.MijnOverheid.fullName"),
		Attributes: map[string]string{
			"firstnames": "Jan Hendrik",
			"firstname":  "Jan",
			"familyname": "Klaassen",
			"prefix":     "van",
		},
	})
	requestorSessionHelper(t, ir, client)

	dr := irma.NewDisclosureRequest()
	dr.Disclose = irma.AttributeConDisCon{
		irma.AttributeDisCon{
			irma.AttributeCon{
				irma.NewAttributeRequest("irma-demo.MijnOverheid.root.BSN"),
				irma.NewAttributeRequest("irma-demo.MijnOverheid.fullName.firstname"),
				irma.NewAttributeRequest("irma-demo.MijnOverheid.fullName.familyname"),
			},
			irma.AttributeCon{
				irma.NewAttributeRequest("irma-demo.RU.studentCard.studentID"),
				irma.NewAttributeRequest("irma-demo.RU.studentCard.university"),
			},
		},
		//irma.AttributeDisCon{
		//	irma.AttributeCon{
		//		irma.NewAttributeRequest("irma-demo.MijnOverheid.fullName.firstname"),
		//		irma.NewAttributeRequest("irma-demo.MijnOverheid.fullName.familyname"),
		//	},
		//},
	}

	requestorSessionHelper(t, dr, client)
165
}