github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/types/logroot_test.go (about)

     1  // Copyright 2017 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  
    23  func TestLogRoot(t *testing.T) {
    24  	for _, logRoot := range []interface {
    25  		encoding.BinaryMarshaler
    26  		encoding.BinaryUnmarshaler
    27  	}{
    28  		&LogRootV1{
    29  			RootHash: []byte("foo"),
    30  			Metadata: []byte{},
    31  		},
    32  	} {
    33  		b, err := logRoot.MarshalBinary()
    34  		if err != nil {
    35  			t.Errorf("%v MarshalBinary(): %v", logRoot, err)
    36  			continue
    37  		}
    38  		var got LogRootV1
    39  		if err := got.UnmarshalBinary(b); err != nil {
    40  			t.Errorf("UnmarshalBinary(): %v", err)
    41  			continue
    42  		}
    43  		if !reflect.DeepEqual(&got, logRoot) {
    44  			t.Errorf("serialize/parse round trip failed. got %#v, want %#v", got, logRoot)
    45  		}
    46  	}
    47  }
    48  
    49  func TestUnmarshalLogRoot(t *testing.T) {
    50  	for _, tc := range []struct {
    51  		logRoot []byte
    52  		wantErr bool
    53  	}{
    54  		{logRoot: MustMarshalLogRoot(&LogRootV1{})},
    55  		{
    56  			logRoot: func() []byte {
    57  				b := MustMarshalLogRoot(&LogRootV1{})
    58  				b[0] = 1 // Corrupt the version tag.
    59  				return b
    60  			}(),
    61  			wantErr: true,
    62  		},
    63  		{
    64  			// Correct type, but junk afterwards.
    65  			logRoot: []byte{0, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
    66  			wantErr: true,
    67  		},
    68  		{
    69  			// Incorrect type.
    70  			logRoot: []byte{0},
    71  			wantErr: true,
    72  		},
    73  		{logRoot: []byte("foo"), wantErr: true},
    74  		{logRoot: nil, wantErr: true},
    75  	} {
    76  
    77  		var got LogRootV1
    78  		err := got.UnmarshalBinary(tc.logRoot)
    79  		if got, want := err != nil, tc.wantErr; got != want {
    80  			t.Errorf("UnmarshalBinary(): %v, wantErr %v", err, want)
    81  		}
    82  	}
    83  
    84  	// Unmarshaling to a nil should throw an error.
    85  	var nilPtr *LogRootV1
    86  	if err := nilPtr.UnmarshalBinary(MustMarshalLogRoot(&LogRootV1{})); err == nil {
    87  		t.Errorf("nil.UnmarshalBinary(): %v, want err", err)
    88  	}
    89  }
    90  
    91  func MustMarshalLogRoot(root *LogRootV1) []byte {
    92  	b, err := root.MarshalBinary()
    93  	if err != nil {
    94  		panic(err)
    95  	}
    96  	return b
    97  }
    98  
    99  func TestKeyHint(t *testing.T) {
   100  	for _, tc := range []struct {
   101  		hint    []byte
   102  		want    int64
   103  		wantErr bool
   104  	}{
   105  		{hint: SerializeKeyHint(4), want: 4},
   106  		{hint: SerializeKeyHint(3561657513447883733), want: 3561657513447883733},
   107  		{hint: []byte{0, 0, 0, 0, 0, 0, 0, 4}, want: 4},
   108  		{hint: []byte{0xff, 0, 0, 0, 0, 0, 4}, want: 0, wantErr: true},    // Integer overflow
   109  		{hint: []byte{0, 0, 0, 0, 0, 0, 0, 4, 0}, want: 0, wantErr: true}, // Wrong byte len
   110  	} {
   111  		logID, err := ParseKeyHint(tc.hint)
   112  		if got, want := err != nil, tc.wantErr; got != want {
   113  			t.Errorf("ParseKeyHint(%v): %v, wantErr: %v", tc.hint, err, want)
   114  		}
   115  		if got, want := logID, tc.want; got != want {
   116  			t.Errorf("ParseKeyHint(%v): %v, want: %v", tc.hint, got, want)
   117  		}
   118  	}
   119  }