proof_test.go (3181B)
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 "fmt" 19 "strconv" 20 "testing" 21 22 "certichain/internal/crypto" 23 ) 24 25 // TestGenerateProof_InvalidIndex checks the out of bound 26 // indexes. 27 func TestGenerateProof_InvalidIndex(t *testing.T) { 28 t.Parallel() 29 30 data := makeLeaves(3) 31 tree := mustTree(t, data) 32 33 tests := []struct { 34 name string 35 index int 36 }{ 37 {"negative index", -1}, 38 {"out of index", 3}, 39 {"equal to length", len(data)}, 40 } 41 42 for _, tt := range tests { 43 t.Run(tt.name, func(t *testing.T) { 44 t.Parallel() 45 46 _, err := tree.GenerateProof(tt.index) 47 if err == nil { 48 t.Fatal("expected error, got nil") 49 } 50 }) 51 } 52 } 53 54 func TestGenerateProof_ValidateIndex(t *testing.T) { 55 t.Parallel() 56 57 data := makeLeaves(3) 58 tree := mustTree(t, data) 59 60 for i := range data { 61 t.Run(fmt.Sprintf("leaf-%d", i), func(t *testing.T) { 62 t.Parallel() 63 64 proof := mustProof(t, tree, i) 65 66 if proof == nil { 67 t.Fatal("proof is nil") 68 } 69 70 ok, err := Verify(data[i], proof, tree.Root()) 71 if err != nil { 72 t.Fatalf("Verify() error = %v", err) 73 } 74 75 if !ok { 76 t.Fatal("proof verification failed") 77 } 78 79 if proof.Index != i { 80 t.Fatalf( 81 "wrong proof index: got %d want %d", 82 proof.Index, 83 i, 84 ) 85 } 86 }) 87 } 88 } 89 90 // TestVerify_InvalidInput checks that Verify rejects all type 91 // of malformed input. 92 func TestVerify_InvalidInput(t *testing.T) { 93 t.Parallel() 94 95 tree := mustTree(t, makeLeaves(4)) 96 validleaf := []byte(strconv.Itoa(0)) 97 validProof := mustProof(t, tree, 0) 98 validRoot := tree.Root() 99 100 tests := []struct { 101 name string 102 leafData []byte 103 proof *Proof 104 root []byte 105 }{ 106 { 107 name: "nil leaf data", 108 leafData: nil, 109 proof: validProof, 110 root: validRoot, 111 }, 112 { 113 name: "nil proof", 114 leafData: validleaf, 115 proof: nil, 116 root: validRoot, 117 }, 118 { 119 name: "nil root", 120 leafData: validleaf, 121 proof: validProof, 122 root: nil, 123 }, 124 { 125 name: "empty root", 126 leafData: validleaf, 127 proof: validProof, 128 root: []byte{}, 129 }, 130 { 131 name: "invalid root size (long)", 132 leafData: validleaf, 133 proof: validProof, 134 root: make([]byte, crypto.HashSize+1), 135 }, 136 { 137 name: "invalid root size (short)", 138 leafData: validleaf, 139 proof: validProof, 140 root: make([]byte, crypto.HashSize-1), 141 }, 142 } 143 144 for _, tt := range tests { 145 t.Run(tt.name, func(t *testing.T) { 146 t.Parallel() 147 148 ok, err := Verify(tt.leafData, tt.proof, tt.root) 149 if err == nil { 150 t.Fatal("expected error, got nil") 151 } 152 if ok { 153 t.Fatal("expected false, for true") 154 } 155 }) 156 } 157 }