github.com/mre-fog/trillianxx@v1.1.2-0.20180615153820-ae375a99d36a/types/maproot_test.go (about)

     1  // Copyright 2018 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package types
    16  
    17  import (
    18  	"encoding"
    19  	"reflect"
    20  	"testing"
    21  
    22  	_ "github.com/golang/glog" // Don't crash when --logtostderr is supplied
    23  )
    24  
    25  func TestMapRoot(t *testing.T) {
    26  	for _, mapRoot := range []interface {
    27  		encoding.BinaryMarshaler
    28  		encoding.BinaryUnmarshaler
    29  	}{
    30  		&MapRootV1{
    31  			RootHash: []byte("foo"),
    32  			Metadata: []byte{},
    33  		},
    34  	} {
    35  		b, err := mapRoot.MarshalBinary()
    36  		if err != nil {
    37  			t.Errorf("%v MarshalBinary(): %v", mapRoot, err)
    38  		}
    39  		var got MapRootV1
    40  		if err := got.UnmarshalBinary(b); err != nil {
    41  			t.Errorf("UnmarshalBinary(): %v", err)
    42  		}
    43  		if !reflect.DeepEqual(&got, mapRoot) {
    44  			t.Errorf("serialize/parse round trip failed. got %#v, want %#v", got, mapRoot)
    45  		}
    46  	}
    47  }
    48  
    49  func MustMarshalMapRoot(root *MapRootV1) []byte {
    50  	b, err := root.MarshalBinary()
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  	return b
    55  }
    56  
    57  func TestUnmarshalMapRoot(t *testing.T) {
    58  	for _, tc := range []struct {
    59  		mapRoot []byte
    60  		want    MapRootV1
    61  		wantErr bool
    62  	}{
    63  		{
    64  			mapRoot: MustMarshalMapRoot(&MapRootV1{
    65  				RootHash: []byte("aaa"),
    66  			}),
    67  			want: MapRootV1{
    68  				RootHash: []byte("aaa"),
    69  				Metadata: []byte{},
    70  			},
    71  		},
    72  		{
    73  			// Correct type, but junk afterwards.
    74  			mapRoot: []byte{0, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
    75  			wantErr: true,
    76  		},
    77  		{
    78  			// Incorrect type
    79  			mapRoot: []byte{0},
    80  			wantErr: true,
    81  		},
    82  		{mapRoot: []byte("foo"), wantErr: true},
    83  		{mapRoot: nil, wantErr: true},
    84  	} {
    85  		var r MapRootV1
    86  		err := r.UnmarshalBinary(tc.mapRoot)
    87  		if got, want := err != nil, tc.wantErr; got != want {
    88  			t.Errorf("UnmarshalBinary(): %v, wantErr: %v", err, want)
    89  		}
    90  		if got, want := r, tc.want; !reflect.DeepEqual(got, want) {
    91  			t.Errorf("UnmarshalBinary(): \n%#v, want: \n%#v", got, want)
    92  		}
    93  	}
    94  	// Unmarshaling to a nil should throw an error.
    95  	var nilPtr *MapRootV1
    96  	if err := nilPtr.UnmarshalBinary(MustMarshalMapRoot(&MapRootV1{})); err == nil {
    97  		t.Errorf("nil.UnmarshalBinary(): %v, want err", err)
    98  	}
    99  }