// Test the handler for TOPIC messages func TestTOPIC(t *testing.T) { c, s := setUp(t) defer s.tearDown() chan1 := state.NewChannel("#test1", s.log) // Assert that it has no topic originally if chan1.Topic != "" { t.Errorf("Test channel already has a topic.") } // Send a TOPIC line s.st.EXPECT().GetChannel("#test1").Return(chan1) c.h_TOPIC(parseLine(":[email protected] TOPIC #test1 :something something")) // Make sure the channel's topic has been changed if chan1.Topic != "something something" { t.Errorf("Topic of test channel not set correctly.") } // Check error paths -- send a topic for an unknown channel gomock.InOrder( s.st.EXPECT().GetChannel("#test2").Return(nil), s.log.EXPECT().Warn("irc.TOPIC(): topic change on unknown channel %s", "#test2"), ) c.h_TOPIC(parseLine(":[email protected] TOPIC #test2 :dark side")) }
// Test the handler for 332 / RPL_TOPIC func Test332(t *testing.T) { c, s := setUp(t) defer s.tearDown() // Create #test1, whose topic we don't know chan1 := state.NewChannel("#test1", s.log) // Assert that it has no topic originally if chan1.Topic != "" { t.Errorf("Test channel already has a topic.") } // Send a 332 reply s.st.EXPECT().GetChannel("#test1").Return(chan1) c.h_332(parseLine(":irc.server.org 332 test #test1 :something something")) // Make sure the channel's topic has been changed if chan1.Topic != "something something" { t.Errorf("Topic of test channel not set correctly.") } // Check unknown channel causes an error gomock.InOrder( s.st.EXPECT().GetChannel("#test2").Return(nil), s.log.EXPECT().Warn("irc.332(): received TOPIC value for unknown "+ "channel %s", "#test2"), ) c.h_332(parseLine(":irc.server.org 332 test #test2 :dark side")) }
// Test the handler for KICK messages // (this is very similar to the PART message test) func TestKICK(t *testing.T) { c, s := setUp(t) defer s.tearDown() // We need some valid and associated nicks / channels to KICK. chan1 := state.NewChannel("#test1", s.log) nick1 := state.NewNick("user1", s.log) // KICK should dissociate a nick from a channel. gomock.InOrder( s.st.EXPECT().GetChannel("#test1").Return(chan1), s.st.EXPECT().GetNick("user1").Return(nick1), s.st.EXPECT().Dissociate(chan1, nick1), ) c.h_KICK(parseLine(":[email protected] KICK #test1 user1 :Bye!")) }
// Test the handler for PART messages func TestPART(t *testing.T) { c, s := setUp(t) defer s.tearDown() // We need some valid and associated nicks / channels to PART with. chan1 := state.NewChannel("#test1", s.log) nick1 := state.NewNick("user1", s.log) // PART should dissociate a nick from a channel. gomock.InOrder( s.st.EXPECT().GetChannel("#test1").Return(chan1), s.st.EXPECT().GetNick("user1").Return(nick1), s.st.EXPECT().Dissociate(chan1, nick1), ) c.h_PART(parseLine(":[email protected] PART #test1 :Bye!")) }
// Test the handler for MODE messages func TestMODE(t *testing.T) { c, s := setUp(t) defer s.tearDown() chan1 := state.NewChannel("#test1", s.log) nick1 := state.NewNick("user1", s.log) // Send a channel mode line. Inconveniently, Channel and Nick objects // aren't mockable with gomock as they're not interface types (and I // don't want them to be, writing accessors for struct fields sucks). // This makes testing whether ParseModes is called correctly harder. s.st.EXPECT().GetChannel("#test1").Return(chan1) c.h_MODE(parseLine(":[email protected] MODE #test1 +sk somekey")) if !chan1.Modes.Secret || chan1.Modes.Key != "somekey" { t.Errorf("Channel.ParseModes() not called correctly.") } // Send a nick mode line, returning Me gomock.InOrder( s.st.EXPECT().GetChannel("test").Return(nil), s.st.EXPECT().GetNick("test").Return(c.Me), ) c.h_MODE(parseLine(":[email protected] MODE test +i")) if !c.Me.Modes.Invisible { t.Errorf("Nick.ParseModes() not called correctly.") } // Check error paths gomock.InOrder( // send a valid user mode that's not us s.st.EXPECT().GetChannel("user1").Return(nil), s.st.EXPECT().GetNick("user1").Return(nick1), s.log.EXPECT().Warn("irc.MODE(): recieved MODE %s for (non-me) nick %s", "+w", "user1"), // Send a random mode for an unknown channel s.st.EXPECT().GetChannel("#test2").Return(nil), s.st.EXPECT().GetNick("#test2").Return(nil), s.log.EXPECT().Warn("irc.MODE(): not sure what to do with MODE %s", "#test2 +is"), ) c.h_MODE(parseLine(":[email protected] MODE user1 +w")) c.h_MODE(parseLine(":[email protected] MODE #test2 +is")) }
// Test the handler for 324 / RPL_CHANNELMODEIS func Test324(t *testing.T) { c, s := setUp(t) defer s.tearDown() // Create #test1, whose modes we don't know chan1 := state.NewChannel("#test1", s.log) // Send a 324 reply s.st.EXPECT().GetChannel("#test1").Return(chan1) c.h_324(parseLine(":irc.server.org 324 test #test1 +sk somekey")) if !chan1.Modes.Secret || chan1.Modes.Key != "somekey" { t.Errorf("Channel.ParseModes() not called correctly.") } // Check unknown channel causes an error gomock.InOrder( s.st.EXPECT().GetChannel("#test2").Return(nil), s.log.EXPECT().Warn("irc.324(): received MODE settings for unknown "+ "channel %s", "#test2"), ) c.h_324(parseLine(":irc.server.org 324 test #test2 +pmt")) }
// Test the handler for 353 / RPL_NAMREPLY func Test353(t *testing.T) { c, s := setUp(t) defer s.tearDown() // Create #test1, whose user list we're mostly unfamiliar with chan1 := state.NewChannel("#test1", s.log) // Create maps for testing -- this is what the mock ST calls will return nicks := make(map[string]*state.Nick) privs := make(map[string]*state.ChanPrivs) nicks["test"] = c.Me privs["test"] = new(state.ChanPrivs) for _, n := range []string{"user1", "user2", "voice", "halfop", "op", "admin", "owner"} { nicks[n] = state.NewNick(n, s.log) privs[n] = new(state.ChanPrivs) } // 353 handler is called twice, so GetChannel will be called twice s.st.EXPECT().GetChannel("#test1").Return(chan1).Times(2) gomock.InOrder( // "test" is Me, i am known, and already on the channel s.st.EXPECT().GetNick("test").Return(c.Me), s.st.EXPECT().IsOn("#test1", "test").Return(privs["test"], true), // user1 is known, but not on the channel, so should be associated s.st.EXPECT().GetNick("user1").Return(nicks["user1"]), s.st.EXPECT().IsOn("#test1", "user1").Return(nil, false), s.st.EXPECT().Associate(chan1, nicks["user1"]).Return(privs["user1"]), ) for _, n := range []string{"user2", "voice", "halfop", "op", "admin", "owner"} { gomock.InOrder( s.st.EXPECT().GetNick(n).Return(nil), s.st.EXPECT().NewNick(n).Return(nicks[n]), s.st.EXPECT().IsOn("#test1", n).Return(nil, false), s.st.EXPECT().Associate(chan1, nicks[n]).Return(privs[n]), ) } // Send a couple of names replies (complete with trailing space) c.h_353(parseLine(":irc.server.org 353 test = #test1 :test @user1 user2 +voice ")) c.h_353(parseLine(":irc.server.org 353 test = #test1 :%halfop @op &admin ~owner ")) if p := privs["user2"]; p.Voice || p.HalfOp || p.Op || p.Admin || p.Owner { t.Errorf("353 handler incorrectly set modes on nick.") } if !privs["user1"].Op || !privs["voice"].Voice || !privs["halfop"].HalfOp || !privs["op"].Op || !privs["admin"].Admin || !privs["owner"].Owner { t.Errorf("353 handler failed to set correct modes for nicks.") } // Check unknown channel causes a warning gomock.InOrder( s.st.EXPECT().GetChannel("#test2").Return(nil), s.log.EXPECT().Warn("irc.353(): received NAMES list for unknown "+ "channel %s", "#test2"), ) c.h_353(parseLine(":irc.server.org 353 test = #test2 :test ~user3")) }
// Test the handler for JOIN messages func TestJOIN(t *testing.T) { c, s := setUp(t) defer s.tearDown() // The state tracker should be creating a new channel in this first test chan1 := state.NewChannel("#test1", s.log) gomock.InOrder( s.st.EXPECT().GetChannel("#test1").Return(nil), s.st.EXPECT().GetNick("test").Return(c.Me), s.st.EXPECT().NewChannel("#test1").Return(chan1), s.st.EXPECT().Associate(chan1, c.Me), ) // Use #test1 to test expected behaviour // Call handler with JOIN by test to #test1 c.h_JOIN(parseLine(":[email protected] JOIN :#test1")) // Verify that the MODE and WHO commands are sent correctly s.nc.Expect("MODE #test1") s.nc.Expect("WHO #test1") // In this second test, we should be creating a new nick nick1 := state.NewNick("user1", s.log) gomock.InOrder( s.st.EXPECT().GetChannel("#test1").Return(chan1), s.st.EXPECT().GetNick("user1").Return(nil), s.st.EXPECT().NewNick("user1").Return(nick1), s.st.EXPECT().Associate(chan1, nick1), ) // OK, now #test1 exists, JOIN another user we don't know about c.h_JOIN(parseLine(":[email protected] JOIN :#test1")) // Verify that the WHO command is sent correctly s.nc.Expect("WHO user1") // In this third test, we'll be pretending we know about the nick already. nick2 := state.NewNick("user2", c.l) gomock.InOrder( s.st.EXPECT().GetChannel("#test1").Return(chan1), s.st.EXPECT().GetNick("user2").Return(nick2), s.st.EXPECT().Associate(chan1, nick2), ) c.h_JOIN(parseLine(":[email protected] JOIN :#test1")) // Test error paths gomock.InOrder( // unknown channel, unknown nick s.st.EXPECT().GetChannel("#test2").Return(nil), s.st.EXPECT().GetNick("blah").Return(nil), s.log.EXPECT().Warn("irc.JOIN(): JOIN to unknown channel %s "+ "received from (non-me) nick %s", "#test2", "blah"), // unknown channel, known nick that isn't Me. s.st.EXPECT().GetChannel("#test2").Return(nil), s.st.EXPECT().GetNick("user2").Return(nick2), s.log.EXPECT().Warn("irc.JOIN(): JOIN to unknown channel %s "+ "received from (non-me) nick %s", "#test2", "user2"), ) c.h_JOIN(parseLine(":[email protected] JOIN :#test2")) c.h_JOIN(parseLine(":[email protected] JOIN :#test2")) }