/
message.spec.js
183 lines (147 loc) · 4.13 KB
/
message.spec.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* eslint-env mocha */
'use strict'
const expect = require('chai').expect
const fs = require('fs')
const Block = require('ipfs-block')
const protobuf = require('protocol-buffers')
const path = require('path')
const mh = require('multihashes')
const pbm = protobuf(fs.readFileSync(path.join(__dirname, '../src/message/message.proto')))
const BitswapMessage = require('../src/message')
describe('BitswapMessage', () => {
it('go interop', () => {
const goEncoded = new Buffer('CioKKAoiEiAs8k26X7CjDiboOyrFueKeGxYeXB+nQl5zBDNik4uYJBAKGAA=', 'base64')
const m = new BitswapMessage(false)
m.addEntry(mh.fromB58String('QmRN6wdp1S2A5EtjW9A3M1vKSBuQQGcgvuhoMUoEz4iiT5'), 10)
expect(
BitswapMessage.fromProto(goEncoded)
).to.be.eql(
m
)
expect(
m.toProto()
).to.be.eql(
goEncoded
)
})
it('append wanted', () => {
const str = 'foo'
const block = new Block(str)
const m = new BitswapMessage(true)
m.addEntry(block.key, 1)
expect(
pbm.Message.decode(m.toProto()).wantlist.entries[0]
).to.be.eql({
block: block.key,
priority: 1,
cancel: false
})
})
it('encodes blocks', () => {
const block = new Block('hello')
const m = new BitswapMessage(true)
m.addBlock(block)
expect(
pbm.Message.decode(m.toProto()).blocks
).to.be.eql([
block.data
])
})
it('new message fromProto', () => {
const raw = pbm.Message.encode({
wantlist: {
entries: [{
block: new Buffer('hello'),
cancel: false
}],
full: true
},
blocks: ['hello', 'world']
})
const protoMessage = BitswapMessage.fromProto(raw)
expect(
protoMessage.full
).to.be.eql(
true
)
expect(
Array.from(protoMessage.wantlist)
).to.be.eql([
[mh.toB58String(new Buffer('hello')), new BitswapMessage.Entry(new Buffer('hello'), 0, false)]
])
const b1 = new Block('hello')
const b2 = new Block('world')
expect(
Array.from(protoMessage.blocks)
).to.be.eql([
[mh.toB58String(b1.key), b1],
[mh.toB58String(b2.key), b2]
])
})
it('duplicates', () => {
const b = new Block('foo')
const m = new BitswapMessage(true)
m.addEntry(b.key, 1)
m.addEntry(b.key, 1)
expect(m.wantlist.size).to.be.eql(1)
m.addBlock(b)
m.addBlock(b)
expect(m.blocks.size).to.be.eql(1)
})
it('empty', () => {
const m = new BitswapMessage(true)
expect(
m.empty
).to.be.eql(
true
)
})
it('non full message', () => {
const m = new BitswapMessage(false)
expect(
pbm.Message.decode(m.toProto()).wantlist.full
).to.be.eql(
false
)
})
describe('.equals', () => {
it('true, same message', () => {
const b = new Block('foo')
const m1 = new BitswapMessage(true)
const m2 = new BitswapMessage(true)
m1.addEntry(b.key, 1)
m1.addBlock(b)
m2.addEntry(b.key, 1)
m2.addBlock(b)
expect(m1.equals(m2)).to.be.eql(true)
})
it('false, different entries', () => {
const b = new Block('foo')
const m1 = new BitswapMessage(true)
const m2 = new BitswapMessage(true)
m1.addEntry(b.key, 1)
m1.addBlock(b)
m2.addEntry(b.key, 2)
m2.addBlock(b)
expect(m1.equals(m2)).to.be.eql(false)
})
})
describe('Entry', () => {
it('exposes the wantlist entry properties', () => {
const entry = new BitswapMessage.Entry(new Buffer('hello'), 5, false)
expect(entry).to.have.property('key')
expect(entry).to.have.property('priority', 5)
expect(entry).to.have.property('cancel', false)
})
it('allows setting properties on the wantlist entry', () => {
const entry = new BitswapMessage.Entry(new Buffer('hello'), 5, false)
expect(entry.entry).to.have.property('key')
expect(entry.entry).to.have.property('priority', 5)
entry.key = new Buffer('world')
entry.priority = 2
expect(entry.entry).to.have.property('key')
expect(entry.entry.key.equals(new Buffer('world')))
expect(entry.entry).to.have.property('priority', 2)
})
})
})