1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package crc16
import (
"bytes"
"encoding"
"testing"
)
type testCase struct {
Message []byte
CRC uint16
}
func TestARC(t *testing.T) {
tests := []testCase{
{[]byte("123456789"), 0xBB3D}}
table := MakeTable(IBM)
for _, testcase := range tests {
result := ^Update(0xFFFF, table, testcase.Message)
if testcase.CRC != result {
t.Fatalf("ARC CRC-16 value is incorrect, expected %x, received %x.", testcase.CRC, result)
}
}
}
func TestModbus(t *testing.T) {
tests := []testCase{
{[]byte{0xEA, 0x03, 0x00, 0x00, 0x00, 0x64}, 0x3A53},
{[]byte{0x4B, 0x03, 0x00, 0x2C, 0x00, 0x37}, 0xBFCB},
{[]byte("123456789"), 0x4B37},
{[]byte{0x0D, 0x01, 0x00, 0x62, 0x00, 0x33}, 0x0DDD},
{[]byte{0x01, 0x03, 0x00, 0x85, 0x00, 0x01}, 0xE395},
}
for _, testcase := range tests {
result := ^ChecksumIBM(testcase.Message)
if testcase.CRC != result {
t.Fatalf("Modbus CRC-16 value is incorrect, expected %X, received %X.", testcase.CRC, result)
}
}
}
func BenchmarkChecksumIBM(b *testing.B) {
for i := 0; i < b.N; i++ {
ChecksumIBM([]byte{0xEA, 0x03, 0x00, 0x00, 0x00, 0x64})
}
}
func BenchmarkMakeTable(b *testing.B) {
for i := 0; i < b.N; i++ {
MakeTable(IBM)
}
}
func TestCCITTFalse(t *testing.T) {
data := []byte("testdata")
target := uint16(0xDC7C)
actual := ChecksumCCITTFalse(data)
if actual != target {
t.Fatalf("CCITT checksum did not return the correct value, expected %x, received %x", target, actual)
}
}
func TestBinaryMarshal(t *testing.T) {
input1 := "The tunneling gopher digs downwards, "
input2 := "unaware of what he will find."
first := New(IBMTable)
firstrev := New(MBusTable)
first.Write([]byte(input1))
firstrev.Write([]byte(input1))
marshaler, ok := first.(encoding.BinaryMarshaler)
if !ok {
t.Fatal("first does not implement encoding.BinaryMarshaler")
}
state, err := marshaler.MarshalBinary()
if err != nil {
t.Fatal("unable to marshal hash:", err)
}
marshalerrev, rok := firstrev.(encoding.BinaryMarshaler)
if !rok {
t.Fatal("firstrev does not implement encoding.BinaryMarshaler")
}
staterev, err := marshalerrev.MarshalBinary()
if err != nil {
t.Fatal("unable to marshal hash:", err)
}
second := New(IBMTable)
secondrev := New(MBusTable)
unmarshaler, ok := second.(encoding.BinaryUnmarshaler)
if !ok {
t.Fatal("second does not implement encoding.BinaryUnmarshaler")
}
if err := unmarshaler.UnmarshalBinary(state); err != nil {
t.Fatal("unable to unmarshal hash:", err)
}
unmarshalerrev, rok := secondrev.(encoding.BinaryUnmarshaler)
if !rok {
t.Fatal("secondrev does not implement encoding.BinaryUnmarshaler")
}
if err := unmarshalerrev.UnmarshalBinary(staterev); err != nil {
t.Fatal("unable to unmarshal hash:", err)
}
first.Write([]byte(input2))
second.Write([]byte(input2))
firstrev.Write([]byte(input2))
secondrev.Write([]byte(input2))
if !bytes.Equal(first.Sum(nil), second.Sum(nil)) {
t.Fatalf("does not match!")
}
if !bytes.Equal(firstrev.Sum(nil), secondrev.Sum(nil)) {
t.Fatalf("does not match!")
}
if bytes.Equal(first.Sum(nil), secondrev.Sum(nil)) {
t.Fatalf("should not match!")
}
}
|