Example #1
0
// Test the handler for MODE messages
func TestMODE(t *testing.T) {
	c, s := setUp(t)
	defer s.tearDown()

	// Channel modes
	gomock.InOrder(
		s.st.EXPECT().GetChannel("#test1").Return(&state.Channel{Name: "#test1"}),
		s.st.EXPECT().ChannelModes("#test1", "+sk", "somekey"),
	)
	c.h_MODE(ParseLine(":[email protected] MODE #test1 +sk somekey"))

	// Nick modes for Me.
	gomock.InOrder(
		s.st.EXPECT().GetChannel("test").Return(nil),
		s.st.EXPECT().GetNick("test").Return(c.cfg.Me),
		s.st.EXPECT().Me().Return(c.cfg.Me),
		s.st.EXPECT().NickModes("test", "+i"),
	)
	c.h_MODE(ParseLine(":[email protected] MODE test +i"))

	// 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(&state.Nick{Nick: "user1"}),
		s.st.EXPECT().Me().Return(c.cfg.Me),
		// Send a random mode for an unknown channel
		s.st.EXPECT().GetChannel("#test2").Return(nil),
		s.st.EXPECT().GetNick("#test2").Return(nil),
	)
	c.h_MODE(ParseLine(":[email protected] MODE user1 +w"))
	c.h_MODE(ParseLine(":[email protected] MODE #test2 +is"))
}
Example #2
0
// Test the handler for 352 / RPL_WHOREPLY
func Test352(t *testing.T) {
	c, s := setUp(t)
	defer s.tearDown()

	// Ensure 352 reply calls NickInfo and NickModes
	gomock.InOrder(
		s.st.EXPECT().GetNick("user1").Return(&state.Nick{Nick: "user1"}),
		s.st.EXPECT().Me().Return(c.cfg.Me),
		s.st.EXPECT().NickInfo("user1", "ident1", "host1.com", "name"),
	)
	c.h_352(ParseLine(":irc.server.org 352 test #test1 ident1 host1.com irc.server.org user1 G :0 name"))

	// Check that modes are set correctly from WHOREPLY
	gomock.InOrder(
		s.st.EXPECT().GetNick("user1").Return(&state.Nick{Nick: "user1"}),
		s.st.EXPECT().Me().Return(c.cfg.Me),
		s.st.EXPECT().NickInfo("user1", "ident1", "host1.com", "name"),
		s.st.EXPECT().NickModes("user1", "+o"),
		s.st.EXPECT().NickModes("user1", "+i"),
	)
	c.h_352(ParseLine(":irc.server.org 352 test #test1 ident1 host1.com irc.server.org user1 H* :0 name"))

	// Check error paths -- send a 352 for an unknown nick
	s.st.EXPECT().GetNick("user2").Return(nil)
	c.h_352(ParseLine(":irc.server.org 352 test #test2 ident2 host2.com irc.server.org user2 G :0 fooo"))
}
Example #3
0
// 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")

	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")

	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")
	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),
		// unknown channel, known nick that isn't Me.
		s.st.EXPECT().GetChannel("#test2").Return(nil),
		s.st.EXPECT().GetNick("user2").Return(nick2),
	)
	c.h_JOIN(parseLine(":[email protected] JOIN :#test2"))
	c.h_JOIN(parseLine(":[email protected] JOIN :#test2"))
}
Example #4
0
// 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")

	// 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)
		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 error paths -- send 353 for an unknown channel
	s.st.EXPECT().GetChannel("#test2").Return(nil)
	c.h_353(parseLine(":irc.server.org 353 test = #test2 :test ~user3"))
}
Example #5
0
// 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"))
}
Example #6
0
// Test the handler for 311 / RPL_WHOISUSER
func Test311(t *testing.T) {
	c, s := setUp(t)
	defer s.tearDown()

	// Create user1, who we know little about
	nick1 := state.NewNick("user1", s.log)

	// Send a 311 reply
	s.st.EXPECT().GetNick("user1").Return(nick1)
	c.h_311(parseLine(":irc.server.org 311 test user1 ident1 host1.com * :name"))

	// Verify we now know more about user1
	if nick1.Ident != "ident1" ||
		nick1.Host != "host1.com" ||
		nick1.Name != "name" {
		t.Errorf("WHOIS info of user1 not set correctly.")
	}

	// Check error paths -- send a 311 for an unknown nick
	gomock.InOrder(
		s.st.EXPECT().GetNick("user2").Return(nil),
		s.log.EXPECT().Warn("irc.311(): received WHOIS info for unknown nick %s",
			"user2"),
	)
	c.h_311(parseLine(":irc.server.org 311 test user2 ident2 host2.com * :dongs"))
}
Example #7
0
// 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"))
}
Example #8
0
func TestStartSupervisedUpgrade(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	mockDocker := NewMockdockerClient(mockCtrl)
	mockDownloader := NewMockdownloader(mockCtrl)

	gomock.InOrder(
		mockDocker.EXPECT().RemoveExistingAgentContainer(),
		mockDocker.EXPECT().StartAgent().Return(upgradeAgentExitCode, nil),
		mockDownloader.EXPECT().LoadDesiredAgent().Return(&os.File{}, nil),
		mockDocker.EXPECT().LoadImage(gomock.Any()),
		mockDownloader.EXPECT().RecordCachedAgent(),
		mockDocker.EXPECT().RemoveExistingAgentContainer(),
		mockDocker.EXPECT().StartAgent().Return(terminalSuccessAgentExitCode, nil),
	)

	engine := &Engine{
		downloader: mockDownloader,
		docker:     mockDocker,
	}
	err := engine.StartSupervised()
	if err != nil {
		t.Error("Expected error to be nil but was returned")
	}
}
Example #9
0
func TestStartSupervisedExitsWhenTerminalSuccess(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	mockDocker := NewMockdockerClient(mockCtrl)

	gomock.InOrder(
		mockDocker.EXPECT().RemoveExistingAgentContainer(),
		mockDocker.EXPECT().StartAgent().Return(1, nil),
		mockDocker.EXPECT().RemoveExistingAgentContainer(),
		mockDocker.EXPECT().StartAgent().Return(1, nil),
		mockDocker.EXPECT().RemoveExistingAgentContainer(),
		mockDocker.EXPECT().StartAgent().Return(1, nil),
		mockDocker.EXPECT().RemoveExistingAgentContainer(),
		mockDocker.EXPECT().StartAgent().Return(terminalSuccessAgentExitCode, nil),
	)

	engine := &Engine{
		docker: mockDocker,
	}
	err := engine.StartSupervised()
	if err != nil {
		t.Error("Expected error to be nil but was returned")
	}
}
Example #10
0
// TestCase: Wrong message type has been received in the client.
func TestClientWrongMessageType(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	transport := thrift.NewTMemoryBuffer()
	protocol := NewMockTProtocol(mockCtrl)
	gomock.InOrder(
		protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)),
		protocol.EXPECT().WriteStructBegin("testString_args"),
		protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)),
		protocol.EXPECT().WriteString("test"),
		protocol.EXPECT().WriteFieldEnd(),
		protocol.EXPECT().WriteFieldStop(),
		protocol.EXPECT().WriteStructEnd(),
		protocol.EXPECT().WriteMessageEnd(),
		protocol.EXPECT().Flush(),
		protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.INVALID_TMESSAGE_TYPE, int32(1), nil),
	)

	client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
	_, err := client.TestString("test")
	mockCtrl.Finish()
	appErr, ok := err.(thrift.TApplicationException)
	if !ok {
		t.Fatal("Expected TApplicationException")
	}
	if appErr.TypeId() != thrift.INVALID_MESSAGE_TYPE_EXCEPTION {
		t.Fatal("Expected INVALID_MESSAGE_TYPE_EXCEPTION error")
	}
}
Example #11
0
// Test the handler for 671 (unreal specific)
func Test671(t *testing.T) {
	c, s := setUp(t)
	defer s.tearDown()

	// Create user1, who should not be secure
	nick1 := state.NewNick("user1", s.log)
	if nick1.Modes.SSL {
		t.Errorf("Test nick user1 is already using SSL?")
	}

	// Send a 671 reply
	s.st.EXPECT().GetNick("user1").Return(nick1)
	c.h_671(parseLine(":irc.server.org 671 test user1 :some ignored text"))

	// Ensure user1 is now known to be on an SSL connection
	if !nick1.Modes.SSL {
		t.Errorf("Test nick user1 not using SSL?")
	}

	// Check error paths -- send a 671 for an unknown nick
	gomock.InOrder(
		s.st.EXPECT().GetNick("user2").Return(nil),
		s.log.EXPECT().Warn("irc.671(): received WHOIS SSL info for unknown "+
			"nick %s", "user2"),
	)
	c.h_671(parseLine(":irc.server.org 671 test user2 :some ignored text"))
}
Example #12
0
// Test the handler for 433 / ERR_NICKNAMEINUSE
func Test433(t *testing.T) {
	c, s := setUp(t)
	defer s.tearDown()

	// Call handler with a 433 line, not triggering c.cfg.Me.Renick()
	s.st.EXPECT().Me().Return(c.cfg.Me)
	c.h_433(ParseLine(":irc.server.org 433 test new :Nickname is already in use."))
	s.nc.Expect("NICK new_")

	// Send a line that will trigger a renick. This happens when our wanted
	// nick is unavailable during initial negotiation, so we must choose a
	// different one before the connection can proceed. No NICK line will be
	// sent by the server to confirm nick change in this case.
	gomock.InOrder(
		s.st.EXPECT().Me().Return(c.cfg.Me),
		s.st.EXPECT().ReNick("test", "test_").Return(c.cfg.Me),
	)
	c.h_433(ParseLine(":irc.server.org 433 test test :Nickname is already in use."))
	s.nc.Expect("NICK test_")

	// Test the code path that *doesn't* involve state tracking.
	c.st = nil
	c.h_433(ParseLine(":irc.server.org 433 test test :Nickname is already in use."))
	s.nc.Expect("NICK test_")

	if c.cfg.Me.Nick != "test_" {
		t.Errorf("My nick not updated from '%s'.", c.cfg.Me.Nick)
	}
	c.st = s.st
}
Example #13
0
// TestCase: Mismatching sequence id has been received in the client.
func TestClientSeqIdMismatch(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	transport := thrift.NewTMemoryBuffer()
	protocol := NewMockTProtocol(mockCtrl)
	gomock.InOrder(
		protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)),
		protocol.EXPECT().WriteStructBegin("testString_args"),
		protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)),
		protocol.EXPECT().WriteString("test"),
		protocol.EXPECT().WriteFieldEnd(),
		protocol.EXPECT().WriteFieldStop(),
		protocol.EXPECT().WriteStructEnd(),
		protocol.EXPECT().WriteMessageEnd(),
		protocol.EXPECT().Flush(),
		protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.REPLY, int32(2), nil),
	)

	client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
	_, err := client.TestString("test")
	mockCtrl.Finish()
	appErr, ok := err.(thrift.TApplicationException)
	if !ok {
		t.Fatal("Expected TApplicationException")
	}
	if appErr.TypeId() != thrift.BAD_SEQUENCE_ID {
		t.Fatal("Expected BAD_SEQUENCE_ID error")
	}
}
Example #14
0
// Test the handler for 001 / RPL_WELCOME
func Test001(t *testing.T) {
	c, s := setUp(t)
	defer s.tearDown()

	l := ParseLine(":irc.server.org 001 test :Welcome to IRC [email protected]")
	// Set up a handler to detect whether connected handler is called from 001
	hcon := false
	c.HandleFunc("connected", func(conn *Conn, line *Line) {
		hcon = true
	})

	// Test state tracking first.
	gomock.InOrder(
		s.st.EXPECT().Me().Return(c.cfg.Me),
		s.st.EXPECT().NickInfo("test", "test", "somehost.com", "Testing IRC"),
	)
	// Call handler with a valid 001 line
	c.h_001(l)
	<-time.After(time.Millisecond)
	if !hcon {
		t.Errorf("001 handler did not dispatch connected event.")
	}

	// Now without state tracking.
	c.st = nil
	c.h_001(l)
	// Check host parsed correctly
	if c.cfg.Me.Host != "somehost.com" {
		t.Errorf("Host parsing failed, host is '%s'.", c.cfg.Me.Host)
	}
	c.st = s.st
}
Example #15
0
// Test the handler for 353 / RPL_NAMREPLY
func Test353(t *testing.T) {
	c, s := setUp(t)
	defer s.tearDown()

	// 353 handler is called twice, so GetChannel will be called twice
	s.st.EXPECT().GetChannel("#test1").Return(&state.Channel{Name: "#test1"}).Times(2)
	gomock.InOrder(
		// "test" is Me, i am known, and already on the channel
		s.st.EXPECT().GetNick("test").Return(c.cfg.Me),
		s.st.EXPECT().IsOn("#test1", "test").Return(&state.ChanPrivs{}, true),
		// user1 is known, but not on the channel, so should be associated
		s.st.EXPECT().GetNick("user1").Return(&state.Nick{Nick: "user1"}),
		s.st.EXPECT().IsOn("#test1", "user1").Return(nil, false),
		s.st.EXPECT().Associate("#test1", "user1").Return(&state.ChanPrivs{}),
		s.st.EXPECT().ChannelModes("#test1", "+o", "user1"),
	)
	for n, m := range map[string]string{
		"user2":  "",
		"voice":  "+v",
		"halfop": "+h",
		"op":     "+o",
		"admin":  "+a",
		"owner":  "+q",
	} {
		calls := []*gomock.Call{
			s.st.EXPECT().GetNick(n).Return(nil),
			s.st.EXPECT().NewNick(n).Return(&state.Nick{Nick: n}),
			s.st.EXPECT().IsOn("#test1", n).Return(nil, false),
			s.st.EXPECT().Associate("#test1", n).Return(&state.ChanPrivs{}),
		}
		if m != "" {
			calls = append(calls, s.st.EXPECT().ChannelModes("#test1", m, n))
		}
		gomock.InOrder(calls...)
	}

	// 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 "))

	// Check error paths -- send 353 for an unknown channel
	s.st.EXPECT().GetChannel("#test2").Return(nil)
	c.h_353(ParseLine(":irc.server.org 353 test = #test2 :test ~user3"))
}
Example #16
0
// 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"))
}
Example #17
0
//Make sure that optional fields are not being serialized
func TestNoOptionalUnsetFieldsOnWire(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	proto := NewMockTProtocol(mockCtrl)
	gomock.InOrder(
		proto.EXPECT().WriteStructBegin("all_optional").Return(nil),
		proto.EXPECT().WriteFieldStop().Return(nil),
		proto.EXPECT().WriteStructEnd().Return(nil),
	)
	ao := optionalfieldstest.NewAllOptional()
	ao.Write(proto)
}
Example #18
0
func commonTestOrderedCalls(t *testing.T) (reporter *ErrorReporter, ctrl *gomock.Controller, subjectOne, subjectTwo *Subject) {
	reporter, ctrl = createFixtures(t)

	subjectOne = new(Subject)
	subjectTwo = new(Subject)

	gomock.InOrder(
		ctrl.RecordCall(subjectOne, "FooMethod", "1").AnyTimes(),
		ctrl.RecordCall(subjectTwo, "FooMethod", "2"),
		ctrl.RecordCall(subjectTwo, "BarMethod", "3"),
	)

	return
}
Example #19
0
// Test the handler for 324 / RPL_CHANNELMODEIS
func Test324(t *testing.T) {
	c, s := setUp(t)
	defer s.tearDown()

	// Ensure 324 reply calls ChannelModes
	gomock.InOrder(
		s.st.EXPECT().GetChannel("#test1").Return(&state.Channel{Name: "#test1"}),
		s.st.EXPECT().ChannelModes("#test1", "+sk", "somekey"),
	)
	c.h_324(ParseLine(":irc.server.org 324 test #test1 +sk somekey"))

	// Check error paths -- send 324 for an unknown channel
	s.st.EXPECT().GetChannel("#test2").Return(nil)
	c.h_324(ParseLine(":irc.server.org 324 test #test2 +pmt"))
}
Example #20
0
// Test the handler for TOPIC messages
func TestTOPIC(t *testing.T) {
	c, s := setUp(t)
	defer s.tearDown()

	// Ensure TOPIC reply calls Topic
	gomock.InOrder(
		s.st.EXPECT().GetChannel("#test1").Return(&state.Channel{Name: "#test1"}),
		s.st.EXPECT().Topic("#test1", "something something"),
	)
	c.h_TOPIC(ParseLine(":[email protected] TOPIC #test1 :something something"))

	// Check error paths -- send a topic for an unknown channel
	s.st.EXPECT().GetChannel("#test2").Return(nil)
	c.h_TOPIC(ParseLine(":[email protected] TOPIC #test2 :dark side"))
}
Example #21
0
// Test the handler for 332 / RPL_TOPIC
func Test332(t *testing.T) {
	c, s := setUp(t)
	defer s.tearDown()

	// Ensure 332 reply calls Topic
	gomock.InOrder(
		s.st.EXPECT().GetChannel("#test1").Return(&state.Channel{Name: "#test1"}),
		s.st.EXPECT().Topic("#test1", "something something"),
	)
	c.h_332(ParseLine(":irc.server.org 332 test #test1 :something something"))

	// Check error paths -- send 332 for an unknown channel
	s.st.EXPECT().GetChannel("#test2").Return(nil)
	c.h_332(ParseLine(":irc.server.org 332 test #test2 :dark side"))
}
Example #22
0
// Test the handler for 671 (unreal specific)
func Test671(t *testing.T) {
	c, s := setUp(t)
	defer s.tearDown()

	// Ensure 671 reply calls NickModes
	gomock.InOrder(
		s.st.EXPECT().GetNick("user1").Return(&state.Nick{Nick: "user1"}),
		s.st.EXPECT().NickModes("user1", "+z"),
	)
	c.h_671(ParseLine(":irc.server.org 671 test user1 :some ignored text"))

	// Check error paths -- send a 671 for an unknown nick
	s.st.EXPECT().GetNick("user2").Return(nil)
	c.h_671(ParseLine(":irc.server.org 671 test user2 :some ignored text"))
}
Example #23
0
func TestOneEmptyBinSetFieldOnWire(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	proto := NewMockTProtocol(mockCtrl)
	gomock.InOrder(
		proto.EXPECT().WriteStructBegin("all_optional").Return(nil),
		proto.EXPECT().WriteFieldBegin("bin", thrift.TType(thrift.STRING), int16(13)).Return(nil),
		proto.EXPECT().WriteBinary([]byte{}).Return(nil),
		proto.EXPECT().WriteFieldEnd().Return(nil),
		proto.EXPECT().WriteFieldStop().Return(nil),
		proto.EXPECT().WriteStructEnd().Return(nil),
	)
	ao := optionalfieldstest.NewAllOptional()
	ao.Bin = []byte{}
	ao.Write(proto)
}
Example #24
0
//Make sure that only one field is being serialized when set to non-default
func TestOneISetFieldOnWire(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	proto := NewMockTProtocol(mockCtrl)
	gomock.InOrder(
		proto.EXPECT().WriteStructBegin("all_optional").Return(nil),
		proto.EXPECT().WriteFieldBegin("i", thrift.TType(thrift.I64), int16(2)).Return(nil),
		proto.EXPECT().WriteI64(int64(123)).Return(nil),
		proto.EXPECT().WriteFieldEnd().Return(nil),
		proto.EXPECT().WriteFieldStop().Return(nil),
		proto.EXPECT().WriteStructEnd().Return(nil),
	)
	ao := optionalfieldstest.NewAllOptional()
	ao.I = 123
	ao.Write(proto)
}
Example #25
0
// Test the handler for 311 / RPL_WHOISUSER
func Test311(t *testing.T) {
	c, s := setUp(t)
	defer s.tearDown()

	// Ensure 311 reply calls NickInfo
	gomock.InOrder(
		s.st.EXPECT().GetNick("user1").Return(&state.Nick{Nick: "user1"}),
		s.st.EXPECT().Me().Return(c.cfg.Me),
		s.st.EXPECT().NickInfo("user1", "ident1", "host1.com", "name"),
	)
	c.h_311(ParseLine(":irc.server.org 311 test user1 ident1 host1.com * :name"))

	// Check error paths -- send a 311 for an unknown nick
	s.st.EXPECT().GetNick("user2").Return(nil)
	c.h_311(ParseLine(":irc.server.org 311 test user2 ident2 host2.com * :dongs"))
}
func TestBitmapperSolver(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	r := &FakeReader{}
	mockBitmapper := mock_spoj.NewMockBitmapperI(mockCtrl)
	expected := "the answer"
	gomock.InOrder(
		mockBitmapper.EXPECT().ReadInput(gomock.Eq(r)),
		mockBitmapper.EXPECT().Solve().Return(expected),
	)
	actual := spoj.BitmapSolver(r, mockBitmapper)
	if actual != expected {
		t.Errorf("Bitmap solver method is wrong. %v != %v", expected, actual)
	}
}
Example #27
0
// 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!"))
}
Example #28
0
// 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!"))
}
func TestCommandFramework(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	itemType1 := gamerules.ItemType{1, "1", 64, 0, 0}

	mockGame := gamerules.NewMockIGame(mockCtrl)
	mockPlayer := gamerules.NewMockIPlayerClient(mockCtrl)
	mockOther := gamerules.NewMockIPlayerClient(mockCtrl)

	cf := NewCommandFramework("/")

	mockGame.EXPECT().BroadcastMessage("§dthis is a broadcast")
	cf.Process(mockPlayer, "/say this is a broadcast", mockGame)

	mockGame.EXPECT().PlayerByName("thePlayer").Return(mockPlayer)
	mockGame.EXPECT().ItemTypeById(1).Return(itemType1, true)
	mockPlayer.EXPECT().EchoMessage("Giving 64 of '1' to thePlayer")
	mockPlayer.EXPECT().GiveItem(gamerules.Slot{1, 64, 0})
	cf.Process(mockPlayer, "/give thePlayer 1 64", mockGame)

	mockGame.EXPECT().PlayerByName("otherPlayer")
	mockPlayer.EXPECT().EchoMessage("'otherPlayer' is not logged in")
	cf.Process(mockPlayer, "/give otherPlayer 1 64", mockGame)

	mockGame.EXPECT().PlayerByName("otherPlayer").Return(mockOther)
	mockGame.EXPECT().ItemTypeById(1).Return(gamerules.ItemType{}, false)
	mockPlayer.EXPECT().EchoMessage("'1' is not a valid item id")
	cf.Process(mockPlayer, "/give otherPlayer 1 64", mockGame)

	mockGame.EXPECT().PlayerByName("otherPlayer").Return(mockOther)
	mockGame.EXPECT().ItemTypeById(1).Return(itemType1, true)
	mockPlayer.EXPECT().EchoMessage("Cannot give more than 512 items at once")
	cf.Process(mockPlayer, "/give otherPlayer 1 513", mockGame)

	mockPlayer.EXPECT().EchoMessage(&testmatcher.StringPrefix{"Commands:"})
	cf.Process(mockPlayer, "/help", mockGame)

	gomock.InOrder(
		mockPlayer.EXPECT().EchoMessage("Command: /help"),
		mockPlayer.EXPECT().EchoMessage("Usage: help|?"),
		mockPlayer.EXPECT().EchoMessage("Description: Shows a list of all commands."),
	)
	cf.Process(mockPlayer, "/help help", mockGame)
}
Example #30
0
func TestOneLSetFieldOnWire(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	proto := NewMockTProtocol(mockCtrl)
	gomock.InOrder(
		proto.EXPECT().WriteStructBegin("all_optional").Return(nil),
		proto.EXPECT().WriteFieldBegin("l", thrift.TType(thrift.LIST), int16(9)).Return(nil),
		proto.EXPECT().WriteListBegin(thrift.TType(thrift.I64), 2).Return(nil),
		proto.EXPECT().WriteI64(int64(1)).Return(nil),
		proto.EXPECT().WriteI64(int64(2)).Return(nil),
		proto.EXPECT().WriteListEnd().Return(nil),
		proto.EXPECT().WriteFieldEnd().Return(nil),
		proto.EXPECT().WriteFieldStop().Return(nil),
		proto.EXPECT().WriteStructEnd().Return(nil),
	)
	ao := OptionalFieldsTest.NewAllOptional()
	ao.L = []int64{1, 2}
	ao.Write(proto)
}