-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add side-by-side tests with official proto.Marshal and Unmarshal
This test ensures that our encoding is compatible with official proto modules. It doesn't verify that binary form is equivalent because we do encode zero values, unlike official encoder. Also add simple fuzzing. Closes #2 Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
- Loading branch information
Showing
10 changed files
with
991 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
kind: golang.Generate | ||
spec: | ||
vtProtobufEnabled: false | ||
baseSpecPath: /messages/ | ||
specs: | ||
- source: /messages/messages.proto | ||
genGateway: false | ||
external: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package messages_test | ||
|
||
import ( | ||
"encoding/hex" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/siderolabs/protoenc" | ||
) | ||
|
||
func FuzzBasicMessage(f *testing.F) { | ||
testcases := [][]byte{ | ||
hexToBytes(f, "08 01 18 02 29 03 00 00 00 00 00 00 00 32 0b 73 6f 6d 65 20 73 74 72 69 6e 67 3a 0a 73 6f 6d 65 20 62 79 74 65 73"), | ||
hexToBytes(f, "08 00 18 00 29 00 00 00 00 00 00 00 00 32 00 3a 0a 73 6f 6d 65 20 62 79 74 65 73"), | ||
} | ||
for _, tc := range testcases { | ||
f.Add(tc) // Use f.Add to provide a seed corpus | ||
} | ||
|
||
f.Fuzz(func(t *testing.T, data []byte) { | ||
var ourBasicMessage BasicMessage | ||
err := protoenc.Unmarshal(data, &ourBasicMessage) | ||
if err != nil { | ||
errText := err.Error() | ||
|
||
switch { | ||
case strings.Contains(errText, "index out of range"), | ||
strings.Contains(errText, "assignment to entry in nil map"), | ||
strings.Contains(errText, "invalid memory address or nil pointer dereference"): | ||
t.FailNow() | ||
} | ||
} | ||
}) | ||
} | ||
|
||
// hexToBytes converts a hex string to a byte slice, removing any whitespace. | ||
func hexToBytes(f *testing.F, s string) []byte { | ||
f.Helper() | ||
|
||
s = strings.ReplaceAll(s, "|", "") | ||
s = strings.ReplaceAll(s, "[", "") | ||
s = strings.ReplaceAll(s, "]", "") | ||
s = strings.ReplaceAll(s, " ", "") | ||
|
||
b, err := hex.DecodeString(s) | ||
require.NoError(f, err) | ||
|
||
return b | ||
} | ||
|
||
func TestName(t *testing.T) { | ||
ourBasicMessage := BasicMessage{ | ||
Int64: 0, | ||
UInt64: 0, | ||
Fixed64: protoenc.FixedU64(0), | ||
SomeString: "", | ||
SomeBytes: nil, | ||
} | ||
encoded1 := must(protoenc.Marshal(&ourBasicMessage))(t) | ||
t.Logf("\n%s", hex.Dump(encoded1)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package messages_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/proto" | ||
|
||
"github.com/siderolabs/protoenc" | ||
) | ||
|
||
func shouldBeEqual[T any](t *testing.T, left, right T) { | ||
t.Helper() | ||
|
||
opts := makeOpts[T]() | ||
|
||
if !cmp.Equal(left, right, opts...) { | ||
t.Log(cmp.Diff(left, right, opts...)) | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func makeOpts[T any]() []cmp.Option { | ||
var zero T | ||
|
||
typ := reflect.TypeOf(zero) | ||
for typ.Kind() == reflect.Ptr { | ||
typ = typ.Elem() | ||
} | ||
|
||
if typ.Kind() == reflect.Struct { | ||
return []cmp.Option{cmpopts.IgnoreUnexported(reflect.New(typ).Elem().Interface())} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type msg[T any] interface { | ||
*T | ||
proto.Message | ||
} | ||
|
||
func protoUnmarshal[T any, V msg[T]](t *testing.T, data []byte) V { | ||
t.Helper() | ||
|
||
var msg T | ||
|
||
err := proto.Unmarshal(data, V(&msg)) | ||
require.NoError(t, err) | ||
|
||
return &msg | ||
} | ||
|
||
func ourUnmarshal[T any](t *testing.T, data []byte) T { | ||
t.Helper() | ||
|
||
var msg T | ||
|
||
err := protoenc.Unmarshal(data, &msg) | ||
require.NoError(t, err) | ||
|
||
return msg | ||
} | ||
|
||
func must[T any](v T, err error) func(t *testing.T) T { | ||
return func(t *testing.T) T { | ||
t.Helper() | ||
require.NoError(t, err) | ||
|
||
return v | ||
} | ||
} |
Oops, something went wrong.