archivesManager_test.go 8.39 KB
Newer Older
1
package entities
Caro Axel's avatar
Caro Axel committed
2 3

import (
Caro Axel's avatar
Caro Axel committed
4
	"crypto/sha512"
5 6 7 8
	"fmt"
	"os"
	"testing"

Caro Axel's avatar
Caro Axel committed
9 10
	cAPI "dfss/dfssc/api"
	"dfss/mgdb"
11
	"github.com/stretchr/testify/assert"
Caro Axel's avatar
Caro Axel committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25
	"gopkg.in/mgo.v2/bson"
)

var (
	db         string
	collection *mgdb.MongoCollection
	dbManager  *mgdb.MongoManager

	sequence             []uint32
	signers              [][]byte
	contractDocumentHash []byte
	signatureUUID        string
	signatureUUIDBson    bson.ObjectId

26
	seal []byte
Caro Axel's avatar
Caro Axel committed
27

28
	signersEntities []Signer
Caro Axel's avatar
Caro Axel committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

	err error
)

func init() {
	db = os.Getenv("DFSS_MONGO_URI")
	if db == "" {
		db = "mongodb://localhost/dfss-test"
	}

	sequence = []uint32{0, 1, 2, 0, 1, 2, 0, 1, 2}

	for i := 0; i < 3; i++ {
		h := sha512.Sum512([]byte{byte(i)})
		signer := h[:]
		signers = append(signers, signer)
	}

	contractDocumentHash = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
	signatureUUIDBson = bson.NewObjectId()
	signatureUUID = signatureUUIDBson.Hex()

51
	seal = []byte{}
Caro Axel's avatar
Caro Axel committed
52

53
	signersEntities = make([]Signer, 0)
Caro Axel's avatar
Caro Axel committed
54
	for _, s := range signers {
55
		signerEntity := NewSigner(s)
Caro Axel's avatar
Caro Axel committed
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
		signersEntities = append(signersEntities, *signerEntity)
	}
}

func TestMain(m *testing.M) {
	dbManager, err = mgdb.NewManager(db)
	if err != nil {
		fmt.Fprintln(os.Stderr, "An error occured during the connection to MongoDB:", err)
		os.Exit(2)
	}

	collection = dbManager.Get("signatures")
	err = collection.Drop()
	code := m.Run()
	err = collection.Drop()

	if err != nil {
		fmt.Println("An error occurred while droping the collection")
	}

	dbManager.Close()

	os.Exit(code)
}

func TestInitializeArchives(t *testing.T) {
	promise := &cAPI.Promise{
		Context: &cAPI.Context{
			ContractDocumentHash: contractDocumentHash,
			Sequence:             sequence,
			Signers:              signers,
			SignatureUUID:        signatureUUID,
88
			Seal:                 seal,
Caro Axel's avatar
Caro Axel committed
89 90 91
		},
	}
	manager := &ArchivesManager{
Caro Axel's avatar
Caro Axel committed
92
		DB: dbManager,
Caro Axel's avatar
Caro Axel committed
93
	}
94
	arch := NewSignatureArchives(signatureUUIDBson, sequence, signersEntities, contractDocumentHash, seal)
Caro Axel's avatar
Caro Axel committed
95

Caro Axel's avatar
Caro Axel committed
96 97
	err = manager.InitializeArchives(promise, signatureUUIDBson, &signersEntities)
	assert.Nil(t, err)
Caro Axel's avatar
Caro Axel committed
98 99 100
	arch.Signers = manager.Archives.Signers
	assert.Equal(t, manager.Archives, arch)

Caro Axel's avatar
Caro Axel committed
101
	ok, err := collection.DeleteByID(*manager.Archives)
Caro Axel's avatar
Caro Axel committed
102 103 104 105 106
	assert.Equal(t, err, nil)
	assert.Equal(t, ok, true)
}

func TestContainsSignature(t *testing.T) {
107
	archives := NewSignatureArchives(signatureUUIDBson, sequence, signersEntities, contractDocumentHash, seal)
Caro Axel's avatar
Caro Axel committed
108 109 110 111 112 113 114
	manager := &ArchivesManager{
		DB:       dbManager,
		Archives: archives,
	}

	b, arch := manager.ContainsSignature(signatureUUIDBson)
	assert.Equal(t, b, false)
115
	assert.Equal(t, arch, &SignatureArchives{})
Caro Axel's avatar
Caro Axel committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

	ok, err := collection.Insert(archives)
	assert.Equal(t, ok, true)
	assert.Equal(t, err, nil)

	b, arch = manager.ContainsSignature(signatureUUIDBson)
	assert.Equal(t, b, true)
	assert.Equal(t, arch, archives)

	ok, err = collection.DeleteByID(*archives)
	assert.Equal(t, err, nil)
	assert.Equal(t, ok, true)
}

func TestHasReceivedAbortToken(t *testing.T) {
131
	archives := NewSignatureArchives(signatureUUIDBson, sequence, signersEntities, contractDocumentHash, seal)
Caro Axel's avatar
Caro Axel committed
132 133 134 135 136
	manager := &ArchivesManager{
		DB:       dbManager,
		Archives: archives,
	}
	signerIndex := uint32(1)
137 138
	abortedSigner0 := NewAbortedSigner(uint32(0), uint32(1))
	abortedSigner1 := NewAbortedSigner(signerIndex, uint32(1))
Caro Axel's avatar
Caro Axel committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

	assert.Equal(t, len(archives.AbortedSigners), 0)

	aborted := manager.HasReceivedAbortToken(signerIndex)
	assert.Equal(t, aborted, false)

	archives.AbortedSigners = append(archives.AbortedSigners, *abortedSigner0)

	aborted = manager.HasReceivedAbortToken(signerIndex)
	assert.Equal(t, aborted, false)

	archives.AbortedSigners = append(archives.AbortedSigners, *abortedSigner1)

	aborted = manager.HasReceivedAbortToken(signerIndex)
	assert.Equal(t, aborted, true)
}

func TestWasContractSigned(t *testing.T) {
157
	archives := NewSignatureArchives(signatureUUIDBson, sequence, signersEntities, contractDocumentHash, seal)
Caro Axel's avatar
Caro Axel committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	manager := &ArchivesManager{
		DB:       dbManager,
		Archives: archives,
	}

	signed, contract := manager.WasContractSigned()
	assert.Equal(t, signed, false)
	assert.Equal(t, len(contract), 0)

	c := []byte{0}
	archives.SignedContract = c
	signed, contract = manager.WasContractSigned()
	assert.Equal(t, signed, true)
	assert.Equal(t, contract, c)
}

func TestHasSignerPromised(t *testing.T) {
175
	archives := NewSignatureArchives(signatureUUIDBson, sequence, signersEntities, contractDocumentHash, seal)
Caro Axel's avatar
Caro Axel committed
176 177 178 179 180 181 182 183
	manager := &ArchivesManager{
		DB:       dbManager,
		Archives: archives,
	}
	ok := manager.HasSignerPromised(1)
	assert.Equal(t, len(archives.ReceivedPromises), 0)
	assert.Equal(t, ok, false)

184
	promise0 := &Promise{
Caro Axel's avatar
Caro Axel committed
185 186 187 188 189 190 191 192 193
		RecipientKeyIndex: 1,
		SenderKeyIndex:    0,
	}
	archives.ReceivedPromises = append(archives.ReceivedPromises, *promise0)
	assert.Equal(t, len(archives.ReceivedPromises), 1)

	ok = manager.HasSignerPromised(1)
	assert.Equal(t, ok, false)

194
	promise1 := &Promise{
Caro Axel's avatar
Caro Axel committed
195 196 197 198 199 200 201
		RecipientKeyIndex: 1,
		SenderKeyIndex:    1,
	}
	archives.ReceivedPromises = append(archives.ReceivedPromises, *promise1)
	assert.Equal(t, len(archives.ReceivedPromises), 2)

	ok = manager.HasSignerPromised(1)
Caro Axel's avatar
Caro Axel committed
202
	assert.Equal(t, ok, true)
Caro Axel's avatar
Caro Axel committed
203

204
	promise2 := &Promise{
Caro Axel's avatar
Caro Axel committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218
		RecipientKeyIndex: 0,
		SenderKeyIndex:    1,
	}
	archives.ReceivedPromises = append(archives.ReceivedPromises, *promise2)
	assert.Equal(t, len(archives.ReceivedPromises), 3)

	ok = manager.HasSignerPromised(1)
	assert.Equal(t, ok, true)
}

// TO MODIFY WHEN SOURCE FUNCTION WILL BE UPDATED
func TestAddToAbort(t *testing.T) {
	// TODO
	// Test the abortedIndex field, when promises will be implemented
219
	archives := NewSignatureArchives(signatureUUIDBson, sequence, signersEntities, contractDocumentHash, seal)
Caro Axel's avatar
Caro Axel committed
220 221 222 223 224 225 226 227 228 229 230 231 232
	manager := &ArchivesManager{
		DB:       dbManager,
		Archives: archives,
	}
	promise := &cAPI.Promise{
		Context: &cAPI.Context{
			Signers:  [][]byte{},
			Sequence: sequence,
		},
	}

	assert.Equal(t, len(archives.AbortedSigners), 0)

233
	sIndex, err := GetIndexOfSigner(promise, signers[1])
Caro Axel's avatar
Caro Axel committed
234 235 236
	assert.Equal(t, err.Error(), "Signer's hash couldn't be matched")

	promise.Context.Signers = signers
237
	sIndex, err = GetIndexOfSigner(promise, signers[1])
Caro Axel's avatar
Caro Axel committed
238 239 240 241 242 243 244 245 246 247 248 249 250
	assert.Equal(t, err, nil)
	assert.Equal(t, sIndex, uint32(1))

	manager.AddToAbort(sIndex)
	assert.Equal(t, len(archives.AbortedSigners), 1)
	assert.Equal(t, archives.AbortedSigners[0].SignerIndex, uint32(1))

	manager.AddToAbort(sIndex)
	assert.Equal(t, len(archives.AbortedSigners), 1)
	assert.Equal(t, archives.AbortedSigners[0].SignerIndex, uint32(1))
}

func TestAddToDishonest(t *testing.T) {
251
	archives := NewSignatureArchives(signatureUUIDBson, sequence, signersEntities, contractDocumentHash, seal)
Caro Axel's avatar
Caro Axel committed
252 253 254 255 256 257 258 259 260 261 262 263 264
	manager := &ArchivesManager{
		DB:       dbManager,
		Archives: archives,
	}
	promise := &cAPI.Promise{
		Context: &cAPI.Context{
			Signers:  [][]byte{},
			Sequence: sequence,
		},
	}

	assert.Equal(t, len(archives.DishonestSigners), 0)

265
	sIndex, err := GetIndexOfSigner(promise, signers[1])
Caro Axel's avatar
Caro Axel committed
266 267 268
	assert.Equal(t, err.Error(), "Signer's hash couldn't be matched")

	promise.Context.Signers = signers
269
	sIndex, err = GetIndexOfSigner(promise, signers[1])
Caro Axel's avatar
Caro Axel committed
270 271 272 273 274 275 276 277 278 279 280 281 282
	assert.Equal(t, err, nil)
	assert.Equal(t, sIndex, uint32(1))

	manager.AddToDishonest(sIndex)
	assert.Equal(t, len(archives.DishonestSigners), 1)
	assert.Equal(t, archives.DishonestSigners[0], uint32(1))

	manager.AddToDishonest(sIndex)
	assert.Equal(t, len(archives.DishonestSigners), 1)
	assert.Equal(t, archives.DishonestSigners[0], uint32(1))
}

func TestAddPromise(t *testing.T) {
283
	archives := NewSignatureArchives(signatureUUIDBson, sequence, signersEntities, contractDocumentHash, seal)
Caro Axel's avatar
Caro Axel committed
284 285 286 287 288 289
	manager := &ArchivesManager{
		DB:       dbManager,
		Archives: archives,
	}
	assert.Equal(t, len(archives.ReceivedPromises), 0)

290
	promise0 := &Promise{
Caro Axel's avatar
Caro Axel committed
291 292 293 294
		RecipientKeyIndex: 1,
		SenderKeyIndex:    0,
		SequenceIndex:     0,
	}
295
	promise1 := &Promise{
Caro Axel's avatar
Caro Axel committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
		RecipientKeyIndex: 0,
		SenderKeyIndex:    1,
		SequenceIndex:     1,
	}

	manager.AddPromise(promise0)
	assert.Equal(t, len(archives.ReceivedPromises), 1)
	assert.Equal(t, archives.ReceivedPromises[0].RecipientKeyIndex, uint32(1))
	assert.Equal(t, archives.ReceivedPromises[0].SenderKeyIndex, uint32(0))
	assert.Equal(t, archives.ReceivedPromises[0].SequenceIndex, uint32(0))

	manager.AddPromise(promise1)
	assert.Equal(t, len(archives.ReceivedPromises), 2)
	assert.Equal(t, archives.ReceivedPromises[1].RecipientKeyIndex, uint32(0))
	assert.Equal(t, archives.ReceivedPromises[1].SenderKeyIndex, uint32(1))
	assert.Equal(t, archives.ReceivedPromises[1].SequenceIndex, uint32(1))

	manager.AddPromise(promise0)
	assert.Equal(t, len(archives.ReceivedPromises), 2)
}