certichain

Blockchain built from scratch for verifying document's legitimacy.
Log | Files | Refs | README | LICENSE

tree_test.go (1388B)


      1 // Copyright 2026 Shivakumar (Certichain)
      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 merkle
     16 
     17 import (
     18 	"bytes"
     19 	"strconv"
     20 	"testing"
     21 )
     22 
     23 func TestTree_RootReturnCopy(t *testing.T) {
     24 	t.Parallel()
     25 
     26 	data := [][]byte{
     27 		[]byte("certichain"),
     28 	}
     29 
     30 	tree := mustTree(t, data)
     31 
     32 	root1 := tree.Root()
     33 	root2 := tree.Root()
     34 
     35 	if !bytes.Equal(root1, root2) {
     36 		t.Fatal("Root() returned inconsistent root hashes")
     37 	}
     38 
     39 	root1[0] ^= 0xff
     40 
     41 	if bytes.Equal(root1, root2) {
     42 		t.Fatal("Root() did not return a defensive copy")
     43 	}
     44 }
     45 
     46 func TestTree_LeafCount(t *testing.T) {
     47 	t.Parallel()
     48 
     49 	const leafCount = 10_000
     50 
     51 	data := make([][]byte, leafCount)
     52 	for i := range data {
     53 		data[i] = []byte(strconv.Itoa(i))
     54 	}
     55 
     56 	tree := mustTree(t, data)
     57 
     58 	if got := tree.LeafCount(); got != leafCount {
     59 		t.Fatalf(
     60 			"unexpected leaf count: got = %x\nwant = %x",
     61 			got,
     62 			leafCount,
     63 		)
     64 	}
     65 }