github.com/zorawar87/trillian@v1.2.1/client/log_verifier_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 client 16 17 import ( 18 "crypto" 19 "testing" 20 21 "github.com/google/trillian" 22 "github.com/google/trillian/crypto/keys/pem" 23 "github.com/google/trillian/merkle/rfc6962" 24 "github.com/google/trillian/testonly" 25 "github.com/google/trillian/types" 26 27 tcrypto "github.com/google/trillian/crypto" 28 ) 29 30 func TestVerifyRootErrors(t *testing.T) { 31 // Test setup 32 key, err := pem.UnmarshalPrivateKey(testonly.DemoPrivateKey, testonly.DemoPrivateKeyPass) 33 if err != nil { 34 t.Fatalf("Failed to open test key, err=%v", err) 35 } 36 signer := tcrypto.NewSigner(0, key, crypto.SHA256) 37 pk, err := pem.UnmarshalPublicKey(testonly.DemoPublicKey) 38 if err != nil { 39 t.Fatalf("Failed to load public key, err=%v", err) 40 } 41 42 signedRoot, err := signer.SignLogRoot(&types.LogRootV1{}) 43 if err != nil { 44 t.Fatal("Failed to create test signature") 45 } 46 47 // Test execution 48 tests := []struct { 49 desc string 50 trusted *types.LogRootV1 51 newRoot *trillian.SignedLogRoot 52 }{ 53 {desc: "newRootNil", trusted: &types.LogRootV1{}, newRoot: nil}, 54 {desc: "trustedNil", trusted: nil, newRoot: signedRoot}, 55 } 56 for _, test := range tests { 57 logVerifier := NewLogVerifier(rfc6962.DefaultHasher, pk, crypto.SHA256) 58 59 // This also makes sure that no nil pointer dereference errors occur (as this would cause a panic). 60 if _, err := logVerifier.VerifyRoot(test.trusted, test.newRoot, nil); err == nil { 61 t.Errorf("%v: VerifyRoot() error expected, but got nil", test.desc) 62 } 63 } 64 } 65 66 func TestVerifyInclusionAtIndexErrors(t *testing.T) { 67 logVerifier := NewLogVerifier(nil, nil, crypto.SHA256) 68 // An error is expected because the first parameter (trusted) is nil 69 err := logVerifier.VerifyInclusionAtIndex(nil, []byte{0, 0, 0}, 1, [][]byte{{0, 0}}) 70 if err == nil { 71 t.Errorf("VerifyInclusionAtIndex() error expected, but got nil") 72 } 73 } 74 75 func TestVerifyInclusionByHashErrors(t *testing.T) { 76 tests := []struct { 77 desc string 78 trusted *types.LogRootV1 79 proof *trillian.Proof 80 }{ 81 {desc: "trustedNil", trusted: nil, proof: &trillian.Proof{}}, 82 {desc: "proofNil", trusted: &types.LogRootV1{}, proof: nil}, 83 } 84 for _, test := range tests { 85 86 logVerifier := NewLogVerifier(nil, nil, crypto.SHA256) 87 err := logVerifier.VerifyInclusionByHash(test.trusted, nil, test.proof) 88 if err == nil { 89 t.Errorf("%v: VerifyInclusionByHash() error expected, but got nil", test.desc) 90 } 91 } 92 }