Commit 6ed6d237 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Generalize session tests to all currently supported servers

parent daaef1f9
......@@ -15,7 +15,8 @@ func TestMain(m *testing.M) {
test.StartSchemeManagerHttpServer()
defer test.StopSchemeManagerHttpServer()
StartIrmaJwtServer()
TestType = "irmaserver-jwt"
StartIrmaServer(JwtServerConfiguration)
defer StopIrmaServer()
test.CreateTestStorage(nil)
......
package sessiontest
import (
"encoding/json"
"path/filepath"
"testing"
"time"
"github.com/Sirupsen/logrus"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/test"
"github.com/privacybydesign/irmago/server"
"github.com/privacybydesign/irmago/server/irmaserver"
"github.com/stretchr/testify/require"
)
func StartIrmaServer() {
testdata := test.FindTestdataFolder(nil)
var (
logger = logrus.New()
testdata = test.FindTestdataFolder(nil)
)
logger := logrus.New()
func init() {
logger.Level = logrus.WarnLevel
logger.Formatter = &logrus.TextFormatter{}
}
func StartIrmaServer(configuration *irmaserver.Configuration) {
go func() {
err := irmaserver.Start(&irmaserver.Configuration{
Configuration: &server.Configuration{
Logger: logger,
IrmaConfigurationPath: filepath.Join(testdata, "irma_configuration"),
IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
},
Port: 48682,
})
err := irmaserver.Start(configuration)
if err != nil {
panic("Starting server failed: " + err.Error())
}
......@@ -41,83 +34,37 @@ func StopIrmaServer() {
irmaserver.Stop()
}
func StartIrmaJwtServer() {
testdata := test.FindTestdataFolder(nil)
logger := logrus.New()
logger.Level = logrus.WarnLevel
logger.Formatter = &logrus.TextFormatter{}
go func() {
err := irmaserver.Start(&irmaserver.Configuration{
Configuration: &server.Configuration{
Logger: logger,
IrmaConfigurationPath: filepath.Join(testdata, "irma_configuration"),
IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
},
Port: 48682,
AuthenticateRequestors: true,
GlobalPermissions: irmaserver.Permissions{
Disclosing: []string{"*"},
Signing: []string{"*"},
Issuing: []string{"*"},
},
Requestors: map[string]irmaserver.Requestor{
"testrequestor": irmaserver.Requestor{
AuthenticationMethod: irmaserver.AuthenticationMethodPublicKey,
AuthenticationKey: filepath.Join(testdata, "jwtkeys", "testrequestor.pem"),
},
},
PrivateKey: filepath.Join(testdata, "jwtkeys", "sk.pem"),
})
if err != nil {
panic("Starting server failed: " + err.Error())
}
}()
time.Sleep(100 * time.Millisecond) // Give server time to start
}
func serverSessionHelper(t *testing.T, request irma.SessionRequest) *server.SessionResult {
client := parseStorage(t)
defer test.ClearTestStorage(t)
clientChan := make(chan *SessionResult)
transport := irma.NewHTTPTransport("http://localhost:48682")
var qr irma.Qr
err := transport.Post("create", &qr, request)
require.NoError(t, err)
token := qr.URL
qr.URL = "http://localhost:48682/irma/" + 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)
}
var result server.SessionResult
transport.Get("result/"+token, &result)
return &result
var IrmaServerConfiguration = &irmaserver.Configuration{
Configuration: &server.Configuration{
Logger: logger,
IrmaConfigurationPath: filepath.Join(testdata, "irma_configuration"),
IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
},
Port: 48682,
}
func TestIrmaServer(t *testing.T) {
id := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
serverResult := serverSessionHelper(t, &irma.DisclosureRequest{
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"])
var JwtServerConfiguration = &irmaserver.Configuration{
Configuration: &server.Configuration{
Logger: logger,
IrmaConfigurationPath: filepath.Join(testdata, "irma_configuration"),
IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
},
Port: 48682,
AuthenticateRequestors: true,
GlobalPermissions: irmaserver.Permissions{
Disclosing: []string{"*"},
Signing: []string{"*"},
Issuing: []string{"*"},
},
Requestors: map[string]irmaserver.Requestor{
"requestor1": irmaserver.Requestor{
AuthenticationMethod: irmaserver.AuthenticationMethodPublicKey,
AuthenticationKey: filepath.Join(testdata, "jwtkeys", "requestor1.pem"),
},
"requestor2": {
AuthenticationMethod: irmaserver.AuthenticationMethodPSK,
AuthenticationKey: "xa6=*&9?8jeUu5>.f-%rVg`f63pHim",
},
},
PrivateKey: filepath.Join(testdata, "jwtkeys", "sk.pem"),
}
......@@ -2,7 +2,6 @@ package sessiontest
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"testing"
......@@ -109,24 +108,67 @@ func getCombinedIssuanceRequest(id irma.AttributeTypeIdentifier) *irma.IssuanceR
return request
}
func getJwt(t *testing.T, request irma.SessionRequest, url string) string {
var TestType = "irmaserver"
func startSession(t *testing.T, request irma.SessionRequest, sessiontype string) (*irma.Qr, string) {
var qr irma.Qr
var err error
var token string
switch TestType {
case "apiserver":
url := "http://localhost:8088/irma_api_server/api/v2/" + sessiontype
err = irma.NewHTTPTransport(url).Post("", &qr, getJwt(t, request, sessiontype, false))
token = qr.URL
qr.URL = url + "/" + qr.URL
case "irmaserver-jwt":
url := "http://localhost:48682"
err = irma.NewHTTPTransport(url).Post("create", &qr, getJwt(t, request, sessiontype, true))
token = qr.URL
qr.URL = url + "/irma/" + qr.URL
case "irmaserver":
url := "http://localhost:48682"
err = irma.NewHTTPTransport(url).Post("create", &qr, request)
token = qr.URL
qr.URL = url + "/irma/" + qr.URL
default:
t.Fatal("Invalid TestType")
}
require.NoError(t, err)
return &qr, token
}
func getJwt(t *testing.T, request irma.SessionRequest, sessiontype string, signed bool) string {
var jwtcontents irma.RequestorJwt
switch url {
var kid string
switch sessiontype {
case "issue":
kid = "testip"
jwtcontents = irma.NewIdentityProviderJwt("testip", request.(*irma.IssuanceRequest))
case "verification":
kid = "testsp"
jwtcontents = irma.NewServiceProviderJwt("testsp", request.(*irma.DisclosureRequest))
case "signature":
kid = "testsigclient"
jwtcontents = irma.NewSignatureRequestorJwt("testsigclient", request.(*irma.SignatureRequest))
}
skbts, err := ioutil.ReadFile(filepath.Join(test.FindTestdataFolder(t), "jwtkeys", "testrequestor-sk.pem"))
require.NoError(t, err)
sk, err := jwt.ParseRSAPrivateKeyFromPEM(skbts)
require.NoError(t, err)
tok := jwt.NewWithClaims(jwt.SigningMethodRS256, jwtcontents)
tok.Header["kid"] = "testrequestor"
j, err := tok.SignedString(sk)
var j string
var err error
if signed {
skbts, err := ioutil.ReadFile(filepath.Join(test.FindTestdataFolder(t), "jwtkeys", "requestor1-sk.pem"))
require.NoError(t, err)
sk, err := jwt.ParseRSAPrivateKeyFromPEM(skbts)
require.NoError(t, err)
tok := jwt.NewWithClaims(jwt.SigningMethodRS256, jwtcontents)
tok.Header["kid"] = "requestor1"
j, err = tok.SignedString(sk)
} else {
tok := jwt.NewWithClaims(jwt.SigningMethodNone, jwtcontents)
tok.Header["kid"] = kid
j, err = tok.SignedString(jwt.UnsafeAllowNoneSignatureType)
}
require.NoError(t, err)
return j
......@@ -138,19 +180,7 @@ func sessionHelper(t *testing.T, request irma.SessionRequest, sessiontype string
defer test.ClearTestStorage(t)
}
//transport := irma.NewHTTPTransport("http://localhost:48682")
//var qr irma.Qr
//err := transport.Post("create", &qr, request)
//require.NoError(t, err)
//qr.URL = "http://localhost:48682/irma/" + qr.URL
url := "http://localhost:48682"
server := irma.NewHTTPTransport(url)
var qr irma.Qr
err := server.Post("create", &qr, getJwt(t, request, sessiontype))
require.NoError(t, err)
token := qr.URL
qr.URL = url + "/irma/" + qr.URL
qr, _ := startSession(t, request, sessiontype)
c := make(chan *SessionResult)
h := TestHandler{t, c, client}
......@@ -161,13 +191,6 @@ func sessionHelper(t *testing.T, request irma.SessionRequest, sessiontype string
if result := <-c; result != nil {
require.NoError(t, result.Err)
}
bts, err := server.GetBytes("getproof/" + token)
require.NoError(t, err)
fmt.Println(string(bts))
bts, err = server.GetBytes("result/" + token)
require.NoError(t, err)
fmt.Println(string(bts))
}
func keyshareSessions(t *testing.T, client *irmaclient.Client) {
......
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