From 9b6c6ff53ab24b86cfc38e2519a498a0a8346850 Mon Sep 17 00:00:00 2001 From: LiangliangSui Date: Thu, 18 Apr 2024 20:15:47 +0800 Subject: [PATCH] test Signed-off-by: LiangliangSui --- .github/workflows/ci.yml | 1 + go/fury/fury.go | 16 ++++++++++++++++ go/fury/fury_test.go | 12 ++++++++++++ 3 files changed, 29 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c3528629b0..1e59e9c865 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,7 @@ on: push: branches: - main + - go_magic_number - 'releases/**' - 'deploy/**' - 'test*' diff --git a/go/fury/fury.go b/go/fury/fury.go index dff88c9049..16463837c4 100644 --- a/go/fury/fury.go +++ b/go/fury/fury.go @@ -90,6 +90,8 @@ const ( isOutOfBandFlag ) +const MAGIC_NUMBER int16 = 0x62d4 + type Fury struct { typeResolver *typeResolver refResolver *RefResolver @@ -121,6 +123,11 @@ func (f *Fury) Serialize(buf *ByteBuffer, v interface{}, callback BufferCallback buffer = f.buffer buffer.writerIndex = 0 } + if f.language == XLANG { + buffer.WriteInt16(MAGIC_NUMBER) + } else { + return fmt.Errorf("%d language is not supported", f.language) + } var bitmap byte = 0 if isNil(reflect.ValueOf(v)) { bitmap |= isNilFlag @@ -310,6 +317,15 @@ func (f *Fury) Unmarshal(data []byte, v interface{}) error { func (f *Fury) Deserialize(buf *ByteBuffer, v interface{}, buffers []*ByteBuffer) error { defer f.resetRead() + if f.language == XLANG { + magicNumber := buf.ReadInt16() + if magicNumber != MAGIC_NUMBER { + return fmt.Errorf("the XLANG cross-language protocol is not currently used. For details," + + "see https://github.com/apache/incubator-fury/blob/main/docs/specification/xlang_serialization_spec.md") + } + } else { + return fmt.Errorf("%d language is not supported", f.language) + } var bitmap = buf.ReadByte_() if bitmap&isNilFlag == isNilFlag { return nil diff --git a/go/fury/fury_test.go b/go/fury/fury_test.go index 5e80db1f03..a6c4d834fe 100644 --- a/go/fury/fury_test.go +++ b/go/fury/fury_test.go @@ -243,6 +243,18 @@ func TestSerializeStructSimple(t *testing.T) { } } +func TestSerializeBeginWithMagicNumber(t *testing.T) { + strSlice := []string{"str1", "str1", "", "", "str2"} + fury := NewFury(true) + bytes, err := fury.Marshal(strSlice) + require.Nil(t, err, fmt.Sprintf("serialize value %s with type %s failed: %s", + reflect.ValueOf(strSlice), reflect.TypeOf(strSlice), err)) + // Contains at least two bytes. + require.True(t, len(bytes) > 2) + magicNumber := int16(bytes[0]) | (int16(bytes[1]) << 8) + require.Equal(t, magicNumber, MAGIC_NUMBER) +} + type Foo struct { F1 int32 F2 string