github.com/bartle-stripe/trillian@v1.2.1/testonly/hammer/replay_test.go (about)

     1  // Copyright 2018 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 hammer
    16  
    17  import (
    18  	"bufio"
    19  	"bytes"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/golang/protobuf/proto"
    24  	"github.com/google/trillian"
    25  	"github.com/google/trillian/testonly"
    26  )
    27  
    28  var dehex = testonly.MustHexDecode
    29  
    30  func TestWriteReadMessage(t *testing.T) {
    31  	var tests = []struct {
    32  		desc string
    33  		in   proto.Message
    34  		want string
    35  	}{
    36  		{
    37  			desc: "get-smr-req",
    38  			in:   &trillian.GetSignedMapRootRequest{MapId: 123},
    39  			want: "type.googleapis.com/trillian.GetSignedMapRootRequest",
    40  		},
    41  		{
    42  			desc: "get-tree",
    43  			in:   &trillian.Tree{TreeId: 123},
    44  			want: "type.googleapis.com/trillian.Tree",
    45  		},
    46  	}
    47  
    48  	for _, test := range tests {
    49  		t.Run(test.desc, func(t *testing.T) {
    50  			var buf bytes.Buffer
    51  			writer := bufio.NewWriter(&buf)
    52  			err := writeMessage(writer, test.in)
    53  			if err != nil {
    54  				t.Fatalf("writeMessage(%T)=%v; want nil", test.in, err)
    55  			}
    56  			writer.Flush()
    57  			data := buf.Bytes()
    58  			t.Logf("%+v => %x", test.in, data)
    59  
    60  			readFrom := bytes.NewBuffer(data)
    61  			got, err := readMessage(readFrom)
    62  			if err != nil {
    63  				t.Fatalf("readMessage(%x)=nil,%v; want _,nil", data, err)
    64  			}
    65  			if got.TypeUrl != test.want {
    66  				t.Errorf("readMessage(%x)=%q; want %q", data, got.TypeUrl, test.want)
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  func TestReadMessage(t *testing.T) {
    73  	var tests = []struct {
    74  		desc    string
    75  		in      []byte
    76  		want    string
    77  		wantErr string
    78  	}{
    79  		{
    80  			desc: "get-tree",
    81  			in:   dehex("00000027" + "0a21747970652e676f6f676c65617069732e636f6d2f7472696c6c69616e2e547265651202087b"),
    82  			want: "type.googleapis.com/trillian.Tree",
    83  		},
    84  		{
    85  			desc:    "len-too-short",
    86  			in:      dehex("0000"),
    87  			wantErr: "expected 4-byte length",
    88  		},
    89  		{
    90  			desc:    "data-too-short",
    91  			in:      dehex("00000027" + "0a21747970652e676f6f676c65617069732e636f6d2f7472696c6c69616e2e54726565120208"),
    92  			wantErr: "expected 38 bytes of data",
    93  		},
    94  		{
    95  			desc:    "empty",
    96  			in:      dehex(""),
    97  			wantErr: "EOF",
    98  		},
    99  	}
   100  
   101  	for _, test := range tests {
   102  		t.Run(test.desc, func(t *testing.T) {
   103  			readFrom := bytes.NewBuffer(test.in)
   104  			got, err := readMessage(readFrom)
   105  			if err != nil {
   106  				if test.wantErr == "" {
   107  					t.Fatalf("readMessage(%x)=nil,%v; want _,nil", test.in, err)
   108  				} else if !strings.Contains(err.Error(), test.wantErr) {
   109  					t.Fatalf("readMessage(%x)=nil,%v; want error containing %q", test.in, err, test.wantErr)
   110  				}
   111  				return
   112  			}
   113  			if test.wantErr != "" {
   114  				t.Fatalf("readMessage(%x)=%+v,nil; want nil,error containing %q", test.in, got, test.wantErr)
   115  			}
   116  			if got.TypeUrl != test.want {
   117  				t.Errorf("readMessage(%x).TypeUrl=%q; want %q", test.in, got.TypeUrl, test.want)
   118  			}
   119  		})
   120  	}
   121  }
   122  
   123  func TestConvertMsg(t *testing.T) {
   124  	mapmap := map[int64]int64{999: 123}
   125  	var tests = []struct {
   126  		desc string
   127  		in   proto.Message
   128  		want proto.Message
   129  	}{
   130  		{
   131  			desc: "get-smr-req-mapped",
   132  			in:   &trillian.GetSignedMapRootRequest{MapId: 999},
   133  			want: &trillian.GetSignedMapRootRequest{MapId: 123},
   134  		},
   135  		{
   136  			desc: "get-smr-req-unmapped",
   137  			in:   &trillian.GetSignedMapRootRequest{MapId: 456},
   138  			want: &trillian.GetSignedMapRootRequest{MapId: 456},
   139  		},
   140  		{
   141  			desc: "tree-id-instead",
   142  			in:   &trillian.Tree{TreeId: 999},
   143  			want: &trillian.Tree{TreeId: 999},
   144  		},
   145  	}
   146  
   147  	for _, test := range tests {
   148  		t.Run(test.desc, func(t *testing.T) {
   149  			got := test.in
   150  			convertMessage(got, mapmap)
   151  			if !proto.Equal(got, test.want) {
   152  				t.Fatalf("convertMsg(%+v)=%+v; want %+v", test.in, got, test.want)
   153  			}
   154  		})
   155  	}
   156  }