github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/crc/crc32_test.go (about) 1 // Copyright 2021 DataStax 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package crc 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 ) 22 23 func TestChecksumIEEE(t *testing.T) { 24 tests := []struct { 25 name string 26 data []byte 27 expected uint32 28 }{ 29 { 30 "zero", 31 []byte{}, 32 initialChecksum, 33 }, 34 // test cases below are snapshots taken from actual payloads processed by the DataStax Java driver 35 { 36 "QUERY (SELECT cluster_name FROM system.local)", 37 []byte{ 38 6, 16, 0, 0, 7, 0, 0, 0, 47, 0, 0, 0, 37, 83, 69, 76, 69, 67, 84, 32, 99, 108, 117, 115, 116, 101, 114, 39 95, 110, 97, 109, 101, 32, 70, 82, 79, 77, 32, 115, 121, 115, 116, 101, 109, 46, 108, 111, 99, 97, 108, 40 0, 1, 0, 0, 0, 0}, 41 37932456, 42 }, 43 { 44 "QUERY (SELECT * FROM system.local) + QUERY (SELECT * FROM system.peers_v2)", 45 []byte{ 46 6, 16, 0, 0, 7, 0, 0, 0, 36, 0, 0, 0, 26, 83, 69, 76, 69, 67, 84, 32, 42, 32, 70, 82, 79, 77, 32, 115, 47 121, 115, 116, 101, 109, 46, 108, 111, 99, 97, 108, 0, 1, 0, 0, 0, 0, 6, 16, 0, 1, 7, 0, 0, 0, 39, 0, 48 0, 0, 29, 83, 69, 76, 69, 67, 84, 32, 42, 32, 70, 82, 79, 77, 32, 115, 121, 115, 116, 101, 109, 46, 49 112, 101, 101, 114, 115, 95, 118, 50, 0, 1, 0, 0, 0, 0}, 50 // Java driver CRC32 for this payload is the (signed) int -642004664; 51 // Go's uint32 equivalent is -642004664 & 0xffffffff = 3652962632. 52 uint32(int64(-642004664) & int64(0xffffffff)), 53 }, 54 } 55 for _, tt := range tests { 56 t.Run(tt.name, func(t *testing.T) { 57 actual := ChecksumIEEE(tt.data) 58 assert.Equal(t, tt.expected, actual) 59 }) 60 } 61 }