requestor_test.go 5.02 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
}

Sietse Ringers's avatar
Sietse Ringers committed
46
func TestRequestorSignatureSession(t *testing.T) {
47
	id := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
48
	serverResult := requestorSessionHelper(t, irma.NewSignatureRequest("message", id), nil)
49 50 51 52

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

Sietse Ringers's avatar
Sietse Ringers committed
57
func TestRequestorDisclosureSession(t *testing.T) {
58
	id := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
59
	request := irma.NewDisclosureRequest(id)
60 61
	serverResult := testRequestorDisclosure(t, request)
	require.Len(t, serverResult.Disclosed, 1)
62 63
	require.Equal(t, id, serverResult.Disclosed[0][0].Identifier)
	require.Equal(t, "456", serverResult.Disclosed[0][0].Value["en"])
64
}
65

66
func TestRequestorDisclosureMultipleAttrs(t *testing.T) {
67 68 69 70
	request := irma.NewDisclosureRequest(
		irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID"),
		irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.level"),
	)
71 72 73 74 75
	serverResult := testRequestorDisclosure(t, request)
	require.Len(t, serverResult.Disclosed, 2)
}

func testRequestorDisclosure(t *testing.T, request *irma.DisclosureRequest) *server.SessionResult {
76
	serverResult := requestorSessionHelper(t, request, nil)
77 78 79
	require.Nil(t, serverResult.Err)
	require.Equal(t, irma.ProofStatusValid, serverResult.ProofStatus)
	return serverResult
80 81
}

Sietse Ringers's avatar
Sietse Ringers committed
82
func TestRequestorIssuanceSession(t *testing.T) {
83 84 85 86
	testRequestorIssuance(t, false)
}

func testRequestorIssuance(t *testing.T, keyshare bool) {
87
	attrid := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
88
	request := irma.NewIssuanceRequest([]*irma.CredentialRequest{{
89 90 91 92 93 94 95 96 97 98 99 100
		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",
		},
101
	}}, attrid)
102 103 104 105 106 107
	if keyshare {
		request.Credentials = append(request.Credentials, &irma.CredentialRequest{
			CredentialTypeID: irma.NewCredentialTypeIdentifier("test.test.mijnirma"),
			Attributes:       map[string]string{"email": "testusername"},
		})
	}
108

109
	result := requestorSessionHelper(t, request, nil)
110 111 112
	require.Nil(t, result.Err)
	require.Equal(t, irma.ProofStatusValid, result.ProofStatus)
	require.NotEmpty(t, result.Disclosed)
113 114 115 116 117 118 119 120 121 122 123 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
	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)
154
}