packetfu 1.1.13 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/workflows/verify.yml +72 -0
- data/.travis.yml +10 -6
- data/LICENSE.txt +1 -1
- data/README.md +8 -8
- data/certs/todb.pem +25 -0
- data/examples/100kpackets.rb +2 -2
- data/examples/ackscan.rb +7 -6
- data/examples/pcap2pcapng.rb +2 -2
- data/examples/readpcap.rb +28 -0
- data/lib/packetfu/capture.rb +1 -1
- data/lib/packetfu/config.rb +2 -2
- data/lib/packetfu/inject.rb +1 -1
- data/lib/packetfu/packet.rb +6 -3
- data/lib/packetfu/pcap.rb +25 -25
- data/lib/packetfu/pcapng/file.rb +1 -1
- data/lib/packetfu/protos/arp.rb +1 -8
- data/lib/packetfu/protos/eth.rb +0 -7
- data/lib/packetfu/protos/hsrp.rb +0 -7
- data/lib/packetfu/protos/icmp/header.rb +7 -10
- data/lib/packetfu/protos/icmp.rb +0 -7
- data/lib/packetfu/protos/icmpv6.rb +4 -17
- data/lib/packetfu/protos/ip/header.rb +2 -2
- data/lib/packetfu/protos/ip/mixin.rb +9 -0
- data/lib/packetfu/protos/ip.rb +0 -8
- data/lib/packetfu/protos/ipv6/mixin.rb +12 -0
- data/lib/packetfu/protos/ipv6.rb +0 -7
- data/lib/packetfu/protos/lldp.rb +1 -8
- data/lib/packetfu/protos/tcp.rb +73 -30
- data/lib/packetfu/protos/udp/header.rb +4 -5
- data/lib/packetfu/protos/udp.rb +6 -18
- data/lib/packetfu/structfu.rb +1 -1
- data/lib/packetfu/version.rb +1 -1
- data/packetfu.gemspec +10 -18
- data/spec/arp_spec.rb +1 -1
- data/spec/capture_spec.rb +137 -0
- data/spec/eth_spec.rb +1 -1
- data/spec/icmp_spec.rb +1 -1
- data/spec/icmpv6_spec.rb +1 -1
- data/spec/inject_spec.rb +95 -0
- data/spec/ip_spec.rb +23 -1
- data/spec/packetfu_spec.rb +1 -1
- data/spec/pcap_spec.rb +3 -3
- data/spec/pcapng/file_spec.rb +1 -1
- data/spec/spec_helper.rb +4 -2
- data/spec/structfu_spec.rb +86 -82
- data/spec/tcp_spec.rb +155 -53
- data/test/sample-ipv6.pcap +0 -0
- data.tar.gz.sig +0 -0
- metadata +64 -37
- metadata.gz.sig +0 -0
- data/test/test_capture.rb +0 -58
- data/test/test_inject.rb +0 -31
- data/test/test_structfu.rb +0 -114
data/spec/inject_spec.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# -*- coding: binary -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'packetfu/protos/eth'
|
4
|
+
require 'packetfu/protos/ip'
|
5
|
+
require 'packetfu/protos/ipv6'
|
6
|
+
require 'packetfu/protos/tcp'
|
7
|
+
require 'packetfu/protos/udp'
|
8
|
+
require 'packetfu/protos/icmp'
|
9
|
+
require 'packetfu/config'
|
10
|
+
require 'packetfu/pcap'
|
11
|
+
require 'packetfu/utils'
|
12
|
+
require 'tempfile'
|
13
|
+
|
14
|
+
include PacketFu
|
15
|
+
|
16
|
+
describe Inject do
|
17
|
+
context "when creating an object from scratch" do
|
18
|
+
before :each do
|
19
|
+
@inject = PacketFu::Inject.new
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should have sane defaults" do
|
23
|
+
expect(@inject.array).to be_kind_of(Array)
|
24
|
+
expect(@inject.stream).to be_kind_of(Array)
|
25
|
+
expect(@inject.iface).to be_kind_of(String)
|
26
|
+
expect(@inject.snaplen).to eql(65535)
|
27
|
+
expect(@inject.promisc).to eql(false)
|
28
|
+
expect(@inject.timeout).to eql(1)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should allow creating an inject object with non-std attributes" do
|
32
|
+
# Can only run this if we're root
|
33
|
+
if Process.uid == 0
|
34
|
+
options = {
|
35
|
+
:iface => PacketFu::Utils::default_int,
|
36
|
+
:snaplen => 0xfffe,
|
37
|
+
:promisc => true,
|
38
|
+
:timeout => 5,
|
39
|
+
}
|
40
|
+
@inject = PacketFu::Capture.new(options)
|
41
|
+
|
42
|
+
expect(@inject.array).to be_kind_of(Array)
|
43
|
+
expect(@inject.stream).to be_kind_of(Array)
|
44
|
+
expect(@inject.iface).to eql(options[:iface])
|
45
|
+
expect(@inject.snaplen).to eql(options[:snaplen])
|
46
|
+
expect(@inject.promisc).to eql(options[:promisc])
|
47
|
+
expect(@inject.timeout).to eql(options[:timeout])
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when injecting on the wire" do
|
53
|
+
before :each do
|
54
|
+
@inject = PacketFu::Inject.new
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should have sane defaults" do
|
58
|
+
expect(@inject.array).to be_kind_of(Array)
|
59
|
+
expect(@inject.stream).to be_kind_of(Array)
|
60
|
+
expect(@inject.iface).to be_kind_of(String)
|
61
|
+
expect(@inject.snaplen).to eql(65535)
|
62
|
+
expect(@inject.promisc).to eql(false)
|
63
|
+
expect(@inject.timeout).to eql(1)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Can only run these if we're root
|
67
|
+
if Process.uid == 0
|
68
|
+
it "should allow creating an inject object with non-std attributes" do
|
69
|
+
udp_packet = PacketFu::UDPPacket.new(:iface => PacketFu::Utils::default_int)
|
70
|
+
udp_packet.ip_dst = PacketFu::Utils.rand_routable_daddr.to_s
|
71
|
+
udp_packet.udp_dport = 12345
|
72
|
+
udp_packet.udp_sport = 12345
|
73
|
+
udp_packet.payload = "PacketFu test packet"
|
74
|
+
udp_packet.recalc
|
75
|
+
|
76
|
+
expect(udp_packet.to_w).to eql([1, 1, 62])
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should allow creating an inject object with non-std attributes" do
|
80
|
+
packet_array = []
|
81
|
+
|
82
|
+
udp_packet = PacketFu::UDPPacket.new(:iface => PacketFu::Utils::default_int)
|
83
|
+
udp_packet.ip_dst = PacketFu::Utils.rand_routable_daddr.to_s
|
84
|
+
udp_packet.udp_dport = 12345
|
85
|
+
udp_packet.udp_sport = 12345
|
86
|
+
udp_packet.payload = "PacketFu test packet"
|
87
|
+
udp_packet.recalc
|
88
|
+
3.times { packet_array << udp_packet.to_s}
|
89
|
+
|
90
|
+
inject = PacketFu::Inject.new(:iface => PacketFu::Utils::default_int)
|
91
|
+
expect(inject.array_to_wire(:array => packet_array)).to eql([3, 3, 186])
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/spec/ip_spec.rb
CHANGED
@@ -23,8 +23,30 @@ describe IPHeader do
|
|
23
23
|
expect(@ip_header.ip_sum).to eql(65535)
|
24
24
|
expect(@ip_header.ip_src).to eql(0)
|
25
25
|
expect(@ip_header.ip_dst).to eql(0)
|
26
|
+
expect(@ip_header.ip_src).to be_a(Integer)
|
27
|
+
expect(@ip_header.ip_dst).to be_a(Integer)
|
26
28
|
expect(@ip_header.body).to eql("")
|
27
29
|
end
|
30
|
+
|
31
|
+
it "should parse a raw IPHeader" do
|
32
|
+
raw_header = "\x45\x10\x00\x4f\x16\xa9\x40\x00\x40\x06\xa2\x9c\xc0\xa8\x00\x02\xc0\xa8\x00\x01"
|
33
|
+
@ip_header.read(raw_header)
|
34
|
+
|
35
|
+
expect(@ip_header.ip_v).to eql(4)
|
36
|
+
expect(@ip_header.ip_hl).to eql(5)
|
37
|
+
expect(@ip_header.ip_tos).to eql(16)
|
38
|
+
expect(@ip_header.ip_len).to eql(79)
|
39
|
+
expect(@ip_header.ip_id).to be_kind_of(Integer)
|
40
|
+
expect(@ip_header.ip_frag).to eql(16384)
|
41
|
+
expect(@ip_header.ip_proto).to eql(6)
|
42
|
+
expect(@ip_header.ip_sum).to eql(41628)
|
43
|
+
expect(@ip_header.ip_src).to eql(3232235522)
|
44
|
+
expect(@ip_header.ip_dst).to eql(3232235521)
|
45
|
+
expect(@ip_header.ip_src).to be_a(Integer)
|
46
|
+
expect(@ip_header.ip_dst).to be_a(Integer)
|
47
|
+
expect(@ip_header.body).to eql("")
|
48
|
+
end
|
49
|
+
|
28
50
|
end
|
29
51
|
end
|
30
52
|
|
@@ -76,7 +98,7 @@ describe IPPacket do
|
|
76
98
|
expect(@temp_file.read).to eql("")
|
77
99
|
|
78
100
|
@ip_packet.to_f(@temp_file.path, 'a')
|
79
|
-
expect(File.
|
101
|
+
expect(File.exist?(@temp_file.path))
|
80
102
|
expect(@temp_file.read.size).to be >= 49
|
81
103
|
end
|
82
104
|
end
|
data/spec/packetfu_spec.rb
CHANGED
@@ -7,7 +7,7 @@ require 'fake_packets'
|
|
7
7
|
|
8
8
|
describe PacketFu, "version information" do
|
9
9
|
it "reports a version number" do
|
10
|
-
PacketFu::VERSION.should match /^
|
10
|
+
PacketFu::VERSION.should match /^[0123]\.[0-9]+\.[0-9]+(.pre)?$/
|
11
11
|
end
|
12
12
|
its(:version) {should eq PacketFu::VERSION}
|
13
13
|
|
data/spec/pcap_spec.rb
CHANGED
@@ -194,7 +194,7 @@ describe PcapFile do
|
|
194
194
|
|
195
195
|
it "should read via #file_to_array and write via #to_f" do
|
196
196
|
# TODO: Figure out why this is failing to write properly when converted to a Tempfile
|
197
|
-
File.unlink('out.pcap') if File.
|
197
|
+
File.unlink('out.pcap') if File.exist? 'out.pcap'
|
198
198
|
pcaps = PcapFile.new.file_to_array(:filename => 'test/sample.pcap')
|
199
199
|
pcaps.each {|pkt|
|
200
200
|
packet = Packet.parse pkt
|
@@ -279,8 +279,8 @@ describe Write do
|
|
279
279
|
Write.array_to_file(:array => pkts, :file => @temp_file.path)
|
280
280
|
|
281
281
|
pkts_new = Read.file_to_array(:file => @temp_file.path)
|
282
|
-
expect(
|
283
|
-
expect(
|
282
|
+
expect(pkts_new).to be_kind_of(Array)
|
283
|
+
expect(pkts_new.size).to eql(11)
|
284
284
|
end
|
285
285
|
end
|
286
286
|
end
|
data/spec/pcapng/file_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
data/spec/structfu_spec.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe StructFu, "mixin methods" do
|
4
|
-
|
5
4
|
before :each do
|
6
5
|
class StructClass
|
7
6
|
include StructFu
|
@@ -10,42 +9,40 @@ describe StructFu, "mixin methods" do
|
|
10
9
|
end
|
11
10
|
|
12
11
|
it "should provide the basic StructFu methods" do
|
13
|
-
@sc.respond_to
|
14
|
-
@sc.respond_to
|
15
|
-
@sc.respond_to
|
16
|
-
@sc.respond_to
|
12
|
+
expect(@sc).to respond_to(:sz)
|
13
|
+
expect(@sc).to respond_to(:len)
|
14
|
+
expect(@sc).to respond_to(:typecast)
|
15
|
+
expect(@sc).to respond_to(:body=)
|
17
16
|
end
|
18
17
|
end
|
19
18
|
|
20
19
|
describe StructFu::Int, "basic Int class" do
|
21
|
-
|
22
20
|
before :each do
|
23
21
|
@int = StructFu::Int.new(8)
|
24
22
|
end
|
25
23
|
|
26
24
|
it "should have an initial state" do
|
27
25
|
new_int = StructFu::Int.new
|
28
|
-
new_int.value.
|
29
|
-
new_int.endian.
|
30
|
-
new_int.width.
|
31
|
-
new_int.default.
|
26
|
+
expect(new_int.value).to be_nil
|
27
|
+
expect(new_int.endian).to be_nil
|
28
|
+
expect(new_int.width).to be_nil
|
29
|
+
expect(new_int.default).to eql(0)
|
32
30
|
end
|
33
31
|
|
34
32
|
it "should raise when to_s'ed directly" do
|
35
|
-
expect
|
33
|
+
expect{ @int.to_s}.to raise_error(StandardError, "StructFu::Int#to_s accessed, must be redefined.")
|
36
34
|
end
|
37
35
|
|
38
36
|
it "should have a value of 8" do
|
39
|
-
@int.value.
|
40
|
-
@int.to_i.
|
41
|
-
@int.to_f.to_s.
|
37
|
+
expect(@int.value).to eql(8)
|
38
|
+
expect(@int.to_i).to eql(8)
|
39
|
+
expect(@int.to_f.to_s).to eql("8.0")
|
42
40
|
end
|
43
41
|
|
44
42
|
it "should read an integer" do
|
45
43
|
@int.read(7)
|
46
|
-
@int.to_i.
|
44
|
+
expect(@int.to_i).to eql(7)
|
47
45
|
end
|
48
|
-
|
49
46
|
end
|
50
47
|
|
51
48
|
describe StructFu::Int8, "one byte value" do
|
@@ -56,82 +53,85 @@ describe StructFu::Int8, "one byte value" do
|
|
56
53
|
|
57
54
|
it "should have an initial state" do
|
58
55
|
new_int = StructFu::Int8.new
|
59
|
-
new_int.value.
|
60
|
-
new_int.endian.
|
61
|
-
new_int.width.
|
62
|
-
new_int.default.
|
56
|
+
expect(new_int.value).to be_nil
|
57
|
+
expect(new_int.endian).to be_nil
|
58
|
+
expect(new_int.width).to eql(1)
|
59
|
+
expect(new_int.default).to eql(0)
|
63
60
|
end
|
64
61
|
|
65
62
|
it "should print a one character packed string" do
|
66
|
-
@int.to_s.
|
63
|
+
expect(@int.to_s).to eql("\x0b")
|
67
64
|
end
|
68
65
|
|
69
66
|
it "should have a value of 11" do
|
70
|
-
@int.value.
|
71
|
-
@int.to_i.
|
72
|
-
@int.to_f.to_s.
|
67
|
+
expect(@int.value).to eql(11)
|
68
|
+
expect(@int.to_i).to eql(11)
|
69
|
+
expect(@int.to_f.to_s).to eql("11.0")
|
73
70
|
end
|
74
71
|
|
75
72
|
it "should reset with a new integer" do
|
76
73
|
@int.read(2)
|
77
|
-
@int.to_i.
|
78
|
-
@int.to_s.
|
74
|
+
expect(@int.to_i).to eql(2)
|
75
|
+
expect(@int.to_s).to eql("\x02")
|
76
|
+
|
79
77
|
@int.read(254)
|
80
|
-
@int.to_i.
|
81
|
-
@int.to_s.
|
78
|
+
expect(@int.to_i).to eql(254)
|
79
|
+
expect(@int.to_s).to eql("\xfe".force_encoding("binary"))
|
82
80
|
end
|
83
81
|
|
84
82
|
end
|
85
83
|
|
86
84
|
describe StructFu::Int16, "two byte value" do
|
87
|
-
|
88
85
|
before :each do
|
89
86
|
@int = StructFu::Int16.new(11)
|
90
87
|
end
|
91
88
|
|
92
89
|
it "should have an initial state" do
|
93
90
|
new_int = StructFu::Int16.new
|
94
|
-
new_int.value.
|
95
|
-
new_int.endian.
|
96
|
-
new_int.width.
|
97
|
-
new_int.default.
|
91
|
+
expect(new_int.value).to be_nil
|
92
|
+
expect(new_int.endian).to eql(:big)
|
93
|
+
expect(new_int.width).to eql(2)
|
94
|
+
expect(new_int.default).to eql(0)
|
98
95
|
end
|
99
96
|
|
100
97
|
it "should print a two character packed string" do
|
101
|
-
@int.to_s.
|
98
|
+
expect(@int.to_s).to eql("\x00\x0b".force_encoding("binary"))
|
102
99
|
end
|
103
100
|
|
104
101
|
it "should have a value of 11" do
|
105
|
-
@int.value.
|
106
|
-
@int.to_i.
|
107
|
-
@int.to_f.to_s.
|
102
|
+
expect(@int.value).to eql(11)
|
103
|
+
expect(@int.to_i).to eql(11)
|
104
|
+
expect(@int.to_f.to_s).to eql("11.0")
|
108
105
|
end
|
109
106
|
|
110
107
|
it "should reset with a new integer" do
|
111
108
|
@int.read(2)
|
112
|
-
@int.to_i.
|
113
|
-
@int.to_s.
|
109
|
+
expect(@int.to_i).to eql(2)
|
110
|
+
expect(@int.to_s).to eql("\x00\x02")
|
111
|
+
|
114
112
|
@int.read(254)
|
115
|
-
@int.to_i.
|
116
|
-
@int.to_s.
|
113
|
+
expect(@int.to_i).to eql(254)
|
114
|
+
expect(@int.to_s).to eql("\x00\xfe".force_encoding("binary"))
|
117
115
|
end
|
118
116
|
|
119
117
|
it "should be able to set endianness" do
|
120
118
|
int_be = StructFu::Int16.new(11,:big)
|
121
|
-
int_be.to_s.
|
119
|
+
expect(int_be.to_s).to eql("\x00\x0b")
|
120
|
+
|
122
121
|
int_le = StructFu::Int16.new(11,:little)
|
123
|
-
int_le.to_s.
|
122
|
+
expect(int_le.to_s).to eql("\x0b\x00")
|
124
123
|
end
|
125
124
|
|
126
125
|
it "should be able to switch endianness" do
|
127
126
|
@int.endian.should == :big
|
128
|
-
@int.to_s.
|
127
|
+
expect(@int.to_s).to eql("\x00\x0b")
|
128
|
+
|
129
129
|
@int.endian = :little
|
130
|
-
@int.endian.
|
130
|
+
expect(@int.endian).to eql(:little)
|
131
|
+
|
131
132
|
@int.read(11)
|
132
|
-
@int.to_s.
|
133
|
+
expect(@int.to_s).to eql("\x0b\x00")
|
133
134
|
end
|
134
|
-
|
135
135
|
end
|
136
136
|
|
137
137
|
describe StructFu::Int16le, "2 byte little-endian value" do
|
@@ -141,7 +141,7 @@ describe StructFu::Int16le, "2 byte little-endian value" do
|
|
141
141
|
end
|
142
142
|
|
143
143
|
it "should behave pretty much like any other 16 bit int" do
|
144
|
-
@int.to_s.
|
144
|
+
expect(@int.to_s).to eql("\x0b\x00")
|
145
145
|
end
|
146
146
|
|
147
147
|
it "should raise when you try to change endianness" do
|
@@ -158,7 +158,7 @@ describe StructFu::Int16be, "2 byte big-endian value" do
|
|
158
158
|
end
|
159
159
|
|
160
160
|
it "should behave pretty much like any other 16 bit int" do
|
161
|
-
@int.to_s.
|
161
|
+
expect(@int.to_s).to eql("\x00\x0b")
|
162
162
|
end
|
163
163
|
|
164
164
|
it "should raise when you try to change endianness" do
|
@@ -176,45 +176,49 @@ describe StructFu::Int32, "four byte value" do
|
|
176
176
|
|
177
177
|
it "should have an initial state" do
|
178
178
|
new_int = StructFu::Int32.new
|
179
|
-
new_int.value.
|
180
|
-
new_int.endian.
|
181
|
-
new_int.width.
|
182
|
-
new_int.default.
|
179
|
+
expect(new_int.value).to be_nil
|
180
|
+
expect(new_int.endian).to eql(:big)
|
181
|
+
expect(new_int.width).to eql(4)
|
182
|
+
expect(new_int.default).to eql(0)
|
183
183
|
end
|
184
184
|
|
185
185
|
it "should print a four character packed string" do
|
186
|
-
@int.to_s.
|
186
|
+
expect(@int.to_s).to eql("\x00\x00\x00\x0b")
|
187
187
|
end
|
188
188
|
|
189
189
|
it "should have a value of 11" do
|
190
|
-
@int.value.
|
191
|
-
@int.to_i.
|
192
|
-
@int.to_f.to_s.
|
190
|
+
expect(@int.value).to eql(11)
|
191
|
+
expect(@int.to_i).to eql(11)
|
192
|
+
expect(@int.to_f.to_s).to eql("11.0")
|
193
193
|
end
|
194
194
|
|
195
195
|
it "should reset with a new integer" do
|
196
196
|
@int.read(2)
|
197
|
-
@int.to_i.
|
198
|
-
@int.to_s.
|
197
|
+
expect(@int.to_i).to eql(2)
|
198
|
+
expect(@int.to_s).to eql("\x00\x00\x00\x02")
|
199
|
+
|
199
200
|
@int.read(254)
|
200
|
-
@int.to_i.
|
201
|
-
@int.to_s.
|
201
|
+
expect(@int.to_i).to eql(254)
|
202
|
+
expect(@int.to_s).to eql("\x00\x00\x00\xfe".force_encoding("binary"))
|
202
203
|
end
|
203
204
|
|
204
205
|
it "should be able to set endianness" do
|
205
206
|
int_be = StructFu::Int32.new(11,:big)
|
206
|
-
int_be.to_s.
|
207
|
+
expect(int_be.to_s).to eql("\x00\x00\x00\x0b")
|
208
|
+
|
207
209
|
int_le = StructFu::Int32.new(11,:little)
|
208
|
-
int_le.to_s.
|
210
|
+
expect(int_le.to_s).to eql("\x0b\x00\x00\x00")
|
209
211
|
end
|
210
212
|
|
211
213
|
it "should be able to switch endianness" do
|
212
214
|
@int.endian.should == :big
|
213
|
-
@int.to_s.
|
215
|
+
expect(@int.to_s).to eql("\x00\x00\x00\x0b")
|
216
|
+
|
214
217
|
@int.endian = :little
|
215
|
-
@int.endian.
|
218
|
+
expect(@int.endian).to eql(:little)
|
219
|
+
|
216
220
|
@int.read(11)
|
217
|
-
@int.to_s.
|
221
|
+
expect(@int.to_s).to eql("\x0b\x00\x00\x00")
|
218
222
|
end
|
219
223
|
|
220
224
|
end
|
@@ -226,7 +230,7 @@ describe StructFu::Int32le, "4 byte little-endian value" do
|
|
226
230
|
end
|
227
231
|
|
228
232
|
it "should behave pretty much like any other 32 bit int" do
|
229
|
-
@int.to_s.
|
233
|
+
expect(@int.to_s).to eql("\x0b\x00\x00\x00")
|
230
234
|
end
|
231
235
|
|
232
236
|
it "should raise when you try to change endianness" do
|
@@ -243,7 +247,7 @@ describe StructFu::Int32be, "4 byte big-endian value" do
|
|
243
247
|
end
|
244
248
|
|
245
249
|
it "should behave pretty much like any other 32 bit int" do
|
246
|
-
@int.to_s.
|
250
|
+
expect(@int.to_s).to eql("\x00\x00\x00\x0b")
|
247
251
|
end
|
248
252
|
|
249
253
|
it "should raise when you try to change endianness" do
|
@@ -260,16 +264,16 @@ describe StructFu::String, "a sligtly more special String" do
|
|
260
264
|
end
|
261
265
|
|
262
266
|
it "should behave pretty much like a string" do
|
263
|
-
@str.
|
267
|
+
expect(@str).to be_kind_of(String)
|
264
268
|
end
|
265
269
|
|
266
270
|
it "should have a read method" do
|
267
|
-
@str.
|
271
|
+
expect(@str).to respond_to(:read)
|
268
272
|
end
|
269
273
|
|
270
274
|
it "should read data like other StructFu things" do
|
271
275
|
@str.read("hello")
|
272
|
-
@str.
|
276
|
+
expect(@str).to eql("hello")
|
273
277
|
end
|
274
278
|
|
275
279
|
end
|
@@ -282,17 +286,17 @@ describe StructFu::IntString do
|
|
282
286
|
|
283
287
|
it "should have a length and value" do
|
284
288
|
istr = StructFu::IntString.new("Avast!")
|
285
|
-
istr.to_s.
|
289
|
+
expect(istr.to_s).to eql("\x06Avast!")
|
286
290
|
end
|
287
291
|
|
288
292
|
it "should have a 16-bit length and a value" do
|
289
293
|
istr = StructFu::IntString.new("Avast!",StructFu::Int16)
|
290
|
-
istr.to_s.
|
294
|
+
expect(istr.to_s).to eql("\x00\x06Avast!")
|
291
295
|
end
|
292
296
|
|
293
297
|
it "should have a 32-bit length and a value" do
|
294
298
|
istr = StructFu::IntString.new("Avast!",StructFu::Int32)
|
295
|
-
istr.to_s.
|
299
|
+
expect(istr.to_s).to eql("\x00\x00\x00\x06Avast!")
|
296
300
|
end
|
297
301
|
|
298
302
|
before :each do
|
@@ -300,35 +304,35 @@ describe StructFu::IntString do
|
|
300
304
|
end
|
301
305
|
|
302
306
|
it "should report the correct length with a new string" do
|
303
|
-
@istr.to_s.
|
307
|
+
expect(@istr.to_s).to eql("\x00\x00\x00\x06Avast!")
|
308
|
+
|
304
309
|
@istr.string = "Ahoy!"
|
305
|
-
@istr.to_s.
|
310
|
+
expect(@istr.to_s).to eql("\x00\x00\x00\x05Ahoy!")
|
306
311
|
end
|
307
312
|
|
308
313
|
it "should report the correct length with a new string" do
|
309
314
|
@istr.string = "Ahoy!"
|
310
|
-
@istr.to_s.
|
315
|
+
expect(@istr.to_s).to eql("\x00\x00\x00\x05Ahoy!")
|
311
316
|
end
|
312
317
|
|
313
318
|
it "should keep the old length with a new string" do
|
314
319
|
@istr[:string] = "Ahoy!"
|
315
|
-
@istr.to_s.
|
320
|
+
expect(@istr.to_s).to eql("\x00\x00\x00\x06Ahoy!")
|
316
321
|
end
|
317
322
|
|
318
323
|
it "should allow for adjusting the length manually" do
|
319
324
|
@istr.len = 16
|
320
|
-
@istr.to_s.
|
325
|
+
expect(@istr.to_s).to eql("\x00\x00\x00\x10Avast!")
|
321
326
|
end
|
322
327
|
|
323
328
|
it "should read in an expected string" do
|
324
329
|
data = "\x00\x00\x00\x09Yo ho ho!"
|
325
330
|
@istr.read(data)
|
326
|
-
@istr.to_s.
|
331
|
+
expect(@istr.to_s).to eql(data)
|
327
332
|
end
|
328
333
|
|
329
334
|
it "should raise when a string is too short" do
|
330
335
|
data = "\x01A"
|
331
|
-
expect
|
336
|
+
expect{ @istr.read(data) }.to raise_error(StandardError, "String is too short for type StructFu::Int32")
|
332
337
|
end
|
333
|
-
|
334
338
|
end
|