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

import (
4
5
	"fmt"
	"math/big"
Sietse Ringers's avatar
Sietse Ringers committed
6
	"os"
7
8
	"testing"
	"time"
Sietse Ringers's avatar
Sietse Ringers committed
9

10
11
12
	"github.com/stretchr/testify/assert"
)

Sietse Ringers's avatar
Sietse Ringers committed
13
func TestMain(m *testing.M) {
14
15
	if len(MetaStore.SchemeManagers) == 0 { // FIXME
		MetaStore.ParseFolder("testdata/irma_configuration")
Sietse Ringers's avatar
Sietse Ringers committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
	}
	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)
	}
36

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

40
41
42
43
44
45
46
// A convenience function for initializing big integers from known correct (10
// base) strings. Use with care, errors are ignored.
func s2big(s string) (r *big.Int) {
	r, _ = new(big.Int).SetString(s, 10)
	return
}

Sietse Ringers's avatar
Sietse Ringers committed
47
48
49
50
func parseAndroidStorage(t *testing.T) {
	err := Manager.Init("testdata/storage/test")
	assert.NoError(t, err, "Manager.Init() failed")
	err = Manager.ParseAndroidStorage()
51
	assert.NoError(t, err, "ParseAndroidStorage failed")
Sietse Ringers's avatar
Sietse Ringers committed
52
53
54
55
56
57
}

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")
58
59
60
	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
61
		cred.Signature.Verify(cred.PublicKey(), cred.Attributes),
62
63
64
		"Credential should be valid",
	)
}
Sietse Ringers's avatar
Sietse Ringers committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78

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)
}
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

func TestParseStore(t *testing.T) {
	err := MetaStore.ParseFolder("testdata/irma_configuration")
	if err != nil {
		t.Fatal(err)
	}

	assert.NotNil(t, MetaStore.Issuers["irma-demo.RU"].CurrentPublicKey().N, "irma-demo.RU public key has no modulus")
	assert.Equal(t,
		"Irma Demo",
		MetaStore.SchemeManagers["irma-demo"].HRName.Translation("en"),
		"irma-demo scheme manager has unexpected name")
	assert.Equal(t,
		"Radboud Universiteit Nijmegen",
		MetaStore.Issuers["irma-demo.RU"].HRName.Translation("en"),
		"irma-demo.RU issuer has unexpected name")
	assert.Equal(t,
		"Student Card",
		MetaStore.Credentials["irma-demo.RU.studentCard"].HRShortName.Translation("en"),
		"irma-demo.RU.studentCard has unexpected name")

	assert.Equal(t,
		"studentID",
		MetaStore.Credentials["irma-demo.RU.studentCard"].Attributes[2].ID,
		"irma-demo.RU.studentCard.studentID has unexpected name")

	// Hash algorithm pseudocode:
	// Base64(SHA256("irma-demo.RU.studentCard")[0:16])
	assert.Contains(t, MetaStore.reverseHashes, "1stqlPad5edpfS1Na1U+DA==",
		"irma-demo.RU.studentCard had improper hash")
	assert.Contains(t, MetaStore.reverseHashes, "CLjnADMBYlFcuGOT7Z0xRg==",
		"irma-demo.MijnOverheid.root had improper hash")
}

func TestMetadataAttribute(t *testing.T) {
	metadata := NewMetadataAttribute()
	if metadata.Version() != 0x02 {
		t.Errorf("Unexpected metadata version: %d", metadata.Version())
	}

	expiry := metadata.SigningDate().Unix() + int64(metadata.ValidityDuration()*ExpiryFactor)
	if !time.Unix(expiry, 0).Equal(metadata.Expiry()) {
		t.Errorf("Invalid signing date")
	}

	if metadata.KeyCounter() != 0 {
		t.Errorf("Unexpected key counter")
	}
}

func TestMetadataCompatibility(t *testing.T) {
	err := MetaStore.ParseFolder("testdata/irma_configuration")
	if err != nil {
		t.Fatal(err)
	}

	// An actual metadata attribute of an IRMA credential extracted from the IRMA app
	attr := MetadataFromInt(s2big("49043481832371145193140299771658227036446546573739245068"))
	assert.NotNil(t, attr.CredentialType(), "attr.CredentialType() should not be nil")

	assert.Equal(t,
		"irma-demo.RU.studentCard",
		attr.CredentialType().Identifier(),
		"Metadata credential type was not irma-demo.RU.studentCard",
	)
	assert.Equal(t, byte(0x02), attr.Version(), "Unexpected metadata version")
	assert.Equal(t, time.Unix(1499904000, 0), attr.SigningDate(), "Unexpected signing date")
	assert.Equal(t, time.Unix(1516233600, 0), attr.Expiry(), "Unexpected expiry date")
	assert.Equal(t, 2, attr.KeyCounter(), "Unexpected key counter")
}