packetfu 1.1.9 → 1.1.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. data/bench/octets.rb +9 -9
  2. data/examples/100kpackets.rb +12 -12
  3. data/examples/ackscan.rb +16 -16
  4. data/examples/arp.rb +35 -35
  5. data/examples/arphood.rb +36 -36
  6. data/examples/dissect_thinger.rb +6 -6
  7. data/examples/new-simple-stats.rb +23 -23
  8. data/examples/packetfu-shell.rb +25 -25
  9. data/examples/simple-sniffer.rb +9 -9
  10. data/examples/simple-stats.rb +23 -23
  11. data/examples/slammer.rb +3 -3
  12. data/lib/packetfu.rb +127 -127
  13. data/lib/packetfu/capture.rb +169 -169
  14. data/lib/packetfu/config.rb +52 -52
  15. data/lib/packetfu/inject.rb +56 -56
  16. data/lib/packetfu/packet.rb +528 -528
  17. data/lib/packetfu/pcap.rb +579 -579
  18. data/lib/packetfu/protos/arp.rb +90 -90
  19. data/lib/packetfu/protos/arp/header.rb +158 -158
  20. data/lib/packetfu/protos/arp/mixin.rb +36 -36
  21. data/lib/packetfu/protos/eth.rb +44 -44
  22. data/lib/packetfu/protos/eth/header.rb +243 -243
  23. data/lib/packetfu/protos/eth/mixin.rb +3 -3
  24. data/lib/packetfu/protos/hsrp.rb +69 -69
  25. data/lib/packetfu/protos/hsrp/header.rb +107 -107
  26. data/lib/packetfu/protos/hsrp/mixin.rb +29 -29
  27. data/lib/packetfu/protos/icmp.rb +71 -71
  28. data/lib/packetfu/protos/icmp/header.rb +82 -82
  29. data/lib/packetfu/protos/icmp/mixin.rb +14 -14
  30. data/lib/packetfu/protos/invalid.rb +49 -49
  31. data/lib/packetfu/protos/ip.rb +69 -69
  32. data/lib/packetfu/protos/ip/header.rb +291 -291
  33. data/lib/packetfu/protos/ip/mixin.rb +40 -40
  34. data/lib/packetfu/protos/ipv6.rb +50 -50
  35. data/lib/packetfu/protos/ipv6/header.rb +188 -188
  36. data/lib/packetfu/protos/ipv6/mixin.rb +29 -29
  37. data/lib/packetfu/protos/tcp.rb +176 -176
  38. data/lib/packetfu/protos/tcp/ecn.rb +35 -35
  39. data/lib/packetfu/protos/tcp/flags.rb +74 -74
  40. data/lib/packetfu/protos/tcp/header.rb +268 -268
  41. data/lib/packetfu/protos/tcp/hlen.rb +32 -32
  42. data/lib/packetfu/protos/tcp/mixin.rb +46 -46
  43. data/lib/packetfu/protos/tcp/option.rb +321 -321
  44. data/lib/packetfu/protos/tcp/options.rb +95 -95
  45. data/lib/packetfu/protos/tcp/reserved.rb +35 -35
  46. data/lib/packetfu/protos/udp.rb +116 -116
  47. data/lib/packetfu/protos/udp/header.rb +91 -91
  48. data/lib/packetfu/protos/udp/mixin.rb +3 -3
  49. data/lib/packetfu/structfu.rb +280 -280
  50. data/lib/packetfu/utils.rb +226 -217
  51. data/lib/packetfu/version.rb +41 -41
  52. data/packetfu.gemspec +2 -1
  53. data/spec/ethpacket_spec.rb +48 -48
  54. data/spec/packet_spec.rb +57 -57
  55. data/spec/packet_subclasses_spec.rb +8 -8
  56. data/spec/packetfu_spec.rb +59 -59
  57. data/spec/structfu_spec.rb +268 -268
  58. data/spec/tcp_spec.rb +75 -75
  59. data/test/all_tests.rb +13 -13
  60. data/test/func_lldp.rb +3 -3
  61. data/test/ptest.rb +2 -2
  62. data/test/test_arp.rb +116 -116
  63. data/test/test_capture.rb +45 -45
  64. data/test/test_eth.rb +68 -68
  65. data/test/test_hsrp.rb +9 -9
  66. data/test/test_icmp.rb +52 -52
  67. data/test/test_inject.rb +18 -18
  68. data/test/test_invalid.rb +16 -16
  69. data/test/test_ip.rb +36 -36
  70. data/test/test_ip6.rb +48 -48
  71. data/test/test_octets.rb +21 -21
  72. data/test/test_packet.rb +154 -154
  73. data/test/test_pcap.rb +170 -170
  74. data/test/test_structfu.rb +97 -97
  75. data/test/test_tcp.rb +320 -320
  76. data/test/test_udp.rb +76 -76
  77. metadata +4 -3
@@ -1,8 +1,9 @@
1
1
  require 'rake'
2
+ require './lib/packetfu/version'
2
3
 
3
4
  Gem::Specification.new do |s|
4
5
  s.name = 'packetfu'
5
- s.version = '1.1.9'
6
+ s.version = PacketFu::VERSION
6
7
  s.authors = ['Tod Beardsley']
7
8
  s.email = 'todb@packetfu.com'
8
9
  s.summary = 'PacketFu is a mid-level packet manipulation library.'
@@ -5,70 +5,70 @@ include PacketFu
5
5
 
6
6
  describe EthPacket, "when read from a pcap file" do
7
7
 
8
- before :all do
9
- parsed_packets = PcapFile.read_packets(File.join(File.dirname(__FILE__),"sample.pcap"))
10
- @eth_packet = parsed_packets.first
11
- end
8
+ before :all do
9
+ parsed_packets = PcapFile.read_packets(File.join(File.dirname(__FILE__),"sample.pcap"))
10
+ @eth_packet = parsed_packets.first
11
+ end
12
12
 
13
- context "is a regular ethernet packet" do
13
+ context "is a regular ethernet packet" do
14
14
 
15
- subject { @eth_packet }
15
+ subject { @eth_packet }
16
16
 
17
- it "should be an EthPacket kind of packet" do
18
- subject.should be_kind_of EthPacket
19
- end
17
+ it "should be an EthPacket kind of packet" do
18
+ subject.should be_kind_of EthPacket
19
+ end
20
20
 
21
- it "should have a dest mac address" do
22
- subject.eth_daddr.should == "00:03:2f:1a:74:de"
23
- end
21
+ it "should have a dest mac address" do
22
+ subject.eth_daddr.should == "00:03:2f:1a:74:de"
23
+ end
24
24
 
25
- it "should have a source mac address" do
26
- subject.eth_saddr.should == "00:1b:11:51:b7:ce"
27
- end
25
+ it "should have a source mac address" do
26
+ subject.eth_saddr.should == "00:1b:11:51:b7:ce"
27
+ end
28
28
 
29
- its(:size) { should == 78 }
29
+ its(:size) { should == 78 }
30
30
 
31
- it "should have a payload in its first header" do
32
- subject.headers.first.body.should_not be_nil
33
- end
31
+ it "should have a payload in its first header" do
32
+ subject.headers.first.body.should_not be_nil
33
+ end
34
34
 
35
- context "an EthPacket's first header" do
35
+ context "an EthPacket's first header" do
36
36
 
37
- subject { @eth_packet.headers.first }
37
+ subject { @eth_packet.headers.first }
38
38
 
39
- it "should be 64 bytes" do
40
- subject.body.sz.should == 64
41
- end
39
+ it "should be 64 bytes" do
40
+ subject.body.sz.should == 64
41
+ end
42
42
 
43
- context "EthHeader struct members" do
44
- if RUBY_VERSION =~ /^1\.8/
45
- its(:members) { should include :eth_dst.to_s }
46
- its(:members) { should include :eth_src.to_s }
47
- its(:members) { should include :eth_proto.to_s }
48
- its(:members) { should include :body.to_s }
49
- else
50
- its(:members) { should include :eth_dst }
51
- its(:members) { should include :eth_src }
52
- its(:members) { should include :eth_proto }
53
- its(:members) { should include :body }
54
- end
55
- end
43
+ context "EthHeader struct members" do
44
+ if RUBY_VERSION =~ /^1\.8/
45
+ its(:members) { should include :eth_dst.to_s }
46
+ its(:members) { should include :eth_src.to_s }
47
+ its(:members) { should include :eth_proto.to_s }
48
+ its(:members) { should include :body.to_s }
49
+ else
50
+ its(:members) { should include :eth_dst }
51
+ its(:members) { should include :eth_src }
52
+ its(:members) { should include :eth_proto }
53
+ its(:members) { should include :body }
54
+ end
55
+ end
56
56
 
57
- end
57
+ end
58
58
 
59
- end
59
+ end
60
60
 
61
- context "isn't a regular Ethernet packet" do
61
+ context "isn't a regular Ethernet packet" do
62
62
 
63
- subject {
64
- parsed_packets = PcapFile.read_packets(File.join(File.dirname(__FILE__),"vlan-pcapr.cap"))
65
- parsed_packets.first
66
- }
63
+ subject {
64
+ parsed_packets = PcapFile.read_packets(File.join(File.dirname(__FILE__),"vlan-pcapr.cap"))
65
+ parsed_packets.first
66
+ }
67
67
 
68
- it "should not be an EthPacket" do
69
- subject.should_not be_kind_of EthPacket
70
- end
68
+ it "should not be an EthPacket" do
69
+ subject.should_not be_kind_of EthPacket
70
+ end
71
71
 
72
- end
72
+ end
73
73
 
74
74
  end
@@ -3,71 +3,71 @@ require 'packetfu'
3
3
 
4
4
  describe PacketFu::Packet, "abstract packet class behavior" do
5
5
 
6
- before(:all) do
7
- class PacketFu::FooPacket < PacketFu::Packet; end
8
- class PacketFu::BarPacket < PacketFu::Packet; end
9
- end
6
+ before(:all) do
7
+ class PacketFu::FooPacket < PacketFu::Packet; end
8
+ class PacketFu::BarPacket < PacketFu::Packet; end
9
+ end
10
10
 
11
- it "should not be instantiated" do
12
- expect { PacketFu::Packet.new }.to raise_error(NoMethodError)
13
- end
11
+ it "should not be instantiated" do
12
+ expect { PacketFu::Packet.new }.to raise_error(NoMethodError)
13
+ end
14
14
 
15
- it "should allow subclasses to instantiate" do
16
- expect { PacketFu::FooPacket.new }. to be
17
- PacketFu.packet_classes.include?(PacketFu::FooPacket).should be_true
18
- end
15
+ it "should allow subclasses to instantiate" do
16
+ expect { PacketFu::FooPacket.new }. to be
17
+ PacketFu.packet_classes.include?(PacketFu::FooPacket).should be_true
18
+ end
19
19
 
20
- it "should register packet classes with PacketFu" do
21
- PacketFu.packet_classes {should include(FooPacket) }
22
- PacketFu.packet_classes {should include(BarPacket) }
23
- end
20
+ it "should register packet classes with PacketFu" do
21
+ PacketFu.packet_classes {should include(FooPacket) }
22
+ PacketFu.packet_classes {should include(BarPacket) }
23
+ end
24
24
 
25
- it "should disallow badly named subclasses" do
26
- expect {
27
- class PacketFu::PacketNot < PacketFu::Packet
28
- end
29
- }.to raise_error
30
- PacketFu.packet_classes.include?(PacketFu::PacketNot).should be_false
31
- PacketFu.packet_classes {should_not include(PacketNot) }
32
- end
25
+ it "should disallow badly named subclasses" do
26
+ expect {
27
+ class PacketFu::PacketNot < PacketFu::Packet
28
+ end
29
+ }.to raise_error
30
+ PacketFu.packet_classes.include?(PacketFu::PacketNot).should be_false
31
+ PacketFu.packet_classes {should_not include(PacketNot) }
32
+ end
33
33
 
34
- before(:each) do
35
- @tcp_packet = PacketFu::TCPPacket.new
36
- @tcp_packet.ip_saddr = "10.10.10.10"
37
- end
34
+ before(:each) do
35
+ @tcp_packet = PacketFu::TCPPacket.new
36
+ @tcp_packet.ip_saddr = "10.10.10.10"
37
+ end
38
38
 
39
- it "should shallow copy with dup()" do
40
- p2 = @tcp_packet.dup
41
- p2.ip_saddr = "20.20.20.20"
42
- p2.ip_saddr.should == @tcp_packet.ip_saddr
43
- p2.headers[1].object_id.should == @tcp_packet.headers[1].object_id
44
- end
39
+ it "should shallow copy with dup()" do
40
+ p2 = @tcp_packet.dup
41
+ p2.ip_saddr = "20.20.20.20"
42
+ p2.ip_saddr.should == @tcp_packet.ip_saddr
43
+ p2.headers[1].object_id.should == @tcp_packet.headers[1].object_id
44
+ end
45
45
 
46
- it "should deep copy with clone()" do
47
- p3 = @tcp_packet.clone
48
- p3.ip_saddr = "30.30.30.30"
49
- p3.ip_saddr.should_not == @tcp_packet.ip_saddr
50
- p3.headers[1].object_id.should_not == @tcp_packet.headers[1].object_id
51
- end
46
+ it "should deep copy with clone()" do
47
+ p3 = @tcp_packet.clone
48
+ p3.ip_saddr = "30.30.30.30"
49
+ p3.ip_saddr.should_not == @tcp_packet.ip_saddr
50
+ p3.headers[1].object_id.should_not == @tcp_packet.headers[1].object_id
51
+ end
52
52
 
53
- it "should have senisble equality" do
54
- p4 = @tcp_packet.dup
55
- p4.should == @tcp_packet
56
- p5 = @tcp_packet.clone
57
- p5.should == @tcp_packet
58
- end
53
+ it "should have senisble equality" do
54
+ p4 = @tcp_packet.dup
55
+ p4.should == @tcp_packet
56
+ p5 = @tcp_packet.clone
57
+ p5.should == @tcp_packet
58
+ end
59
59
 
60
- # It's actually kinda hard to manually create identical TCP packets
61
- it "should be possible to manually create identical packets" do
62
- p6 = @tcp_packet.clone
63
- p6.should == @tcp_packet
64
- p7 = PacketFu::TCPPacket.new
65
- p7.ip_saddr = p6.ip_saddr
66
- p7.ip_id = p6.ip_id
67
- p7.tcp_seq = p6.tcp_seq
68
- p7.tcp_src = p6.tcp_src
69
- p7.tcp_sum = p6.tcp_sum
70
- p7.should == p6
71
- end
60
+ # It's actually kinda hard to manually create identical TCP packets
61
+ it "should be possible to manually create identical packets" do
62
+ p6 = @tcp_packet.clone
63
+ p6.should == @tcp_packet
64
+ p7 = PacketFu::TCPPacket.new
65
+ p7.ip_saddr = p6.ip_saddr
66
+ p7.ip_id = p6.ip_id
67
+ p7.tcp_seq = p6.tcp_seq
68
+ p7.tcp_src = p6.tcp_src
69
+ p7.tcp_sum = p6.tcp_sum
70
+ p7.should == p6
71
+ end
72
72
 
73
73
  end
@@ -2,12 +2,12 @@ $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib")
2
2
  require 'packetfu'
3
3
 
4
4
  PacketFu.packet_classes.each do |pclass|
5
- describe pclass, "peek format" do
6
- it "will display sensible peek information" do
7
- p = pclass.new
8
- p.respond_to?(:peek).should be_true
9
- p.peek.size.should be <= 80, p.peek.inspect
10
- p.peek.should match(/^[A-Z0-9?]../)
11
- end
12
- end
5
+ describe pclass, "peek format" do
6
+ it "will display sensible peek information" do
7
+ p = pclass.new
8
+ p.respond_to?(:peek).should be_true
9
+ p.peek.size.should be <= 80, p.peek.inspect
10
+ p.peek.should match(/^[A-Z0-9?]../)
11
+ end
12
+ end
13
13
  end
@@ -2,84 +2,84 @@ $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib")
2
2
  require 'packetfu'
3
3
 
4
4
  describe PacketFu, "version information" do
5
- it "reports a version number" do
6
- PacketFu::VERSION.should match /^1\.[0-9]\.[0-9]$/
7
- end
8
- its(:version) {should eq PacketFu::VERSION}
5
+ it "reports a version number" do
6
+ PacketFu::VERSION.should match /^1\.[0-9]\.[0-9]$/
7
+ end
8
+ its(:version) {should eq PacketFu::VERSION}
9
9
 
10
- it "can compare version strings" do
11
- PacketFu.binarize_version("1.2.3").should == 0x010203
12
- PacketFu.binarize_version("3.0").should == 0x030000
13
- PacketFu.at_least?("1.0").should be_true
14
- PacketFu.at_least?("4.0").should be_false
15
- PacketFu.older_than?("4.0").should be_true
16
- PacketFu.newer_than?("1.0").should be_true
17
- end
10
+ it "can compare version strings" do
11
+ PacketFu.binarize_version("1.2.3").should == 0x010203
12
+ PacketFu.binarize_version("3.0").should == 0x030000
13
+ PacketFu.at_least?("1.0").should be_true
14
+ PacketFu.at_least?("4.0").should be_false
15
+ PacketFu.older_than?("4.0").should be_true
16
+ PacketFu.newer_than?("1.0").should be_true
17
+ end
18
18
 
19
- it "can handle .pre versions" do
20
- PacketFu.binarize_version("1.7.6.pre").should == 0x010706
21
- PacketFu.at_least?("0.9.0.pre").should be_true
22
- end
19
+ it "can handle .pre versions" do
20
+ PacketFu.binarize_version("1.7.6.pre").should == 0x010706
21
+ PacketFu.at_least?("0.9.0.pre").should be_true
22
+ end
23
23
  end
24
24
 
25
25
  describe PacketFu, "instance variables" do
26
- it "should have a bunch of instance variables" do
27
- PacketFu.instance_variable_get(:@byte_order).should == :little
28
- PacketFu.instance_variable_get(:@pcaprub_loaded).should_not be_nil
29
- end
26
+ it "should have a bunch of instance variables" do
27
+ PacketFu.instance_variable_get(:@byte_order).should == :little
28
+ PacketFu.instance_variable_get(:@pcaprub_loaded).should_not be_nil
29
+ end
30
30
  end
31
31
 
32
32
  describe PacketFu, "pcaprub deps" do
33
- it "should check for pcaprub" do
34
- begin
35
- has_pcap = false
36
- require 'pcaprub'
37
- has_pcap = true
38
- rescue LoadError
39
- end
40
- if has_pcap
41
- PacketFu.instance_variable_get(:@pcaprub_loaded).should be_true
42
- else
43
- PacketFu.instance_variable_get(:@pcaprub_loaded).should be_false
44
- end
45
- end
33
+ it "should check for pcaprub" do
34
+ begin
35
+ has_pcap = false
36
+ require 'pcaprub'
37
+ has_pcap = true
38
+ rescue LoadError
39
+ end
40
+ if has_pcap
41
+ PacketFu.instance_variable_get(:@pcaprub_loaded).should be_true
42
+ else
43
+ PacketFu.instance_variable_get(:@pcaprub_loaded).should be_false
44
+ end
45
+ end
46
46
  end
47
47
 
48
48
  describe PacketFu, "protocol requires" do
49
- it "should have some protocols defined" do
50
- PacketFu::EthPacket.should_not be_nil
51
- PacketFu::IPPacket.should_not be_nil
52
- PacketFu::TCPPacket.should_not be_nil
53
- expect { PacketFu::FakePacket }.to raise_error
54
- end
49
+ it "should have some protocols defined" do
50
+ PacketFu::EthPacket.should_not be_nil
51
+ PacketFu::IPPacket.should_not be_nil
52
+ PacketFu::TCPPacket.should_not be_nil
53
+ expect { PacketFu::FakePacket }.to raise_error
54
+ end
55
55
  end
56
56
 
57
57
  describe PacketFu, "packet class list management" do
58
58
 
59
- before(:all) do
60
- class PacketFu::FooPacket < PacketFu::Packet; end
61
- class PacketFu::BarPacket < PacketFu::Packet; end
62
- class PacketFu::PacketBaz; end
63
- end
59
+ before(:all) do
60
+ class PacketFu::FooPacket < PacketFu::Packet; end
61
+ class PacketFu::BarPacket < PacketFu::Packet; end
62
+ class PacketFu::PacketBaz; end
63
+ end
64
64
 
65
- it "should allow packet class registration" do
66
- PacketFu.add_packet_class(PacketFu::FooPacket).should be_kind_of Array
67
- PacketFu.add_packet_class(PacketFu::BarPacket).should be_kind_of Array
68
- end
65
+ it "should allow packet class registration" do
66
+ PacketFu.add_packet_class(PacketFu::FooPacket).should be_kind_of Array
67
+ PacketFu.add_packet_class(PacketFu::BarPacket).should be_kind_of Array
68
+ end
69
69
 
70
- its(:packet_classes) {should include(PacketFu::FooPacket)}
70
+ its(:packet_classes) {should include(PacketFu::FooPacket)}
71
71
 
72
- it "should disallow non-classes as packet classes" do
73
- expect { PacketFu.add_packet_class("A String") }.to raise_error
74
- end
72
+ it "should disallow non-classes as packet classes" do
73
+ expect { PacketFu.add_packet_class("A String") }.to raise_error
74
+ end
75
75
 
76
- its(:packet_prefixes) {should include("bar")}
76
+ its(:packet_prefixes) {should include("bar")}
77
77
 
78
- # Don't really have much utility for this right now.
79
- it "should allow packet class deregistration" do
80
- PacketFu.remove_packet_class(PacketFu::BarPacket)
81
- PacketFu.packet_prefixes.should_not include("bar")
82
- PacketFu.add_packet_class(PacketFu::BarPacket)
83
- end
78
+ # Don't really have much utility for this right now.
79
+ it "should allow packet class deregistration" do
80
+ PacketFu.remove_packet_class(PacketFu::BarPacket)
81
+ PacketFu.packet_prefixes.should_not include("bar")
82
+ PacketFu.add_packet_class(PacketFu::BarPacket)
83
+ end
84
84
 
85
85
  end
@@ -3,333 +3,333 @@ require 'packetfu'
3
3
 
4
4
  describe StructFu, "mixin methods" do
5
5
 
6
- before :each do
7
- class StructClass
8
- include StructFu
9
- end
10
- @sc = StructClass.new
11
- end
12
-
13
- it "should provide the basic StructFu methods" do
14
- @sc.respond_to?(:sz).should be_true
15
- @sc.respond_to?(:len).should be_true
16
- @sc.respond_to?(:typecast).should be_true
17
- @sc.respond_to?(:body=).should be_true
18
- end
6
+ before :each do
7
+ class StructClass
8
+ include StructFu
9
+ end
10
+ @sc = StructClass.new
11
+ end
12
+
13
+ it "should provide the basic StructFu methods" do
14
+ @sc.respond_to?(:sz).should be_true
15
+ @sc.respond_to?(:len).should be_true
16
+ @sc.respond_to?(:typecast).should be_true
17
+ @sc.respond_to?(:body=).should be_true
18
+ end
19
19
  end
20
20
 
21
21
  describe StructFu::Int, "basic Int class" do
22
22
 
23
- before :each do
24
- @int = StructFu::Int.new(8)
25
- end
26
-
27
- it "should have an initial state" do
28
- new_int = StructFu::Int.new
29
- new_int.value.should be_nil
30
- new_int.endian.should be_nil
31
- new_int.width.should be_nil
32
- new_int.default.should == 0
33
- end
34
-
35
- it "should raise when to_s'ed directly" do
36
- expect { @int.to_s}.to raise_error
37
- end
38
-
39
- it "should have a value of 8" do
40
- @int.value.should == 8
41
- @int.to_i.should == 8
42
- @int.to_f.to_s.should == "8.0"
43
- end
44
-
45
- it "should read an integer" do
46
- @int.read(7)
47
- @int.to_i.should == 7
48
- end
23
+ before :each do
24
+ @int = StructFu::Int.new(8)
25
+ end
26
+
27
+ it "should have an initial state" do
28
+ new_int = StructFu::Int.new
29
+ new_int.value.should be_nil
30
+ new_int.endian.should be_nil
31
+ new_int.width.should be_nil
32
+ new_int.default.should == 0
33
+ end
34
+
35
+ it "should raise when to_s'ed directly" do
36
+ expect { @int.to_s}.to raise_error
37
+ end
38
+
39
+ it "should have a value of 8" do
40
+ @int.value.should == 8
41
+ @int.to_i.should == 8
42
+ @int.to_f.to_s.should == "8.0"
43
+ end
44
+
45
+ it "should read an integer" do
46
+ @int.read(7)
47
+ @int.to_i.should == 7
48
+ end
49
49
 
50
50
  end
51
51
 
52
52
  describe StructFu::Int8, "one byte value" do
53
53
 
54
- before :each do
55
- @int = StructFu::Int8.new(11)
56
- end
57
-
58
- it "should have an initial state" do
59
- new_int = StructFu::Int8.new
60
- new_int.value.should be_nil
61
- new_int.endian.should be_nil
62
- new_int.width.should == 1
63
- new_int.default.should == 0
64
- end
65
-
66
- it "should print a one character packed string" do
67
- @int.to_s.should == "\x0b"
68
- end
69
-
70
- it "should have a value of 11" do
71
- @int.value.should == 11
72
- @int.to_i.should == 11
73
- @int.to_f.to_s.should == "11.0"
74
- end
75
-
76
- it "should reset with a new integer" do
77
- @int.read(2)
78
- @int.to_i.should == 2
79
- @int.to_s.should == "\x02"
80
- @int.read(254)
81
- @int.to_i.should == 254
82
- @int.to_s.should == "\xfe"
83
- end
54
+ before :each do
55
+ @int = StructFu::Int8.new(11)
56
+ end
57
+
58
+ it "should have an initial state" do
59
+ new_int = StructFu::Int8.new
60
+ new_int.value.should be_nil
61
+ new_int.endian.should be_nil
62
+ new_int.width.should == 1
63
+ new_int.default.should == 0
64
+ end
65
+
66
+ it "should print a one character packed string" do
67
+ @int.to_s.should == "\x0b"
68
+ end
69
+
70
+ it "should have a value of 11" do
71
+ @int.value.should == 11
72
+ @int.to_i.should == 11
73
+ @int.to_f.to_s.should == "11.0"
74
+ end
75
+
76
+ it "should reset with a new integer" do
77
+ @int.read(2)
78
+ @int.to_i.should == 2
79
+ @int.to_s.should == "\x02"
80
+ @int.read(254)
81
+ @int.to_i.should == 254
82
+ @int.to_s.should == "\xfe"
83
+ end
84
84
 
85
85
  end
86
86
 
87
87
  describe StructFu::Int16, "two byte value" do
88
88
 
89
- before :each do
90
- @int = StructFu::Int16.new(11)
91
- end
92
-
93
- it "should have an initial state" do
94
- new_int = StructFu::Int16.new
95
- new_int.value.should be_nil
96
- new_int.endian.should == :big
97
- new_int.width.should == 2
98
- new_int.default.should == 0
99
- end
100
-
101
- it "should print a two character packed string" do
102
- @int.to_s.should == "\x00\x0b"
103
- end
104
-
105
- it "should have a value of 11" do
106
- @int.value.should == 11
107
- @int.to_i.should == 11
108
- @int.to_f.to_s.should == "11.0"
109
- end
110
-
111
- it "should reset with a new integer" do
112
- @int.read(2)
113
- @int.to_i.should == 2
114
- @int.to_s.should == "\x00\x02"
115
- @int.read(254)
116
- @int.to_i.should == 254
117
- @int.to_s.should == "\x00\xfe"
118
- end
119
-
120
- it "should be able to set endianness" do
121
- int_be = StructFu::Int16.new(11,:big)
122
- int_be.to_s.should == "\x00\x0b"
123
- int_le = StructFu::Int16.new(11,:little)
124
- int_le.to_s.should == "\x0b\x00"
125
- end
126
-
127
- it "should be able to switch endianness" do
128
- @int.endian.should == :big
129
- @int.to_s.should == "\x00\x0b"
130
- @int.endian = :little
131
- @int.endian.should == :little
132
- @int.read(11)
133
- @int.to_s.should == "\x0b\x00"
134
- end
89
+ before :each do
90
+ @int = StructFu::Int16.new(11)
91
+ end
92
+
93
+ it "should have an initial state" do
94
+ new_int = StructFu::Int16.new
95
+ new_int.value.should be_nil
96
+ new_int.endian.should == :big
97
+ new_int.width.should == 2
98
+ new_int.default.should == 0
99
+ end
100
+
101
+ it "should print a two character packed string" do
102
+ @int.to_s.should == "\x00\x0b"
103
+ end
104
+
105
+ it "should have a value of 11" do
106
+ @int.value.should == 11
107
+ @int.to_i.should == 11
108
+ @int.to_f.to_s.should == "11.0"
109
+ end
110
+
111
+ it "should reset with a new integer" do
112
+ @int.read(2)
113
+ @int.to_i.should == 2
114
+ @int.to_s.should == "\x00\x02"
115
+ @int.read(254)
116
+ @int.to_i.should == 254
117
+ @int.to_s.should == "\x00\xfe"
118
+ end
119
+
120
+ it "should be able to set endianness" do
121
+ int_be = StructFu::Int16.new(11,:big)
122
+ int_be.to_s.should == "\x00\x0b"
123
+ int_le = StructFu::Int16.new(11,:little)
124
+ int_le.to_s.should == "\x0b\x00"
125
+ end
126
+
127
+ it "should be able to switch endianness" do
128
+ @int.endian.should == :big
129
+ @int.to_s.should == "\x00\x0b"
130
+ @int.endian = :little
131
+ @int.endian.should == :little
132
+ @int.read(11)
133
+ @int.to_s.should == "\x0b\x00"
134
+ end
135
135
 
136
136
  end
137
137
 
138
138
  describe StructFu::Int16le, "2 byte little-endian value" do
139
139
 
140
- before :each do
141
- @int = StructFu::Int16le.new(11)
142
- end
140
+ before :each do
141
+ @int = StructFu::Int16le.new(11)
142
+ end
143
143
 
144
- it "should behave pretty much like any other 16 bit int" do
145
- @int.to_s.should == "\x0b\x00"
146
- end
144
+ it "should behave pretty much like any other 16 bit int" do
145
+ @int.to_s.should == "\x0b\x00"
146
+ end
147
147
 
148
- it "should raise when you try to change endianness" do
149
- expect { @int.endian = :big }.to raise_error
150
- expect { @int.endian = :little }.to raise_error
151
- end
148
+ it "should raise when you try to change endianness" do
149
+ expect { @int.endian = :big }.to raise_error
150
+ expect { @int.endian = :little }.to raise_error
151
+ end
152
152
 
153
153
  end
154
154
 
155
155
  describe StructFu::Int16be, "2 byte big-endian value" do
156
156
 
157
- before :each do
158
- @int = StructFu::Int16be.new(11)
159
- end
157
+ before :each do
158
+ @int = StructFu::Int16be.new(11)
159
+ end
160
160
 
161
- it "should behave pretty much like any other 16 bit int" do
162
- @int.to_s.should == "\x00\x0b"
163
- end
161
+ it "should behave pretty much like any other 16 bit int" do
162
+ @int.to_s.should == "\x00\x0b"
163
+ end
164
164
 
165
- it "should raise when you try to change endianness" do
166
- expect { @int.endian = :big }.to raise_error
167
- expect { @int.endian = :little }.to raise_error
168
- end
165
+ it "should raise when you try to change endianness" do
166
+ expect { @int.endian = :big }.to raise_error
167
+ expect { @int.endian = :little }.to raise_error
168
+ end
169
169
 
170
170
  end
171
171
 
172
172
  describe StructFu::Int32, "four byte value" do
173
173
 
174
- before :each do
175
- @int = StructFu::Int32.new(11)
176
- end
177
-
178
- it "should have an initial state" do
179
- new_int = StructFu::Int32.new
180
- new_int.value.should be_nil
181
- new_int.endian.should == :big
182
- new_int.width.should == 4
183
- new_int.default.should == 0
184
- end
185
-
186
- it "should print a four character packed string" do
187
- @int.to_s.should == "\x00\x00\x00\x0b"
188
- end
189
-
190
- it "should have a value of 11" do
191
- @int.value.should == 11
192
- @int.to_i.should == 11
193
- @int.to_f.to_s.should == "11.0"
194
- end
195
-
196
- it "should reset with a new integer" do
197
- @int.read(2)
198
- @int.to_i.should == 2
199
- @int.to_s.should == "\x00\x00\x00\x02"
200
- @int.read(254)
201
- @int.to_i.should == 254
202
- @int.to_s.should == "\x00\x00\x00\xfe"
203
- end
204
-
205
- it "should be able to set endianness" do
206
- int_be = StructFu::Int32.new(11,:big)
207
- int_be.to_s.should == "\x00\x00\x00\x0b"
208
- int_le = StructFu::Int32.new(11,:little)
209
- int_le.to_s.should == "\x0b\x00\x00\x00"
210
- end
211
-
212
- it "should be able to switch endianness" do
213
- @int.endian.should == :big
214
- @int.to_s.should == "\x00\x00\x00\x0b"
215
- @int.endian = :little
216
- @int.endian.should == :little
217
- @int.read(11)
218
- @int.to_s.should == "\x0b\x00\x00\x00"
219
- end
174
+ before :each do
175
+ @int = StructFu::Int32.new(11)
176
+ end
177
+
178
+ it "should have an initial state" do
179
+ new_int = StructFu::Int32.new
180
+ new_int.value.should be_nil
181
+ new_int.endian.should == :big
182
+ new_int.width.should == 4
183
+ new_int.default.should == 0
184
+ end
185
+
186
+ it "should print a four character packed string" do
187
+ @int.to_s.should == "\x00\x00\x00\x0b"
188
+ end
189
+
190
+ it "should have a value of 11" do
191
+ @int.value.should == 11
192
+ @int.to_i.should == 11
193
+ @int.to_f.to_s.should == "11.0"
194
+ end
195
+
196
+ it "should reset with a new integer" do
197
+ @int.read(2)
198
+ @int.to_i.should == 2
199
+ @int.to_s.should == "\x00\x00\x00\x02"
200
+ @int.read(254)
201
+ @int.to_i.should == 254
202
+ @int.to_s.should == "\x00\x00\x00\xfe"
203
+ end
204
+
205
+ it "should be able to set endianness" do
206
+ int_be = StructFu::Int32.new(11,:big)
207
+ int_be.to_s.should == "\x00\x00\x00\x0b"
208
+ int_le = StructFu::Int32.new(11,:little)
209
+ int_le.to_s.should == "\x0b\x00\x00\x00"
210
+ end
211
+
212
+ it "should be able to switch endianness" do
213
+ @int.endian.should == :big
214
+ @int.to_s.should == "\x00\x00\x00\x0b"
215
+ @int.endian = :little
216
+ @int.endian.should == :little
217
+ @int.read(11)
218
+ @int.to_s.should == "\x0b\x00\x00\x00"
219
+ end
220
220
 
221
221
  end
222
222
 
223
223
  describe StructFu::Int32le, "4 byte little-endian value" do
224
224
 
225
- before :each do
226
- @int = StructFu::Int32le.new(11)
227
- end
225
+ before :each do
226
+ @int = StructFu::Int32le.new(11)
227
+ end
228
228
 
229
- it "should behave pretty much like any other 32 bit int" do
230
- @int.to_s.should == "\x0b\x00\x00\x00"
231
- end
229
+ it "should behave pretty much like any other 32 bit int" do
230
+ @int.to_s.should == "\x0b\x00\x00\x00"
231
+ end
232
232
 
233
- it "should raise when you try to change endianness" do
234
- expect { @int.endian = :big }.to raise_error
235
- expect { @int.endian = :little }.to raise_error
236
- end
233
+ it "should raise when you try to change endianness" do
234
+ expect { @int.endian = :big }.to raise_error
235
+ expect { @int.endian = :little }.to raise_error
236
+ end
237
237
 
238
238
  end
239
239
 
240
240
  describe StructFu::Int32be, "4 byte big-endian value" do
241
241
 
242
- before :each do
243
- @int = StructFu::Int32be.new(11)
244
- end
242
+ before :each do
243
+ @int = StructFu::Int32be.new(11)
244
+ end
245
245
 
246
- it "should behave pretty much like any other 32 bit int" do
247
- @int.to_s.should == "\x00\x00\x00\x0b"
248
- end
246
+ it "should behave pretty much like any other 32 bit int" do
247
+ @int.to_s.should == "\x00\x00\x00\x0b"
248
+ end
249
249
 
250
- it "should raise when you try to change endianness" do
251
- expect { @int.endian = :big }.to raise_error
252
- expect { @int.endian = :little }.to raise_error
253
- end
250
+ it "should raise when you try to change endianness" do
251
+ expect { @int.endian = :big }.to raise_error
252
+ expect { @int.endian = :little }.to raise_error
253
+ end
254
254
 
255
255
  end
256
256
 
257
257
  describe StructFu::String, "a sligtly more special String" do
258
258
 
259
- before :each do
260
- @str = StructFu::String.new("Oi, a string")
261
- end
259
+ before :each do
260
+ @str = StructFu::String.new("Oi, a string")
261
+ end
262
262
 
263
- it "should behave pretty much like a string" do
264
- @str.should be_kind_of(String)
265
- end
263
+ it "should behave pretty much like a string" do
264
+ @str.should be_kind_of(String)
265
+ end
266
266
 
267
- it "should have a read method" do
268
- @str.should respond_to(:read)
269
- end
267
+ it "should have a read method" do
268
+ @str.should respond_to(:read)
269
+ end
270
270
 
271
- it "should read data like other StructFu things" do
272
- @str.read("hello")
273
- @str.should == "hello"
274
- end
271
+ it "should read data like other StructFu things" do
272
+ @str.read("hello")
273
+ @str.should == "hello"
274
+ end
275
275
 
276
276
  end
277
277
 
278
278
  describe StructFu::IntString do
279
279
 
280
- it "should be" do
281
- StructFu::IntString.should be
282
- end
283
-
284
- it "should have a length and value" do
285
- istr = StructFu::IntString.new("Avast!")
286
- istr.to_s.should == "\x06Avast!"
287
- end
288
-
289
- it "should have a 16-bit length and a value" do
290
- istr = StructFu::IntString.new("Avast!",StructFu::Int16)
291
- istr.to_s.should == "\x00\x06Avast!"
292
- end
293
-
294
- it "should have a 32-bit length and a value" do
295
- istr = StructFu::IntString.new("Avast!",StructFu::Int32)
296
- istr.to_s.should == "\x00\x00\x00\x06Avast!"
297
- end
298
-
299
- before :each do
300
- @istr = StructFu::IntString.new("Avast!",StructFu::Int32)
301
- end
302
-
303
- it "should report the correct length with a new string" do
304
- @istr.to_s.should == "\x00\x00\x00\x06Avast!"
305
- @istr.string = "Ahoy!"
306
- @istr.to_s.should == "\x00\x00\x00\x05Ahoy!"
307
- end
308
-
309
- it "should report the correct length with a new string" do
310
- @istr.string = "Ahoy!"
311
- @istr.to_s.should == "\x00\x00\x00\x05Ahoy!"
312
- end
313
-
314
- it "should keep the old length with a new string" do
315
- @istr[:string] = "Ahoy!"
316
- @istr.to_s.should == "\x00\x00\x00\x06Ahoy!"
317
- end
318
-
319
- it "should allow for adjusting the length manually" do
320
- @istr.len = 16
321
- @istr.to_s.should == "\x00\x00\x00\x10Avast!"
322
- end
323
-
324
- it "should read in an expected string" do
325
- data = "\x00\x00\x00\x09Yo ho ho!"
326
- @istr.read(data)
327
- @istr.to_s.should == data
328
- end
329
-
330
- it "should raise when a string is too short" do
331
- data = "\x01A"
332
- expect { @istr.read(data) }.to raise_error
333
- end
280
+ it "should be" do
281
+ StructFu::IntString.should be
282
+ end
283
+
284
+ it "should have a length and value" do
285
+ istr = StructFu::IntString.new("Avast!")
286
+ istr.to_s.should == "\x06Avast!"
287
+ end
288
+
289
+ it "should have a 16-bit length and a value" do
290
+ istr = StructFu::IntString.new("Avast!",StructFu::Int16)
291
+ istr.to_s.should == "\x00\x06Avast!"
292
+ end
293
+
294
+ it "should have a 32-bit length and a value" do
295
+ istr = StructFu::IntString.new("Avast!",StructFu::Int32)
296
+ istr.to_s.should == "\x00\x00\x00\x06Avast!"
297
+ end
298
+
299
+ before :each do
300
+ @istr = StructFu::IntString.new("Avast!",StructFu::Int32)
301
+ end
302
+
303
+ it "should report the correct length with a new string" do
304
+ @istr.to_s.should == "\x00\x00\x00\x06Avast!"
305
+ @istr.string = "Ahoy!"
306
+ @istr.to_s.should == "\x00\x00\x00\x05Ahoy!"
307
+ end
308
+
309
+ it "should report the correct length with a new string" do
310
+ @istr.string = "Ahoy!"
311
+ @istr.to_s.should == "\x00\x00\x00\x05Ahoy!"
312
+ end
313
+
314
+ it "should keep the old length with a new string" do
315
+ @istr[:string] = "Ahoy!"
316
+ @istr.to_s.should == "\x00\x00\x00\x06Ahoy!"
317
+ end
318
+
319
+ it "should allow for adjusting the length manually" do
320
+ @istr.len = 16
321
+ @istr.to_s.should == "\x00\x00\x00\x10Avast!"
322
+ end
323
+
324
+ it "should read in an expected string" do
325
+ data = "\x00\x00\x00\x09Yo ho ho!"
326
+ @istr.read(data)
327
+ @istr.to_s.should == data
328
+ end
329
+
330
+ it "should raise when a string is too short" do
331
+ data = "\x01A"
332
+ expect { @istr.read(data) }.to raise_error
333
+ end
334
334
 
335
335
  end