1 # -*- encoding: binary -*-
6 # every time something is mocked, trust levels in tests cases dimish
7 # This is not needed under Ruby 1.8
8 class MyStringIO < StringIO
9 unless instance_methods.include?(:readpartial)
10 warn "W: defining readpartial for MyStringIO RUBY_VERSION=#{RUBY_VERSION}"
12 def readpartial(length, buf = "")
15 rv.nil? and raise EOFError
21 class TestIO < Test::Unit::TestCase
24 @raw = MyStringIO.new("")
25 @io = Sunshowers::IO.new(@raw) # binary
26 if defined?(Encoding::Binary)
27 assert(Encoding::Binary, @io.to_io.encoding)
32 @raw.string << "\x00FOO\xff"
34 assert_equal "FOO", rv
35 assert_equal Encoding::UTF_8, rv.encoding if rv.respond_to?(:encoding)
38 def test_gets_bad_utf8
39 @raw.string << "\x00\xe0\xff"
40 assert_raises(Sunshowers::ProtocolError) { @io.gets }
43 def test_gets_bad_alignment
44 @raw.string << "\x00FOO"
48 assert_equal "FOO", rv
49 assert_equal Encoding::UTF_8, rv.encoding if rv.respond_to?(:encoding)
56 assert_equal "\0#{str}\xff", wr
57 assert_equal Encoding::BINARY, wr.encoding if wr.respond_to?(:encoding)
61 str = "\xff\xff\x00\x00\xff"
63 assert_equal Encoding::BINARY, str.encoding if str.respond_to?(:encoding)
66 assert_equal "\x80\x05#{expect}", wr
67 assert_equal Encoding::BINARY, wr.encoding if wr.respond_to?(:encoding)
70 def test_read_write_binary_big
71 @io.keep_binary = true
72 [ 127, 128, 129, 5, 200 ].each do |i|
75 assert_equal Encoding::BINARY, str.encoding if str.respond_to?(:encoding)
81 assert_equal Encoding::BINARY, rd.encoding if str.respond_to?(:encoding)
82 assert_equal rd, expect
87 str = "\xff\xff\x00\x00\xff"
89 assert_equal Encoding::BINARY, str.encoding if str.respond_to?(:encoding)
92 assert_equal "\x80\x05#{expect}", wr
93 assert_equal Encoding::BINARY, wr.encoding if wr.respond_to?(:encoding)
97 assert_equal "", @raw.string
100 if str.respond_to?(:encode)
101 str.force_encoding(Encoding::UTF_8)
102 assert_equal Encoding::UTF_8, str.encoding
106 assert_equal "\0#{str}\xff", wr
107 assert_equal Encoding::BINARY, wr.encoding if wr.respond_to?(:encoding)
111 max = Sunshowers::IO::MAX_UTF8_SIZE
112 @raw.string << "\0#{'.' * max}\xff"
113 assert_equal max, @io.gets.length
116 def test_utf8_over_limit
117 max = Sunshowers::IO::MAX_UTF8_SIZE + Sunshowers::IO::RD_SIZE
118 @raw.string << "\0#{'.' * max}\xff"
119 assert_raises(Sunshowers::ProtocolError) { @io.gets }
122 def test_binary_limit
123 @io.keep_binary = true
124 max = Sunshowers::IO::MAX_BINARY_SIZE
125 @io.write_binary('.' * max)
127 assert_equal('.' * max, @io.gets)
130 def test_binary_over_limit
131 @io.keep_binary = true
132 max = Sunshowers::IO::MAX_BINARY_SIZE + Sunshowers::IO::RD_SIZE
133 @io.write_binary('.' * max)
135 assert_raises(Sunshowers::ProtocolError) { @io.gets }