From f0b98ab3714c67a92d2f4653f2505d6a20053c3d Mon Sep 17 00:00:00 2001 From: scruzin Date: Sat, 12 Jan 2019 13:46:21 +1030 Subject: [PATCH] Added TestProperties and TestObjects. --- rtmp/amf/amf_test.go | 151 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 138 insertions(+), 13 deletions(-) diff --git a/rtmp/amf/amf_test.go b/rtmp/amf/amf_test.go index 0a5a56ac..8ea11ba8 100644 --- a/rtmp/amf/amf_test.go +++ b/rtmp/amf/amf_test.go @@ -3,7 +3,7 @@ NAME amf_test.go DESCRIPTION - AMF tests + AMF test suite. AUTHORS Saxon Nelson-Milton @@ -33,15 +33,32 @@ import ( "testing" ) +// Test data. +var testStrings = [...]string{ + "", + "foo", + "bar", + "bazz", +} + +var testNumbers = [...]int32{ + 0, + 1, + 0xababab, + 0xffffff, +} + +// TestSanity checks that we haven't accidentally changed constants. +func TestSanity(t *testing.T) { + if TypeObjectEnd != 0x09 { + t.Errorf("TypeObjectEnd has wrong value; got %d, expected %d", TypeObjectEnd, 0x09) + } +} + // TestStrings tests string encoding and decoding. func TestStrings(t *testing.T) { - var testStrings = [...]string{ - "foo", - "bar", - } - for _, s := range testStrings { - // Short string encoding is as follows + // Short string encoding is as follows: // enc[0] = data type (typeString) // end[1:3] = size // enc[3:] = data @@ -63,12 +80,6 @@ func TestStrings(t *testing.T) { // TestNumbers tests 24-bit encoding and encoding. // We don't test the others as they are just wrappers for standard functions in encoding/binary. func TestNumbers(t *testing.T) { - var testNumbers = [...]int32{ - 0x0, - 0xababab, - 0xffffff, - } - for _, n := range testNumbers { buf := make([]byte, 4) // NB: encoder requires an extra byte for some reason enc := EncodeInt24(buf, n) @@ -81,3 +92,117 @@ func TestNumbers(t *testing.T) { } } } + +// TestProperties tests encoding and decoding of properties. +func TestProperties(t *testing.T) { + var buf [1024]byte + + // Encode/decode number properties. + enc := buf[:] + for i, _ := range testNumbers { + enc = PropEncode(&Property{dtype: typeNumber, number: float64(testNumbers[i])}, enc) + if enc == nil { + t.Errorf("PropEncode of number failed") + } + + } + var n int32 + prop := Property{} + dec := buf[:] + for i, _ := range testNumbers { + n = PropDecode(&prop, dec, 0) + if n < 0 { + t.Errorf("PropDecode of number failed") + } + if int32(prop.number) != testNumbers[i] { + t.Errorf("PropEncode/PropDecode returned wrong number; got %v, expected %v", int32(prop.number), testNumbers[i]) + } + dec = dec[n:] + } + + // Encode/decode string properties. + enc = buf[:] + for i, _ := range testStrings { + enc = PropEncode(&Property{dtype: typeString, str: testStrings[i]}, enc) + if enc == nil { + t.Errorf("PropEncode of string failed") + } + + } + prop = Property{} + dec = buf[:] + for i, _ := range testStrings { + n = PropDecode(&prop, dec, 0) + if n < 0 { + t.Errorf("PropDecode of string failed") + } + if prop.str != testStrings[i] { + t.Errorf("PropEncode/PropDecode returned wrong string; got %s, expected %s", prop.str, testStrings[i]) + } + dec = dec[n:] + } + +} + +// TestObject tests encoding and decoding of objects. +func TestObject(t *testing.T) { + var buf [1024]byte + + // Construct a simple object that has one property, the number 42. + prop1 := Property{dtype: typeNumber, number: 42} + obj1 := Object{} + obj1.Props = append(obj1.Props, prop1) + + // Encode it + enc := buf[:] + enc = Encode(&obj1, enc) + if enc == nil { + t.Errorf("Encode of object failed") + } + + // Check the encoding + if uint8(buf[0]) != TypeObject { + t.Errorf("Encoded wrong type; expected %d, got %v", TypeObject, uint8(buf[0])) + } + if uint8(buf[1]) != typeNumber { + t.Errorf("Encoded wrong type; expected %d, got %v", typeNumber, uint8(buf[0])) + } + num := DecodeNumber(buf[2:10]) + if num != 42 { + t.Errorf("Encoded wrong number") + } + end := int32(DecodeInt24(buf[10:13])) + if end != TypeObjectEnd { + t.Errorf("Did not encode TypeObjectEnd") + } + + // Decode it + dec := buf[1:] + var dobj1 Object + n := Decode(&dobj1, dec, 0) + if n < 0 { + t.Errorf("Decode of object failed") + } + + // Construct a more complicated object. + var obj2 Object + for i, _ := range testStrings { + obj2.Props = append(obj2.Props, Property{dtype: typeString, str: testStrings[i]}) + obj2.Props = append(obj2.Props, Property{dtype: typeNumber, number: float64(testNumbers[i])}) + } + + // Encode it. + enc = buf[:] + enc = Encode(&obj2, enc) + if enc == nil { + t.Errorf("Encode of object failed") + } + + // Decode it. + dec = buf[1:] + var dobj2 Object + n = Decode(&dobj2, dec, 0) + if n < 0 { + t.Errorf("Decode of object failed") + } +}