github.com/vmware/govmomi@v0.51.0/vim25/types/byte_slice_test.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package types 6 7 import ( 8 "bytes" 9 "testing" 10 11 "github.com/vmware/govmomi/vim25/xml" 12 ) 13 14 func TestByteSlice(t *testing.T) { 15 in := &ArrayOfByte{ 16 Byte: []byte("xmhell"), 17 } 18 19 res, err := xml.Marshal(in) 20 if err != nil { 21 t.Fatal(err) 22 } 23 24 var out ArrayOfByte 25 if err := xml.Unmarshal(res, &out); err != nil { 26 t.Logf("%s", string(res)) 27 t.Fatal(err) 28 } 29 30 if !bytes.Equal(in.Byte, out.Byte) { 31 t.Errorf("out=%#v", out.Byte) 32 } 33 } 34 35 func TestSignedByteSlice(t *testing.T) { 36 // int8: <byte>4</byte><byte>-80</byte><byte>-79</byte><byte>-78</byte> 37 // uint8: <byte>4</byte><byte>176</byte><byte>177</byte><byte>178</byte> 38 in := &ArrayOfByte{ 39 Byte: []uint8{0x4, 0xb0, 0xb1, 0xb2}, 40 } 41 42 res, err := xml.Marshal(in) 43 if err != nil { 44 t.Fatal(err) 45 } 46 47 var out struct { 48 Byte []int8 `xml:"byte,omitempty" json:"_value"` 49 } 50 51 // ByteSlice.MarshalXML must output signed byte, otherwise this fails with: 52 // strconv.ParseInt: parsing "176": value out of range 53 if err := xml.Unmarshal(res, &out); err != nil { 54 t.Logf("%s", string(res)) 55 t.Fatal(err) 56 } 57 58 for i := range in.Byte { 59 if in.Byte[i] != byte(out.Byte[i]) { 60 t.Errorf("out=%x", out.Byte[i]) 61 } 62 } 63 }