Class: IBBTest

Inherits:
Test::Unit::TestCase show all
Includes:
ClientTester
Defined in:
lib/gems/xmpp4r-0.4/test/bytestreams/tc_ibb.rb

Instance Method Summary collapse

Methods inherited from Test::Unit::TestCase

#assert_array_equal, expect, #run

Instance Method Details

#create_buffer(size) ⇒ Object



16
17
18
# File 'lib/gems/xmpp4r-0.4/test/bytestreams/tc_ibb.rb', line 16

def create_buffer(size)
  ([nil] * size).collect { rand(256).chr }.join
end

#test_ibb_errorObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/gems/xmpp4r-0.4/test/bytestreams/tc_ibb.rb', line 137

def test_ibb_error
  target = Bytestreams::IBBTarget.new(@server, '1', nil, '[email protected]/1')
  initiator = Bytestreams::IBBInitiator.new(@client, '1', nil, '[email protected]/1')

  Thread.new do
    target.accept

    @server.send("<message from='[email protected]/1' type='error'>
                    <data xmlns='http://jabber.org/protocol/ibb' sid='#{target.instance_variable_get(:@session_id)}' seq='0'/>
                  </message>")
  end


  initiator.open

  assert_nil(initiator.read)

  initiator.close
end

#test_ibb_inactiveObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/gems/xmpp4r-0.4/test/bytestreams/tc_ibb.rb', line 157

def test_ibb_inactive
  target = Bytestreams::IBBTarget.new(@server, '1', nil, '[email protected]/1')
  initiator = Bytestreams::IBBInitiator.new(@client, '1', nil, '[email protected]/1')

  assert_nil(target.read)
  assert_nil(initiator.read)

  assert_raise(RuntimeError) {
    target.write('a' * target.block_size)
  }
  assert_raise(RuntimeError) {
    initiator.write('a' * initiator.block_size)
  }
end

#test_ibb_initiator2targetObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gems/xmpp4r-0.4/test/bytestreams/tc_ibb.rb', line 46

def test_ibb_initiator2target
  target = Bytestreams::IBBTarget.new(@server, '1', nil, '[email protected]/1')
  initiator = Bytestreams::IBBInitiator.new(@client, '1', nil, '[email protected]/1')

  buffer = create_buffer(9999)

  Thread.new do
    Thread.pass
    initiator.open
    initiator.write(buffer)
    Thread.pass
    initiator.close
  end


  target.accept

  received = ''
  while buf = target.read
    received += buf
  end

  target.close

  assert_equal(buffer, received)
end

#test_ibb_pingpongObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/gems/xmpp4r-0.4/test/bytestreams/tc_ibb.rb', line 73

def test_ibb_pingpong
  ignored_stanzas = 0
  wait = Semaphore.new
  @server.add_message_callback { ignored_stanzas += 1; wait.run }
  @server.add_iq_callback { ignored_stanzas += 1; wait.run }


  target = Bytestreams::IBBTarget.new(@server, '1', nil, '[email protected]/1')
  initiator = Bytestreams::IBBInitiator.new(@client, '1', nil, '[email protected]/1')

  Thread.new do
    target.accept

    while buf = target.read
      target.write(buf)
      target.flush
    end

    target.close
  end


  assert_equal(0, ignored_stanzas)
  @client.send("<iq from='[email protected]/1' type='set'>
                  <close xmlns='http://jabber.org/protocol/ibb' sid='another session id'/>
                </iq>")
  wait.wait
  assert_equal(1, ignored_stanzas)


  initiator.open


  assert_equal(1, ignored_stanzas)
  @client.send("<message from='[email protected]/1' type='error'>
                  <data xmlns='http://jabber.org/protocol/ibb' sid='another session id' seq='0'/>
                </message>")
  wait.wait
  assert_equal(2, ignored_stanzas)
  @client.send("<iq from='[email protected]/1' type='set'>
                  <close xmlns='http://jabber.org/protocol/ibb' sid='another session id'/>
                </iq>")
  wait.wait
  assert_equal(3, ignored_stanzas)


  10.times do
    buf = create_buffer(9999)
    initiator.write(buf)
    initiator.flush

    bufr = ''
    begin
      bufr += initiator.read
    end while bufr.size < buf.size
    assert_equal(buf, bufr)
  end

  initiator.close


  assert_equal(3, ignored_stanzas)
end

#test_ibb_queueitemObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/gems/xmpp4r-0.4/test/bytestreams/tc_ibb.rb', line 172

def test_ibb_queueitem
  i1 = Bytestreams::IBBQueueItem.new(:close)
  assert_equal(:close, i1.type)
  assert_nil(i1.seq)

  i2 = Bytestreams::IBBQueueItem.new(:data, 1, ['blah'].pack('m'))
  assert_equal(:data, i2.type)
  assert_equal(1, i2.seq)
  assert_equal('blah', i2.data)

  assert_raise(RuntimeError) {
    i3 = Bytestreams::IBBQueueItem.new(:invalid)
  }
end

#test_ibb_target2initiatorObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gems/xmpp4r-0.4/test/bytestreams/tc_ibb.rb', line 20

def test_ibb_target2initiator
  target = Bytestreams::IBBTarget.new(@server, '1', nil, '[email protected]/1')
  initiator = Bytestreams::IBBInitiator.new(@client, '1', nil, '[email protected]/1')

  buffer = create_buffer(9999)

  Thread.new do
    target.accept
    target.write(buffer)
    Thread.pass
    target.close
  end


  initiator.open

  received = ''
  while buf = initiator.read
    received += buf
  end

  initiator.close

  assert_equal(buffer, received)
end