Пример #1
0
// Test receiving valid packets
func TestRecvRicochetPacket(t *testing.T) {
	var buf bytes.Buffer
	for _, td := range packetTests {
		if _, err := buf.Write(td.encoded); err != nil {
			t.Error(err)
			return
		}
	}

	// Use a HalfReader to test behavior on short socket reads also
	reader := iotest.HalfReader(&buf)
	rni := RicochetNetwork{}

	for _, td := range packetTests {
		packet, err := rni.RecvRicochetPacket(reader)
		if err != nil {
			t.Errorf("Error receiving packet %v: %v", td.packet, err)
			return
		} else if !packet.Equals(td.packet) {
			t.Errorf("Expected unserialized packet %v but got %v", td.packet, packet)
		}
	}

	if packet, err := rni.RecvRicochetPacket(reader); err != io.EOF {
		if err != nil {
			t.Errorf("Expected EOF on packet stream but received error: %v", err)
		} else {
			t.Errorf("Expected EOF but received packet: %v", packet)
		}
	}
}
Пример #2
0
func (s *ReaderSuite) TestMultipleReads(c *C) {
	text := "SEND\ndestination:xxx\n\nPayload\x00\n" +
		"SEND\ndestination:yyy\ncontent-length:12\n" +
		"dodgy\\c\\n\\cheader:dodgy\\c\\n\\r\\nvalue\\  \\\n\n" +
		"123456789AB\x00\x00"

	ioreaders := []io.Reader{
		strings.NewReader(text),
		iotest.DataErrReader(strings.NewReader(text)),
		iotest.HalfReader(strings.NewReader(text)),
		iotest.OneByteReader(strings.NewReader(text)),
	}

	for _, ioreader := range ioreaders {
		// uncomment the following line to view the bytes being read
		//ioreader = iotest.NewReadLogger("RX", ioreader)
		reader := NewReader(ioreader)
		frame, err := reader.Read()
		c.Assert(err, IsNil)
		c.Assert(frame, NotNil)
		c.Assert(frame.Command, Equals, "SEND")
		c.Assert(frame.Header.Len(), Equals, 1)
		v := frame.Header.Get("destination")
		c.Assert(v, Equals, "xxx")
		c.Assert(string(frame.Body), Equals, "Payload")

		// now read a heart-beat from the input
		frame, err = reader.Read()
		c.Assert(err, IsNil)
		c.Assert(frame, IsNil)

		// this frame has content-length
		frame, err = reader.Read()
		c.Assert(err, IsNil)
		c.Assert(frame, NotNil)
		c.Assert(frame.Command, Equals, "SEND")
		c.Assert(frame.Header.Len(), Equals, 3)
		v = frame.Header.Get("destination")
		c.Assert(v, Equals, "yyy")
		n, ok, err := frame.Header.ContentLength()
		c.Assert(n, Equals, 12)
		c.Assert(ok, Equals, true)
		c.Assert(err, IsNil)
		k, v := frame.Header.GetAt(2)
		c.Assert(k, Equals, "dodgy:\n:header")
		c.Assert(v, Equals, "dodgy:\n\r\nvalue\\  \\")
		c.Assert(string(frame.Body), Equals, "123456789AB\x00")

		// ensure we are at the end of input
		frame, err = reader.Read()
		c.Assert(frame, IsNil)
		c.Assert(err, Equals, io.EOF)
	}
}
Пример #3
0
func (t *ReaderTest) TestPeekBitsWithShortReads(c *C) {
	r := &Reader{
		reader: iotest.HalfReader(
			bytes.NewReader([]byte("\x12\x23\x34\x45\x56\x67\x78"))),
		bits: BitSequence{0x1000000000000000, 4}}

	bits, _ := r.PeekBits()
	c.Check(bits, Equals, BitSequence{0x1122334450000000, 36})
	bits, _ = r.PeekBits()
	c.Check(bits, Equals, BitSequence{0x1122334455667000, 52})
	bits, _ = r.PeekBits()
	c.Check(bits, Equals, BitSequence{0x1122334455667780, 60})
}