1require_relative 'utils'
2require 'stringio'
3
4class OpenSSL::TestBuffering < Test::Unit::TestCase
5
6  class IO
7    include OpenSSL::Buffering
8
9    attr_accessor :sync
10
11    def initialize
12      @io = ""
13      def @io.sync
14        true
15      end
16
17      super
18
19      @sync = false
20    end
21
22    def string
23      @io
24    end
25
26    def sysread(size)
27      str = @io.slice!(0, size)
28      raise EOFError if str.empty?
29      str
30    end
31
32    def syswrite(str)
33      @io << str
34      str.size
35    end
36  end
37
38  def setup
39    @io = IO.new
40  end
41
42  def test_flush
43    @io.write 'a'
44
45    refute @io.sync
46    assert_empty @io.string
47
48    assert_equal @io, @io.flush
49
50    refute @io.sync
51    assert_equal 'a', @io.string
52  end
53
54  def test_flush_error
55    @io.write 'a'
56
57    refute @io.sync
58    assert_empty @io.string
59
60    def @io.syswrite *a
61      raise SystemCallError, 'fail'
62    end
63
64    assert_raises SystemCallError do
65      @io.flush
66    end
67
68    refute @io.sync, 'sync must not change'
69  end
70
71  def test_getc
72    @io.syswrite('abc')
73    assert_equal(?a, @io.getc)
74    assert_equal(?b, @io.getc)
75    assert_equal(?c, @io.getc)
76  end
77
78  def test_each_byte
79    @io.syswrite('abc')
80    res = []
81    @io.each_byte do |c|
82      res << c
83    end
84    assert_equal([97, 98, 99], res)
85  end
86
87end if defined?(OpenSSL)
88