2020-08-14 10:57:19 +00:00
|
|
|
package proto_test
|
|
|
|
|
|
|
|
import (
|
2024-07-15 08:16:29 +00:00
|
|
|
"encoding/binary"
|
2020-08-14 13:32:19 +00:00
|
|
|
"math"
|
2020-08-14 10:57:19 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-03-07 10:38:56 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto/test"
|
2020-08-14 10:57:19 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-07-15 08:16:29 +00:00
|
|
|
"google.golang.org/protobuf/encoding/protowire"
|
2020-10-19 18:51:59 +00:00
|
|
|
goproto "google.golang.org/protobuf/proto"
|
2020-08-14 10:57:19 +00:00
|
|
|
)
|
|
|
|
|
2020-08-14 14:10:15 +00:00
|
|
|
type SomeEnum int32
|
|
|
|
|
2020-08-14 10:57:19 +00:00
|
|
|
type stablePrimitives struct {
|
|
|
|
FieldA []byte
|
2020-08-14 12:29:48 +00:00
|
|
|
FieldB string
|
2020-08-14 12:53:57 +00:00
|
|
|
FieldC bool
|
2020-08-14 13:32:19 +00:00
|
|
|
FieldD int32
|
|
|
|
FieldE uint32
|
|
|
|
FieldF int64
|
|
|
|
FieldG uint64
|
2020-08-14 14:10:15 +00:00
|
|
|
FieldH SomeEnum
|
2020-12-21 07:51:04 +00:00
|
|
|
FieldI uint64 // fixed64
|
2021-03-24 07:10:30 +00:00
|
|
|
FieldJ float64
|
2021-11-18 13:14:49 +00:00
|
|
|
FieldK uint32 // fixed32
|
2020-08-14 10:57:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 14:22:47 +00:00
|
|
|
type stableRepPrimitives struct {
|
|
|
|
FieldA [][]byte
|
|
|
|
FieldB []string
|
|
|
|
FieldC []int32
|
|
|
|
FieldD []uint32
|
|
|
|
FieldE []int64
|
|
|
|
FieldF []uint64
|
2024-07-15 08:16:29 +00:00
|
|
|
|
|
|
|
FieldFu []uint64
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 14:10:15 +00:00
|
|
|
const (
|
|
|
|
ENUM_UNKNOWN SomeEnum = 0
|
2023-05-03 11:09:59 +00:00
|
|
|
ENUM_POSITIVE SomeEnum = 1
|
|
|
|
ENUM_NEGATIVE SomeEnum = -1
|
2020-08-14 14:10:15 +00:00
|
|
|
)
|
|
|
|
|
2020-08-14 12:29:48 +00:00
|
|
|
func (s *stablePrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte, error) {
|
2020-08-14 10:57:19 +00:00
|
|
|
if s == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, s.stableSize())
|
|
|
|
}
|
|
|
|
|
2024-03-14 07:31:18 +00:00
|
|
|
var i, offset, fieldNum int
|
2020-08-14 10:57:19 +00:00
|
|
|
|
2020-08-14 12:29:48 +00:00
|
|
|
fieldNum = 1
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.BytesMarshal(fieldNum, buf, s.FieldA)
|
2020-08-14 10:57:19 +00:00
|
|
|
|
2020-08-14 12:29:48 +00:00
|
|
|
fieldNum = 2
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
2020-08-14 10:57:19 +00:00
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.StringMarshal(fieldNum, buf, s.FieldB)
|
2020-08-14 12:53:57 +00:00
|
|
|
|
|
|
|
fieldNum = 200
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.BoolMarshal(fieldNum, buf, s.FieldC)
|
2020-08-14 10:57:19 +00:00
|
|
|
|
2020-08-14 13:32:19 +00:00
|
|
|
fieldNum = 201
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.Int32Marshal(fieldNum, buf, s.FieldD)
|
2020-08-14 13:32:19 +00:00
|
|
|
|
|
|
|
fieldNum = 202
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.UInt32Marshal(fieldNum, buf, s.FieldE)
|
2020-08-14 13:32:19 +00:00
|
|
|
|
|
|
|
fieldNum = 203
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.Int64Marshal(fieldNum, buf, s.FieldF)
|
2020-08-14 13:32:19 +00:00
|
|
|
|
|
|
|
fieldNum = 204
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.UInt64Marshal(fieldNum, buf, s.FieldG)
|
2020-08-14 13:32:19 +00:00
|
|
|
|
2020-12-21 07:51:04 +00:00
|
|
|
fieldNum = 205
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.Fixed64Marshal(fieldNum, buf, s.FieldI)
|
2020-12-21 07:51:04 +00:00
|
|
|
|
2021-03-24 07:10:30 +00:00
|
|
|
fieldNum = 206
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.Float64Marshal(fieldNum, buf, s.FieldJ)
|
2021-03-24 07:10:30 +00:00
|
|
|
|
2021-11-18 13:14:49 +00:00
|
|
|
fieldNum = 207
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = proto.Fixed32Marshal(fieldNum, buf, s.FieldK)
|
|
|
|
|
|
|
|
i += offset
|
|
|
|
|
2020-08-14 14:10:15 +00:00
|
|
|
fieldNum = 300
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.EnumMarshal(fieldNum, buf, int32(s.FieldH))
|
2020-08-14 14:10:15 +00:00
|
|
|
|
2020-08-14 10:57:19 +00:00
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stablePrimitives) stableSize() int {
|
2020-08-14 12:29:48 +00:00
|
|
|
return proto.BytesSize(1, s.FieldA) +
|
2020-08-14 12:53:57 +00:00
|
|
|
proto.StringSize(2, s.FieldB) +
|
2020-08-14 13:32:19 +00:00
|
|
|
proto.BoolSize(200, s.FieldC) +
|
|
|
|
proto.Int32Size(201, s.FieldD) +
|
|
|
|
proto.UInt32Size(202, s.FieldE) +
|
|
|
|
proto.Int64Size(203, s.FieldF) +
|
2020-08-14 14:10:15 +00:00
|
|
|
proto.UInt64Size(204, s.FieldG) +
|
2020-12-21 07:51:04 +00:00
|
|
|
proto.Fixed64Size(205, s.FieldI) +
|
2021-03-24 07:10:30 +00:00
|
|
|
proto.Float64Size(206, s.FieldJ) +
|
2021-11-18 13:14:49 +00:00
|
|
|
proto.Fixed32Size(207, s.FieldK) +
|
2020-08-14 14:10:15 +00:00
|
|
|
proto.EnumSize(300, int32(s.FieldH))
|
2020-08-14 10:57:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 14:22:47 +00:00
|
|
|
func (s *stableRepPrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte, error) {
|
|
|
|
if s == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, s.stableSize())
|
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
var i, fieldNum int
|
2020-08-14 14:22:47 +00:00
|
|
|
|
|
|
|
fieldNum = 1
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.RepeatedBytesMarshal(fieldNum, buf, s.FieldA)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
|
|
|
fieldNum = 2
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.RepeatedStringMarshal(fieldNum, buf, s.FieldB)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
|
|
|
fieldNum = 3
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.RepeatedInt32Marshal(fieldNum, buf, s.FieldC)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
|
|
|
fieldNum = 4
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.RepeatedUInt32Marshal(fieldNum, buf, s.FieldD)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
|
|
|
fieldNum = 5
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.RepeatedInt64Marshal(fieldNum, buf, s.FieldE)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
|
|
|
fieldNum = 6
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
2022-03-12 12:20:19 +00:00
|
|
|
i += proto.RepeatedUInt64Marshal(fieldNum, buf, s.FieldF)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
2024-07-15 08:16:29 +00:00
|
|
|
fieldNum = 7
|
|
|
|
if wrongField {
|
|
|
|
fieldNum++
|
|
|
|
}
|
|
|
|
for j := range s.FieldFu {
|
|
|
|
{
|
|
|
|
prefix := protowire.EncodeTag(
|
|
|
|
protowire.Number(fieldNum),
|
|
|
|
protowire.VarintType)
|
|
|
|
i += binary.PutUvarint(buf[i:], uint64(prefix))
|
|
|
|
i += binary.PutUvarint(buf[i:], s.FieldFu[j])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-14 14:22:47 +00:00
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stableRepPrimitives) stableSize() int {
|
|
|
|
f1 := proto.RepeatedBytesSize(1, s.FieldA)
|
|
|
|
f2 := proto.RepeatedStringSize(2, s.FieldB)
|
|
|
|
f3, _ := proto.RepeatedInt32Size(3, s.FieldC)
|
|
|
|
f4, _ := proto.RepeatedUInt32Size(4, s.FieldD)
|
|
|
|
f5, _ := proto.RepeatedInt64Size(5, s.FieldE)
|
|
|
|
f6, _ := proto.RepeatedUInt64Size(6, s.FieldF)
|
|
|
|
|
2024-07-15 08:16:29 +00:00
|
|
|
var f7 int
|
|
|
|
for i := range s.FieldFu {
|
|
|
|
f7 += protowire.SizeGroup(protowire.Number(7), protowire.SizeVarint(s.FieldFu[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return f1 + f2 + f3 + f4 + f5 + f6 + f7
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 10:57:19 +00:00
|
|
|
func TestBytesMarshal(t *testing.T) {
|
|
|
|
t.Run("not empty", func(t *testing.T) {
|
|
|
|
data := []byte("Hello World")
|
|
|
|
testBytesMarshal(t, data, false)
|
|
|
|
testBytesMarshal(t, data, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
testBytesMarshal(t, []byte{}, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
testBytesMarshal(t, nil, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-14 12:53:57 +00:00
|
|
|
func TestStringMarshal(t *testing.T) {
|
|
|
|
t.Run("not empty", func(t *testing.T) {
|
|
|
|
data := "Hello World"
|
|
|
|
testStringMarshal(t, data, false)
|
|
|
|
testStringMarshal(t, data, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
testStringMarshal(t, "", false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBoolMarshal(t *testing.T) {
|
|
|
|
t.Run("true", func(t *testing.T) {
|
|
|
|
testBoolMarshal(t, true, false)
|
|
|
|
testBoolMarshal(t, true, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("false", func(t *testing.T) {
|
|
|
|
testBoolMarshal(t, false, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-14 13:32:19 +00:00
|
|
|
func TestInt32Marshal(t *testing.T) {
|
|
|
|
t.Run("zero", func(t *testing.T) {
|
|
|
|
testInt32Marshal(t, 0, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
testInt32Marshal(t, math.MaxInt32, false)
|
|
|
|
testInt32Marshal(t, math.MaxInt32, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("negative", func(t *testing.T) {
|
|
|
|
testInt32Marshal(t, math.MinInt32, false)
|
|
|
|
testInt32Marshal(t, math.MinInt32, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUInt32Marshal(t *testing.T) {
|
|
|
|
t.Run("zero", func(t *testing.T) {
|
|
|
|
testUInt32Marshal(t, 0, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("non zero", func(t *testing.T) {
|
|
|
|
testUInt32Marshal(t, math.MaxUint32, false)
|
|
|
|
testUInt32Marshal(t, math.MaxUint32, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInt64Marshal(t *testing.T) {
|
|
|
|
t.Run("zero", func(t *testing.T) {
|
|
|
|
testInt32Marshal(t, 0, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
testInt64Marshal(t, math.MaxInt64, false)
|
|
|
|
testInt64Marshal(t, math.MaxInt64, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("negative", func(t *testing.T) {
|
|
|
|
testInt64Marshal(t, math.MinInt64, false)
|
|
|
|
testInt64Marshal(t, math.MinInt64, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUInt64Marshal(t *testing.T) {
|
|
|
|
t.Run("zero", func(t *testing.T) {
|
|
|
|
testUInt64Marshal(t, 0, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("non zero", func(t *testing.T) {
|
|
|
|
testUInt64Marshal(t, math.MaxUint64, false)
|
|
|
|
testUInt64Marshal(t, math.MaxUint64, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-14 14:10:15 +00:00
|
|
|
func TestEnumMarshal(t *testing.T) {
|
|
|
|
testEnumMarshal(t, ENUM_UNKNOWN, false)
|
|
|
|
testEnumMarshal(t, ENUM_POSITIVE, false)
|
|
|
|
testEnumMarshal(t, ENUM_POSITIVE, true)
|
|
|
|
testEnumMarshal(t, ENUM_NEGATIVE, false)
|
|
|
|
testEnumMarshal(t, ENUM_NEGATIVE, true)
|
|
|
|
}
|
|
|
|
|
2020-08-14 14:22:47 +00:00
|
|
|
func TestRepeatedBytesMarshal(t *testing.T) {
|
|
|
|
t.Run("not empty", func(t *testing.T) {
|
|
|
|
data := [][]byte{[]byte("One"), []byte("Two"), []byte("Three")}
|
|
|
|
testRepeatedBytesMarshal(t, data, false)
|
|
|
|
testRepeatedBytesMarshal(t, data, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
testRepeatedBytesMarshal(t, [][]byte{}, false)
|
|
|
|
})
|
|
|
|
|
2022-08-26 11:18:07 +00:00
|
|
|
t.Run("empty element", func(t *testing.T) {
|
|
|
|
testRepeatedBytesMarshal(t, [][]byte{{1}, {}}, false)
|
|
|
|
})
|
|
|
|
|
2020-08-14 14:22:47 +00:00
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
testRepeatedBytesMarshal(t, nil, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepeatedStringMarshal(t *testing.T) {
|
|
|
|
t.Run("not empty", func(t *testing.T) {
|
|
|
|
data := []string{"One", "Two", "Three"}
|
|
|
|
testRepeatedStringMarshal(t, data, false)
|
|
|
|
testRepeatedStringMarshal(t, data, true)
|
|
|
|
})
|
|
|
|
|
2022-08-26 11:18:07 +00:00
|
|
|
t.Run("empty element", func(t *testing.T) {
|
|
|
|
testRepeatedStringMarshal(t, []string{""}, false)
|
|
|
|
})
|
|
|
|
|
2020-08-14 14:22:47 +00:00
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
testRepeatedStringMarshal(t, []string{}, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
testRepeatedStringMarshal(t, nil, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepeatedInt32Marshal(t *testing.T) {
|
|
|
|
t.Run("not empty", func(t *testing.T) {
|
|
|
|
data := []int32{-1, 0, 1, 2, 3, 4, 5}
|
|
|
|
testRepeatedInt32Marshal(t, data, false)
|
|
|
|
testRepeatedInt32Marshal(t, data, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
testRepeatedInt32Marshal(t, []int32{}, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
testRepeatedInt32Marshal(t, nil, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepeatedUInt32Marshal(t *testing.T) {
|
|
|
|
t.Run("not empty", func(t *testing.T) {
|
|
|
|
data := []uint32{0, 1, 2, 3, 4, 5}
|
|
|
|
testRepeatedUInt32Marshal(t, data, false)
|
|
|
|
testRepeatedUInt32Marshal(t, data, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
testRepeatedUInt32Marshal(t, []uint32{}, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
testRepeatedUInt32Marshal(t, nil, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepeatedInt64Marshal(t *testing.T) {
|
|
|
|
t.Run("not empty", func(t *testing.T) {
|
|
|
|
data := []int64{-1, 0, 1, 2, 3, 4, 5}
|
|
|
|
testRepeatedInt64Marshal(t, data, false)
|
|
|
|
testRepeatedInt64Marshal(t, data, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
testRepeatedInt64Marshal(t, []int64{}, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
testRepeatedInt64Marshal(t, nil, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepeatedUInt64Marshal(t *testing.T) {
|
|
|
|
t.Run("not empty", func(t *testing.T) {
|
|
|
|
data := []uint64{0, 1, 2, 3, 4, 5}
|
|
|
|
testRepeatedUInt64Marshal(t, data, false)
|
|
|
|
testRepeatedUInt64Marshal(t, data, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
testRepeatedUInt64Marshal(t, []uint64{}, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
testRepeatedUInt64Marshal(t, nil, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-15 08:16:29 +00:00
|
|
|
func TestRepeatedUInt64MarshalUnpacked(t *testing.T) {
|
|
|
|
t.Run("not empty", func(t *testing.T) {
|
|
|
|
data := []uint64{0, 1, 2, 3, 4, 5}
|
|
|
|
testRepeatedUInt64MarshalUnpacked(t, data, false)
|
|
|
|
testRepeatedUInt64MarshalUnpacked(t, data, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
testRepeatedUInt64MarshalUnpacked(t, []uint64{}, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
testRepeatedUInt64MarshalUnpacked(t, nil, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-21 07:51:04 +00:00
|
|
|
func TestFixed64Marshal(t *testing.T) {
|
|
|
|
t.Run("zero", func(t *testing.T) {
|
|
|
|
testFixed64Marshal(t, 0, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("non zero", func(t *testing.T) {
|
|
|
|
testFixed64Marshal(t, math.MaxUint64, false)
|
|
|
|
testFixed64Marshal(t, math.MaxUint64, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-24 07:10:30 +00:00
|
|
|
func TestFloat64Marshal(t *testing.T) {
|
|
|
|
t.Run("zero", func(t *testing.T) {
|
|
|
|
testFloat64Marshal(t, 0, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("non zero", func(t *testing.T) {
|
|
|
|
f := math.Float64frombits(12345677890)
|
|
|
|
|
|
|
|
testFloat64Marshal(t, f, false)
|
|
|
|
testFloat64Marshal(t, f, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:14:49 +00:00
|
|
|
func TestFixed32Marshal(t *testing.T) {
|
|
|
|
t.Run("zero", func(t *testing.T) {
|
|
|
|
testFixed32Marshal(t, 0, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("non zero", func(t *testing.T) {
|
|
|
|
testFixed32Marshal(t, math.MaxUint32, false)
|
|
|
|
testFixed32Marshal(t, math.MaxUint32, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
func testMarshal(t *testing.T, c stablePrimitives, tr *test.Primitives, wrongField bool) *test.Primitives {
|
2020-08-14 10:57:19 +00:00
|
|
|
var (
|
|
|
|
wire []byte
|
|
|
|
err error
|
|
|
|
)
|
2020-08-14 14:22:47 +00:00
|
|
|
wire, err = c.stableMarshal(nil, wrongField)
|
2020-08-14 10:57:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
wireGen, err := goproto.Marshal(tr)
|
2020-08-14 10:57:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
// we can check equality because single field cannot be unstable marshalled
|
|
|
|
require.Equal(t, wireGen, wire)
|
|
|
|
} else {
|
|
|
|
require.NotEqual(t, wireGen, wire)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := new(test.Primitives)
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, result)
|
2020-08-14 10:57:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-08-14 14:22:47 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBytesMarshal(t *testing.T, data []byte, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stablePrimitives{FieldA: data}
|
|
|
|
transport = test.Primitives{FieldA: data}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testMarshal(t, custom, &transport, wrongField)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
2020-08-14 10:57:19 +00:00
|
|
|
if !wrongField {
|
|
|
|
require.Len(t, result.FieldA, len(data))
|
|
|
|
if len(data) > 0 {
|
|
|
|
require.Equal(t, data, result.FieldA)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
require.Len(t, result.FieldA, 0)
|
|
|
|
}
|
|
|
|
}
|
2020-08-14 12:29:48 +00:00
|
|
|
|
|
|
|
func testStringMarshal(t *testing.T, s string, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stablePrimitives{FieldB: s}
|
|
|
|
transport = test.Primitives{FieldB: s}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testMarshal(t, custom, &transport, wrongField)
|
2020-08-14 12:29:48 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Len(t, result.FieldB, len(s))
|
|
|
|
if len(s) > 0 {
|
|
|
|
require.Equal(t, s, result.FieldB)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
require.Len(t, result.FieldB, 0)
|
|
|
|
}
|
|
|
|
}
|
2020-08-14 12:53:57 +00:00
|
|
|
|
|
|
|
func testBoolMarshal(t *testing.T, b bool, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stablePrimitives{FieldC: b}
|
|
|
|
transport = test.Primitives{FieldC: b}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testMarshal(t, custom, &transport, wrongField)
|
2020-08-14 12:53:57 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Equal(t, b, result.FieldC)
|
|
|
|
} else {
|
|
|
|
require.False(t, false, result.FieldC)
|
|
|
|
}
|
|
|
|
}
|
2020-08-14 13:32:19 +00:00
|
|
|
|
|
|
|
func testInt32Marshal(t *testing.T, n int32, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stablePrimitives{FieldD: n}
|
|
|
|
transport = test.Primitives{FieldD: n}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testMarshal(t, custom, &transport, wrongField)
|
2020-08-14 13:32:19 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Equal(t, n, result.FieldD)
|
|
|
|
} else {
|
|
|
|
require.EqualValues(t, 0, result.FieldD)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUInt32Marshal(t *testing.T, n uint32, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stablePrimitives{FieldE: n}
|
|
|
|
transport = test.Primitives{FieldE: n}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testMarshal(t, custom, &transport, wrongField)
|
2020-08-14 13:32:19 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Equal(t, n, result.FieldE)
|
|
|
|
} else {
|
|
|
|
require.EqualValues(t, 0, result.FieldE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testInt64Marshal(t *testing.T, n int64, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stablePrimitives{FieldF: n}
|
|
|
|
transport = test.Primitives{FieldF: n}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testMarshal(t, custom, &transport, wrongField)
|
2020-08-14 13:32:19 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Equal(t, n, result.FieldF)
|
|
|
|
} else {
|
|
|
|
require.EqualValues(t, 0, result.FieldF)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUInt64Marshal(t *testing.T, n uint64, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stablePrimitives{FieldG: n}
|
|
|
|
transport = test.Primitives{FieldG: n}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testMarshal(t, custom, &transport, wrongField)
|
2020-08-14 13:32:19 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Equal(t, n, result.FieldG)
|
|
|
|
} else {
|
|
|
|
require.EqualValues(t, 0, result.FieldG)
|
|
|
|
}
|
|
|
|
}
|
2020-08-14 14:10:15 +00:00
|
|
|
|
2021-03-24 07:10:30 +00:00
|
|
|
func testFloat64Marshal(t *testing.T, n float64, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stablePrimitives{FieldJ: n}
|
|
|
|
transport = test.Primitives{FieldJ: n}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testMarshal(t, custom, &transport, wrongField)
|
2021-03-24 07:10:30 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Equal(t, n, result.FieldJ)
|
|
|
|
} else {
|
|
|
|
require.EqualValues(t, 0, result.FieldJ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-14 14:10:15 +00:00
|
|
|
func testEnumMarshal(t *testing.T, e SomeEnum, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stablePrimitives{FieldH: e}
|
|
|
|
transport = test.Primitives{FieldH: test.Primitives_SomeEnum(e)}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testMarshal(t, custom, &transport, wrongField)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.EqualValues(t, custom.FieldH, result.FieldH)
|
|
|
|
} else {
|
|
|
|
require.EqualValues(t, 0, result.FieldH)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
func testRepMarshal(t *testing.T, c stableRepPrimitives, tr *test.RepPrimitives, wrongField bool) *test.RepPrimitives {
|
2020-08-14 14:22:47 +00:00
|
|
|
var (
|
|
|
|
wire []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
wire, err = c.stableMarshal(nil, wrongField)
|
2020-08-14 14:10:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
wireGen, err := goproto.Marshal(tr)
|
2020-08-14 14:10:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
// we can check equality because single field cannot be unstable marshalled
|
|
|
|
require.Equal(t, wireGen, wire)
|
|
|
|
} else {
|
|
|
|
require.NotEqual(t, wireGen, wire)
|
|
|
|
}
|
|
|
|
|
2020-08-14 14:22:47 +00:00
|
|
|
result := new(test.RepPrimitives)
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, result)
|
2020-08-14 14:10:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-08-14 14:22:47 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRepeatedBytesMarshal(t *testing.T, data [][]byte, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stableRepPrimitives{FieldA: data}
|
|
|
|
transport = test.RepPrimitives{FieldA: data}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testRepMarshal(t, custom, &transport, wrongField)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
2020-08-14 14:10:15 +00:00
|
|
|
if !wrongField {
|
2020-08-14 14:22:47 +00:00
|
|
|
require.Len(t, result.FieldA, len(data))
|
|
|
|
if len(data) > 0 {
|
|
|
|
require.Equal(t, data, result.FieldA)
|
|
|
|
}
|
2020-08-14 14:10:15 +00:00
|
|
|
} else {
|
2020-08-14 14:22:47 +00:00
|
|
|
require.Len(t, result.FieldA, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRepeatedStringMarshal(t *testing.T, s []string, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stableRepPrimitives{FieldB: s}
|
|
|
|
transport = test.RepPrimitives{FieldB: s}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testRepMarshal(t, custom, &transport, wrongField)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Len(t, result.FieldB, len(s))
|
|
|
|
if len(s) > 0 {
|
|
|
|
require.Equal(t, s, result.FieldB)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
require.Len(t, result.FieldB, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRepeatedInt32Marshal(t *testing.T, n []int32, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stableRepPrimitives{FieldC: n}
|
|
|
|
transport = test.RepPrimitives{FieldC: n}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testRepMarshal(t, custom, &transport, wrongField)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Len(t, result.FieldC, len(n))
|
|
|
|
if len(n) > 0 {
|
|
|
|
require.Equal(t, n, result.FieldC)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
require.Len(t, result.FieldC, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRepeatedUInt32Marshal(t *testing.T, n []uint32, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stableRepPrimitives{FieldD: n}
|
|
|
|
transport = test.RepPrimitives{FieldD: n}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testRepMarshal(t, custom, &transport, wrongField)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Len(t, result.FieldD, len(n))
|
|
|
|
if len(n) > 0 {
|
|
|
|
require.Equal(t, n, result.FieldD)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
require.Len(t, result.FieldD, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRepeatedInt64Marshal(t *testing.T, n []int64, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stableRepPrimitives{FieldE: n}
|
|
|
|
transport = test.RepPrimitives{FieldE: n}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testRepMarshal(t, custom, &transport, wrongField)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Len(t, result.FieldE, len(n))
|
|
|
|
if len(n) > 0 {
|
|
|
|
require.Equal(t, n, result.FieldE)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
require.Len(t, result.FieldE, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRepeatedUInt64Marshal(t *testing.T, n []uint64, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stableRepPrimitives{FieldF: n}
|
|
|
|
transport = test.RepPrimitives{FieldF: n}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testRepMarshal(t, custom, &transport, wrongField)
|
2020-08-14 14:22:47 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Len(t, result.FieldF, len(n))
|
|
|
|
if len(n) > 0 {
|
|
|
|
require.Equal(t, n, result.FieldF)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
require.Len(t, result.FieldF, 0)
|
2020-08-14 14:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-21 07:51:04 +00:00
|
|
|
|
2024-07-15 08:16:29 +00:00
|
|
|
func testRepeatedUInt64MarshalUnpacked(t *testing.T, n []uint64, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stableRepPrimitives{FieldFu: n}
|
|
|
|
transport = test.RepPrimitives{FieldFu: n}
|
|
|
|
)
|
|
|
|
|
|
|
|
result := testRepMarshal(t, custom, &transport, wrongField)
|
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Len(t, result.FieldFu, len(n))
|
|
|
|
if len(n) > 0 {
|
|
|
|
require.Equal(t, n, result.FieldFu)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
require.Len(t, result.FieldFu, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 07:51:04 +00:00
|
|
|
func testFixed64Marshal(t *testing.T, n uint64, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stablePrimitives{FieldI: n}
|
|
|
|
transport = test.Primitives{FieldI: n}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testMarshal(t, custom, &transport, wrongField)
|
2020-12-21 07:51:04 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Equal(t, n, result.FieldI)
|
|
|
|
} else {
|
|
|
|
require.EqualValues(t, 0, result.FieldI)
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 13:14:49 +00:00
|
|
|
|
|
|
|
func testFixed32Marshal(t *testing.T, n uint32, wrongField bool) {
|
|
|
|
var (
|
|
|
|
custom = stablePrimitives{FieldK: n}
|
|
|
|
transport = test.Primitives{FieldK: n}
|
|
|
|
)
|
|
|
|
|
2024-07-12 14:45:29 +00:00
|
|
|
result := testMarshal(t, custom, &transport, wrongField)
|
2021-11-18 13:14:49 +00:00
|
|
|
|
|
|
|
if !wrongField {
|
|
|
|
require.Equal(t, n, result.FieldK)
|
|
|
|
} else {
|
|
|
|
require.EqualValues(t, 0, result.FieldK)
|
|
|
|
}
|
|
|
|
}
|