racket2 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. data/README +76 -0
  2. data/examples/arp-send +24 -0
  3. data/examples/arp-send2 +30 -0
  4. data/examples/cdp +39 -0
  5. data/examples/cdp-spew +52 -0
  6. data/examples/dhcp +42 -0
  7. data/examples/dhcp-spew +48 -0
  8. data/examples/dns +38 -0
  9. data/examples/egp +30 -0
  10. data/examples/hsrp +43 -0
  11. data/examples/hsrp_takeover +69 -0
  12. data/examples/icmp-recv +34 -0
  13. data/examples/icmp-spew +50 -0
  14. data/examples/icmpv6 +84 -0
  15. data/examples/icmpv6-spew +50 -0
  16. data/examples/igmpv1 +27 -0
  17. data/examples/igmpv2 +27 -0
  18. data/examples/igrp-send +25 -0
  19. data/examples/ipv6 +35 -0
  20. data/examples/ipv6-option +40 -0
  21. data/examples/ntp +38 -0
  22. data/examples/ntp2 +42 -0
  23. data/examples/sctp +32 -0
  24. data/examples/stp-send +21 -0
  25. data/examples/synflood +147 -0
  26. data/examples/tcp +43 -0
  27. data/examples/tcp2udp +65 -0
  28. data/examples/udp +46 -0
  29. data/examples/vrrp +34 -0
  30. data/examples/vtp +28 -0
  31. data/lib/racket.rb +4 -0
  32. data/lib/racket/l2.rb +30 -0
  33. data/lib/racket/l2/eightotwodotthree.rb +48 -0
  34. data/lib/racket/l2/ethernet.rb +62 -0
  35. data/lib/racket/l2/llc.rb +50 -0
  36. data/lib/racket/l2/misc.rb +67 -0
  37. data/lib/racket/l2/snap.rb +40 -0
  38. data/lib/racket/l2/vlan.rb +61 -0
  39. data/lib/racket/l2/vtp.rb +124 -0
  40. data/lib/racket/l3.rb +30 -0
  41. data/lib/racket/l3/arp.rb +63 -0
  42. data/lib/racket/l3/cdp.rb +85 -0
  43. data/lib/racket/l3/egp.rb +53 -0
  44. data/lib/racket/l3/ipv4.rb +132 -0
  45. data/lib/racket/l3/ipv6.rb +66 -0
  46. data/lib/racket/l3/misc.rb +165 -0
  47. data/lib/racket/l3/stp.rb +81 -0
  48. data/lib/racket/l4.rb +30 -0
  49. data/lib/racket/l4/gre.rb +65 -0
  50. data/lib/racket/l4/icmp.rb +295 -0
  51. data/lib/racket/l4/icmpv6.rb +460 -0
  52. data/lib/racket/l4/igmpv1.rb +79 -0
  53. data/lib/racket/l4/igmpv2.rb +76 -0
  54. data/lib/racket/l4/igrp.rb +138 -0
  55. data/lib/racket/l4/misc.rb +35 -0
  56. data/lib/racket/l4/sctp.rb +163 -0
  57. data/lib/racket/l4/tcp.rb +152 -0
  58. data/lib/racket/l4/udp.rb +81 -0
  59. data/lib/racket/l4/vrrp.rb +95 -0
  60. data/lib/racket/l5.rb +30 -0
  61. data/lib/racket/l5/bootp.rb +106 -0
  62. data/lib/racket/l5/dns.rb +110 -0
  63. data/lib/racket/l5/hsrp.rb +73 -0
  64. data/lib/racket/l5/misc.rb +35 -0
  65. data/lib/racket/l5/ntp.rb +59 -0
  66. data/lib/racket/misc.rb +30 -0
  67. data/lib/racket/misc/lv.rb +108 -0
  68. data/lib/racket/misc/misc.rb +61 -0
  69. data/lib/racket/misc/orderedhash.rb +63 -0
  70. data/lib/racket/misc/raw.rb +35 -0
  71. data/lib/racket/misc/tlv.rb +103 -0
  72. data/lib/racket/misc/vt.rb +114 -0
  73. data/lib/racket/racket.rb +164 -0
  74. data/lib/racket/racketpart.rb +66 -0
  75. data/test/l2/ts_ethernet.rb +22 -0
  76. data/test/l2/ts_misc.rb +23 -0
  77. data/test/l2/ts_vlan.rb +15 -0
  78. data/test/l3/ts_ipv4.rb +44 -0
  79. data/test/l3/ts_ipv6.rb +26 -0
  80. data/test/l3/ts_misc.rb +31 -0
  81. data/test/l4/ts_icmp.rb +38 -0
  82. data/test/l4/ts_tcp.rb +55 -0
  83. data/test/l4/ts_udp.rb +40 -0
  84. data/test/misc/ts_lv.rb +59 -0
  85. data/test/misc/ts_orderedhash.rb +33 -0
  86. data/test/misc/ts_tlv.rb +47 -0
  87. data/test/misc/ts_vt.rb +56 -0
  88. data/test/ts_all.rb +14 -0
  89. metadata +156 -0
@@ -0,0 +1,66 @@
1
+ # $Id: racketpart.rb 14 2008-03-02 05:42:30Z warchild $
2
+ #
3
+ # Copyright (c) 2008, Jon Hart
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ # * Redistributions of source code must retain the above copyright
9
+ # notice, this list of conditions and the following disclaimer.
10
+ # * Redistributions in binary form must reproduce the above copyright
11
+ # notice, this list of conditions and the following disclaimer in the
12
+ # documentation and/or other materials provided with the distribution.
13
+ # * Neither the name of the <organization> nor the
14
+ # names of its contributors may be used to endorse or promote products
15
+ # derived from this software without specific prior written permission.
16
+ #
17
+ # THIS SOFTWARE IS PROVIDED BY Jon Hart ``AS IS'' AND ANY
18
+ # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ # DISCLAIMED. IN NO EVENT SHALL Jon Hart BE LIABLE FOR ANY
21
+ # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
+ # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
+ # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
+ # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ #
28
+ require 'rubygems'
29
+ require 'bit-struct'
30
+
31
+ module Racket
32
+ # Every Racket object is made up of numerous sub-parts, namely layers of the OSI stack.
33
+ class RacketPart < BitStruct
34
+
35
+ # Boolean indicating whether or not this instance should be
36
+ # automatically "fixed" prior to be packed and sent.
37
+ attr_accessor :autofix
38
+
39
+ # Should this instance be automatically fixed
40
+ # prior to being packed and sent?
41
+ def autofix?
42
+ @autofix
43
+ end
44
+
45
+ def initialize(*args)
46
+ @autofix = true
47
+ super(*args)
48
+ end
49
+
50
+
51
+ # Print out all of the fields and all of their values
52
+ def pretty
53
+ s = ""
54
+ self.fields.each do |f|
55
+ unless (f.name == "payload")
56
+ s += "#{f.name}=#{self.send(f.name)} "
57
+ end
58
+ end
59
+ s.gsub(/ $/, '')
60
+ end
61
+
62
+ def fix!
63
+ end
64
+ end
65
+ end
66
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,22 @@
1
+ # $Id: ts_ethernet.rb 129 2009-11-29 01:37:06Z jhart $
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), "../..", "lib")
4
+
5
+ require 'test/unit'
6
+ require 'racket'
7
+
8
+ class TestEthernet < Test::Unit::TestCase
9
+ def test_init
10
+ assert_nothing_raised() { Racket::L2::Ethernet.new }
11
+ assert_nothing_raised() { Racket::L2::Ethernet.new(Racket::Misc.randstring(30)) }
12
+ end
13
+
14
+ def test_attrs
15
+ binary = "\x00\x00\x24\xc1\x8c\x09\x00\x30\x1b\xa0\x63\x16\x08\x00\x45\x10\x00\x3c\x2f\xdf\x40\x00\x40\x06\x89\x17\xc0\xa8\x00\x64\xc0\xa8\x00\x01\x99\xb7\x00\x35\x29\x39\x28\x66\x00\x00\x00\x00\xa0\x02\x16\xd0\xbc\x04\x00\x00\x02\x04\x05\xb4\x04\x02\x08\x0a\x00\x31\x07\xb9\x00\x00\x00\x00\x01\x03\x03\x07"
16
+ e = Racket::L2::Ethernet.new(binary)
17
+ assert_equal(e.src_mac, "00:30:1b:a0:63:16")
18
+ assert_equal(e.dst_mac, "00:00:24:c1:8c:09")
19
+ assert_equal(e.ethertype, 2048)
20
+ end
21
+ end
22
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,23 @@
1
+ # $Id: ts_misc.rb 142 2009-12-13 01:53:14Z jhart $
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), "../..", "lib")
4
+
5
+ require 'test/unit'
6
+ require 'racket'
7
+
8
+ class TestL2Misc < Test::Unit::TestCase
9
+ def test_convert
10
+ (0..rand(1024)).each {
11
+ len = rand(512)+1
12
+ mac = Racket::L2::Misc.randommac(len)
13
+ assert_equal(mac.length, (len*2) + (len-1))
14
+ long = Racket::L2::Misc.mac2long(mac)
15
+ assert_equal(mac, Racket::L2::Misc.long2mac(long, len))
16
+ assert_equal(long, Racket::L2::Misc.mac2long(mac))
17
+ mac = "00:11:22:33:44:55"
18
+ string = Racket::L2::Misc.mac2string(mac)
19
+ assert_equal(mac, Racket::L2::Misc.string2mac(string))
20
+ }
21
+ end
22
+ end
23
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,15 @@
1
+ # $Id: ts_vlan.rb 129 2009-11-29 01:37:06Z jhart $
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), "../..", "lib")
4
+
5
+ require 'test/unit'
6
+ require 'racket'
7
+
8
+ class TestVLAN < Test::Unit::TestCase
9
+ def test_init
10
+ assert_nothing_raised() { Racket::L2::VLAN.new }
11
+ assert_nothing_raised() { Racket::L2::VLAN.new(Racket::Misc.randstring(30)) }
12
+ end
13
+
14
+ end
15
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,44 @@
1
+ # $Id: ts_ipv4.rb 129 2009-11-29 01:37:06Z jhart $
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), "../..", "lib")
4
+
5
+ require 'test/unit'
6
+ require 'racket'
7
+
8
+ class TestIPv4 < Test::Unit::TestCase
9
+ def test_init
10
+ assert_nothing_raised() { Racket::L3::IPv4.new }
11
+ assert_nothing_raised() { Racket::L3::IPv4.new(Racket::Misc.randstring(30)) }
12
+ end
13
+
14
+ def test_attrs
15
+ binary = "\x45\x10\x00\x3c\x2f\xdf\x40\x00\x40\x06\x89\x17\xc0\xa8\x00\x64\xc0\xa8\x00\x01\x99\xb7\x00\x35\x29\x39\x28\x66\x00\x00\x00\x00\xa0\x02\x16\xd0\xbc\x04\x00\x00\x02\x04\x05\xb4\x04\x02\x08\x0a\x00\x31\x07\xb9\x00\x00\x00\x00\x01\x03\x03\x07"
16
+ i = Racket::L3::IPv4.new(binary)
17
+ assert_equal(i.version, 4)
18
+ assert_equal(i.hlen, 5)
19
+ assert_equal(i.tos, 16)
20
+ assert_equal(i.len, 60)
21
+ assert_equal(i.id, 12255)
22
+ assert_equal(i.flags, 2)
23
+ assert_equal(i.foffset, 0)
24
+ assert_equal(i.ttl, 64)
25
+ assert_equal(i.protocol, 6)
26
+ assert_equal(i.checksum, 35095)
27
+ assert_equal(i.src_ip, "192.168.0.100")
28
+ assert_equal(i.dst_ip, "192.168.0.1")
29
+
30
+ i.src_ip = "1.2.3.4"
31
+ i.fix!
32
+ assert_equal(i.checksum, 17950)
33
+ end
34
+
35
+ def test_convert
36
+ (0..512).each {
37
+ long = rand(2**32)
38
+ ipv4 = Racket::L3::Misc.long2ipv4(long)
39
+ assert_equal(long, Racket::L3::Misc.ipv42long(ipv4))
40
+ assert_equal(ipv4, Racket::L3::Misc.long2ipv4(long))
41
+ }
42
+ end
43
+ end
44
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,26 @@
1
+ # $Id: ts_ipv6.rb 129 2009-11-29 01:37:06Z jhart $
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), "../..", "lib")
4
+
5
+ require 'test/unit'
6
+ require 'racket'
7
+
8
+ class TestIPV6 < Test::Unit::TestCase
9
+ def test_init
10
+ assert_nothing_raised() { Racket::L3::IPv6.new }
11
+ assert_nothing_raised() { Racket::L3::IPv6.new(Racket::Misc.randstring(30)) }
12
+ end
13
+
14
+ def test_convert
15
+ long = rand(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
16
+ ipv6 = Racket::L3::Misc.long2ipv6(long)
17
+
18
+ assert_equal(long, Racket::L3::Misc.ipv62long(ipv6))
19
+ assert_equal(ipv6, Racket::L3::Misc.long2ipv6(long))
20
+ assert_nothing_raised { Racket::L3::IPv6.new("a:b:c::1") }
21
+ assert_nothing_raised { Racket::L3::Misc.soll_mcast_addr6(ipv6) }
22
+ mac = Racket::L2::Misc.randommac
23
+ assert_nothing_raised { Racket::L3::Misc.soll_mcast_mac(mac) }
24
+ end
25
+ end
26
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,31 @@
1
+ # $Id: ts_misc.rb 135 2009-12-06 20:39:40Z jhart $
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), "../..", "lib")
4
+
5
+ require 'test/unit'
6
+ require 'racket'
7
+
8
+ class TestL3Misc < Test::Unit::TestCase
9
+ def test_misc
10
+ assert_nothing_raised {
11
+ Racket::L3::Misc.checksum(Racket::Misc.randstring(rand(2048)))
12
+ }
13
+
14
+ mac = "6e:f1:eb:b7:c8:72"
15
+ ll = "fe80::6cf1:ebff:feb7:c872"
16
+ assert_equal(ll, Racket::L3::Misc.linklocaladdr(mac))
17
+
18
+ ipv4 = Racket::L3::Misc.randomipv4
19
+ lipv4 = Racket::L3::Misc.ipv42long(ipv4)
20
+ ipv6 = Racket::L3::Misc.randomipv6
21
+ lipv6 = Racket::L3::Misc.ipv62long(ipv6)
22
+
23
+ assert_equal(ipv4, Racket::L3::Misc.long2ipv4(lipv4))
24
+ assert_equal(ipv6, Racket::L3::Misc.long2ipv6(lipv6))
25
+
26
+ ipv6 = "a:0:0:0f:0:1"
27
+ zipv6 = "a::f:0:1"
28
+ assert_equal(zipv6, Racket::L3::Misc.compressipv6(ipv6))
29
+ end
30
+ end
31
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,38 @@
1
+ # $Id: ts_icmp.rb 129 2009-11-29 01:37:06Z jhart $
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), "../..", "lib")
4
+
5
+ require 'test/unit'
6
+ require 'racket'
7
+
8
+ class TestICMP < Test::Unit::TestCase
9
+ def test_init
10
+ assert_nothing_raised() { Racket::L4::ICMP.new }
11
+ assert_nothing_raised() { Racket::L4::ICMP.new(Racket::Misc.randstring(20)) }
12
+ end
13
+
14
+ def test_raw
15
+ binary="\x9e\x96\x00\x35\x00\x0c\x69\x4f\x66\x6f\x6f\x0a"
16
+ binary="\x08\x00\x05\x7e\xab\x58\x00\x01\x78\xf1\x09\x4b\x00\x00\x00\x00\xce\xe6\x05\x00\x00\x00\x00\x00\x0e\x0f\x0a\x0b\x0c\x0d\x0e\x0f\x0a\x0b\x0c\x0d\x0e\x0f\x0a\x0b\x0c\x0d\x0e\x0f\x0a\x0b\x0c\x0d\x0e\x0f\x0a\x0b\x0c\x0d\x0e\x0f\x0a\x0b\x0c\x0d\x0e\x0f\x0a\x0b"
17
+ assert_nothing_raised() { i = Racket::L4::ICMP.new(binary) }
18
+ i = Racket::L4::ICMP.new(binary)
19
+ assert_equal(i.type, 8)
20
+ assert_equal(i.code, 0)
21
+ assert_equal(i.checksum, 0x057e)
22
+ assert_equal(i.payload,
23
+ "\xab\x58\x00\x01\x78\xf1\x09\x4b\x00\x00\x00\x00\xce\xe6\x05\x00\x00\x00\x00\x00\x0e\x0f\x0a\x0b\x0c\x0d\x0e\x0f\x0a\x0b\x0c\x0d\x0e\x0f\x0a\x0b\x0c\x0d\x0e\x0f\x0a\x0b\x0c\x0d\x0e\x0f\x0a\x0b\x0c\x0d\x0e\x0f\x0a\x0b\x0c\x0d\x0e\x0f\x0a\x0b")
24
+ end
25
+
26
+ def test_build
27
+ i = Racket::L4::ICMP.new
28
+ assert_nothing_raised() {
29
+ i.type = 8
30
+ i.code = 0
31
+ i.payload = "thisisatest"
32
+ i.fix!
33
+ }
34
+ assert_equal(i.checksum, 30152)
35
+ end
36
+
37
+ end
38
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,55 @@
1
+ # $Id: ts_tcp.rb 129 2009-11-29 01:37:06Z jhart $
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), "../..", "lib")
4
+
5
+ require 'test/unit'
6
+ require 'racket'
7
+
8
+ class TestTCP < Test::Unit::TestCase
9
+ def test_init
10
+ assert_nothing_raised() { Racket::L4::TCP.new }
11
+ assert_nothing_raised() { Racket::L4::TCP.new(Racket::Misc.randstring(20)) }
12
+ end
13
+
14
+ def test_attrs
15
+ binary="\x90\xef\x08\xae\x47\x87\xa0\x34\x00\x00\x00\x00\xa0\x02\x16\xd0\xc2\x17\x00\x00\x02\x04\x05\xb4\x04\x02\x08\x0a\x03\x0f\xc8\x6e\x00\x00\x00\x00\x01\x03\x03\x07"
16
+
17
+ assert_nothing_raised() { i = Racket::L4::TCP.new(binary) }
18
+ i = Racket::L4::TCP.new(binary)
19
+ assert_equal(i.dst_port, 2222)
20
+ assert_equal(i.src_port, 37103)
21
+ assert_equal(i.flag_syn, 1)
22
+ assert_equal(i.flag_ack, 0)
23
+ assert_equal(i.seq, 0x4787a034)
24
+ assert_equal(i.ack, 0)
25
+ assert_equal(i.window, 5840)
26
+ assert_equal(i.checksum, 0xc217)
27
+ assert_equal(i.payload,
28
+ "\x02\x04\x05\xb4\x04\x02\x08\x0a\x03\x0f\xc8\x6e\x00\x00\x00\x00\x01\x03\x03\x07")
29
+
30
+ end
31
+
32
+ def test_build_no_options
33
+ i = Racket::L4::TCP.new
34
+ assert_nothing_raised() {
35
+ i.src_port = 44781
36
+ i.dst_port = 5190
37
+ i.seq = 0x4e3e537a
38
+ i.ack = 0xbdca7ef8
39
+ i.flag_ack = 1
40
+ i.flag_psh = 1
41
+ i.offset = 5
42
+ i.window = 61200
43
+ i.payload = "\x2a\x05\x0b\xca\x00\x00"
44
+ }
45
+
46
+ i.checksum!("192.168.1.10", "64.12.26.128")
47
+ assert_equal(i.checksum, 0xccf8)
48
+ end
49
+
50
+ def test_build_with_options
51
+ i = Racket::L4::TCP.new
52
+ i.add_option(1, "blah")
53
+ end
54
+ end
55
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,40 @@
1
+ # $Id: ts_udp.rb 129 2009-11-29 01:37:06Z jhart $
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), "../..", "lib")
4
+
5
+ require 'test/unit'
6
+ require 'racket'
7
+
8
+ class TestUDP < Test::Unit::TestCase
9
+ def test_init
10
+ assert_nothing_raised() { Racket::L4::UDP.new }
11
+ assert_nothing_raised() { Racket::L4::UDP.new(Racket::Misc.randstring(20)) }
12
+ end
13
+
14
+ def test_raw
15
+ binary="\x9e\x96\x00\x35\x00\x0c\x69\x4f\x66\x6f\x6f\x0a"
16
+ assert_nothing_raised() { i = Racket::L4::UDP.new(binary) }
17
+ i = Racket::L4::UDP.new(binary)
18
+ assert_equal(i.dst_port, 53)
19
+ assert_equal(i.src_port, 40598)
20
+ assert_equal(i.checksum, 0x694f)
21
+ assert_equal(i.len, 12)
22
+ assert_equal(i.payload, "foo\n")
23
+ end
24
+
25
+ def test_build
26
+ i = Racket::L4::UDP.new
27
+ assert_nothing_raised() {
28
+ i.src_port = 50243
29
+ i.dst_port = 53
30
+ i.payload = "hhahahahahah"
31
+ i.fix!("192.168.1.10", "1.2.3.4")
32
+ }
33
+
34
+ assert_equal(i.checksum, 0x2623)
35
+ assert_equal(i.len, 20)
36
+
37
+ end
38
+
39
+ end
40
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,59 @@
1
+ # $Id: ts_lv.rb 129 2009-11-29 01:37:06Z jhart $
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), "../..", "lib")
4
+
5
+ require 'test/unit'
6
+ require 'racket'
7
+
8
+ class TestLV < Test::Unit::TestCase
9
+ def test_init
10
+ assert_nothing_raised() { Racket::Misc::LV.new(2) }
11
+ assert_nothing_raised() { Racket::Misc::LV.new(2,4,1,1,6) }
12
+ end
13
+
14
+ def test_decode
15
+ t1 = Racket::Misc::LV.new(2)
16
+ s1 = "\x00\x0b\x73\x70\x6f\x6f\x66\x65\x64\x2e\x6f\x72\x67"
17
+ t1.decode!(s1)
18
+ assert_equal(t1.lengths[0], 11)
19
+ assert_equal(t1.values[0], "spoofed.org")
20
+
21
+ t2 = Racket::Misc::LV.new(1,2,2,4)
22
+ s2 = "\x02\xab\xcd\x00\x01\xff\x00\x04\xde\xad\xba\xbe\x00\x00\x00\x0a\xff\xff\xff\xff\xff\xff\xff\xff\xff\xffwtf?"
23
+ t2.decode!(s2)
24
+ assert_equal(t2.lengths[0], 2)
25
+ assert_equal(t2.values[0], "\xab\xcd")
26
+ assert_equal(t2.lengths[1], 1)
27
+ assert_equal(t2.values[1], "\xff")
28
+ assert_equal(t2.lengths[2], 4)
29
+ assert_equal(t2.values[2], "\xde\xad\xba\xbe")
30
+ assert_equal(t2.lengths[3], 10)
31
+ assert_equal(t2.values[3], "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff")
32
+ assert_equal(t2.rest, "wtf?")
33
+ end
34
+
35
+ def test_encode
36
+ t1 = Racket::Misc::LV.new(2)
37
+ s1 = "\x00\x0bspoofed.org"
38
+ t1.decode!(s1)
39
+ assert_equal(t1.encode, s1)
40
+ assert_equal("#{t1}", s1)
41
+
42
+ t2 = Racket::Misc::LV.new(1,2,2)
43
+ s2 = "\x01\xff\x00\x02\xff\xff\x00\x0bspoofed.org\xff\xff\xffwtf?"
44
+ t2.decode!(s2)
45
+ assert_equal(t2.encode, s2.slice(0, s2.size - 7))
46
+ assert_equal("#{t2}", s2.slice(0, s2.size - 7))
47
+
48
+ t3 = Racket::Misc::LV.new(1,1,1)
49
+ t3.lengths = [2,2,2]
50
+ t3.values = ["ww", "tt", "ff"]
51
+ assert_equal(t3.encode, "\x02ww\x02tt\x02ff")
52
+
53
+ t3 = Racket::Misc::LV.new(1,1,1)
54
+ t3.lengths = [2,2,4]
55
+ t3.values = ["ww", "tt", "ff"]
56
+ assert_equal(t3.encode, "\x02ww\x02tt\x04ff\00\00")
57
+ end
58
+ end
59
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,33 @@
1
+ # $Id: ts_orderedhash.rb 129 2009-11-29 01:37:06Z jhart $
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), "../..", "lib")
4
+
5
+ require 'test/unit'
6
+ require 'racket'
7
+
8
+ class TestBitField < Test::Unit::TestCase
9
+ def test_init
10
+ assert_nothing_raised() { Racket::Misc::OrderedHash.new }
11
+ end
12
+ def test_order
13
+ oh = Racket::Misc::OrderedHash.new
14
+ ("a".."z").each { |c|
15
+ oh[c] = c.unpack("c")[0]
16
+ }
17
+
18
+ 97.upto(122) { |v|
19
+ assert_equal(oh[sprintf("%c", v)], v)
20
+ }
21
+
22
+ assert_nil(oh['kajf'])
23
+
24
+ copy = []
25
+ oh.each_key { |k| copy << k }
26
+ 0.upto(25) { |v|
27
+ assert_equal(copy[v], sprintf("%c", 97 + v))
28
+ }
29
+
30
+ assert_nil(copy[205])
31
+ end
32
+ end
33
+ # vim: set ts=2 et sw=2: