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

Put unit test helper functions in internal package to make them available to...

Put unit test helper functions in internal package to make them available to unit tests of all packages
parent fc3f518f
// Package test contains functionality that should be available to
// all unit tests (which live in separate packages).
package test
import (
"os"
"path/filepath"
"testing"
"github.com/go-errors/errors"
"github.com/privacybydesign/irmago/internal/fs"
"github.com/stretchr/testify/require"
)
func checkError(t *testing.T, err error) {
if err == nil {
return
}
if t != nil {
require.NoError(t, err)
} else {
panic(err)
}
}
// findTestdataFolder finds the "testdata" folder which is in . or ..
// depending on which package is calling us.
func findTestdataFolder(t *testing.T) string {
path := "testdata"
exists, err := fs.PathExists(path)
checkError(t, err)
if !exists {
path = filepath.Join("..", path)
}
exists, err = fs.PathExists(path)
checkError(t, err)
if !exists {
checkError(t, errors.New("testdata folder not found"))
}
return path
}
// 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")
err := os.RemoveAll(path)
checkError(t, err)
}
func CreateTestStorage(t *testing.T) {
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.
// We ignore any error possibly returned by creating the first one, because if it errors,
// then the second one certainly will as well.
_ = fs.EnsureDirectoryExists(path)
err := fs.EnsureDirectoryExists(filepath.Join(path, "test"))
checkError(t, err)
}
func SetupTestStorage(t *testing.T) {
path := findTestdataFolder(t)
err := fs.CopyDirectory(filepath.Join(path, "teststorage"), filepath.Join(path, "storage", "test"))
checkError(t, err)
}
package irmaclient
import (
"fmt"
"math/big"
"os"
"testing"
......@@ -9,37 +8,15 @@ import (
"github.com/mhe/gabi"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/fs"
"github.com/privacybydesign/irmago/internal/test"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
// Remove any output from previously run to ensure a clean state
// Some of the tests don't like it when there is existing state in storage
err := os.RemoveAll("../testdata/storage/test")
if err != nil {
fmt.Println("Could not delete test storage", err.Error())
os.Exit(1)
}
// EnsureDirectoryExists eventually uses mkdir from the OS which is not recursive
// so we have to create the temporary test storage by two function calls.
// We ignore any error possibly returned by creating the first one, because if it errors,
// then the second one certainly will as well.
_ = fs.EnsureDirectoryExists("../testdata/storage")
err = fs.EnsureDirectoryExists("../testdata/storage/test")
if err != nil {
fmt.Println("Could not create test storage: ", err.Error())
os.Exit(1)
}
test.ClearTestStorage(nil)
test.CreateTestStorage(nil)
retCode := m.Run()
err = os.RemoveAll("../testdata/storage/test")
if err != nil {
fmt.Println("Could not delete test storage", err.Error())
os.Exit(1)
}
test.ClearTestStorage(nil)
os.Exit(retCode)
}
......@@ -62,10 +39,6 @@ func parseStorage(t *testing.T) *Client {
return manager
}
func teardown(t *testing.T) {
require.NoError(t, os.RemoveAll("../testdata/storage/test"))
}
func verifyClientIsUnmarshaled(t *testing.T, client *Client) {
cred, err := client.credential(irma.NewCredentialTypeIdentifier("irma-demo.RU.studentCard"), 0)
require.NoError(t, err, "could not fetch credential")
......@@ -126,9 +99,9 @@ func verifyPaillierKey(t *testing.T, PrivateKey *paillierPrivateKey) {
func verifyKeyshareIsUnmarshaled(t *testing.T, client *Client) {
require.NotNil(t, client.paillierKeyCache)
require.NotNil(t, client.keyshareServers)
test := irma.NewSchemeManagerIdentifier("test")
require.Contains(t, client.keyshareServers, test)
kss := client.keyshareServers[test]
testManager := irma.NewSchemeManagerIdentifier("test")
require.Contains(t, client.keyshareServers, testManager)
kss := client.keyshareServers[testManager]
require.NotEmpty(t, kss.Nonce)
verifyPaillierKey(t, kss.PrivateKey)
......@@ -141,7 +114,7 @@ func TestStorageDeserialization(t *testing.T) {
verifyCredentials(t, client)
verifyKeyshareIsUnmarshaled(t, client)
teardown(t)
test.ClearTestStorage(t)
}
func TestLogging(t *testing.T) {
......@@ -172,7 +145,7 @@ func TestLogging(t *testing.T) {
require.NotNil(t, response)
require.IsType(t, &gabi.IssueCommitmentMessage{}, response)
teardown(t)
test.ClearTestStorage(t)
}
func TestCandidates(t *testing.T) {
......@@ -206,7 +179,7 @@ func TestCandidates(t *testing.T) {
require.NotNil(t, attrs)
require.Empty(t, attrs)
teardown(t)
test.ClearTestStorage(t)
}
func TestPaillier(t *testing.T) {
......@@ -236,7 +209,7 @@ func TestPaillier(t *testing.T) {
require.Equal(t, plaintext, expected)
teardown(t)
test.ClearTestStorage(t)
}
func TestCredentialRemoval(t *testing.T) {
......@@ -263,7 +236,7 @@ func TestCredentialRemoval(t *testing.T) {
require.NoError(t, err)
require.Nil(t, cred)
teardown(t)
test.ClearTestStorage(t)
}
func TestWrongSchemeManager(t *testing.T) {
......@@ -283,7 +256,7 @@ func TestWrongSchemeManager(t *testing.T) {
irma.SchemeManagerStatusValid,
)
teardown(t)
test.ClearTestStorage(t)
}
// Test installing a new scheme manager from a qr, and do a(n issuance) session
......@@ -329,5 +302,5 @@ func TestDownloadSchemeManager(t *testing.T) {
require.NoError(t, err)
require.True(t, exists)
teardown(t)
test.ClearTestStorage(t)
}
......@@ -6,6 +6,7 @@ import (
"github.com/go-errors/errors"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/test"
)
type ManualSessionHandler struct {
......@@ -27,7 +28,7 @@ func TestManualSession(t *testing.T) {
client.NewManualSession(request, &manualSessionHandler)
teardown(t)
test.ClearTestStorage(t)
if err := <-channel; err != nil {
t.Fatal(*err)
......@@ -44,7 +45,7 @@ func TestManualKeyShareSession(t *testing.T) {
client.NewManualSession(keyshareRequest, &manualSessionHandler)
teardown(t)
test.ClearTestStorage(t)
if err := <-channel; err != nil {
t.Fatal(*err)
......
......@@ -11,6 +11,7 @@ import (
"github.com/go-errors/errors"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/test"
"github.com/stretchr/testify/require"
)
......@@ -203,7 +204,7 @@ func TestLargeAttribute(t *testing.T) {
jwtcontents = getDisclosureJwt("testsp", irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.university"))
sessionHelper(t, jwtcontents, "verification", client)
teardown(t)
test.ClearTestStorage(t)
}
func sessionHelper(t *testing.T, jwtcontents interface{}, url string, client *Client) {
......@@ -236,7 +237,7 @@ func sessionHelper(t *testing.T, jwtcontents interface{}, url string, client *Cl
}
if init {
teardown(t)
test.ClearTestStorage(t)
}
}
......@@ -292,7 +293,7 @@ func TestKeyshareEnrollmentAndSessions(t *testing.T) {
)
sessionHelper(t, jwt, "signature", client)
teardown(t)
test.ClearTestStorage(t)
}
// Use the existing keyshare enrollment and credentials deserialized from Android storage
......@@ -335,5 +336,5 @@ func TestKeyshareSessions(t *testing.T) {
)
sessionHelper(t, jwt, "signature", client)
teardown(t)
test.ClearTestStorage(t)
}
......@@ -2,13 +2,27 @@ package irma
import (
"encoding/json"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"strconv"
"testing"
"time"
"github.com/privacybydesign/irmago/internal/fs"
"github.com/privacybydesign/irmago/internal/test"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
test.ClearTestStorage(nil)
test.CreateTestStorage(nil)
retCode := m.Run()
test.ClearTestStorage(nil)
os.Exit(retCode)
}
func parseConfiguration(t *testing.T) *Configuration {
conf, err := NewConfiguration("testdata/irma_configuration", "")
require.NoError(t, 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