Ejemplo n.º 1
0
// This test determines the behavior of codec with respect to advancing the
// Reader when decoding error scenarios. It seems that the codec advances
// the Reader if Decode fails, but sets its own state to expect a specific
// type for the next Decode, and thus is functionally the same as not
// advancing the Reader.
func TestCodec(t *testing.T) {
	var buf bytes.Buffer
	mh := &codec.MsgpackHandle{WriteExt: true}
	enc := codec.NewEncoder(&buf, mh)
	dec := codec.NewDecoder(&buf, mh)

	var i int = math.MaxInt32
	err := enc.Encode(i)
	require.Nil(t, err, "expected encoding to succeed")
	require.Equal(t, 5, len(buf.Bytes()), "expected buffer to contain bytes")

	var targetInt int
	err = dec.Decode(&targetInt)
	require.Nil(t, err, "expected decoding to succeed")
	require.Equal(t, math.MaxInt32, targetInt, "expected codec to successfully decode int")
	require.Equal(t, 0, len(buf.Bytes()), "expected buffer to be empty")

	var targetString string
	enc.Encode(i)
	require.Equal(t, 5, len(buf.Bytes()), "expected buffer to contain bytes")
	err = dec.Decode(&targetString)
	require.Error(t, err, "expected error while decoding")
	require.Contains(t, err.Error(), "Unrecognized descriptor byte", "expected error while decoding")
	require.Equal(t, 4, len(buf.Bytes()), "expected buffer to have bytes")
	err = dec.Decode(&targetString)
	require.Error(t, err, "expected error while decoding")
	require.Contains(t, err.Error(), "Unrecognized descriptor byte", "expected error while decoding")
	require.Equal(t, 4, len(buf.Bytes()), "expected buffer to have bytes")

	targetInt = 0
	err = dec.Decode(&targetInt)
	require.Nil(t, err, "expected decoding to succeed")
	require.Equal(t, math.MaxInt32, targetInt, "expected codec to successfully decode int")
	require.Equal(t, 0, len(buf.Bytes()), "expected buffer to be empty")
}
Ejemplo n.º 2
0
func newPacketHandler(reader io.Reader, protocols *protocolHandler, calls *callContainer) *packetHandler {
	return &packetHandler{
		reader:       reader,
		dec:          codec.NewDecoder(reader, newCodecMsgpackHandle()),
		frameDecoder: newDecoderWrapper(),
		protocols:    protocols,
		calls:        calls,
	}
}
Ejemplo n.º 3
0
func newConnDecoder(c net.Conn) *connDecoder {
	br := bufio.NewReader(c)
	mh := &codec.MsgpackHandle{WriteExt: true}

	return &connDecoder{
		Conn:    c,
		Reader:  br,
		decoder: codec.NewDecoder(br, mh),
	}
}
Ejemplo n.º 4
0
// Decode data into out, but make sure that all bytes in data are
// used.
func MsgpackDecodeAll(data []byte, handle *codec.MsgpackHandle, out interface{}) error {
	buf := bytes.NewBuffer(data)
	err := codec.NewDecoder(buf, handle).Decode(out)
	if err != nil {
		return err
	}
	if buf.Len() > 0 {
		return fmt.Errorf("Did not consume entire buffer: %d byte(s) left", buf.Len())
	}
	return nil
}
Ejemplo n.º 5
0
// DecodePacketsUnchecked decodes an array of packets from `reader`. It does *not*
// check that the stream was canonical msgpack.
func DecodePacketsUnchecked(reader io.Reader) (ret KeybasePackets, err error) {
	ch := codecHandle()
	if err = codec.NewDecoder(reader, ch).Decode(&ret); err != nil {
		return
	}
	for _, p := range ret {
		err = p.unpackBody(ch)
		if err != nil {
			return
		}
	}
	return
}
Ejemplo n.º 6
0
// Test an output from objective C that was breaking the server
func TestObjcOutput(t *testing.T) {
	dat, err := ioutil.ReadFile("objc_output.dat")
	require.Nil(t, err, "an error occurred while reading dat file")
	v, err := base64.StdEncoding.DecodeString(string(dat))
	require.Nil(t, err, "an error occurred while decoding base64 dat file")

	buf := bytes.NewBuffer(v)
	var i int
	mh := newCodecMsgpackHandle()
	dec := codec.NewDecoder(buf, mh)
	err = dec.Decode(&i)
	require.Nil(t, err, "an error occurred while decoding an integer")
	require.Equal(t, buf.Len(), i, "Bad frame")

	var a interface{}
	err = dec.Decode(&a)
	require.Nil(t, err, "an error occurred while decoding object")
}
Ejemplo n.º 7
0
func TestMap(t *testing.T) {
	var buf bytes.Buffer
	mh := newCodecMsgpackHandle()
	enc := codec.NewEncoder(&buf, mh)
	dec := codec.NewDecoder(&buf, mh)

	m := map[string]string{
		"hello": "world",
		"foo":   "bar",
	}
	err := enc.Encode(m)
	require.Nil(t, err, "expected encoding to succeed")

	var targetMap map[string]string
	err = dec.Decode(&targetMap)
	require.Nil(t, err, "expected decoding to succeed")
	require.Equal(t, m, targetMap)

	var zeroMap map[string]string
	var targetMapInterface interface{}
	err = enc.Encode(zeroMap)
	require.Nil(t, err, "expected encoding to succeed")

	err = dec.Decode(&targetMapInterface)
	require.Nil(t, err, "expected decoding to succeed")
	require.Equal(t, 0, len(buf.Bytes()))

	err = enc.Encode([]interface{}{"hello", "world", m})
	require.Nil(t, err, "expected encoding to succeed")
	var a string
	var b string
	var c map[string]string
	i := []interface{}{&a, &b, &c}
	err = dec.Decode(&i)
	require.Nil(t, err, "expected decoding to succeed")
	require.Equal(t, 0, len(buf.Bytes()))
	require.Equal(t, "hello", a)
	require.Equal(t, "world", b)
	require.Equal(t, m, c)
}
Ejemplo n.º 8
0
func newMsgpackStream(r io.Reader) *msgpackStream {
	return &msgpackStream{decoder: codec.NewDecoder(r, codecHandle())}
}