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

Ensure that tests always cleanup afterwards using defer

parent 2b41c757
......@@ -5,7 +5,6 @@ import (
"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"
......@@ -14,19 +13,17 @@ import (
func TestMain(m *testing.M) {
// Create HTTP server for scheme managers
test.StartSchemeManagerHttpServer()
defer test.StopSchemeManagerHttpServer()
test.ClearTestStorage(nil)
test.CreateTestStorage(nil)
retCode := m.Run()
test.ClearTestStorage(nil)
defer test.ClearTestStorage(nil)
test.StopSchemeManagerHttpServer()
os.Exit(retCode)
os.Exit(m.Run())
}
func parseStorage(t *testing.T) *irmaclient.Client {
test.SetupTestStorage(t)
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"),
......
......@@ -24,9 +24,9 @@ func createManualSessionHandler(t *testing.T, client *irmaclient.Client) *Manual
}
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 {
if client == nil {
client = parseStorage(t)
defer test.ClearTestStorage(t)
}
client.NewSession(request, h)
......@@ -75,8 +75,6 @@ func TestManualSession(t *testing.T) {
attrs, status = manualSessionHelper(t, nil, ms, request, "", false)
require.Equal(t, irma.ProofStatusValid, status)
require.Equal(t, irma.AttributeProofStatusExtra, attrs[0].Status)
test.ClearTestStorage(t)
}
// Test if proof verification fails with status 'ERROR_CRYPTO' if we verify it with an invalid nonce
......@@ -87,8 +85,6 @@ func TestManualSessionInvalidNonce(t *testing.T) {
_, status := manualSessionHelper(t, nil, ms, request, invalidRequest, false)
require.Equal(t, irma.ProofStatusUnmatchedRequest, status)
test.ClearTestStorage(t)
}
// Test if proof verification fails with status 'MISSING_ATTRIBUTES' if we provide it with a non-matching signature request
......@@ -103,8 +99,6 @@ func TestManualSessionInvalidRequest(t *testing.T) {
require.Equal(t, irma.AttributeProofStatusMissing, attrs[0].Status)
// Second attribute result is EXTRA, since it is disclosed, but not matching the sigrequest
require.Equal(t, irma.AttributeProofStatusExtra, attrs[1].Status)
test.ClearTestStorage(t)
}
// Test if proof verification fails with status 'MISSING_ATTRIBUTES' if we provide it with invalid attribute values
......@@ -116,8 +110,6 @@ func TestManualSessionInvalidAttributeValue(t *testing.T) {
require.Equal(t, irma.ProofStatusMissingAttributes, status)
require.Equal(t, irma.AttributeProofStatusInvalidValue, attrs[0].Status)
test.ClearTestStorage(t)
}
func TestManualKeyShareSession(t *testing.T) {
......@@ -128,12 +120,11 @@ func TestManualKeyShareSession(t *testing.T) {
require.Equal(t, irma.ProofStatusValid, status)
_, status = manualSessionHelper(t, nil, ms, request, "", false)
require.Equal(t, irma.ProofStatusValid, status)
test.ClearTestStorage(t)
}
func TestManualSessionMultiProof(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
// First, we need to issue an extra credential (BSN)
jwtcontents := getIssuanceJwt("testip", true, "")
......@@ -152,8 +143,6 @@ func TestManualSessionMultiProof(t *testing.T) {
require.Equal(t, irma.ProofStatusValid, status)
require.Equal(t, irma.AttributeProofStatusExtra, attrs[0].Status)
require.Equal(t, irma.AttributeProofStatusExtra, attrs[1].Status)
test.ClearTestStorage(t)
}
func TestManualSessionInvalidProof(t *testing.T) {
......@@ -162,8 +151,6 @@ func TestManualSessionInvalidProof(t *testing.T) {
_, status := manualSessionHelper(t, nil, ms, request, request, true)
require.Equal(t, irma.ProofStatusInvalid, status)
test.ClearTestStorage(t)
}
func TestManualDisclosureSession(t *testing.T) {
......@@ -174,8 +161,6 @@ func TestManualDisclosureSession(t *testing.T) {
require.Equal(t, irma.AttributeProofStatusPresent, attrs[0].Status)
require.Equal(t, "456", attrs[0].Value["en"])
require.Equal(t, irma.ProofStatusValid, status)
test.ClearTestStorage(t)
}
// Test if proof verification fails with status 'MISSING_ATTRIBUTES' if we provide it with a non-matching disclosure request
......@@ -190,6 +175,4 @@ func TestManualDisclosureSessionInvalidRequest(t *testing.T) {
require.Equal(t, irma.AttributeProofStatusMissing, attrs[0].Status)
// Second attribute result is EXTRA, since it is disclosed, but not matching the sigrequest
require.Equal(t, irma.AttributeProofStatusExtra, attrs[1].Status)
test.ClearTestStorage(t)
}
......@@ -119,9 +119,9 @@ func startSession(request interface{}, url string) (*irma.Qr, error) {
}
func sessionHelper(t *testing.T, jwtcontents interface{}, url string, client *irmaclient.Client) {
init := client == nil
if init {
if client == nil {
client = parseStorage(t)
defer test.ClearTestStorage(t)
}
url = "http://localhost:8088/irma_api_server/api/v2/" + url
......@@ -149,10 +149,6 @@ func sessionHelper(t *testing.T, jwtcontents interface{}, url string, client *ir
if result := <-c; result != nil {
require.NoError(t, result.Err)
}
if init {
test.ClearTestStorage(t)
}
}
func keyshareSessions(t *testing.T, client *irmaclient.Client) {
......@@ -250,6 +246,8 @@ func TestIssuanceOptionalSetAttributes(t *testing.T) {
func TestLargeAttribute(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
require.NoError(t, client.RemoveAllCredentials())
jwtcontents := getIssuanceJwt("testip", false, "1234567890123456789012345678901234567890") // 40 chars
......@@ -257,12 +255,12 @@ func TestLargeAttribute(t *testing.T) {
jwtcontents = getDisclosureJwt("testsp", irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.university"))
sessionHelper(t, jwtcontents, "verification", client)
test.ClearTestStorage(t)
}
func TestIssuanceSingletonCredential(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
jwtcontents := getIssuanceJwt("testip", true, "")
credid := irma.NewCredentialTypeIdentifier("irma-demo.MijnOverheid.root")
......@@ -285,6 +283,7 @@ signatures being issued in the issuance protocol in two distinct ways, of which
that they have been fixed. */
func TestAttributeByteEncoding(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
require.NoError(t, client.RemoveAllCredentials())
/* After bitshifting the presence bit into the large attribute below, the most significant
......@@ -303,8 +302,6 @@ func TestAttributeByteEncoding(t *testing.T) {
the same positive bigint. */
jwtcontents = getIssuanceJwt("testip", false, "é")
sessionHelper(t, jwtcontents, "issue", client)
test.ClearTestStorage(t)
}
// Use the existing keyshare enrollment and credentials
......@@ -312,14 +309,14 @@ func TestAttributeByteEncoding(t *testing.T) {
// Use keyshareuser.sql to enroll the user at the keyshare server.
func TestKeyshareSessions(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
keyshareSessions(t, client)
test.ClearTestStorage(t)
}
func TestDisclosureNewAttributeUpdateSchemeManager(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
schemeid := irma.NewSchemeManagerIdentifier("irma-demo")
credid := irma.NewCredentialTypeIdentifier("irma-demo.RU.studentCard")
......@@ -340,12 +337,12 @@ func TestDisclosureNewAttributeUpdateSchemeManager(t *testing.T) {
client.Configuration.Download(&disclosureRequest)
require.True(t, client.Configuration.CredentialTypes[credid].ContainsAttribute(attrid))
test.ClearTestStorage(t)
}
func TestIssueNewAttributeUpdateSchemeManager(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
schemeid := irma.NewSchemeManagerIdentifier("irma-demo")
credid := irma.NewCredentialTypeIdentifier("irma-demo.RU.studentCard")
attrid := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.newAttribute")
......@@ -356,12 +353,12 @@ func TestIssueNewAttributeUpdateSchemeManager(t *testing.T) {
issuanceRequest.Credentials[0].Attributes["newAttribute"] = "foobar"
client.Configuration.Download(issuanceRequest)
require.True(t, client.Configuration.CredentialTypes[credid].ContainsAttribute(attrid))
test.ClearTestStorage(t)
}
func TestIssueOptionalAttributeUpdateSchemeManager(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
schemeid := irma.NewSchemeManagerIdentifier("irma-demo")
credid := irma.NewCredentialTypeIdentifier("irma-demo.RU.studentCard")
attrid := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.level")
......@@ -372,8 +369,6 @@ func TestIssueOptionalAttributeUpdateSchemeManager(t *testing.T) {
delete(issuanceRequest.Credentials[0].Attributes, "level")
client.Configuration.Download(issuanceRequest)
require.True(t, client.Configuration.CredentialTypes[credid].AttributeType(attrid).IsOptional())
test.ClearTestStorage(t)
}
// Test installing a new scheme manager from a qr, and do a(n issuance) session
......@@ -381,6 +376,7 @@ func TestIssueOptionalAttributeUpdateSchemeManager(t *testing.T) {
// issuers, and public keys.
func TestDownloadSchemeManager(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
// Remove irma-demo scheme manager as we need to test adding it
irmademo := irma.NewSchemeManagerIdentifier("irma-demo")
......@@ -419,6 +415,4 @@ func TestDownloadSchemeManager(t *testing.T) {
exists, err = fs.PathExists(basepath + "/RU/Issues/studentCard/description.xml")
require.NoError(t, err)
require.True(t, exists)
test.ClearTestStorage(t)
}
......@@ -94,6 +94,7 @@ func ClearTestStorage(t *testing.T) {
}
func CreateTestStorage(t *testing.T) {
ClearTestStorage(t)
path := filepath.Join(FindTestdataFolder(t), "storage")
// EnsureDirectoryExists eventually uses mkdir from the OS which is not recursive
......@@ -106,6 +107,7 @@ func CreateTestStorage(t *testing.T) {
}
func SetupTestStorage(t *testing.T) {
CreateTestStorage(t)
path := FindTestdataFolder(t)
err := fs.CopyDirectory(filepath.Join(path, "teststorage"), filepath.Join(path, "storage", "test"))
checkError(t, err)
......
......@@ -18,17 +18,16 @@ import (
func TestMain(m *testing.M) {
// Create HTTP server for scheme managers
test.StartSchemeManagerHttpServer()
defer test.StopSchemeManagerHttpServer()
test.ClearTestStorage(nil)
test.CreateTestStorage(nil)
retCode := m.Run()
test.ClearTestStorage(nil)
defer test.ClearTestStorage(nil)
test.StopSchemeManagerHttpServer()
os.Exit(retCode)
os.Exit(m.Run())
}
func parseStorage(t *testing.T) *Client {
test.SetupTestStorage(t)
require.NoError(t, fs.CopyDirectory("../testdata/teststorage", "../testdata/storage/test"))
client, err := New(
"../testdata/storage/test",
......@@ -112,11 +111,11 @@ func verifyKeyshareIsUnmarshaled(t *testing.T, client *Client) {
func TestStorageDeserialization(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
verifyClientIsUnmarshaled(t, client)
verifyCredentials(t, client)
verifyKeyshareIsUnmarshaled(t, client)
test.ClearTestStorage(t)
}
//func disabledTestLogging(t *testing.T) {
......@@ -188,6 +187,7 @@ func TestStorageDeserialization(t *testing.T) {
// satisfy the attribute disjunction.
func TestCandidates(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
// client contains one instance of the studentCard credential, whose studentID attribute is 456.
attrtype := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
......@@ -246,12 +246,11 @@ func TestCandidates(t *testing.T) {
json.Unmarshal([]byte(`{"attributes":{"irma-demo.MijnOverheid.ageLower.over12":null}}`), &disjunction)
attrs = client.Candidates(disjunction)
require.Empty(t, attrs)
test.ClearTestStorage(t)
}
func TestPaillier(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
challenge, _ := gabi.RandomBigInt(256)
comm, _ := gabi.RandomBigInt(1000)
......@@ -277,12 +276,11 @@ func TestPaillier(t *testing.T) {
expected.Mul(expected, resp).Add(expected, comm)
require.Equal(t, plaintext, expected)
test.ClearTestStorage(t)
}
func TestCredentialRemoval(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
id := irma.NewCredentialTypeIdentifier("irma-demo.RU.studentCard")
id2 := irma.NewCredentialTypeIdentifier("test.test.mijnirma")
......@@ -304,12 +302,11 @@ func TestCredentialRemoval(t *testing.T) {
cred, err = client.credential(id2, 0)
require.NoError(t, err)
require.Nil(t, cred)
test.ClearTestStorage(t)
}
func TestWrongSchemeManager(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
irmademo := irma.NewSchemeManagerIdentifier("irma-demo")
require.Contains(t, client.Configuration.SchemeManagers, irmademo)
......@@ -324,18 +321,15 @@ func TestWrongSchemeManager(t *testing.T) {
client.Configuration.SchemeManagers[irmademo].Status,
irma.SchemeManagerStatusValid,
)
test.ClearTestStorage(t)
}
// Test pinchange interaction
func TestKeyshareChangePin(t *testing.T) {
client := parseStorage(t)
defer test.ClearTestStorage(t)
require.NoError(t, client.keyshareChangePinWorker(irma.NewSchemeManagerIdentifier("test"), "12345", "54321"))
require.NoError(t, client.keyshareChangePinWorker(irma.NewSchemeManagerIdentifier("test"), "54321", "12345"))
test.ClearTestStorage(t)
}
// ------
......
......@@ -2,7 +2,6 @@ package irma
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
......@@ -14,14 +13,6 @@ import (
"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)
......@@ -37,6 +28,9 @@ func s2big(s string) (r *big.Int) {
}
func TestConfigurationAutocopy(t *testing.T) {
test.CreateTestStorage(t)
defer test.ClearTestStorage(t)
path := filepath.Join("testdata", "storage", "test", "irma_configuration")
require.NoError(t, fs.CopyDirectory(filepath.Join("testdata", "irma_configuration"), path))
conf, err := NewConfiguration(path, filepath.Join("testdata", "irma_configuration_updated"))
......@@ -46,8 +40,6 @@ func TestConfigurationAutocopy(t *testing.T) {
credid := NewCredentialTypeIdentifier("irma-demo.RU.studentCard")
attrid := NewAttributeTypeIdentifier("irma-demo.RU.studentCard.newAttribute")
require.True(t, conf.CredentialTypes[credid].ContainsAttribute(attrid))
test.ClearTestStorage(t)
}
func TestParseInvalidIrmaConfiguration(t *testing.T) {
......@@ -72,20 +64,22 @@ func TestParseInvalidIrmaConfiguration(t *testing.T) {
func TestRetryHTTPRequest(t *testing.T) {
test.StartBadHttpServer(3, 1*time.Second, "42")
defer test.StopBadHttpServer()
transport := NewHTTPTransport("http://localhost:48682")
transport.client.HTTPClient.Timeout = 500 * time.Millisecond
bts, err := transport.GetBytes("")
require.NoError(t, err)
require.Equal(t, "42\n", string(bts))
test.StopBadHttpServer()
}
func TestInvalidIrmaConfigurationRestoreFromRemote(t *testing.T) {
test.StartSchemeManagerHttpServer()
defer test.StopSchemeManagerHttpServer()
test.CreateTestStorage(t)
defer test.ClearTestStorage(t)
require.NoError(t, fs.EnsureDirectoryExists("testdata/storage/test"))
conf, err := NewConfiguration("testdata/storage/test/irma_configuration", "testdata/irma_configuration_invalid")
require.NoError(t, err)
......@@ -94,13 +88,12 @@ func TestInvalidIrmaConfigurationRestoreFromRemote(t *testing.T) {
require.Empty(t, conf.DisabledSchemeManagers)
require.Contains(t, conf.SchemeManagers, NewSchemeManagerIdentifier("irma-demo"))
require.Contains(t, conf.CredentialTypes, NewCredentialTypeIdentifier("irma-demo.RU.studentCard"))
test.StopSchemeManagerHttpServer()
test.ClearTestStorage(t)
}
func TestInvalidIrmaConfigurationRestoreFromAssets(t *testing.T) {
require.NoError(t, fs.EnsureDirectoryExists("testdata/storage/test"))
test.CreateTestStorage(t)
defer test.ClearTestStorage(t)
conf, err := NewConfiguration("testdata/storage/test/irma_configuration", "testdata/irma_configuration_invalid")
require.NoError(t, err)
......@@ -116,8 +109,6 @@ func TestInvalidIrmaConfigurationRestoreFromAssets(t *testing.T) {
require.Empty(t, conf.DisabledSchemeManagers)
require.Contains(t, conf.SchemeManagers, NewSchemeManagerIdentifier("irma-demo"))
require.Contains(t, conf.CredentialTypes, NewCredentialTypeIdentifier("irma-demo.RU.studentCard"))
test.ClearTestStorage(t)
}
func TestParseIrmaConfiguration(t *testing.T) {
......
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