github.com/braveheart12/insolar-09-08-19@v0.8.7/ledger/storage/record/hash_test.go (about)

     1  /*
     2   *    Copyright 2019 Insolar Technologies
     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 record
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/insolar/insolar/core"
    25  	"github.com/insolar/insolar/platformpolicy"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  type recordgen func() Record
    30  
    31  var emptyRecordsGens = []recordgen{
    32  	// request records
    33  	func() Record { return &RequestRecord{} },
    34  	// result records
    35  	func() Record { return &ObjectActivateRecord{} },
    36  	func() Record { return &CodeRecord{} },
    37  	func() Record { return &DeactivationRecord{} },
    38  	func() Record { return &ObjectAmendRecord{} },
    39  	func() Record { return &TypeRecord{} },
    40  	func() Record { return &ChildRecord{} },
    41  	func() Record { return &GenesisRecord{} },
    42  }
    43  
    44  func getRecordHashData(rec Record) []byte {
    45  	buff := bytes.NewBuffer(nil)
    46  	rec.WriteHashData(buff)
    47  	return buff.Bytes()
    48  }
    49  
    50  func Test_HashesNotTheSameOnDifferentTypes(t *testing.T) {
    51  	found := make(map[string]string)
    52  	for _, recFn := range emptyRecordsGens {
    53  		rec := recFn()
    54  		recType := fmt.Sprintf("%T", rec)
    55  		hashHex := fmt.Sprintf("%x", getRecordHashData(rec))
    56  		// fmt.Println(recType, "=>", hashHex)
    57  		typename, ok := found[hashHex]
    58  		if !ok {
    59  			found[hashHex] = recType
    60  			continue
    61  		}
    62  		t.Errorf("same hashes for %s and %s types, empty struct with different types should not be the same", recType, typename)
    63  	}
    64  }
    65  
    66  func Test_HashesTheSame(t *testing.T) {
    67  	hashes := make([]string, len(emptyRecordsGens))
    68  	for i, recFn := range emptyRecordsGens {
    69  		rec := recFn()
    70  		hashHex := fmt.Sprintf("%x", getRecordHashData(rec))
    71  		hashes[i] = hashHex
    72  	}
    73  
    74  	// same struct with different should produce the same hashes
    75  	for i, recFn := range emptyRecordsGens {
    76  		rec := recFn()
    77  		hashHex := fmt.Sprintf("%x", getRecordHashData(rec))
    78  		assert.Equal(t, hashes[i], hashHex)
    79  	}
    80  }
    81  
    82  var pcs = platformpolicy.NewPlatformCryptographyScheme()
    83  var hashtestsRecordsMutate = []struct {
    84  	typ     string
    85  	records []Record
    86  }{
    87  	{
    88  		"CodeRecord",
    89  		[]Record{
    90  			&CodeRecord{},
    91  			&CodeRecord{Code: CalculateIDForBlob(pcs, core.GenesisPulse.PulseNumber, []byte{1, 2, 3})},
    92  			&CodeRecord{
    93  				Code: CalculateIDForBlob(pcs, core.GenesisPulse.PulseNumber, []byte{1, 2, 3}),
    94  				SideEffectRecord: SideEffectRecord{
    95  					Domain: core.RecordRef{1, 2, 3},
    96  				},
    97  			},
    98  		},
    99  	},
   100  }
   101  
   102  func Test_CBORhashesMutation(t *testing.T) {
   103  	for _, tt := range hashtestsRecordsMutate {
   104  		found := make(map[string]string)
   105  		for _, rec := range tt.records {
   106  			h := getRecordHashData(rec)
   107  			hHex := fmt.Sprintf("%x", h)
   108  
   109  			typ, ok := found[hHex]
   110  			if !ok {
   111  				found[hHex] = tt.typ
   112  				continue
   113  			}
   114  			t.Errorf("%s failed: found %s hash for \"%s\" test", tt.typ, hHex, typ)
   115  		}
   116  	}
   117  }