packet-protocols 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c1aba4c63ddb0e0c6340ee262953c8d7241036f3
4
- data.tar.gz: 335edd5dbd1e6d375d86a6f4a6a2cbdfcde57890
3
+ metadata.gz: 1b0fb967c63926543adcf75b50b47530383258e4
4
+ data.tar.gz: eeef4792b002e62fab8cac751cfd8bb9c2f13b62
5
5
  SHA512:
6
- metadata.gz: 151c96d8f69edae4c26df54b802378ad0aa279d2370daa7956403f9ad9cbdb54b1118a622f793461be29e3040c9d69324428c52723432caae72e05f22316ca24
7
- data.tar.gz: 94d6c2988bc3ef0895926d679c7f7f0c7a515ea3d2702b944f15a0d14c973fdae761cb43093ffcab51c16c13317dc1bd616b8f63b7a5dd0a47dc5b5e5ab8e5f7
6
+ metadata.gz: bea53b197dbc675b690c121905cf3811caface576284bdd9d40a8375440b0796ccbb803856c75a339c52433a0e4fd3cc73b34cdaa755328477d73cba4d350b0c
7
+ data.tar.gz: c8ad9ea0388ce2b867e652ce53005fce67cd17376ca52f1aee68f4e36a301266be5c7e357acd9bb367282d265a6ab9d59db69111c0004e777be697d0c9f72631
@@ -0,0 +1,3 @@
1
+ module PacketProtocols
2
+ VERSION = '0.1.2'
3
+ end
data/spec/arp_spec.rb ADDED
@@ -0,0 +1,60 @@
1
+ require_relative '../lib/arp'
2
+
3
+ describe Arp do
4
+ let(:binary) {
5
+ [
6
+ 0, 1, # hardware
7
+ 8, 0, # protocol
8
+ 6, # hardware_length
9
+ 4, # protocol_length
10
+ 0, 2, # operation
11
+ 0, 0, 0, 0, 0, 1, # mac_source
12
+ 192, 168, 0, 1, # ip_source
13
+ 0, 0, 0, 0, 0, 2, # mac_destination
14
+ 192, 168, 0, 2 # ip_destination
15
+ ].pack('C*')
16
+ }
17
+
18
+ it 'must read binary data' do
19
+ arp = Arp.read(binary)
20
+ expect(arp.hardware).to eq(:ethernet)
21
+ expect(arp.protocol).to eq(:ipv4)
22
+ expect(arp.hardware_length).to eq(6)
23
+ expect(arp.protocol_length).to eq(4)
24
+ expect(arp.operation).to eq(:reply)
25
+ expect(arp.mac_source).to eq('00:00:00:00:00:01')
26
+ expect(arp.ip_source).to eq('192.168.0.1')
27
+ expect(arp.mac_destination).to eq('00:00:00:00:00:02')
28
+ expect(arp.ip_destination).to eq('192.168.0.2')
29
+ expect(arp.to_binary_s).to eq(binary)
30
+ end
31
+
32
+ it 'must initialize with default values' do
33
+ arp = Arp.new
34
+ expect(arp.hardware).to eq(:ethernet)
35
+ expect(arp.protocol).to eq(:ipv4)
36
+ expect(arp.hardware_length).to eq(6)
37
+ expect(arp.protocol_length).to eq(4)
38
+ expect(arp.operation).to eq(:request)
39
+ expect(arp.mac_source).to eq('00:00:00:00:00:00')
40
+ expect(arp.ip_source).to eq('0.0.0.0')
41
+ expect(arp.mac_destination).to eq('00:00:00:00:00:00')
42
+ expect(arp.ip_destination).to eq('0.0.0.0')
43
+ end
44
+
45
+ it 'must be able to change accessors' do
46
+ arp = Arp.new
47
+ arp.operation = :reply
48
+ expect(arp.operation).to eq(:reply)
49
+ arp.mac_source = '01:23:45:67:89:01'
50
+ expect(arp.mac_source).to eq('01:23:45:67:89:01')
51
+ arp.ip_source = '128.0.0.1'
52
+ expect(arp.ip_source).to eq('128.0.0.1')
53
+ end
54
+
55
+ it 'must initialize with options' do
56
+ arp = Arp.new(operation: :reply, ip_source: '192.168.0.1')
57
+ expect(arp.operation).to eq(:reply)
58
+ expect(arp.ip_source).to eq('192.168.0.1')
59
+ end
60
+ end
@@ -0,0 +1,296 @@
1
+ require_relative '../lib/ethernet'
2
+
3
+ describe Ethernet do
4
+ let(:binary) {
5
+ [
6
+ 0, 0, 0, 0, 0, 2, # mac_destination
7
+ 0, 0, 0, 0, 0, 1, # mac_source
8
+ 0x81, 0, # protocol
9
+
10
+ # vlan
11
+ 0x20, 1, # pcp & cfi & vlan_id
12
+ 8, 6, # protocol
13
+
14
+ # arp
15
+ 0, 1, # hardware
16
+ 8, 0, # protocol
17
+ 6, # hardware_length
18
+ 4, # protocol_length
19
+ 0, 2, # operation
20
+ 0, 0, 0, 0, 0, 1, # mac_source
21
+ 192, 168, 0, 1, # ip_source
22
+ 0, 0, 0, 0, 0, 2, # mac_destination
23
+ 192, 168, 0, 2 # ip_destination
24
+ ].pack('C*')
25
+ }
26
+
27
+ it 'must read binary data' do
28
+ eth = Ethernet.read(binary)
29
+ expect(eth.mac_destination).to eq('00:00:00:00:00:02')
30
+ expect(eth.mac_source).to eq('00:00:00:00:00:01')
31
+ expect(eth.protocol).to eq(:arp)
32
+ expect(eth.has_vlan?).to be true
33
+ expect(eth.payload.hardware).to eq(:ethernet)
34
+ expect(eth.payload.ip_source).to eq('192.168.0.1')
35
+ expect(eth.to_binary_s).to eq(binary)
36
+ end
37
+
38
+ it 'must initialize with default values' do
39
+ eth = Ethernet.new
40
+ expect(eth.mac_destination).to eq('00:00:00:00:00:00')
41
+ expect(eth.mac_source).to eq('00:00:00:00:00:00')
42
+ expect(eth.protocol).to eq(:ipv4)
43
+ expect(eth.payload.protocol).to eq(:tcp)
44
+ expect(eth.length).to eq(54)
45
+ end
46
+
47
+ it 'must be able to change accessors' do
48
+ eth = Ethernet.new
49
+ eth.mac_destination = '00:00:00:00:00:02'
50
+ expect(eth.mac_destination).to eq('00:00:00:00:00:02')
51
+ end
52
+
53
+ it 'must initialize with options' do
54
+ eth = Ethernet.new(mac_destination: '00:00:00:00:00:02')
55
+ expect(eth.mac_destination).to eq('00:00:00:00:00:02')
56
+ end
57
+ end
58
+
59
+
60
+ # require 'spec_helper'
61
+ #
62
+ # describe Ethernet do
63
+ # it 'should read an unkown protocol' do
64
+ # packet = Ethernet.read [
65
+ # 0, 0, 0, 0, 0, 2, # mac_destination
66
+ # 0, 0, 0, 0, 0, 1, # mac_source
67
+ # 0, 0 # protocol
68
+ # ].pack('C*')
69
+ # expect(packet.mac_destination).to eq('00:00:00:00:00:02')
70
+ # expect(packet.mac_source).to eq('00:00:00:00:00:01')
71
+ # expect(packet.protocol).to eq(0)
72
+ # expect(packet.payload).to be_empty
73
+ # end
74
+ #
75
+ # it 'should read a VLAN packet' do
76
+ # packet = Ethernet.read [
77
+ # 0, 0, 0, 0, 0, 2, # mac_destination
78
+ # 0, 0, 0, 0, 0, 1, # mac_source
79
+ # 0x81, 0, # protocol
80
+ #
81
+ # # vlan
82
+ # 0x20, 1, # pcp & cfi & vlan_id
83
+ # 0, 0 # protocol
84
+ # ].pack('C*')
85
+ # expect(packet.mac_destination).to eq('00:00:00:00:00:02')
86
+ # expect(packet.mac_source).to eq('00:00:00:00:00:01')
87
+ # expect(packet.protocol).to eq(0)
88
+ # expect(packet.payload).to be_empty
89
+ # expect(packet.vlan.pcp).to eq(1)
90
+ # expect(packet.vlan.cfi).to eq(0)
91
+ # expect(packet.vlan.id).to eq(1)
92
+ # end
93
+ #
94
+ # describe 'with ARP' do
95
+ # it 'should read binary' do
96
+ # packet = Ethernet.read [
97
+ # 0, 0, 0, 0, 0, 2, # mac_destination
98
+ # 0, 0, 0, 0, 0, 1, # mac_source
99
+ # 8, 6, # protocol
100
+ #
101
+ # # arp
102
+ # 0, 1, # hardware
103
+ # 8, 0, # protocol
104
+ # 6, # hardware_length
105
+ # 4, # protocol_length
106
+ # 0, 2, # operation
107
+ # 0, 0, 0, 0, 0, 1, # mac_source
108
+ # 192, 168, 0, 1, # ip_source
109
+ # 0, 0, 0, 0, 0, 2, # mac_destination
110
+ # 192, 168, 0, 2 # ip_destination
111
+ # ].pack('C*')
112
+ # expect(packet.mac_destination).to eq('00:00:00:00:00:02')
113
+ # expect(packet.mac_source).to eq('00:00:00:00:00:01')
114
+ # expect(packet.protocol).to eq(:arp)
115
+ # expect(packet.payload.hardware).to eq(:ethernet)
116
+ # expect(packet.payload.protocol).to eq(:ipv4)
117
+ # expect(packet.payload.hardware_length).to eq(6)
118
+ # expect(packet.payload.protocol_length).to eq(4)
119
+ # expect(packet.payload.operation).to eq(:reply)
120
+ # expect(packet.payload.mac_source).to eq('00:00:00:00:00:01')
121
+ # expect(packet.payload.ip_source).to eq('192.168.0.1')
122
+ # expect(packet.payload.mac_destination).to eq('00:00:00:00:00:02')
123
+ # expect(packet.payload.ip_destination).to eq('192.168.0.2')
124
+ # end
125
+ # end
126
+ #
127
+ # describe 'with IPv4/TCP' do
128
+ # it 'should read binary' do
129
+ # packet = Ethernet.read [
130
+ # 0, 0, 0, 0, 0, 2, # mac_destination
131
+ # 0, 0, 0, 0, 0, 1, # mac_source
132
+ # 8, 0, # protocol
133
+ #
134
+ # # ipv4
135
+ # 0x45, # version & header_length
136
+ # 7, # tos
137
+ # 0, 44, # total_length
138
+ # 0, 1, # identifier
139
+ # 0, 0, # flags & fragment_offset
140
+ # 64, # time_to_live
141
+ # 6, # protocol
142
+ # 0x73, 0x04, # checksum
143
+ # 192, 168, 0, 1, # ip_source
144
+ # 192, 168, 0, 2, # ip_destination
145
+ #
146
+ # # tcp
147
+ # 0x10, 0, # source_port
148
+ # 0x20, 0, # destination_port
149
+ # 0, 0, 0, 1, # sequence_number
150
+ # 0, 0, 0, 2, # acknowledge_number
151
+ # 0x51, 0xff, # data_offset &
152
+ # 0, 100, # window_size
153
+ # 0, 0, # checksum
154
+ # 0, 0, # urgent_pointer
155
+ # 1, 2, 3, 4 # payload
156
+ # ].pack('C*')
157
+ # expect(packet.mac_destination).to eq('00:00:00:00:00:02')
158
+ # expect(packet.mac_source).to eq('00:00:00:00:00:01')
159
+ # expect(packet.protocol).to eq(:ipv4)
160
+ # expect(packet.payload.version).to eq(4)
161
+ # expect(packet.payload.header_length).to eq(5)
162
+ # expect(packet.payload.tos).to eq(7)
163
+ # expect(packet.payload.total_length).to eq(44)
164
+ # expect(packet.payload.identifier).to eq(1)
165
+ # expect(packet.payload.flags).to eq(0)
166
+ # expect(packet.payload.fragment_offset).to eq(0)
167
+ # expect(packet.payload.time_to_live).to eq(64)
168
+ # expect(packet.payload.protocol).to eq(:tcp)
169
+ # expect(packet.payload.checksum).to eq(0x7304)
170
+ # expect(packet.payload.ip_source).to eq('192.168.0.1')
171
+ # expect(packet.payload.ip_destination).to eq('192.168.0.2')
172
+ # expect(packet.payload.options).to be_empty
173
+ # expect(packet.payload.payload.source_port).to eq(0x1000)
174
+ # expect(packet.payload.payload.destination_port).to eq(0x2000)
175
+ # expect(packet.payload.payload.sequence_number).to eq(1)
176
+ # expect(packet.payload.payload.acknowledge_number).to eq(2)
177
+ # expect(packet.payload.payload.data_offset).to eq(5)
178
+ # expect(packet.payload.payload.ns).to eq(1)
179
+ # expect(packet.payload.payload.cwr).to eq(1)
180
+ # expect(packet.payload.payload.ece).to eq(1)
181
+ # expect(packet.payload.payload.urg).to eq(1)
182
+ # expect(packet.payload.payload.ack).to eq(1)
183
+ # expect(packet.payload.payload.psh).to eq(1)
184
+ # expect(packet.payload.payload.rst).to eq(1)
185
+ # expect(packet.payload.payload.syn).to eq(1)
186
+ # expect(packet.payload.payload.fin).to eq(1)
187
+ # expect(packet.payload.payload.window_size).to eq(100)
188
+ # expect(packet.payload.payload.checksum).to eq(0)
189
+ # expect(packet.payload.payload.urgent_pointer).to eq(0)
190
+ # expect(packet.payload.payload.options).to be_empty
191
+ # expect(packet.payload.payload.payload).to eq([1, 2, 3, 4].pack('C*'))
192
+ # end
193
+ # end
194
+ #
195
+ # describe 'with IPv4/UDP' do
196
+ # it 'should read binary' do
197
+ # packet = Ethernet.read [
198
+ # 0, 0, 0, 0, 0, 2, # mac_destination
199
+ # 0, 0, 0, 0, 0, 1, # mac_source
200
+ # 8, 0, # protocol
201
+ #
202
+ # # ipv4
203
+ # 0x45, # version & header_length
204
+ # 7, # tos
205
+ # 0, 44, # total_length
206
+ # 0, 1, # identifier
207
+ # 0, 0, # flags & fragment_offset
208
+ # 64, # time_to_live
209
+ # 17, # protocol
210
+ # 0xc5, 0x5b, # checksum
211
+ # 192, 168, 0, 1, # ip_source
212
+ # 192, 168, 0, 2, # ip_destination
213
+ #
214
+ # # udp
215
+ # 0x10, 0, # source_port
216
+ # 0x20, 0, # destination_port
217
+ # 0, 4, # len
218
+ # 0, 0, # checksum
219
+ # 1, 2, 3, 4 # payload
220
+ # ].pack('C*')
221
+ # expect(packet.mac_destination).to eq('00:00:00:00:00:02')
222
+ # expect(packet.mac_source).to eq('00:00:00:00:00:01')
223
+ # expect(packet.protocol).to eq(:ipv4)
224
+ # expect(packet.payload.version).to eq(4)
225
+ # expect(packet.payload.header_length).to eq(5)
226
+ # expect(packet.payload.tos).to eq(7)
227
+ # expect(packet.payload.total_length).to eq(44)
228
+ # expect(packet.payload.identifier).to eq(1)
229
+ # expect(packet.payload.flags).to eq(0)
230
+ # expect(packet.payload.fragment_offset).to eq(0)
231
+ # expect(packet.payload.time_to_live).to eq(64)
232
+ # expect(packet.payload.protocol).to eq(:udp)
233
+ # expect(packet.payload.checksum).to eq(0xc55b)
234
+ # expect(packet.payload.ip_source).to eq('192.168.0.1')
235
+ # expect(packet.payload.ip_destination).to eq('192.168.0.2')
236
+ # expect(packet.payload.options).to be_empty
237
+ # expect(packet.payload.payload.source_port).to eq(0x1000)
238
+ # expect(packet.payload.payload.destination_port).to eq(0x2000)
239
+ # expect(packet.payload.payload.len).to eq(4)
240
+ # expect(packet.payload.payload.checksum).to eq(0)
241
+ # expect(packet.payload.payload.payload).to eq([1, 2, 3, 4].pack('C*'))
242
+ # end
243
+ # end
244
+ #
245
+ # describe 'with IPv4/ICMP' do
246
+ # it 'should read binary' do
247
+ # packet = Ethernet.read [
248
+ # 0, 0, 0, 0, 0, 2, # mac_destination
249
+ # 0, 0, 0, 0, 0, 1, # mac_source
250
+ # 8, 0, # protocol
251
+ #
252
+ # # ipv4
253
+ # 0x45, # version & header_length
254
+ # 7, # tos
255
+ # 0, 44, # total_length
256
+ # 0, 1, # identifier
257
+ # 0, 0, # flags & fragment_offset
258
+ # 64, # time_to_live
259
+ # 1, # protocol
260
+ # 0xf5, 0x6d, # checksum
261
+ # 192, 168, 0, 1, # ip_source
262
+ # 192, 168, 0, 2, # ip_destination
263
+ #
264
+ # # icmp
265
+ # 0, # type
266
+ # 0, # code
267
+ # 0, 0, # checksum
268
+ # 0, 1, # identifier
269
+ # 0, 1, # sequence_number
270
+ # 1, 2, 3, 4 # data
271
+ # ].pack('C*')
272
+ # expect(packet.mac_destination).to eq('00:00:00:00:00:02')
273
+ # expect(packet.mac_source).to eq('00:00:00:00:00:01')
274
+ # expect(packet.protocol).to eq(:ipv4)
275
+ # expect(packet.payload.version).to eq(4)
276
+ # expect(packet.payload.header_length).to eq(5)
277
+ # expect(packet.payload.tos).to eq(7)
278
+ # expect(packet.payload.total_length).to eq(44)
279
+ # expect(packet.payload.identifier).to eq(1)
280
+ # expect(packet.payload.flags).to eq(0)
281
+ # expect(packet.payload.fragment_offset).to eq(0)
282
+ # expect(packet.payload.time_to_live).to eq(64)
283
+ # expect(packet.payload.protocol).to eq(:icmp)
284
+ # expect(packet.payload.checksum).to eq(0xf56d)
285
+ # expect(packet.payload.ip_source).to eq('192.168.0.1')
286
+ # expect(packet.payload.ip_destination).to eq('192.168.0.2')
287
+ # expect(packet.payload.options).to be_empty
288
+ # expect(packet.payload.payload.type).to eq(:echo_reply)
289
+ # expect(packet.payload.payload.code).to eq(0)
290
+ # expect(packet.payload.payload.checksum).to eq(0)
291
+ # expect(packet.payload.payload.identifier).to eq(1)
292
+ # expect(packet.payload.payload.sequence_number).to eq(1)
293
+ # expect(packet.payload.payload.data).to eq([1, 2, 3, 4].pack('C*'))
294
+ # end
295
+ # end
296
+ # end
data/spec/icmp_spec.rb ADDED
@@ -0,0 +1,49 @@
1
+ require_relative '../lib/icmp'
2
+
3
+ describe Icmp do
4
+ let(:binary) {
5
+ [
6
+ 0, # type
7
+ 0, # code
8
+ 0, 0, # checksum
9
+ 0, 1, # identifier
10
+ 0, 1, # sequence_number
11
+ 1, 2, 3, 4 # data
12
+ ].pack('C*')
13
+ }
14
+
15
+ it 'must read binary data' do
16
+ icmp = Icmp.read(binary)
17
+ expect(icmp.type).to eq(:echo_reply)
18
+ expect(icmp.code).to eq(0)
19
+ expect(icmp.checksum).to eq(0)
20
+ expect(icmp.identifier).to eq(1)
21
+ expect(icmp.sequence_number).to eq(1)
22
+ expect(icmp.data).to eq([1, 2, 3, 4].pack('C*'))
23
+ expect(icmp.to_binary_s).to eq(binary)
24
+ end
25
+
26
+ it 'must initialize with default values' do
27
+ icmp = Icmp.new
28
+ expect(icmp.type).to eq(:echo_request)
29
+ expect(icmp.code).to eq(0)
30
+ expect(icmp.checksum).to eq(0)
31
+ expect(icmp.identifier).to eq(0)
32
+ expect(icmp.sequence_number).to eq(0)
33
+ expect(icmp.data).to be_empty
34
+ end
35
+
36
+ it 'must be able to change accessors' do
37
+ icmp = Icmp.new
38
+ icmp.type = :echo_reply
39
+ expect(icmp.type).to eq(:echo_reply)
40
+ icmp.data = [1, 2, 3, 4].pack('C*')
41
+ expect(icmp.data).to eq([1, 2, 3, 4].pack('C*'))
42
+ end
43
+
44
+ it 'must initialize with options' do
45
+ icmp = Icmp.new(type: :echo_reply, sequence_number: 123)
46
+ expect(icmp.type).to eq(:echo_reply)
47
+ expect(icmp.sequence_number).to eq(123)
48
+ end
49
+ end
data/spec/ipv4_spec.rb ADDED
@@ -0,0 +1,77 @@
1
+ require_relative '../lib/ipv4'
2
+
3
+ describe Ipv4 do
4
+ let(:binary) {
5
+ [
6
+ 0x45, # version & header_length
7
+ 0xe0, # dscp & ecn
8
+ 0, 32, # total_length
9
+ 0, 1, # identifier
10
+ 0, 0, # flags & fragment_offset
11
+ 64, # time_to_live
12
+ 17, # protocol
13
+ 0xc5, 0x5b, # checksum
14
+ 192, 168, 0, 1, # ip_source
15
+ 192, 168, 0, 2, # ip_destination
16
+
17
+ # udp
18
+ 0, 10, # source_port
19
+ 0, 20, # destination_port
20
+ 0, 12, # len
21
+ 0, 0, # checksum
22
+ 1, 2, 3, 4 # payload
23
+ ].pack('C*')
24
+ }
25
+
26
+ it 'must read binary data' do
27
+ ipv4 = Ipv4.read(binary)
28
+ expect(ipv4.version).to eq(4)
29
+ expect(ipv4.header_length).to eq(5)
30
+ expect(ipv4.dscp).to eq(56)
31
+ expect(ipv4.ecn).to eq(0)
32
+ expect(ipv4.total_length).to eq(32)
33
+ expect(ipv4.identifier).to eq(1)
34
+ expect(ipv4.df).to eq(false)
35
+ expect(ipv4.mf).to eq(false)
36
+ expect(ipv4.fragment_offset).to eq(0)
37
+ expect(ipv4.time_to_live).to eq(64)
38
+ expect(ipv4.protocol).to eq(:udp)
39
+ expect(ipv4.checksum).to eq(0xc55b)
40
+ expect(ipv4.ip_source).to eq('192.168.0.1')
41
+ expect(ipv4.ip_destination).to eq('192.168.0.2')
42
+ expect(ipv4.options).to be_empty
43
+ expect(ipv4.payload.source_port).to eq(10)
44
+ expect(ipv4.to_binary_s).to eq(binary)
45
+ end
46
+
47
+ it 'must initialize with default values' do
48
+ ipv4 = Ipv4.new
49
+ expect(ipv4.version).to eq(4)
50
+ expect(ipv4.header_length).to eq(5)
51
+ expect(ipv4.dscp).to eq(0)
52
+ expect(ipv4.ecn).to eq(0)
53
+ expect(ipv4.total_length).to eq(40)
54
+ expect(ipv4.identifier).to eq(0)
55
+ expect(ipv4.df).to eq(false)
56
+ expect(ipv4.mf).to eq(false)
57
+ expect(ipv4.fragment_offset).to eq(0)
58
+ expect(ipv4.time_to_live).to eq(64)
59
+ expect(ipv4.protocol).to eq(:tcp)
60
+ expect(ipv4.checksum).to eq(0)
61
+ expect(ipv4.ip_source).to eq('0.0.0.0')
62
+ expect(ipv4.ip_destination).to eq('0.0.0.0')
63
+ expect(ipv4.options).to be_empty
64
+ expect(ipv4.payload.source_port).to eq(0)
65
+ end
66
+
67
+ it 'must be able to change accessors' do
68
+ ipv4 = Ipv4.new
69
+ ipv4.identifier = 10
70
+ expect(ipv4.identifier).to eq(10)
71
+ end
72
+
73
+ it 'must initialize with options' do
74
+ ipv4 = Ipv4.new(identifier: 10)
75
+ expect(ipv4.identifier).to eq(10)
76
+ end
77
+ end
data/spec/tcp_spec.rb ADDED
@@ -0,0 +1,60 @@
1
+ require_relative '../lib/tcp'
2
+
3
+ describe Tcp do
4
+ let(:binary) {
5
+ [
6
+ 0, 10, # source_port
7
+ 0, 20, # destination_port
8
+ 0, 0, 0, 1, # sequence_number
9
+ 0, 0, 0, 2, # acknowledge_number
10
+ 0x51, 0xff, # data_offset & flags
11
+ 0, 100, # window_size
12
+ 0, 0, # checksum
13
+ 0, 0, # urgent_pointer
14
+ 1, 2, 3, 4 # payload
15
+ ].pack('C*')
16
+ }
17
+
18
+ it 'must read binary data' do
19
+ tcp = Tcp.read(binary)
20
+ expect(tcp.source_port).to eq(10)
21
+ expect(tcp.destination_port).to eq(20)
22
+ expect(tcp.sequence_number).to eq(1)
23
+ expect(tcp.acknowledge_number).to eq(2)
24
+ expect(tcp.data_offset).to eq(5)
25
+ expect(tcp.ns).to eq(true)
26
+ expect(tcp.cwr).to eq(true)
27
+ expect(tcp.ece).to eq(true)
28
+ expect(tcp.urg).to eq(true)
29
+ expect(tcp.ack).to eq(true)
30
+ expect(tcp.psh).to eq(true)
31
+ expect(tcp.rst).to eq(true)
32
+ expect(tcp.syn).to eq(true)
33
+ expect(tcp.fin).to eq(true)
34
+ expect(tcp.window_size).to eq(100)
35
+ expect(tcp.checksum).to eq(0)
36
+ expect(tcp.urgent_pointer).to eq(0)
37
+ expect(tcp.options).to be_empty
38
+ expect(tcp.payload).to eq([1, 2, 3, 4].pack('C*'))
39
+ expect(tcp.to_binary_s).to eq(binary)
40
+ end
41
+
42
+ it 'must initialize with default values' do
43
+ tcp = Tcp.new
44
+ expect(tcp.source_port).to eq(0)
45
+ expect(tcp.data_offset).to eq(5)
46
+ end
47
+
48
+ it 'must be able to change accessors' do
49
+ tcp = Tcp.new
50
+ tcp.source_port = 10
51
+ expect(tcp.source_port).to eq(10)
52
+ end
53
+
54
+ it 'must initialize with options' do
55
+ tcp = Tcp.new(source_port: 10, options: [1, 2, 3, 4].pack('C*'))
56
+ expect(tcp.source_port).to eq(10)
57
+ expect(tcp.data_offset).to eq(6)
58
+ expect(tcp.options).to eq([1, 2, 3, 4].pack('C*'))
59
+ end
60
+ end
data/spec/udp_spec.rb ADDED
@@ -0,0 +1,45 @@
1
+ require_relative '../lib/udp'
2
+
3
+ describe Udp do
4
+ let(:binary) {
5
+ [
6
+ 0, 10, # source_port
7
+ 0, 20, # destination_port
8
+ 0, 12, # length
9
+ 0, 0, # checksum
10
+ 1, 2, 3, 4 # payload
11
+ ].pack('C*')
12
+ }
13
+
14
+ it 'must read binary data' do
15
+ udp = Udp.read(binary)
16
+ expect(udp.source_port).to eq(10)
17
+ expect(udp.destination_port).to eq(20)
18
+ expect(udp.length).to eq(12)
19
+ expect(udp.checksum).to eq(0)
20
+ expect(udp.payload).to eq([1, 2, 3, 4].pack('C*'))
21
+ expect(udp.to_binary_s).to eq(binary)
22
+ end
23
+
24
+ it 'must initialize with default values' do
25
+ udp = Udp.new
26
+ expect(udp.source_port).to eq(0)
27
+ expect(udp.destination_port).to eq(0)
28
+ expect(udp.length).to eq(8)
29
+ expect(udp.checksum).to eq(0)
30
+ expect(udp.payload).to be_empty
31
+ end
32
+
33
+ it 'must be able to change accessors' do
34
+ udp = Udp.new
35
+ udp.destination_port = 20
36
+ expect(udp.destination_port).to eq(20)
37
+ end
38
+
39
+ it 'must initialize with options' do
40
+ udp = Udp.new(source_port: 10, payload: [1, 2].pack('C*'))
41
+ expect(udp.source_port).to eq(10)
42
+ expect(udp.length).to eq(10)
43
+ expect(udp.payload).to eq([1, 2].pack('C*'))
44
+ end
45
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: packet-protocols
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jérémy Pagé
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-08 00:00:00.000000000 Z
11
+ date: 2016-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bindata-contrib
@@ -50,8 +50,15 @@ files:
50
50
  - lib/icmp.rb
51
51
  - lib/ipv4.rb
52
52
  - lib/packet-protocols.rb
53
+ - lib/packet-protocols/version.rb
53
54
  - lib/tcp.rb
54
55
  - lib/udp.rb
56
+ - spec/arp_spec.rb
57
+ - spec/ethernet_spec.rb
58
+ - spec/icmp_spec.rb
59
+ - spec/ipv4_spec.rb
60
+ - spec/tcp_spec.rb
61
+ - spec/udp_spec.rb
55
62
  homepage: https://github.com/jejepage/packet-protocols
56
63
  licenses:
57
64
  - MIT
@@ -72,8 +79,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
79
  version: '0'
73
80
  requirements: []
74
81
  rubyforge_project:
75
- rubygems_version: 2.4.8
82
+ rubygems_version: 2.5.1
76
83
  signing_key:
77
84
  specification_version: 4
78
85
  summary: Packet Protocols
79
- test_files: []
86
+ test_files:
87
+ - spec/arp_spec.rb
88
+ - spec/ethernet_spec.rb
89
+ - spec/icmp_spec.rb
90
+ - spec/ipv4_spec.rb
91
+ - spec/tcp_spec.rb
92
+ - spec/udp_spec.rb