github.com/vmware/govmomi@v0.37.2/toolbox/vix/property_test.go (about)

     1  /*
     2  Copyright (c) 2017 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vix
    18  
    19  import (
    20  	"encoding/base64"
    21  	"encoding/binary"
    22  	"math"
    23  	"reflect"
    24  	"testing"
    25  )
    26  
    27  func TestToolsStateProperties(t *testing.T) {
    28  	// captured from vmtoolsd
    29  	str := `lxEAAAIAAAAoAAAATGludXggNC40LjAtMjEtZ2VuZXJpYyBVYnVudHUgMTYuMDQgTFRTAKgRAAACAAAACgAAAHVidW50dS02NACfEQAAAgAAAA0AAABWTXdhcmUgVG9vbHMAlBEAAAIAAAAVAAAAMTAuMC41IGJ1aWxkLTMyMjc4NzIAmREAAAIAAAATAAAAdWJ1bnR1LTE2MDQtdm13YXJlAJURAAABAAAABAAAAAEAAACWEQAAAQAAAAQAAAABAAAAmBEAAAIAAAABAAAAAMsAAAACAAAAEQAAAC90bXAvdm13YXJlLXJvb3QApxEAAAEAAAAEAAAAQAAAAK0RAAACAAAACgAAAC9tbnQvaGdmcwC8EQAAAwAAAAEAAAAAvREAAAMAAAABAAAAAL4RAAADAAAAAQAAAAC/EQAAAwAAAAEAAAAAwBEAAAMAAAABAAAAAMERAAADAAAAAQAAAADCEQAAAwAAAAEAAAAAwxEAAAMAAAABAAAAAMQRAAADAAAAAQAAAADFEQAAAwAAAAEAAAAAxhEAAAMAAAABAAAAAMcRAAADAAAAAQAAAADIEQAAAwAAAAEAAAAAyREAAAMAAAABAAAAAMoRAAADAAAAAQAAAADLEQAAAwAAAAEAAAAAzBEAAAMAAAABAAAAAM0RAAADAAAAAQAAAADOEQAAAwAAAAEAAAAAzxEAAAMAAAABAAAAANARAAADAAAAAQAAAADREQAAAwAAAAEAAAAA0hEAAAMAAAABAAAAANMRAAADAAAAAQAAAADUEQAAAwAAAAEAAAAA1REAAAMAAAABAAAAANYRAAADAAAAAQAAAADXEQAAAwAAAAEAAAAA2BEAAAMAAAABAAAAAA==`
    30  
    31  	data, err := base64.StdEncoding.DecodeString(str)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	var props PropertyList
    37  
    38  	err = props.UnmarshalBinary(data)
    39  	if err != nil {
    40  		t.Error(err)
    41  	}
    42  
    43  	data2, err := props.MarshalBinary()
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	str2 := base64.StdEncoding.EncodeToString(data2)
    49  	if str != str2 {
    50  		t.Error("encoding mismatch")
    51  	}
    52  }
    53  
    54  func TestMarshalProperties(t *testing.T) {
    55  	in := PropertyList{
    56  		NewInt32Property(1, math.MaxInt32),
    57  		NewStringProperty(2, "foo"),
    58  		NewBoolProperty(3, true),
    59  		NewInt64Property(4, math.MaxInt64),
    60  		NewBlobProperty(5, []byte("deadbeef")),
    61  	}
    62  
    63  	buf, err := in.MarshalBinary()
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  
    68  	var out PropertyList
    69  
    70  	err = out.UnmarshalBinary(buf)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	if !reflect.DeepEqual(in, out) {
    76  		t.Error("marshal mismatch")
    77  	}
    78  
    79  }
    80  
    81  // hit unmarshal error paths
    82  func TestUnmarshalPropertiesErrors(t *testing.T) {
    83  	props := PropertyList{
    84  		NewBoolProperty(1, true),
    85  		NewStringProperty(1, "foo"),
    86  		NewBlobProperty(2, []byte("deadbeef")),
    87  	}
    88  
    89  	props[0].header.Kind = 0xff
    90  
    91  	for _, prop := range props {
    92  		buf, _ := prop.MarshalBinary()
    93  
    94  		for i, l := range []int{1, binary.Size(prop.header)} {
    95  			err := prop.UnmarshalBinary(buf[:l])
    96  			if err == nil {
    97  				t.Errorf("test %d (len=%d) expected error", i, l)
    98  			}
    99  		}
   100  	}
   101  
   102  	buf, _ := props.MarshalBinary()
   103  	err := props.UnmarshalBinary(buf)
   104  	if err == nil {
   105  		t.Error("expected error")
   106  	}
   107  }