1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
// Copyright 2017 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"context"
"math/rand"
"github.com/google/trillian"
"github.com/google/trillian/types"
"google.golang.org/grpc"
"k8s.io/klog/v2"
)
// MutatingLogClient supports applying mutations to the return values of the TrillianLogClient
// for testing.
type MutatingLogClient struct {
trillian.TrillianLogClient
mutateInclusionProof bool
mutateConsistencyProof bool
mutateRootSize bool
}
// GetLatestSignedLogRoot forwards requests and optionally modifies the returned size.
func (c *MutatingLogClient) GetLatestSignedLogRoot(ctx context.Context, in *trillian.GetLatestSignedLogRootRequest, opts ...grpc.CallOption) (*trillian.GetLatestSignedLogRootResponse, error) {
resp, err := c.TrillianLogClient.GetLatestSignedLogRoot(ctx, in)
if c.mutateRootSize {
var root types.LogRootV1
if err := root.UnmarshalBinary(resp.SignedLogRoot.LogRoot); err != nil {
panic("failed to unmarshal")
}
root.TreeSize += 10000
resp.SignedLogRoot.LogRoot, _ = root.MarshalBinary()
}
return resp, err
}
// GetInclusionProof forwards requests and optionally corrupts the response.
func (c *MutatingLogClient) GetInclusionProof(ctx context.Context, in *trillian.GetInclusionProofRequest, opts ...grpc.CallOption) (*trillian.GetInclusionProofResponse, error) {
resp, err := c.TrillianLogClient.GetInclusionProof(ctx, in)
if c.mutateInclusionProof {
i := rand.Intn(len(resp.Proof.Hashes))
j := rand.Intn(len(resp.Proof.Hashes[i]))
resp.Proof.Hashes[i][j] ^= 4
}
return resp, err
}
// GetInclusionProofByHash forwards requests and optionaly corrupts responses.
func (c *MutatingLogClient) GetInclusionProofByHash(ctx context.Context, in *trillian.GetInclusionProofByHashRequest, opts ...grpc.CallOption) (*trillian.GetInclusionProofByHashResponse, error) {
resp, err := c.TrillianLogClient.GetInclusionProofByHash(ctx, in)
if err != nil {
return nil, err
}
if c.mutateInclusionProof {
h := rand.Intn(len(resp.Proof))
if len(resp.Proof[h].Hashes) == 0 {
klog.Warningf("Inclusion proof not modified because treesize = 0")
return resp, nil
}
i := rand.Intn(len(resp.Proof[h].Hashes))
j := rand.Intn(len(resp.Proof[h].Hashes[i]))
resp.Proof[h].Hashes[i][j] ^= 4
}
return resp, nil
}
// GetConsistencyProof forwards requests and optionally corrupts responses.
func (c *MutatingLogClient) GetConsistencyProof(ctx context.Context, in *trillian.GetConsistencyProofRequest, opts ...grpc.CallOption) (*trillian.GetConsistencyProofResponse, error) {
resp, err := c.TrillianLogClient.GetConsistencyProof(ctx, in)
if err != nil {
return nil, err
}
if c.mutateConsistencyProof {
if len(resp.Proof.Hashes) == 0 {
klog.Warningf("Consistency proof not modified because len(Hashes) = 0")
return resp, nil
}
i := rand.Intn(len(resp.Proof.Hashes))
j := rand.Intn(len(resp.Proof.Hashes[i]))
resp.Proof.Hashes[i][j] ^= 4
}
return resp, nil
}
|