|
10 | 10 | end |
11 | 11 |
|
12 | 12 | describe '#encode_message' do |
| 13 | + let(:no_body) { "\xE4\x01" } |
| 14 | + let(:body) { "\xE4\x01p2p" } |
13 | 15 | it 'properly encodes messages without a body' do |
14 | 16 | expect(kad.encode_message(1)).to eq("\xE4\x01") |
15 | 17 | end |
|
45 | 47 | expect(payload).to eq('testtesttest') |
46 | 48 | end |
47 | 49 | end |
48 | | - |
49 | | - describe '#decode_pong' do |
50 | | - it 'does not decode overly small pongs' do |
51 | | - expect(kad.decode_pong("\xE4\x61\x01")).to eq(nil) |
52 | | - end |
53 | | - |
54 | | - it 'does not decode overly large pongs' do |
55 | | - expect(kad.decode_pong("\xE4\x61\x01\x02\x03")).to eq(nil) |
56 | | - end |
57 | | - |
58 | | - it 'properly decodes valid pongs' do |
59 | | - expect(kad.decode_pong("\xE4\x61\x9E\x86")).to eq(34462) |
60 | | - end |
61 | | - end |
62 | | - |
63 | | - describe '#decode_bootstrap_peer' do |
64 | | - it 'does not decode overly small peer responses' do |
65 | | - expect(kad.decode_bootstrap_peer("this is too small")).to eq(nil) |
66 | | - end |
67 | | - |
68 | | - it 'does not decode overly large peer responses' do |
69 | | - expect(kad.decode_bootstrap_peer("this is much, much, much too large")).to eq(nil) |
70 | | - end |
71 | | - |
72 | | - it 'properly extracts peer info' do |
73 | | - data = |
74 | | - "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F" + # peer ID |
75 | | - "\x04\x28\xA8\xC0" + # 192.168.40.4 |
76 | | - "\x31\xd4" + # UDP port 54321 |
77 | | - "\x39\x30" + # TCP port 12345 |
78 | | - "\x08" # peer type |
79 | | - peer_id, ip, udp_port, tcp_port, type = kad.decode_bootstrap_peer(data) |
80 | | - expect(peer_id).to eq('3020100070605040B0A09080F0E0D0C') |
81 | | - expect(ip).to eq('192.168.40.4') |
82 | | - expect(udp_port).to eq(54321) |
83 | | - expect(tcp_port).to eq(12345) |
84 | | - expect(type).to eq(8) |
85 | | - end |
86 | | - end |
87 | | - |
88 | | - describe '#decode_bootstrap_peers' do |
89 | | - it 'does not decode overly small bootstrap responses' do |
90 | | - expect(kad.decode_bootstrap_peer("this is too small")).to eq(nil) |
91 | | - end |
92 | | - |
93 | | - it 'does not decode overly large bootstrap responses' do |
94 | | - expect(kad.decode_bootstrap_peer("this is large enough but truncated")).to eq(nil) |
95 | | - end |
96 | | - |
97 | | - it 'properly extracts peers info' do |
98 | | - data = |
99 | | - "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F" + # peer ID |
100 | | - "\x04\x28\xA8\xC0" + # 192.168.40.4 |
101 | | - "\x31\xd4" + # UDP port 54321 |
102 | | - "\x39\x30" + # TCP port 12345 |
103 | | - "\x08" + # peer type |
104 | | - "\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08" + # peer ID |
105 | | - "\x05\x28\xA8\xC0" + # 192.168.40.5 |
106 | | - "\x5c\x11" + # UDP port 4444 |
107 | | - "\xb3\x15" + # TCP port 5555 |
108 | | - "\x09" # peer type |
109 | | - peers = kad.decode_bootstrap_peers(data) |
110 | | - expect(peers.size).to eq(2) |
111 | | - peer1_id, peer1_ip, peer1_udp, peer1_tcp, peer1_type = peers.first |
112 | | - expect(peer1_id).to eq('3020100070605040B0A09080F0E0D0C') |
113 | | - expect(peer1_ip).to eq('192.168.40.4') |
114 | | - expect(peer1_udp).to eq(54321) |
115 | | - expect(peer1_tcp).to eq(12345) |
116 | | - expect(peer1_type).to eq(8) |
117 | | - peer2_id, peer2_ip, peer2_udp, peer2_tcp, peer2_type = peers.last |
118 | | - expect(peer2_id).to eq('2020101040403030606050508080707') |
119 | | - expect(peer2_ip).to eq('192.168.40.5') |
120 | | - expect(peer2_udp).to eq(4444) |
121 | | - expect(peer2_tcp).to eq(5555) |
122 | | - expect(peer2_type).to eq(9) |
123 | | - end |
124 | | - end |
125 | | - |
126 | | - describe '#decode_bootstrap_res' do |
127 | | - it 'properly decodes valid bootstrap responses' do |
128 | | - data = IO.read(File.join(File.dirname(__FILE__), 'kademlia_bootstrap_res.bin')) |
129 | | - peer_id, tcp, version, peers = kad.decode_bootstrap_res(data) |
130 | | - expect(peer_id).to eq('B54A83462529B21EF51FD54B956B07B0') |
131 | | - expect(tcp).to eq(4662) |
132 | | - expect(version).to eq(8) |
133 | | - # don't bother checking every peer |
134 | | - expect(peers.size).to eq(20) |
135 | | - end |
136 | | - end |
137 | | - |
138 | | - describe '#decode_peer_id' do |
139 | | - it 'decodes a peer ID properly' do |
140 | | - bytes = "\x00\x60\x89\x9B\x0A\x0B\xBE\xAE\x45\x35\xCB\x0E\x07\xA1\x77\x71" |
141 | | - peer_id = "9B896000AEBE0B0A0ECB35457177A107" |
142 | | - expect(kad.decode_peer_id(bytes)).to eq(peer_id) |
143 | | - end |
144 | | - end |
145 | | - |
146 | | - describe '#encode_peer' do |
147 | | - skip 'encodes a peer ID properly' do |
148 | | - bytes = "\x00\x60\x89\x9B\x0A\x0B\xBE\xAE\x45\x35\xCB\x0E\x07\xA1\x77\x71" |
149 | | - peer_id = "9B896000AEBE0B0A0ECB35457177A107" |
150 | | - expect(kad.encode_peer_id(peer_id)).to eq(bytes) |
151 | | - end |
152 | | - end |
153 | 50 | end |
0 commit comments