diff options
author | Ayush Ranjan <ayushranjan@google.com> | 2021-03-22 19:15:48 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-03-22 19:17:49 -0700 |
commit | c0bd71c5a596af6cf7f05a712232464623da0ba9 (patch) | |
tree | bf0d412f98f69dd163fc038258f23df9b19368f0 /tools/go_marshal/test/marshal_test.go | |
parent | 9e86dfc9c5b56eaa91485826bcf3f1f7617d2eb0 (diff) |
[lisa] Support dynamic types for all types.
We were only supporting dynamic struct types. With this change, users can make
any type dynamic. The tool (correctly) blindly just generates the remaining
methods needed to implement Marshallable using the 3 methods defined by the
user on the dynamic type.
This is helpful in situations like:
type StringArray []string
Added a test for such a use case.
PiperOrigin-RevId: 364463164
Diffstat (limited to 'tools/go_marshal/test/marshal_test.go')
-rw-r--r-- | tools/go_marshal/test/marshal_test.go | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/tools/go_marshal/test/marshal_test.go b/tools/go_marshal/test/marshal_test.go index b0091dc64..733689c79 100644 --- a/tools/go_marshal/test/marshal_test.go +++ b/tools/go_marshal/test/marshal_test.go @@ -515,20 +515,39 @@ func TestLimitedSliceMarshalling(t *testing.T) { } } -func TestDynamicType(t *testing.T) { +func TestDynamicTypeStruct(t *testing.T) { t12 := test.Type12Dynamic{ X: 32, Y: []primitive.Int64{5, 6, 7}, } + var cc mockCopyContext + cc.setLimit(t12.SizeBytes()) - var m marshal.Marshallable - m = &t12 // Ensure that all methods were generated. - b := make([]byte, m.SizeBytes()) - m.MarshalBytes(b) + if _, err := t12.CopyOut(&cc, usermem.Addr(0)); err != nil { + t.Fatalf("cc.CopyOut faile: %v", err) + } - var res test.Type12Dynamic - res.UnmarshalBytes(b) + res := test.Type12Dynamic{ + Y: make([]primitive.Int64, len(t12.Y)), + } + res.CopyIn(&cc, usermem.Addr(0)) if !reflect.DeepEqual(t12, res) { t.Errorf("dynamic type is not same after marshalling and unmarshalling: before = %+v, after = %+v", t12, res) } } + +func TestDynamicTypeIdentifier(t *testing.T) { + s := test.Type13Dynamic("go_marshal") + var cc mockCopyContext + cc.setLimit(s.SizeBytes()) + + if _, err := s.CopyOut(&cc, usermem.Addr(0)); err != nil { + t.Fatalf("cc.CopyOut faile: %v", err) + } + + res := test.Type13Dynamic(make([]byte, len(s))) + res.CopyIn(&cc, usermem.Addr(0)) + if res != s { + t.Errorf("dynamic type is not same after marshalling and unmarshalling: before = %s, after = %s", s, res) + } +} |