| 1 | #!/usr/bin/env ruby |
|---|
| 2 | |
|---|
| 3 | $LOAD_PATH << "lib" |
|---|
| 4 | $LOAD_PATH << "../lib" |
|---|
| 5 | |
|---|
| 6 | require "net/irc" |
|---|
| 7 | require "net/irc/client/channel_manager" |
|---|
| 8 | include Net::IRC |
|---|
| 9 | include Constants |
|---|
| 10 | |
|---|
| 11 | class ChannelManagerTestServerSession < Net::IRC::Server::Session |
|---|
| 12 | @@testq = SizedQueue.new(1) |
|---|
| 13 | @@instance = nil |
|---|
| 14 | |
|---|
| 15 | def self.testq |
|---|
| 16 | @@testq |
|---|
| 17 | end |
|---|
| 18 | |
|---|
| 19 | def self.instance |
|---|
| 20 | @@instance |
|---|
| 21 | end |
|---|
| 22 | |
|---|
| 23 | def initialize(*args) |
|---|
| 24 | super |
|---|
| 25 | @@instance = self |
|---|
| 26 | end |
|---|
| 27 | |
|---|
| 28 | def on_message(m) |
|---|
| 29 | @@testq << m |
|---|
| 30 | end |
|---|
| 31 | end |
|---|
| 32 | |
|---|
| 33 | class ChannelManagerTestClient < Net::IRC::Client |
|---|
| 34 | include Net::IRC::Client::ChannelManager |
|---|
| 35 | @@testq = SizedQueue.new(1) |
|---|
| 36 | |
|---|
| 37 | def self.testq |
|---|
| 38 | @@testq |
|---|
| 39 | end |
|---|
| 40 | |
|---|
| 41 | def on_message(m) |
|---|
| 42 | @@testq << m |
|---|
| 43 | end |
|---|
| 44 | end |
|---|
| 45 | |
|---|
| 46 | describe Net::IRC, "server and client" do |
|---|
| 47 | before :all do |
|---|
| 48 | @port = nil |
|---|
| 49 | @server, @client = nil, nil |
|---|
| 50 | |
|---|
| 51 | Thread.abort_on_exception = true |
|---|
| 52 | Thread.start do |
|---|
| 53 | @server = Net::IRC::Server.new("localhost", @port, ChannelManagerTestServerSession, { |
|---|
| 54 | :logger => Logger.new(nil), |
|---|
| 55 | }) |
|---|
| 56 | @server.start |
|---|
| 57 | end |
|---|
| 58 | |
|---|
| 59 | true until @server.instance_variable_get(:@serv) |
|---|
| 60 | |
|---|
| 61 | @port = @server.instance_variable_get(:@serv).addr[1] |
|---|
| 62 | |
|---|
| 63 | Thread.start do |
|---|
| 64 | @client = ChannelManagerTestClient.new("localhost", @port, { |
|---|
| 65 | :nick => "foonick", |
|---|
| 66 | :user => "foouser", |
|---|
| 67 | :real => "foo real name", |
|---|
| 68 | :pass => "foopass", |
|---|
| 69 | :logger => Logger.new(nil), |
|---|
| 70 | }) |
|---|
| 71 | @client.start |
|---|
| 72 | end |
|---|
| 73 | end |
|---|
| 74 | |
|---|
| 75 | server_q = ChannelManagerTestServerSession.testq |
|---|
| 76 | client_q = ChannelManagerTestClient.testq |
|---|
| 77 | |
|---|
| 78 | it "client should manage channel mode/users correctly" do |
|---|
| 79 | client = @client |
|---|
| 80 | client.instance_variable_set(:@prefix, Prefix.new("foonick!foouser@localhost")) |
|---|
| 81 | |
|---|
| 82 | ChannelManagerTestServerSession.instance.instance_eval do |
|---|
| 83 | Thread.exclusive do |
|---|
| 84 | post client.prefix, JOIN, "#test" |
|---|
| 85 | post nil, NOTICE, "#test", "sep1" |
|---|
| 86 | end |
|---|
| 87 | end |
|---|
| 88 | |
|---|
| 89 | true until client_q.pop.to_s == "NOTICE #test sep1\r\n" |
|---|
| 90 | |
|---|
| 91 | c = @client.instance_variable_get(:@channels) |
|---|
| 92 | c.synchronize do |
|---|
| 93 | c.should be_a_kind_of(Hash) |
|---|
| 94 | c["#test"].should be_a_kind_of(Hash) |
|---|
| 95 | c["#test"][:modes].should be_a_kind_of(Array) |
|---|
| 96 | c["#test"][:users].should be_a_kind_of(Array) |
|---|
| 97 | c["#test"][:users].should == ["foonick"] |
|---|
| 98 | end |
|---|
| 99 | |
|---|
| 100 | ChannelManagerTestServerSession.instance.instance_eval do |
|---|
| 101 | Thread.exclusive do |
|---|
| 102 | post "test1!test@localhost", JOIN, "#test" |
|---|
| 103 | post "test2!test@localhost", JOIN, "#test" |
|---|
| 104 | post nil, NOTICE, "#test", "sep2" |
|---|
| 105 | end |
|---|
| 106 | end |
|---|
| 107 | |
|---|
| 108 | true until client_q.pop.to_s == "NOTICE #test sep2\r\n" |
|---|
| 109 | c.synchronize do |
|---|
| 110 | c["#test"][:users].should == ["foonick", "test1", "test2"] |
|---|
| 111 | end |
|---|
| 112 | |
|---|
| 113 | ChannelManagerTestServerSession.instance.instance_eval do |
|---|
| 114 | Thread.exclusive do |
|---|
| 115 | post nil, RPL_NAMREPLY, client.prefix.nick, "@", "#test", "foo1 foo2 foo3 @foo4 +foo5" |
|---|
| 116 | post nil, NOTICE, "#test", "sep3" |
|---|
| 117 | end |
|---|
| 118 | end |
|---|
| 119 | |
|---|
| 120 | true until client_q.pop.to_s == "NOTICE #test sep3\r\n" |
|---|
| 121 | c.synchronize do |
|---|
| 122 | c["#test"][:users].should == ["foonick", "test1", "test2", "foo1", "foo2", "foo3", "foo4", "foo5"] |
|---|
| 123 | c["#test"][:modes].should include([:s, nil]) |
|---|
| 124 | c["#test"][:modes].should include([:o, "foo4"]) |
|---|
| 125 | c["#test"][:modes].should include([:v, "foo5"]) |
|---|
| 126 | end |
|---|
| 127 | |
|---|
| 128 | ChannelManagerTestServerSession.instance.instance_eval do |
|---|
| 129 | Thread.exclusive do |
|---|
| 130 | post nil, RPL_NAMREPLY, client.prefix.nick, "@", "#test1", "foo1 foo2 foo3 @foo4 +foo5" |
|---|
| 131 | post "foo4!foo@localhost", QUIT, "message" |
|---|
| 132 | post "foo5!foo@localhost", PART, "#test1", "message" |
|---|
| 133 | post client.prefix, KICK, "#test", "foo1", "message" |
|---|
| 134 | post client.prefix, MODE, "#test", "+o", "foo2" |
|---|
| 135 | post nil, NOTICE, "#test", "sep4" |
|---|
| 136 | end |
|---|
| 137 | end |
|---|
| 138 | |
|---|
| 139 | true until client_q.pop.to_s == "NOTICE #test sep4\r\n" |
|---|
| 140 | c.synchronize do |
|---|
| 141 | c["#test"][:users].should == ["foonick", "test1", "test2", "foo2", "foo3", "foo5"] |
|---|
| 142 | c["#test1"][:users].should == ["foo1", "foo2", "foo3"] |
|---|
| 143 | c["#test"][:modes].should_not include([:o, "foo4"]) |
|---|
| 144 | c["#test"][:modes].should include([:v, "foo5"]) |
|---|
| 145 | c["#test1"][:modes].should_not include([:v, "foo5"]) |
|---|
| 146 | c["#test"][:modes].should include([:o, "foo2"]) |
|---|
| 147 | end |
|---|
| 148 | |
|---|
| 149 | ChannelManagerTestServerSession.instance.instance_eval do |
|---|
| 150 | Thread.exclusive do |
|---|
| 151 | post "foonick!test@localhost", NICK, "foonick2" |
|---|
| 152 | post "foonick2!test@localhost", NICK, "foonick" |
|---|
| 153 | post "foo2!test@localhost", NICK, "bar2" |
|---|
| 154 | post "foo3!test@localhost", NICK, "bar3" |
|---|
| 155 | post nil, NOTICE, "#test", "sep5" |
|---|
| 156 | end |
|---|
| 157 | end |
|---|
| 158 | |
|---|
| 159 | true until client_q.pop.to_s == "NOTICE #test sep5\r\n" |
|---|
| 160 | c.synchronize do |
|---|
| 161 | c["#test"][:users].should == ["foonick", "test1", "test2", "bar2", "bar3", "foo5"] |
|---|
| 162 | c["#test1"][:users].should == ["foo1", "bar2", "bar3"] |
|---|
| 163 | c["#test"][:modes].should_not include([:o, "foo4"]) |
|---|
| 164 | c["#test"][:modes].should include([:v, "foo5"]) |
|---|
| 165 | c["#test1"][:modes].should_not include([:v, "foo5"]) |
|---|
| 166 | c["#test"][:modes].should_not include([:o, "foo2"]) |
|---|
| 167 | c["#test"][:modes].should include([:o, "bar2"]) |
|---|
| 168 | end |
|---|
| 169 | end |
|---|
| 170 | |
|---|
| 171 | after :all do |
|---|
| 172 | @server.finish |
|---|
| 173 | @client.finish |
|---|
| 174 | end |
|---|
| 175 | end |
|---|
| 176 | |
|---|