Commit cf4ba926 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Move all session integration tests to new internal package

parent 6ca06e1e
package irmaclient
package sessiontest
import (
"encoding/json"
......@@ -7,6 +7,7 @@ import (
"github.com/mhe/gabi"
"github.com/pkg/errors"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/irmaclient"
)
type TestClientHandler struct {
......@@ -62,7 +63,7 @@ func (i *TestClientHandler) ChangePinBlocked(manager irma.SchemeManagerIdentifie
type TestHandler struct {
t *testing.T
c chan *SessionResult
client *Client
client *irmaclient.Client
}
func (th TestHandler) KeyshareEnrollmentIncomplete(manager irma.SchemeManagerIdentifier) {
......@@ -97,7 +98,7 @@ func (th TestHandler) UnsatisfiableRequest(serverName string, missing irma.Attri
ErrorType: irma.ErrorType("UnsatisfiableRequest"),
})
}
func (th TestHandler) RequestVerificationPermission(request irma.DisclosureRequest, ServerName string, callback PermissionHandler) {
func (th TestHandler) RequestVerificationPermission(request irma.DisclosureRequest, ServerName string, callback irmaclient.PermissionHandler) {
choice := &irma.DisclosureChoice{
Attributes: []*irma.AttributeIdentifier{},
}
......@@ -111,20 +112,20 @@ func (th TestHandler) RequestVerificationPermission(request irma.DisclosureReque
}
callback(true, choice)
}
func (th TestHandler) RequestIssuancePermission(request irma.IssuanceRequest, ServerName string, callback PermissionHandler) {
func (th TestHandler) RequestIssuancePermission(request irma.IssuanceRequest, ServerName string, callback irmaclient.PermissionHandler) {
dreq := irma.DisclosureRequest{
BaseRequest: request.BaseRequest,
Content: request.Disclose,
}
th.RequestVerificationPermission(dreq, ServerName, callback)
}
func (th TestHandler) RequestSignaturePermission(request irma.SignatureRequest, ServerName string, callback PermissionHandler) {
func (th TestHandler) RequestSignaturePermission(request irma.SignatureRequest, ServerName string, callback irmaclient.PermissionHandler) {
th.RequestVerificationPermission(request.DisclosureRequest, ServerName, callback)
}
func (th TestHandler) RequestSchemeManagerPermission(manager *irma.SchemeManager, callback func(proceed bool)) {
callback(true)
}
func (th TestHandler) RequestPin(remainingAttempts int, callback PinHandler) {
func (th TestHandler) RequestPin(remainingAttempts int, callback irmaclient.PinHandler) {
callback(true, "12345")
}
......@@ -175,10 +176,10 @@ func (th *ManualTestHandler) Success(result string) {
th.c <- retval
}
func (th *ManualTestHandler) RequestSignaturePermission(request irma.SignatureRequest, requesterName string, ph PermissionHandler) {
func (th *ManualTestHandler) RequestSignaturePermission(request irma.SignatureRequest, requesterName string, ph irmaclient.PermissionHandler) {
th.RequestVerificationPermission(request.DisclosureRequest, requesterName, ph)
}
func (th *ManualTestHandler) RequestIssuancePermission(request irma.IssuanceRequest, issuerName string, ph PermissionHandler) {
func (th *ManualTestHandler) RequestIssuancePermission(request irma.IssuanceRequest, issuerName string, ph irmaclient.PermissionHandler) {
ph(true, nil)
}
......@@ -186,7 +187,7 @@ func (th *ManualTestHandler) RequestIssuancePermission(request irma.IssuanceRequ
func (th *ManualTestHandler) RequestSchemeManagerPermission(manager *irma.SchemeManager, callback func(proceed bool)) {
th.Failure(&irma.SessionError{Err: errors.New("Unexpected session type")})
}
func (th *ManualTestHandler) RequestVerificationPermission(request irma.DisclosureRequest, verifierName string, ph PermissionHandler) {
func (th *ManualTestHandler) RequestVerificationPermission(request irma.DisclosureRequest, verifierName string, ph irmaclient.PermissionHandler) {
var attributes []*irma.AttributeIdentifier
for _, cand := range request.Candidates {
attributes = append(attributes, cand[0])
......
// This package just contains tests.
package sessiontest
package sessiontest
import (
"os"
"path/filepath"
"testing"
"github.com/privacybydesign/irmago/internal/fs"
"github.com/privacybydesign/irmago/internal/test"
"github.com/privacybydesign/irmago/irmaclient"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
// Create HTTP server for scheme managers
test.StartSchemeManagerHttpServer()
test.ClearTestStorage(nil)
test.CreateTestStorage(nil)
retCode := m.Run()
test.ClearTestStorage(nil)
test.StopSchemeManagerHttpServer()
os.Exit(retCode)
}
func parseStorage(t *testing.T) *irmaclient.Client {
path := test.FindTestdataFolder(t)
require.NoError(t, fs.CopyDirectory(filepath.Join(path, "teststorage"), filepath.Join(path, "storage", "test")))
client, err := irmaclient.New(
filepath.Join(path, "storage", "test"),
filepath.Join(path, "irma_configuration"),
"",
&TestClientHandler{t: t},
)
require.NoError(t, err)
return client
}
package irmaclient
package sessiontest
import (
"encoding/json"
......@@ -8,11 +8,12 @@ import (
"github.com/mhe/gabi"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/test"
"github.com/privacybydesign/irmago/irmaclient"
"github.com/stretchr/testify/require"
)
// Create a ManualTestHandler for unit tests
func createManualSessionHandler(t *testing.T, client *Client) *ManualTestHandler {
func createManualSessionHandler(t *testing.T, client *irmaclient.Client) *ManualTestHandler {
return &ManualTestHandler{
TestHandler: TestHandler{
t: t,
......@@ -22,7 +23,7 @@ func createManualSessionHandler(t *testing.T, client *Client) *ManualTestHandler
}
}
func manualSessionHelper(t *testing.T, client *Client, h *ManualTestHandler, request string, verifyAs string, corrupt bool) ([]*irma.DisclosedAttribute, irma.ProofStatus) {
func manualSessionHelper(t *testing.T, client *irmaclient.Client, h *ManualTestHandler, request string, verifyAs string, corrupt bool) ([]*irma.DisclosedAttribute, irma.ProofStatus) {
init := client == nil
if init {
client = parseStorage(t)
......
package irmaclient
package sessiontest
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
......@@ -13,6 +12,7 @@ import (
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/fs"
"github.com/privacybydesign/irmago/internal/test"
"github.com/privacybydesign/irmago/irmaclient"
"github.com/stretchr/testify/require"
)
......@@ -127,7 +127,7 @@ func startSession(request interface{}, url string) (*irma.Qr, error) {
return &response, nil
}
func sessionHelper(t *testing.T, jwtcontents interface{}, url string, client *Client) {
func sessionHelper(t *testing.T, jwtcontents interface{}, url string, client *irmaclient.Client) {
init := client == nil
if init {
client = parseStorage(t)
......@@ -151,7 +151,9 @@ func sessionHelper(t *testing.T, jwtcontents interface{}, url string, client *Cl
c := make(chan *SessionResult)
h := TestHandler{t, c, client}
client.newQrSession(qr, h)
j, err := json.Marshal(qr)
require.NoError(t, err)
client.NewSession(string(j), h)
if result := <-c; result != nil {
require.NoError(t, result.Err)
......@@ -162,7 +164,7 @@ func sessionHelper(t *testing.T, jwtcontents interface{}, url string, client *Cl
}
}
func keyshareSessions(t *testing.T, client *Client) {
func keyshareSessions(t *testing.T, client *irmaclient.Client) {
id := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
expiry := irma.Timestamp(irma.NewMetadataAttribute(0).Expiry())
credid := irma.NewCredentialTypeIdentifier("test.test.mijnirma")
......@@ -263,10 +265,6 @@ func TestLargeAttribute(t *testing.T) {
jwtcontents := getIssuanceJwt("testip", false, "1234567890123456789012345678901234567890") // 40 chars
sessionHelper(t, jwtcontents, "issue", client)
cred, err := client.credential(irma.NewCredentialTypeIdentifier("irma-demo.RU.studentCard"), 0)
require.NoError(t, err)
require.True(t, cred.Signature.Verify(cred.Pk, cred.Attributes))
jwtcontents = getDisclosureJwt("testsp", irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.university"))
sessionHelper(t, jwtcontents, "verification", client)
......@@ -278,13 +276,15 @@ func TestIssuanceSingletonCredential(t *testing.T) {
jwtcontents := getIssuanceJwt("testip", true, "")
credid := irma.NewCredentialTypeIdentifier("irma-demo.MijnOverheid.root")
require.Len(t, client.attrs(credid), 0)
require.Nil(t, client.Attributes(credid, 0))
sessionHelper(t, jwtcontents, "issue", client)
require.Len(t, client.attrs(credid), 1)
require.NotNil(t, client.Attributes(credid, 0))
require.Nil(t, client.Attributes(credid, 1))
sessionHelper(t, jwtcontents, "issue", client)
require.Len(t, client.attrs(credid), 1)
require.NotNil(t, client.Attributes(credid, 0))
require.Nil(t, client.Attributes(credid, 1))
}
/* There is an annoying difference between how Java and Go convert big integers to and from
......@@ -317,36 +317,7 @@ func TestAttributeByteEncoding(t *testing.T) {
test.ClearTestStorage(t)
}
// Enroll at a keyshare server and do an issuance, disclosure,
// and issuance session, also using irma-demo credentials deserialized from Android storage
func TestKeyshareEnrollmentAndSessions(t *testing.T) {
client := parseStorage(t)
credtype := irma.NewCredentialTypeIdentifier("test.test.mijnirma")
// Remove existing registration at test keyshare server
require.NoError(t, client.RemoveCredentialByHash(
client.Attributes(credtype, 0).Hash(),
))
require.NoError(t, client.KeyshareRemove(irma.NewSchemeManagerIdentifier("test")))
// Do a new registration session
c := make(chan error) // channel for TestClientHandler to inform us of result
client.handler.(*TestClientHandler).c = c
bytes := make([]byte, 8, 8)
rand.Read(bytes)
require.NoError(t, client.keyshareEnrollWorker(irma.NewSchemeManagerIdentifier("test"), nil, "12345", "en"))
if err := <-c; err != nil {
t.Fatal(err)
}
require.NotNil(t, client.Attributes(credtype, 0))
keyshareSessions(t, client)
test.ClearTestStorage(t)
}
// Use the existing keyshare enrollment and credentials deserialized from Android storage
// Use the existing keyshare enrollment and credentials
// in a keyshare session of each session type.
// Use keyshareuser.sql to enroll the user at the keyshare server.
func TestKeyshareSessions(t *testing.T) {
......@@ -448,7 +419,7 @@ func TestDownloadSchemeManager(t *testing.T) {
require.Contains(t, client.Configuration.Issuers, irma.NewIssuerIdentifier("irma-demo.RU"))
require.Contains(t, client.Configuration.CredentialTypes, irma.NewCredentialTypeIdentifier("irma-demo.RU.studentCard"))
basepath := "../testdata/storage/test/irma_configuration/irma-demo"
basepath := test.FindTestdataFolder(t) + "/storage/test/irma_configuration/irma-demo"
exists, err := fs.PathExists(basepath + "/description.xml")
require.NoError(t, err)
require.True(t, exists)
......
......@@ -31,7 +31,7 @@ var badServer *http.Server
var badServerCount int
func StartSchemeManagerHttpServer() {
path := findTestdataFolder(nil)
path := FindTestdataFolder(nil)
schemeServer = &http.Server{Addr: ":48681", Handler: http.FileServer(http.Dir(path))}
go func() {
schemeServer.ListenAndServe()
......@@ -67,33 +67,34 @@ func StopBadHttpServer() {
badServer.Close()
}
// findTestdataFolder finds the "testdata" folder which is in . or ..
// FindTestdataFolder finds the "testdata" folder which is in . or ..
// depending on which package is calling us.
func findTestdataFolder(t *testing.T) string {
func FindTestdataFolder(t *testing.T) string {
path := "testdata"
exists, err := fs.PathExists(path)
checkError(t, err)
if !exists {
for i := 0; i < 3; i++ {
exists, err := fs.PathExists(path)
checkError(t, err)
if exists {
return path
}
path = filepath.Join("..", path)
}
exists, err = fs.PathExists(path)
checkError(t, err)
if !exists {
checkError(t, errors.New("testdata folder not found"))
}
return path
checkError(t, errors.New("testdata folder not found"))
return ""
}
// ClearTestStorage removes any output from previously run tests to ensure a clean state;
// some of the tests don't like it when there is existing state in storage.
func ClearTestStorage(t *testing.T) {
path := filepath.Join(findTestdataFolder(t), "storage", "test")
path := filepath.Join(FindTestdataFolder(t), "storage", "test")
err := os.RemoveAll(path)
checkError(t, err)
}
func CreateTestStorage(t *testing.T) {
path := filepath.Join(findTestdataFolder(t), "storage")
path := filepath.Join(FindTestdataFolder(t), "storage")
// EnsureDirectoryExists eventually uses mkdir from the OS which is not recursive
// so we have to create the temporary test storage by two function calls.
......@@ -105,7 +106,7 @@ func CreateTestStorage(t *testing.T) {
}
func SetupTestStorage(t *testing.T) {
path := findTestdataFolder(t)
path := FindTestdataFolder(t)
err := fs.CopyDirectory(filepath.Join(path, "teststorage"), filepath.Join(path, "storage", "test"))
checkError(t, err)
}
......@@ -2,6 +2,7 @@ package irmaclient
import (
"encoding/json"
"errors"
"math/big"
"os"
"testing"
......@@ -28,14 +29,14 @@ func TestMain(m *testing.M) {
func parseStorage(t *testing.T) *Client {
require.NoError(t, fs.CopyDirectory("../testdata/teststorage", "../testdata/storage/test"))
manager, err := New(
client, err := New(
"../testdata/storage/test",
"../testdata/irma_configuration",
"",
&TestClientHandler{t: t},
)
require.NoError(t, err)
return manager
return client
}
func verifyClientIsUnmarshaled(t *testing.T, client *Client) {
......@@ -117,69 +118,69 @@ func TestStorageDeserialization(t *testing.T) {
test.ClearTestStorage(t)
}
func disabledTestLogging(t *testing.T) {
client := parseStorage(t)
logs, err := client.Logs()
oldLogLength := len(logs)
require.NoError(t, err)
attrid := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
// Do issuance session
jwt := getCombinedJwt("testip", attrid)
sessionHelper(t, jwt, "issue", client)
logs, err = client.Logs()
require.NoError(t, err)
require.True(t, len(logs) == oldLogLength+1)
entry := logs[len(logs)-1]
require.NotNil(t, entry)
//require.Equal(t, "testip", entry.Request.GetRequestorName())
require.NoError(t, err)
issued, err := entry.GetIssuedCredentials(client.Configuration)
require.NoError(t, err)
require.NotNil(t, issued)
disclosed, err := entry.GetDisclosedCredentials(client.Configuration)
require.NoError(t, err)
require.NotEmpty(t, disclosed)
// Do disclosure session
jwt = getDisclosureJwt("testsp", attrid)
sessionHelper(t, jwt, "verification", client)
logs, err = client.Logs()
require.NoError(t, err)
require.True(t, len(logs) == oldLogLength+2)
entry = logs[len(logs)-1]
require.NotNil(t, entry)
//require.Equal(t, "testsp", entry.Request.GetRequestorName())
require.NoError(t, err)
disclosed, err = entry.GetDisclosedCredentials(client.Configuration)
require.NoError(t, err)
require.NotEmpty(t, disclosed)
// Do signature session
jwt = getSigningJwt("testsigclient", attrid)
sessionHelper(t, jwt, "signature", client)
logs, err = client.Logs()
require.NoError(t, err)
require.True(t, len(logs) == oldLogLength+3)
entry = logs[len(logs)-1]
require.NotNil(t, entry)
//require.Equal(t, "testsigclient", entry.Request.GetRequestorName())
require.NoError(t, err)
sig, err := entry.GetSignedMessage()
require.NoError(t, err)
require.NotNil(t, sig)
attrs, status := sig.Verify(client.Configuration, nil)
require.Equal(t, irma.ProofStatusValid, status)
require.NotEmpty(t, attrs)
require.Equal(t, attrs[0].Identifier, attrid)
require.Equal(t, "s1234567", attrs[0].Value["en"])
test.ClearTestStorage(t)
}
//func disabledTestLogging(t *testing.T) {
// client := parseStorage(t)
//
// logs, err := client.Logs()
// oldLogLength := len(logs)
// require.NoError(t, err)
// attrid := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
//
// // Do issuance session
// jwt := getCombinedJwt("testip", attrid)
// sessionHelper(t, jwt, "issue", client)
//
// logs, err = client.Logs()
// require.NoError(t, err)
// require.True(t, len(logs) == oldLogLength+1)
//
// entry := logs[len(logs)-1]
// require.NotNil(t, entry)
// //require.Equal(t, "testip", entry.Request.GetRequestorName())
// require.NoError(t, err)
// issued, err := entry.GetIssuedCredentials(client.Configuration)
// require.NoError(t, err)
// require.NotNil(t, issued)
// disclosed, err := entry.GetDisclosedCredentials(client.Configuration)
// require.NoError(t, err)
// require.NotEmpty(t, disclosed)
//
// // Do disclosure session
// jwt = getDisclosureJwt("testsp", attrid)
// sessionHelper(t, jwt, "verification", client)
// logs, err = client.Logs()
// require.NoError(t, err)
// require.True(t, len(logs) == oldLogLength+2)
//
// entry = logs[len(logs)-1]
// require.NotNil(t, entry)
// //require.Equal(t, "testsp", entry.Request.GetRequestorName())
// require.NoError(t, err)
// disclosed, err = entry.GetDisclosedCredentials(client.Configuration)
// require.NoError(t, err)
// require.NotEmpty(t, disclosed)
//
// // Do signature session
// jwt = getSigningJwt("testsigclient", attrid)
// sessionHelper(t, jwt, "signature", client)
// logs, err = client.Logs()
// require.NoError(t, err)
// require.True(t, len(logs) == oldLogLength+3)
// entry = logs[len(logs)-1]
// require.NotNil(t, entry)
// //require.Equal(t, "testsigclient", entry.Request.GetRequestorName())
// require.NoError(t, err)
// sig, err := entry.GetSignedMessage()
// require.NoError(t, err)
// require.NotNil(t, sig)
// attrs, status := sig.Verify(client.Configuration, nil)
// require.Equal(t, irma.ProofStatusValid, status)
// require.NotEmpty(t, attrs)
// require.Equal(t, attrs[0].Identifier, attrid)
// require.Equal(t, "s1234567", attrs[0].Value["en"])
//
// test.ClearTestStorage(t)
//}
// TestCandidates tests the correctness of the function of the client that, given a disjunction of attributes
// requested by the verifier, calculates a list of candidate attributes contained by the client that would
......@@ -334,3 +335,55 @@ func TestKeyshareChangePin(t *testing.T) {
test.ClearTestStorage(t)
}
// ------
type TestClientHandler struct {
t *testing.T
c chan error
}
func (i *TestClientHandler) UpdateConfiguration(new *irma.IrmaIdentifierSet) {}
func (i *TestClientHandler) UpdateAttributes() {}
func (i *TestClientHandler) EnrollmentSuccess(manager irma.SchemeManagerIdentifier) {
select {
case i.c <- nil: // nop
default: // nop
}
}
func (i *TestClientHandler) EnrollmentFailure(manager irma.SchemeManagerIdentifier, err error) {
select {
case i.c <- err: // nop
default:
i.t.Fatal(err)
}
}
func (i *TestClientHandler) ChangePinSuccess(manager irma.SchemeManagerIdentifier) {
select {
case i.c <- nil: // nop
default: // nop
}
}
func (i *TestClientHandler) ChangePinFailure(manager irma.SchemeManagerIdentifier, err error) {
select {
case i.c <- err: //nop
default:
i.t.Fatal(err)
}
}
func (i *TestClientHandler) ChangePinIncorrect(manager irma.SchemeManagerIdentifier, attempts int) {
err := errors.New("incorrect pin")
select {
case i.c <- err: //nop
default:
i.t.Fatal(err)
}
}
func (i *TestClientHandler) ChangePinBlocked(manager irma.SchemeManagerIdentifier, timeout int) {
err := errors.New("blocked account")
select {
case i.c <- err: //nop
default:
i.t.Fatal(err)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment