github.com/mre-fog/trillianxx@v1.1.2-0.20180615153820-ae375a99d36a/merkle/objhasher/objhasher_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 objhasher
    16  
    17  import (
    18  	"bytes"
    19  	"crypto"
    20  	"encoding/hex"
    21  	"testing"
    22  
    23  	"github.com/google/trillian/merkle/maphasher"
    24  	"github.com/google/trillian/merkle/rfc6962"
    25  )
    26  
    27  const treeID = int64(0)
    28  
    29  func TestLeafHash(t *testing.T) {
    30  	h := NewLogHasher(rfc6962.New(crypto.SHA256))
    31  
    32  	for _, tc := range []struct {
    33  		json []byte
    34  		want string
    35  	}{
    36  		{
    37  			// Verify that ordering does not affect output hash.
    38  			json: []byte(`{"k1":"v1","k2":"v2","k3":"v3"}`),
    39  			want: "ddd65f1f7568269a30df7cafc26044537dc2f02a1a0d830da61762fc3e687057",
    40  		},
    41  		{
    42  			// Same values, different order.
    43  			json: []byte(`{"k2":"v2","k1":"v1","k3":"v3"}`),
    44  			want: "ddd65f1f7568269a30df7cafc26044537dc2f02a1a0d830da61762fc3e687057",
    45  		},
    46  	} {
    47  		leaf, err := h.HashLeaf(tc.json)
    48  		if err != nil {
    49  			t.Errorf("HashLeaf(%v): %v", tc.json, err)
    50  		}
    51  		if got := hex.EncodeToString(leaf); got != tc.want {
    52  			t.Errorf("HashLeaf(%v): \n%v, want \n%v", tc.json, got, tc.want)
    53  		}
    54  	}
    55  }
    56  
    57  func TestHashEmpty(t *testing.T) {
    58  	h := NewMapHasher(maphasher.New(crypto.SHA256))
    59  	rfc := maphasher.New(crypto.SHA256)
    60  
    61  	if got, want := h.HashEmpty(treeID, nil, 0), rfc.HashEmpty(treeID, nil, 0); !bytes.Equal(got, want) {
    62  		t.Errorf("HashEmpty():\n%x, want\n%x", got, want)
    63  	}
    64  }
    65  
    66  func TestHashChildren(t *testing.T) {
    67  	h := NewMapHasher(maphasher.New(crypto.SHA256))
    68  	rfc := maphasher.New(crypto.SHA256)
    69  
    70  	for _, tc := range []struct {
    71  		r, l []byte
    72  	}{
    73  		{
    74  			r: []byte("a"), l: []byte("b"),
    75  		},
    76  	} {
    77  		if got, want := h.HashChildren(tc.r, tc.l), rfc.HashChildren(tc.r, tc.l); !bytes.Equal(got, want) {
    78  			t.Errorf("HashChildren(%x, %x):\n%x, want\n%x", tc.r, tc.l, got, want)
    79  		}
    80  	}
    81  }