irmago_test.go 1.61 KB
Newer Older
Sietse Ringers's avatar
Sietse Ringers committed
1
package irmago
2
3
4

import "testing"
import (
Sietse Ringers's avatar
Sietse Ringers committed
5
6
7
	"os"

	"fmt"
8
9
10
11
	"github.com/mhe/gabi"
	"github.com/stretchr/testify/assert"
)

Sietse Ringers's avatar
Sietse Ringers committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
func TestMain(m *testing.M) {
	if len(gabi.MetaStore.SchemeManagers) == 0 { // FIXME
		gabi.MetaStore.ParseFolder("testdata/irma_configuration")
	}
	Manager = newCredentialManager()
	err := os.RemoveAll("testdata/storage/test")
	if err != nil {
		fmt.Errorf("Could not delete test storage")
		os.Exit(1)
	}
	err = os.Mkdir("testdata/storage/test", 0755)
	if err != nil {
		fmt.Errorf("Could not create test storage")
		os.Exit(1)
	}

	retCode := m.Run()

	err = os.RemoveAll("testdata/storage/test")
	if err != nil {
		fmt.Errorf("Could not delete test storage")
		os.Exit(1)
	}
35

Sietse Ringers's avatar
Sietse Ringers committed
36
37
38
39
40
41
42
	os.Exit(retCode)
}

func parseAndroidStorage(t *testing.T) {
	err := Manager.Init("testdata/storage/test")
	assert.NoError(t, err, "Manager.Init() failed")
	err = Manager.ParseAndroidStorage()
43
	assert.NoError(t, err, "ParseAndroidStorage failed")
Sietse Ringers's avatar
Sietse Ringers committed
44
45
46
47
48
49
}

func verifyStoreIsUnmarshaled(t *testing.T) {
	cred, err := Manager.Credential("irma-demo.RU.studentCard", 0)
	assert.NoError(t, err, "could not fetch credential")
	assert.NotNil(t, cred, "Credential should exist")
50
51
52
	assert.NotNil(t, cred.Attributes[0], "Metadata attribute of irma-demo.RU.studentCard should not be nil")

	assert.True(t,
Sietse Ringers's avatar
Sietse Ringers committed
53
		cred.Signature.Verify(cred.PublicKey(), cred.Attributes),
54
55
56
		"Credential should be valid",
	)
}
Sietse Ringers's avatar
Sietse Ringers committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70

func TestAndroidParse(t *testing.T) {
	parseAndroidStorage(t)
	verifyStoreIsUnmarshaled(t)
}

func TestUnmarshaling(t *testing.T) {
	parseAndroidStorage(t)

	Manager = newCredentialManager()
	Manager.Init("testdata/storage/test")

	verifyStoreIsUnmarshaled(t)
}