Commit 41996fd5 authored by Ivar Derksen's avatar Ivar Derksen Committed by Sietse Ringers

Fixed some storage bugs and added tests

parent 5a60e273
......@@ -21,7 +21,6 @@ func TestSessionUsingLegacyStorage(t *testing.T) {
require.NoError(t, err)
// Test whether credential is still there
require.NoError(t, err)
id := irma.NewAttributeTypeIdentifier("irma-demo.MijnOverheid.root.BSN")
sessionHelper(t, getDisclosureRequest(id), "verification", client)
}
......@@ -37,6 +37,10 @@ func TestMain(m *testing.M) {
func parseStorage(t *testing.T) (*irmaclient.Client, *TestClientHandler) {
test.SetupTestStorage(t)
return parseExistingStorage(t)
}
func parseExistingStorage(t *testing.T) (*irmaclient.Client, *TestClientHandler) {
handler := &TestClientHandler{t: t, c: make(chan error)}
path := test.FindTestdataFolder(t)
client, err := irmaclient.New(
......
......@@ -109,6 +109,11 @@ func TestIssuanceSameAttributesNotSingleton(t *testing.T) {
req = getIssuanceRequest(false)
sessionHelper(t, req, "issue", client)
require.Equal(t, prevLen+1, len(client.CredentialInfoList()))
// Also check whether this is actually stored
require.NoError(t, client.Close())
client, _ = parseExistingStorage(t)
require.Equal(t, prevLen+1, len(client.CredentialInfoList()))
}
func TestLargeAttribute(t *testing.T) {
......@@ -140,6 +145,12 @@ func TestIssuanceSingletonCredential(t *testing.T) {
sessionHelper(t, request, "issue", client)
require.NotNil(t, client.Attributes(credid, 0))
require.Nil(t, client.Attributes(credid, 1))
// Also check whether this is actually stored
require.NoError(t, client.Close())
client, _ = parseExistingStorage(t)
require.NotNil(t, client.Attributes(credid, 0))
require.Nil(t, client.Attributes(credid, 1))
}
func TestUnsatisfiableDisclosureSession(t *testing.T) {
......@@ -475,3 +486,16 @@ func TestStaticQRSession(t *testing.T) {
require.NoError(t, s.Shutdown(context.Background()))
require.True(t, received)
}
func TestIssuedCredentialIsStored(t *testing.T) {
client, _ := parseStorage(t)
defer test.ClearTestStorage(t)
issuanceRequest := getNameIssuanceRequest()
sessionHelper(t, issuanceRequest, "issue", client)
require.NoError(t, client.Close())
client, _ = parseExistingStorage(t)
id := irma.NewAttributeTypeIdentifier("irma-demo.MijnOverheid.fullName.familyname")
sessionHelper(t, getDisclosureRequest(id), "verification", client)
}
......@@ -209,7 +209,7 @@ func (client *Client) CredentialInfoList() irma.CredentialInfoList {
// addCredential adds the specified credential to the Client, saving its signature
// imediately, and optionally cm.attributes as well.
func (client *Client) addCredential(cred *credential, storeAttributes bool) (err error) {
func (client *Client) addCredential(cred *credential) (err error) {
id := irma.NewCredentialTypeIdentifier("")
if cred.CredentialType() != nil {
id = cred.CredentialType().Identifier()
......@@ -253,9 +253,7 @@ func (client *Client) addCredential(cred *credential, storeAttributes bool) (err
if err = client.storage.StoreSignature(cred); err != nil {
return
}
if storeAttributes {
err = client.storage.StoreAttributes(id, client.attributes[id])
}
err = client.storage.StoreAttributes(id, client.attributes[id])
return
}
......@@ -269,7 +267,7 @@ func generateSecretKey() (*secretKey, error) {
// Removal methods
func (client *Client) remove(id irma.CredentialTypeIdentifier, index int, storenow bool) error {
func (client *Client) remove(id irma.CredentialTypeIdentifier, index int, storeLog bool) error {
// Remove attributes
list, exists := client.attributes[id]
if !exists || index >= len(list) {
......@@ -277,10 +275,9 @@ func (client *Client) remove(id irma.CredentialTypeIdentifier, index int, storen
}
attrs := list[index]
client.attributes[id] = append(list[:index], list[index+1:]...)
if storenow {
if err := client.storage.StoreAttributes(id, client.attributes[id]); err != nil {
return err
}
if err := client.storage.StoreAttributes(id, client.attributes[id]); err != nil {
return err
}
// Remove credential
......@@ -299,7 +296,7 @@ func (client *Client) remove(id irma.CredentialTypeIdentifier, index int, storen
removed := map[irma.CredentialTypeIdentifier][]irma.TranslatedString{}
removed[id] = attrs.Strings()
if storenow {
if storeLog {
return client.storage.AddLogEntry(&LogEntry{
Type: ActionRemoval,
Time: irma.Timestamp(time.Now()),
......@@ -814,7 +811,7 @@ func (client *Client) ConstructCredentials(msg []*gabi.IssueSignatureMessage, re
if err != nil {
return err
}
if err = client.addCredential(newcred, true); err != nil {
if err = client.addCredential(newcred); err != nil {
return err
}
}
......
......@@ -29,6 +29,10 @@ func TestMain(m *testing.M) {
func parseStorage(t *testing.T) *Client {
test.SetupTestStorage(t)
return parseExistingStorage(t)
}
func parseExistingStorage(t *testing.T) *Client {
client, err := New(
filepath.Join("..", "testdata", "storage", "test"),
filepath.Join("..", "testdata", "irma_configuration"),
......@@ -255,12 +259,7 @@ func TestCredentialRemoval(t *testing.T) {
// Also check whether credential is removed after reloading the storage
err = client.storage.db.Close()
require.NoError(t, err)
client, err = New(
filepath.Join("..", "testdata", "storage", "test"),
filepath.Join("..", "testdata", "irma_configuration"),
&TestClientHandler{t: t},
)
require.NoError(t, err)
client = parseExistingStorage(t)
cred, err = client.credential(id2, 0)
require.NoError(t, err)
require.Nil(t, cred)
......
......@@ -116,7 +116,7 @@ func (s *storage) DeleteAllSignatures() error {
func (s *storage) StoreSignature(cred *credential) error {
return s.db.Update(func(tx *bbolt.Tx) error {
return s.TxStoreSignature(tx, cred.attrs.Hash(), cred.Signature)
return s.TxStoreSignature(tx, cred.AttributeList().Hash(), cred.Signature)
})
}
......
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