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

import (
	"encoding/json"
	"net/http"
	"path/filepath"
	"testing"

	"github.com/Sirupsen/logrus"
	"github.com/privacybydesign/irmago"
11
	"github.com/privacybydesign/irmago/internal/fs"
12
	"github.com/privacybydesign/irmago/internal/test"
Sietse Ringers's avatar
Sietse Ringers committed
13 14
	"github.com/privacybydesign/irmago/server"
	"github.com/privacybydesign/irmago/server/irmarequestor"
15 16 17 18 19
	"github.com/stretchr/testify/require"
)

var irmaServer *http.Server

Sietse Ringers's avatar
Sietse Ringers committed
20
func StartIrmaClientServer(t *testing.T) {
21 22 23 24 25
	testdata := test.FindTestdataFolder(t)

	logger := logrus.New()
	logger.Level = logrus.WarnLevel
	logger.Formatter = &logrus.TextFormatter{}
26 27
	cachepath := filepath.Join(testdata, "storage", "test", "cache")
	fs.EnsureDirectoryExists(cachepath)
Sietse Ringers's avatar
Sietse Ringers committed
28
	require.NoError(t, irmarequestor.Initialize(&server.Configuration{
29 30
		Logger:                logger,
		IrmaConfigurationPath: filepath.Join(testdata, "irma_configuration"),
31
		IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
32
		CachePath:             cachepath,
33 34 35 36 37 38 39 40 41 42
	}))

	mux := http.NewServeMux()
	mux.HandleFunc("/", irmarequestor.HttpHandlerFunc("/"))
	irmaServer = &http.Server{Addr: ":48680", Handler: mux}
	go func() {
		irmaServer.ListenAndServe()
	}()
}

Sietse Ringers's avatar
Sietse Ringers committed
43
func StopIrmaClientServer() {
44 45 46
	irmaServer.Close()
}

Sietse Ringers's avatar
Sietse Ringers committed
47 48 49
func requestorSessionHelper(t *testing.T, request irma.SessionRequest) *server.SessionResult {
	StartIrmaClientServer(t)
	defer StopIrmaClientServer()
50

51
	client := parseStorage(t)
52 53
	defer test.ClearTestStorage(t)

54
	clientChan := make(chan *SessionResult)
Sietse Ringers's avatar
Sietse Ringers committed
55
	serverChan := make(chan *server.SessionResult)
56

Sietse Ringers's avatar
Sietse Ringers committed
57
	qr, token, err := irmarequestor.StartSession(request, func(result *server.SessionResult) {
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
		serverChan <- result
	})
	require.NoError(t, err)
	qr.URL = "http://localhost:48680/" + qr.URL

	h := TestHandler{t, clientChan, client}
	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
73

74 75 76 77
	require.Equal(t, token, serverResult.Token)
	return serverResult
}

Sietse Ringers's avatar
Sietse Ringers committed
78
func TestRequestorSignatureSession(t *testing.T) {
79
	id := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
Sietse Ringers's avatar
Sietse Ringers committed
80
	serverResult := requestorSessionHelper(t, &irma.SignatureRequest{
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
		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
98
func TestRequestorDisclosureSession(t *testing.T) {
99
	id := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
Sietse Ringers's avatar
Sietse Ringers committed
100
	serverResult := requestorSessionHelper(t, &irma.DisclosureRequest{
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
		BaseRequest: irma.BaseRequest{Type: irma.ActionDisclosing},
		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
116
func TestRequestorIssuanceSession(t *testing.T) {
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
	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",
		},
	}, {
		CredentialTypeID: irma.NewCredentialTypeIdentifier("test.test.mijnirma"),
		Attributes:       map[string]string{"email": "testusername"},
	}}
	request.Disclose = []*irma.AttributeDisjunction{{
		Label:      "foo",
		Attributes: []irma.AttributeTypeIdentifier{attrid},
	}}

Sietse Ringers's avatar
Sietse Ringers committed
143
	result := requestorSessionHelper(t, request)
144 145 146 147 148 149
	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"])
}