/
test.js
121 lines (108 loc) · 3.55 KB
/
test.js
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
const chai = require('chai')
const utils = require('./')
const BytesLengthError = require('./errors/BytesLengthError')
const crypto = require('crypto')
const Amorph = require('amorph')
const chaiAmorph = require('chai-amorph')
const Q = require('q')
const random = require('random-amorph')
const keccak256 = require('keccak256-amorph')
chai.use(chaiAmorph)
chai.should()
const tempDb = {}
const tempDbApi = {
addFile: function addFile(file) {
const fileHash = keccak256(file)
tempDb[fileHash.to('hex')] = file
},
getFile: function getFile(fileHash) {
return tempDb[fileHash.to('hex')]
}
}
describe('planetoid-utils', () => {
const params = Array(10).fill(0).map(() => {
return {
timestamp: random(4),
sender: random(20),
gigawei: random(4),
documentLength: random(4),
documentHash: random(32),
}
})
const records = []
const downloadedRecords = []
let recordHash = new Amorph(Array(32).fill(0), 'array')
it('should create 10 records', () => {
const _records = params.map((param, index) => {
params[index].previousRecordHash = recordHash
const record = utils.marshalRecord(
param.timestamp,
param.sender,
param.gigawei,
param.documentLength,
param.documentHash,
recordHash
)
recordHash = keccak256(record)
return record
})
records.push(..._records)
})
it('each record should be 96 bytes long', () => {
records.forEach((record) => {
record.to('array').should.have.length(96)
})
})
it('should add all 10 records tempdb', () => {
records.forEach((record) => {
tempDbApi.addFile(record)
})
})
it('should download records', () => {
return utils.downloadRecords(recordHash, (_recordHash) => {
return Q.resolve(tempDbApi.getFile(_recordHash))
}, (_recordHash, record) => {
downloadedRecords.push(record)
return Q.resolve()
})
})
it('downloaded records should match records', () => {
downloadedRecords.should.have.length(records.length)
downloadedRecords.forEach((downloadedRecord, index) => {
downloadedRecord.should.have.keys([
'timestamp',
'sender',
'gigawei',
'documentLength',
'documentHash',
'previousRecordHash'
])
const param = params[params.length - index - 1]
downloadedRecord.timestamp.should.amorphEqual(param.timestamp, 'array')
downloadedRecord.sender.should.amorphEqual(param.sender)
downloadedRecord.gigawei.should.amorphEqual(param.gigawei)
downloadedRecord.documentHash.should.amorphEqual(param.documentHash)
downloadedRecord.previousRecordHash.should.amorphEqual(param.previousRecordHash)
})
})
it('should throw byteslength error when marshalling with bad arguments lengths', () => {
const param = params[0]
;(() => {
utils.marshalRecord(random(3), param.sender, param.gigawei, param.documentLength, param.documentHash, param.previousRecordHash)
}).should.throw(BytesLengthError)
;(() => {
utils.marshalRecord(param.timestamp, param.sender, random(3), param.documentLength, param.documentHash, param.previousRecordHash)
}).should.throw(BytesLengthError)
})
it('should throw byteslength error when unmarshalling with bad record length', () => {
;(() => {
utils.unmarshalRecord(random(0))
}).should.throw(BytesLengthError)
;(() => {
utils.unmarshalRecord(random(32))
}).should.throw(BytesLengthError)
;(() => {
utils.unmarshalRecord(random(128))
}).should.throw(BytesLengthError)
})
})