github.com/zorawar87/trillian@v1.2.1/merkle/rfc6962/rfc6962_test.go (about)

     1  // Copyright 2016 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  package rfc6962
    15  
    16  import (
    17  	"bytes"
    18  	"encoding/hex"
    19  	"testing"
    20  
    21  	_ "github.com/golang/glog"
    22  )
    23  
    24  func TestRFC6962Hasher(t *testing.T) {
    25  	hasher := DefaultHasher
    26  
    27  	leafHash, err := hasher.HashLeaf([]byte("L123456"))
    28  	if err != nil {
    29  		t.Fatalf("HashLeaf(): %v", err)
    30  	}
    31  	emptyLeafHash, err := hasher.HashLeaf([]byte{})
    32  	if err != nil {
    33  		t.Fatalf("HashLeaf(empty): %v", err)
    34  	}
    35  
    36  	for _, tc := range []struct {
    37  		desc string
    38  		got  []byte
    39  		want string
    40  	}{
    41  		// echo -n | sha256sum
    42  		{
    43  			desc: "RFC6962 Empty",
    44  			want: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
    45  			got:  hasher.EmptyRoot(),
    46  		},
    47  		// Check that the empty hash is not the same as the hash of an empty leaf.
    48  		// echo -n 00 | xxd -r -p | sha256sum
    49  		{
    50  			desc: "RFC6962 Empty Leaf",
    51  			want: "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d",
    52  			got:  emptyLeafHash,
    53  		},
    54  		// echo -n 004C313233343536 | xxd -r -p | sha256sum
    55  		{
    56  			desc: "RFC6962 Leaf",
    57  			want: "395aa064aa4c29f7010acfe3f25db9485bbd4b91897b6ad7ad547639252b4d56",
    58  			got:  leafHash,
    59  		},
    60  		// echo -n 014E3132334E343536 | xxd -r -p | sha256sum
    61  		{
    62  			desc: "RFC6962 Node",
    63  			want: "aa217fe888e47007fa15edab33c2b492a722cb106c64667fc2b044444de66bbb",
    64  			got:  hasher.HashChildren([]byte("N123"), []byte("N456")),
    65  		},
    66  	} {
    67  		t.Run(tc.desc, func(t *testing.T) {
    68  			wantBytes, err := hex.DecodeString(tc.want)
    69  			if err != nil {
    70  				t.Fatalf("hex.DecodeString(%x): %v", tc.want, err)
    71  			}
    72  			if got, want := tc.got, wantBytes; !bytes.Equal(got, want) {
    73  				t.Errorf("got %x, want %x", got, want)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  // TODO(pavelkalinnikov): Apply this test to all LogHasher implementations.
    80  func TestRFC6962HasherCollisions(t *testing.T) {
    81  	hasher := DefaultHasher
    82  
    83  	// Check that different leaves have different hashes.
    84  	leaf1, leaf2 := []byte("Hello"), []byte("World")
    85  	hash1, _ := hasher.HashLeaf(leaf1)
    86  	hash2, _ := hasher.HashLeaf(leaf2)
    87  	if bytes.Equal(hash1, hash2) {
    88  		t.Errorf("Leaf hashes should differ, but both are %x", hash1)
    89  	}
    90  
    91  	// Compute an intermediate subtree hash.
    92  	subHash1 := hasher.HashChildren(hash1, hash2)
    93  	// Check that this is not the same as a leaf hash of their concatenation.
    94  	preimage := append(hash1, hash2...)
    95  	forgedHash, _ := hasher.HashLeaf(preimage)
    96  	if bytes.Equal(subHash1, forgedHash) {
    97  		t.Errorf("Hasher is not second-preimage resistant")
    98  	}
    99  
   100  	// Swap the order of nodes and check that the hash is different.
   101  	subHash2 := hasher.HashChildren(hash2, hash1)
   102  	if bytes.Equal(subHash1, subHash2) {
   103  		t.Errorf("Subtree hash does not depend on the order of leaves")
   104  	}
   105  }