forked from FactomProject/factom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity_test.go
140 lines (124 loc) · 4.73 KB
/
identity_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
package factom
import (
"fmt"
"testing"
"encoding/json"
ed "github.com/FactomProject/ed25519"
)
func TestGetIdentityChainID(t *testing.T) {
name := []string{"John", "Jacob", "Jingleheimer-Schmidt"}
observedChainID := GetIdentityChainID(name)
expectedChainID := "e0cf1713b492e09e783d5d9f4fc6e2c71b5bdc9af4806a7937a5e935819717e9"
if observedChainID != expectedChainID {
t.Errorf("got: %s but expected: %s", observedChainID, expectedChainID)
}
}
func TestNewIdentityChain(t *testing.T) {
name := []string{"John", "Jacob", "Jingleheimer-Schmidt"}
secretKeys := []string{
"idsec2rChEHLz3SPQQx3syQtB11pHAmxyGjux5FntnS7xqTCieHxxTc",
"idsec1xuUyeCCrJhsojf2wLAZqRxPzPFR8Gidd9DRRid1yGy8ncAJG3",
"idsec2J3nNoqdiyboCBKDGauqN9Jb33dyFSqaJKZqTs6i5FmztsTn5f",
"idsec1jztZ7dypqtwtPPWxybZFNpvvpUh6g8oog6Mnk2gGCm1pNBTgE",
}
var keys []string
for _, v := range secretKeys {
k, _ := GetIdentityKey(v)
keys = append(keys, k.PubString())
}
newChain, err := NewIdentityChain(name, keys)
if err != nil {
t.Errorf("Failed to compose identity chain struct %s", err.Error())
}
expectedChainID := "e0cf1713b492e09e783d5d9f4fc6e2c71b5bdc9af4806a7937a5e935819717e9"
t.Run("ChainID", func(t *testing.T) {
if newChain.ChainID != expectedChainID {
fmt.Println(newChain.ChainID)
fmt.Println(expectedChainID)
t.Fail()
}
})
t.Run("Keys accessible from Content", func(t *testing.T) {
var contentMap map[string]interface{}
content := newChain.FirstEntry.Content
err := json.Unmarshal(content, &contentMap)
if err != nil {
t.Errorf("Failed to unmarshal content")
}
for i, v := range contentMap["keys"].([]interface{}) {
if keys[i] != v.(string) {
t.Errorf("Keys not properly formatted")
}
}
})
}
func TestNewIdentityKeyReplacementEntry(t *testing.T) {
chainID := "e0cf1713b492e09e783d5d9f4fc6e2c71b5bdc9af4806a7937a5e935819717e9"
oldKey, _ := GetIdentityKey("idsec1jztZ7dypqtwtPPWxybZFNpvvpUh6g8oog6Mnk2gGCm1pNBTgE")
newKey, _ := GetIdentityKey("idsec2J3nNoqdiyboCBKDGauqN9Jb33dyFSqaJKZqTs6i5FmztsTn5f")
signerKey, _ := GetIdentityKey("idsec2wH72BNR9QZhTMGDbxwLWGrghZQexZvLTros2wCekkc62N9h7s")
observedEntry, err := NewIdentityKeyReplacementEntry(chainID, oldKey.PubString(), newKey.PubString(), signerKey)
if err != nil {
t.Errorf("Failed to compose key replacement entry struct %s", err.Error())
}
t.Run("ChainID", func(t *testing.T) {
if observedEntry.ChainID != chainID {
t.Fail()
}
})
t.Run("ExtIDs", func(t *testing.T) {
if len(observedEntry.ExtIDs) != 5 {
t.Errorf("len(ExtIDs) != 5")
}
if string(observedEntry.ExtIDs[0]) != "ReplaceKey" {
t.Errorf("ReplaceKey is not first ExtID")
}
if string(observedEntry.ExtIDs[1]) != oldKey.String() ||
string(observedEntry.ExtIDs[2]) != newKey.String() ||
string(observedEntry.ExtIDs[4]) != signerKey.String() {
t.Errorf("Keys not formatted properly")
}
})
t.Run("Signature", func(t *testing.T) {
var observedSignature [64]byte
copy(observedSignature[:], observedEntry.ExtIDs[3])
message := []byte(oldKey.String() + newKey.String())
if !ed.Verify(signerKey.Pub, message, &observedSignature) {
t.Fail()
}
})
}
func TestNewIdentityAttributeEntry(t *testing.T) {
receiverChainID := "5ef81cd345fd497a376ca5e5670ef10826d96e73c9f797b33ea46552a47834a3"
destinationChainID := "5a402200c5cf278e47905ce52d7d64529a0291829a7bd230072c5468be709069"
signerChainID := "e0cf1713b492e09e783d5d9f4fc6e2c71b5bdc9af4806a7937a5e935819717e9"
signerKey, err := GetIdentityKey("idsec2J3nNoqdiyboCBKDGauqN9Jb33dyFSqaJKZqTs6i5FmztsTn5f")
if err != nil {
t.Errorf("Failed to get identity key")
}
attributesJSON := `[{"key":"email","value":"[email protected]"}]`
observedEntry := NewIdentityAttributeEntry(receiverChainID, destinationChainID, attributesJSON, signerKey, signerChainID)
if !IsValidAttribute(observedEntry) {
t.Errorf("Improperly formatted attribute")
}
var attributes []IdentityAttribute
if err = json.Unmarshal(observedEntry.Content, &attributes); err != nil {
t.Errorf("Failed to unmarshal content: %v", err)
}
if attributes[0].Key != "email" {
t.Errorf("Incorrect key")
}
if attributes[0].Value != "[email protected]" {
t.Errorf("Incorrect value")
}
}
func TestNewIdentityAttributeEndorsementEntry(t *testing.T) {
destinationChainID := "5a402200c5cf278e47905ce52d7d64529a0291829a7bd230072c5468be709069"
signerChainID := "e0cf1713b492e09e783d5d9f4fc6e2c71b5bdc9af4806a7937a5e935819717e9"
signerKey, _ := GetIdentityKey("idsec2J3nNoqdiyboCBKDGauqN9Jb33dyFSqaJKZqTs6i5FmztsTn5f")
entryHash := "52385948ea3ab6fd67b07664ac6a30ae5f6afa94427a547c142517beaa9054d0"
observedEntry := NewIdentityAttributeEndorsementEntry(destinationChainID, entryHash, signerKey, signerChainID)
if !IsValidEndorsement(observedEntry) {
t.Errorf("Improperly formatted attribute endorsement")
}
}