Files
cloud-services/pkg/utils/vod/crc_test.go

134 lines
4.7 KiB
Go

package vod
import (
"bytes"
"encoding/binary"
"encoding/hex"
"testing"
)
func TestCustomCRC(t *testing.T) {
inputString := "2301084000000101012200010101010001010101000000000000000000FF7EFF7F000101010101000101010100010001010101000101010100000000000000000000000000010101000100000100010101000201010101000101020101000101010200010101010101010101000101000100010001010101010101010101010000000000000100000101020101010101010000000000000000FFFFFF0000000201010202000101"
inputB, err := hex.DecodeString(inputString)
if err != nil {
t.Error(err)
}
p := NewVODHelper(false, false, false)
output := p.GenerateCRC(inputB)
if output != 0x5F {
t.Fail()
}
}
func TestDoesMatchReport(t *testing.T) {
reported := "00A92301084000000101012200010101010001010101000000000000000000FF7EFF7F000101010101000101010100010001010101000101010100000000000000000000000000010101000100000100010101000201010101000101020101000101010200010101010101010101000101000100010001010101010101010101010000000000000100000101020101010101010000000000000000FFFFFF00000002010102020001015F"
reportedBytes, err := hex.DecodeString(reported)
if err != nil {
t.Error(err)
}
inputBytes := reportedBytes[2 : len(reportedBytes)-1]
p := NewVODHelper(false, false, true)
outputBytes := p.AddLengthAndCRC(inputBytes)
if len(reportedBytes) != len(outputBytes) {
t.Logf("Lengths did not match %d %d", len(reportedBytes), len(outputBytes))
t.Fail()
}
if !bytes.Equal(outputBytes, reportedBytes) {
t.Log("input and output did not match")
t.Fail()
}
}
func TestLengthOfFinal(t *testing.T) {
inputString := "2EF1110102030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798990001020304050607080910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626399"
inputB, err := hex.DecodeString(inputString)
if err != nil {
t.Error(err)
}
// Expect the length to be increased by 2 from pre-pending length
p := NewVODHelper(false, false, true)
output := p.AddLengthAndCRC(inputB)
if len(output) != len(inputB)+3 {
t.Logf("Expected a final length of %d but got %d", len(inputB)+3, len(output))
t.Fail()
}
length := binary.BigEndian.Uint16(output[:2])
// Don't include crc with length
if int64(length) != int64(len(output)-1) {
t.Logf("The calculated length does not match actual length %d %d", int64(length), int64(len(output)-1))
t.Fail()
}
}
func TestCRCCorrect(t *testing.T) {
inputString := "00112233"
inputB, err := hex.DecodeString(inputString)
if err != nil {
t.Error(err)
}
// Expect the length to be increased by 2 from pre-pending length
p := NewVODHelper(false, true, false)
output := p.GenerateCRC(inputB)
secondStep := append(inputB, output)
output2 := p.GenerateCRC(secondStep)
if output2 != 0x00 {
t.Fail()
}
}
func TestCRCCorrect2(t *testing.T) {
inputString := "00112233"
inputB, err := hex.DecodeString(inputString)
if err != nil {
t.Error(err)
}
// Expect the length to be increased by 2 from pre-pending length
p := NewVODHelper(false, false, false)
output := p.AddLengthAndCRC(inputB)
// Chopping out length
output = output[2:]
output2 := p.GenerateCRC(output)
if output2 != 0x00 {
t.Fail()
}
}
func TestCRCTotalZero(t *testing.T) {
p := NewVODHelper(false, false, true)
inputString := "030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798990001020304050607080910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626399"
inputB, err := hex.DecodeString(inputString)
if err != nil {
t.Error(err)
}
crc := p.GenerateCRC(inputB)
if crc != 0x00 {
t.Fail()
}
}
func TestCRCAndLengthSame(t *testing.T) {
inputString := "01012301084000000101012200010101010001010101000000000000000000FF7EFF7F000101010101000101010100010001010101000101010100000000000000000000000000010101000100000100010101000201010101000101020101000101010200010101010101010101000101000100010001010101010101010101010000000000000100000101020101010101010000000000000000FFFFFF000101020101020200010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000BA"
inputBytes, err := hex.DecodeString(inputString)
if err != nil {
t.Error(err)
}
//Removing the length and the CRC
p := NewVODHelper(false, false, true)
choppedInput := inputBytes[2 : len(inputBytes)-1]
choppedInput = p.AddLengthAndCRC(choppedInput)
if !bytes.Equal(inputBytes, choppedInput) {
t.Log("CRC and length not calculated as expected")
t.Fail()
}
}