Esempio n. 1
0
// NewTestConnection is for test facilitation.
// Creates a server and then dials the server, returning the connection,
// allowing test to inject state and wait for an expected response
// The connection must be started manually with `go conn.Start()`
// once desired state has been injected
func NewTestConnection(transcript io.Writer) (s *Server, clientConn *textproto.Conn, serverConn *conn.Conn, server *Server, err error) {
	mStore := mailstore.NewDummyMailstore()
	s = NewServer(mStore)
	s.Addr = ":10143"
	s.Transcript = transcript
	if err = s.Listen(); err != nil {
		return nil, nil, nil, nil, err
	}

	c, err := net.Dial("tcp4", "localhost:10143")
	if err != nil {
		return nil, nil, nil, nil, err
	}

	textc := textproto.NewConn(c)
	clientConn = textc

	conn, err := s.listener.Accept()
	if err != nil {
		return nil, nil, nil, nil, err
	}
	fmt.Fprintf(s.Transcript, "Client connected\n")
	serverConn, err = s.newConn(conn)

	return s, clientConn, serverConn, s, nil
}
Esempio n. 2
0
func main() {
	store := mailstore.NewDummyMailstore()
	s := imap.NewServer(store)
	s.Transcript = os.Stdout
	s.Addr = ":10143"

	err := s.ListenAndServe()
	if err != nil {
		fmt.Printf("Error creating test connection: %s\n", err)
	}
}
Esempio n. 3
0
func TestDataRace(t *testing.T) {
	s := NewServer(mailstore.NewDummyMailstore())
	addr := "127.0.0.1:10143"
	l, err := net.Listen("tcp", addr)
	if err != nil {
		t.Fatal(err)
	}

	go func() {
		s.Serve(l)
	}()
	time.Sleep(time.Millisecond)
	l.Close()
}
Esempio n. 4
0
	response, err := reader.ReadLine()
	expected = strings.TrimSpace(expected)
	response = strings.TrimSpace(response)
	Expect(response, err).To(Equal(expected))
}

func ExpectResponsePattern(pattern string) {
	response, err := reader.ReadLine()
	pattern = strings.TrimSpace(pattern)
	response = strings.TrimSpace(response)
	Expect(response, err).To(MatchRegexp(pattern))
}

// === SETUP ====
var _ = BeforeEach(func() {
	mStore = mailstore.NewDummyMailstore()
	mockConn = mock_conn.NewConn()
	tConn = conn.NewConn(mStore, mockConn.Server, GinkgoWriter)
	reader = textproto.NewReader(bufio.NewReader(mockConn.Client))
})

var _ = JustBeforeEach(func() {
	go tConn.Start()
})

// === TEARDOWN ====

var _ = AfterEach(func() {
	tConn.Close()
	mockConn.Close()
})