|
| 1 | +// Copyright (c) 2026 Uber Technologies, Inc. |
| 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 mysql |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/binary" |
| 19 | + "math" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + |
| 25 | + "github.com/uber/submitqueue/submitqueue/extension/conflict/tango" |
| 26 | +) |
| 27 | + |
| 28 | +func TestSignatureTargetsCodec_RoundTrip(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + targets []tango.SignatureTarget |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "empty", |
| 35 | + targets: nil, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "single target", |
| 39 | + targets: []tango.SignatureTarget{{ID: 1, Distance: 0}}, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "several targets in ascending ID order", |
| 43 | + targets: []tango.SignatureTarget{ |
| 44 | + {ID: 0x735cc8dafbb93f02, Distance: 0}, |
| 45 | + {ID: 0x90afc9166e08f639, Distance: 1}, |
| 46 | + {ID: 0xfcf7cdab318e139e, Distance: 2}, |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "extreme IDs and distances", |
| 51 | + targets: []tango.SignatureTarget{ |
| 52 | + {ID: 0, Distance: math.MaxInt32}, |
| 53 | + {ID: math.MaxUint64, Distance: 63}, |
| 54 | + {ID: math.MaxUint64 - 1, Distance: 64}, |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "negative distance", |
| 59 | + targets: []tango.SignatureTarget{{ID: 7, Distance: -1}}, |
| 60 | + }, |
| 61 | + } |
| 62 | + for _, tt := range tests { |
| 63 | + t.Run(tt.name, func(t *testing.T) { |
| 64 | + got, err := decodeSignatureTargets(encodeSignatureTargets(tt.targets)) |
| 65 | + require.NoError(t, err) |
| 66 | + assert.Equal(t, tt.targets, got) |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestEncodeSignatureTargets_EmptyIsNotNil(t *testing.T) { |
| 72 | + assert.NotNil(t, encodeSignatureTargets(nil)) |
| 73 | + assert.Empty(t, encodeSignatureTargets(nil)) |
| 74 | +} |
| 75 | + |
| 76 | +func TestEncodeSignatureTargets_Layout(t *testing.T) { |
| 77 | + got := encodeSignatureTargets([]tango.SignatureTarget{{ID: 0x0102030405060708, Distance: 2}}) |
| 78 | + |
| 79 | + assert.Equal(t, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x04}, got) |
| 80 | +} |
| 81 | + |
| 82 | +func TestDecodeSignatureTargets_Invalid(t *testing.T) { |
| 83 | + idOnly := binary.BigEndian.AppendUint64(nil, 1) |
| 84 | + |
| 85 | + tests := []struct { |
| 86 | + name string |
| 87 | + encoded []byte |
| 88 | + }{ |
| 89 | + { |
| 90 | + name: "truncated ID", |
| 91 | + encoded: []byte{0x01, 0x02, 0x03}, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "ID without a distance", |
| 95 | + encoded: idOnly, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "unterminated distance varint", |
| 99 | + encoded: append(append([]byte{}, idOnly...), 0x80), |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "distance outside int32", |
| 103 | + encoded: binary.AppendVarint(append([]byte{}, idOnly...), math.MaxInt32+1), |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "valid target followed by a truncated one", |
| 107 | + encoded: append(binary.AppendVarint(append([]byte{}, idOnly...), 0), 0x01, 0x02), |
| 108 | + }, |
| 109 | + } |
| 110 | + for _, tt := range tests { |
| 111 | + t.Run(tt.name, func(t *testing.T) { |
| 112 | + _, err := decodeSignatureTargets(tt.encoded) |
| 113 | + assert.Error(t, err) |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
0 commit comments