$:.unshift File.join(File.dirname(__FILE__), "..", "lib") require 'test/unit' require 'libnet4r' class TestHeader1 < Libnet::Header unsigned_field :foo, 4 unsigned_field :bar, 2, :optional => true, :aliases => [ :bar2, :bar3 ] unsigned_field :baz, 1, :read_only => true, :aliases => [ :baz2 ] octets_field :qux end class TestHeader2 < Libnet::Header unsigned_field :checksum, 4 end class TC_Libnet_Header < Test::Unit::TestCase def test_fields assert_equal([:foo, :qux, :bar, :baz], TestHeader1.fields) assert_equal([:foo, :qux], TestHeader1.required_fields) assert_equal([:bar], TestHeader1.optional_fields) assert_equal([:baz], TestHeader1.read_only_fields) assert_equal([:checksum], TestHeader2.fields) assert_equal([:checksum], TestHeader2.required_fields) assert_equal([], TestHeader2.optional_fields) assert_equal([], TestHeader2.read_only_fields) end def test_decode assert_respond_to(TestHeader1, :decode) # subclasses must implement the decode method assert_raise NotImplementedError do p = TestHeader1.new("foobarbaz") end end def test_field_assignment p = TestHeader1.new # writeable fields should have assigment methods assert_respond_to(p, :foo=) assert_respond_to(p, :bar=) assert_respond_to(p, :qux=) assert_equal(p.foo = 1, 1) assert_equal(p.bar = 2, 2) assert_equal(p.qux = "stuff", "stuff") # read-only fields should not have assignment methods assert_raise NoMethodError do p.baz = 1 end end def test_read_query p = TestHeader1.new assert_respond_to(p, :foo) assert_respond_to(p, :foo?) assert_respond_to(p, :bar) assert_respond_to(p, :bar?) assert_respond_to(p, :baz) assert_respond_to(p, :baz?) assert_respond_to(p, :qux) assert_respond_to(p, :qux?) assert_equal(false, p.foo?) assert_equal(false, p.bar?) assert_equal(false, p.baz?) assert_equal(false, p.qux?) p.foo = 1 p.bar = 2 p.qux = "stuff" assert_equal(true, p.foo?) assert_equal(true, p.bar?) assert_equal(false, p.baz?) assert_equal(true, p.qux?) assert_equal(p.foo, 1) assert_equal(p.bar, 2) assert_equal(p.qux, "stuff") end def test_alias p = TestHeader1.new assert_respond_to(p, :bar2) assert_respond_to(p, :bar2=) assert_respond_to(p, :bar2?) assert_respond_to(p, :bar3) assert_respond_to(p, :bar3=) assert_respond_to(p, :bar3?) assert_respond_to(p, :baz2) assert_respond_to(p, :baz2?) assert_equal(false, p.respond_to?(:baz2=)) end def test_range_checks p = TestHeader1.new assert_raise ArgumentError do p.foo = -1 end assert_equal(p.foo = 0, 0) assert_equal(p.foo = 0xffffffff, 0xffffffff) assert_raise ArgumentError do p.foo = 0x100000000 end end def test_ptag p = TestHeader1.new assert_respond_to(p, :ptag) end def test_auto_checksum_toggle p1 = TestHeader1.new p2 = TestHeader2.new assert_raise NoMethodError do p1.auto_checksum? end assert_raise NoMethodError do p1.auto_checksum = false end assert_respond_to(p2, :auto_checksum?) assert_respond_to(p2, :auto_checksum=) end def test_check_packable p = TestHeader1.new assert(p.private_methods.include?('check_packable')) assert_raise ArgumentError, "the following TestHeader1 required fields have not been set: foo, qux" do p.send(:check_packable) end p.foo = 1 assert_raise ArgumentError, "the following TestHeader1 required fields have not been set: qux" do p.send(:check_packable) end p.qux = "stuff" assert_nothing_raised ArgumentError do p.send(:check_packable) end end end