From c2d4e0b4a245c9f1e7854e611d75bab3d16b256c Mon Sep 17 00:00:00 2001 From: scruzin Date: Sat, 12 Jan 2019 08:57:53 +1030 Subject: [PATCH] Initial revision. --- rtmp/amf/amf_test.go | 83 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 rtmp/amf/amf_test.go diff --git a/rtmp/amf/amf_test.go b/rtmp/amf/amf_test.go new file mode 100644 index 00000000..0a5a56ac --- /dev/null +++ b/rtmp/amf/amf_test.go @@ -0,0 +1,83 @@ +/* +NAME + amf_test.go + +DESCRIPTION + AMF tests + +AUTHORS + Saxon Nelson-Milton + Dan Kortschak + Alan Noble + +LICENSE + amf_test.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean) + + It is free software: you can redistribute it and/or modify them + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + It is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with revid in gpl.txt. If not, see http://www.gnu.org/licenses. +*/ + +package amf + +import ( + "testing" +) + +// 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 + // enc[0] = data type (typeString) + // end[1:3] = size + // enc[3:] = data + buf := make([]byte, len(s)+5) + enc := EncodeString(buf, s) + if enc == nil { + t.Errorf("EncodeString failed") + } + if buf[0] != typeString { + t.Errorf("Expected typeString, got %v", buf[0]) + } + ds := DecodeString(buf[1:]) + if s != ds { + t.Errorf("DecodeString did not produce original string, got %v", ds) + } + } +} + +// 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) + if enc == nil { + t.Errorf("EncodeInt24 failed") + } + dn := int32(DecodeInt24(buf)) + if n != dn { + t.Errorf("DecodeInt24 did not produce original number, got %v", dn) + } + } +}