github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/client/mock_log_client.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  	"context"
    19  	"math/rand"
    20  
    21  	"github.com/golang/glog"
    22  	"github.com/google/trillian"
    23  	"google.golang.org/grpc"
    24  )
    25  
    26  // MockLogClient supports applying mutations to the return values of the TrillianLogClient
    27  type MockLogClient struct {
    28  	c                    trillian.TrillianLogClient
    29  	mGetInclusionProof   bool
    30  	mGetConsistencyProof bool
    31  }
    32  
    33  // QueueLeaf forwards requests.
    34  func (c *MockLogClient) QueueLeaf(ctx context.Context, in *trillian.QueueLeafRequest, opts ...grpc.CallOption) (*trillian.QueueLeafResponse, error) {
    35  	return c.c.QueueLeaf(ctx, in)
    36  }
    37  
    38  // QueueLeaves forwards requests.
    39  func (c *MockLogClient) QueueLeaves(ctx context.Context, in *trillian.QueueLeavesRequest, opts ...grpc.CallOption) (*trillian.QueueLeavesResponse, error) {
    40  	return c.c.QueueLeaves(ctx, in)
    41  }
    42  
    43  // AddSequencedLeaf forwards requests.
    44  func (c *MockLogClient) AddSequencedLeaf(ctx context.Context, in *trillian.AddSequencedLeafRequest, opts ...grpc.CallOption) (*trillian.AddSequencedLeafResponse, error) {
    45  	return c.c.AddSequencedLeaf(ctx, in)
    46  }
    47  
    48  // AddSequencedLeaves forwards requests.
    49  func (c *MockLogClient) AddSequencedLeaves(ctx context.Context, in *trillian.AddSequencedLeavesRequest, opts ...grpc.CallOption) (*trillian.AddSequencedLeavesResponse, error) {
    50  	return c.c.AddSequencedLeaves(ctx, in)
    51  }
    52  
    53  // GetInclusionProof forwards requests and optionally corrupts the response.
    54  func (c *MockLogClient) GetInclusionProof(ctx context.Context, in *trillian.GetInclusionProofRequest, opts ...grpc.CallOption) (*trillian.GetInclusionProofResponse, error) {
    55  	resp, err := c.c.GetInclusionProof(ctx, in)
    56  	if c.mGetInclusionProof {
    57  		i := rand.Intn(len(resp.Proof.Hashes))
    58  		j := rand.Intn(len(resp.Proof.Hashes[i]))
    59  		resp.Proof.Hashes[i][j] ^= 4
    60  	}
    61  	return resp, err
    62  }
    63  
    64  // GetInclusionProofByHash forwards requests and optionaly corrupts responses.
    65  func (c *MockLogClient) GetInclusionProofByHash(ctx context.Context, in *trillian.GetInclusionProofByHashRequest, opts ...grpc.CallOption) (*trillian.GetInclusionProofByHashResponse, error) {
    66  	resp, err := c.c.GetInclusionProofByHash(ctx, in)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	if c.mGetInclusionProof {
    71  		h := rand.Intn(len(resp.Proof))
    72  		if len(resp.Proof[h].Hashes) == 0 {
    73  			glog.Warningf("Inclusion proof not modified because treesize = 0")
    74  			return resp, nil
    75  		}
    76  		i := rand.Intn(len(resp.Proof[h].Hashes))
    77  		j := rand.Intn(len(resp.Proof[h].Hashes[i]))
    78  		resp.Proof[h].Hashes[i][j] ^= 4
    79  	}
    80  	return resp, nil
    81  }
    82  
    83  // GetConsistencyProof forwards requests and optionally corrupts responses.
    84  func (c *MockLogClient) GetConsistencyProof(ctx context.Context, in *trillian.GetConsistencyProofRequest, opts ...grpc.CallOption) (*trillian.GetConsistencyProofResponse, error) {
    85  	resp, err := c.c.GetConsistencyProof(ctx, in)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	if c.mGetConsistencyProof {
    90  		if len(resp.Proof.Hashes) == 0 {
    91  			glog.Warningf("Consistency proof not modified because len(Hashes) = 0")
    92  			return resp, nil
    93  		}
    94  		i := rand.Intn(len(resp.Proof.Hashes))
    95  		j := rand.Intn(len(resp.Proof.Hashes[i]))
    96  		resp.Proof.Hashes[i][j] ^= 4
    97  	}
    98  	return resp, nil
    99  }
   100  
   101  // GetLatestSignedLogRoot forwards requests.
   102  func (c *MockLogClient) GetLatestSignedLogRoot(ctx context.Context, in *trillian.GetLatestSignedLogRootRequest, opts ...grpc.CallOption) (*trillian.GetLatestSignedLogRootResponse, error) {
   103  	return c.c.GetLatestSignedLogRoot(ctx, in)
   104  }
   105  
   106  // GetSequencedLeafCount forwards requests.
   107  func (c *MockLogClient) GetSequencedLeafCount(ctx context.Context, in *trillian.GetSequencedLeafCountRequest, opts ...grpc.CallOption) (*trillian.GetSequencedLeafCountResponse, error) {
   108  	return c.c.GetSequencedLeafCount(ctx, in)
   109  }
   110  
   111  // GetLeavesByIndex forwards requests.
   112  func (c *MockLogClient) GetLeavesByIndex(ctx context.Context, in *trillian.GetLeavesByIndexRequest, opts ...grpc.CallOption) (*trillian.GetLeavesByIndexResponse, error) {
   113  	return c.c.GetLeavesByIndex(ctx, in)
   114  }
   115  
   116  // GetLeavesByRange forwards requests.
   117  func (c *MockLogClient) GetLeavesByRange(ctx context.Context, in *trillian.GetLeavesByRangeRequest, opts ...grpc.CallOption) (*trillian.GetLeavesByRangeResponse, error) {
   118  	return c.c.GetLeavesByRange(ctx, in)
   119  }
   120  
   121  // GetLeavesByHash forwards requests.
   122  func (c *MockLogClient) GetLeavesByHash(ctx context.Context, in *trillian.GetLeavesByHashRequest, opts ...grpc.CallOption) (*trillian.GetLeavesByHashResponse, error) {
   123  	return c.c.GetLeavesByHash(ctx, in)
   124  }
   125  
   126  // GetEntryAndProof forwards requests.
   127  func (c *MockLogClient) GetEntryAndProof(ctx context.Context, in *trillian.GetEntryAndProofRequest, opts ...grpc.CallOption) (*trillian.GetEntryAndProofResponse, error) {
   128  	return c.c.GetEntryAndProof(ctx, in)
   129  }
   130  
   131  // InitLog forwards requests.
   132  func (c *MockLogClient) InitLog(ctx context.Context, in *trillian.InitLogRequest, opts ...grpc.CallOption) (*trillian.InitLogResponse, error) {
   133  	return c.c.InitLog(ctx, in)
   134  }