github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/testonly/marshalling.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 testonly
    16  
    17  import (
    18  	"log"
    19  	"testing"
    20  
    21  	"github.com/golang/protobuf/proto"
    22  	"github.com/golang/protobuf/ptypes"
    23  	"github.com/golang/protobuf/ptypes/any"
    24  )
    25  
    26  // MustMarshalAnyNoT is used to Marshal proto messages into the
    27  // protobuf.ptypes.any.Any used throughout the Trillian API and in
    28  // storage.  Use if testing.T not available. Failure to marshal will
    29  // fail the test suite.
    30  func MustMarshalAnyNoT(in proto.Message) []byte {
    31  	protoBytes, err := proto.Marshal(in)
    32  	if err != nil {
    33  		log.Fatalf("failed to marshal %v as 'bytes': err %v", in, err)
    34  	}
    35  	return protoBytes
    36  }
    37  
    38  // MustMarshalAny is used in tests to Marshal proto messages into the
    39  // protobuf.ptypes.any.Any used in the Trillian API and in storage.
    40  // Failure to marshal will fail the test but the suite will continue.
    41  func MustMarshalAny(t *testing.T, in proto.Message) *any.Any {
    42  	// TODO(phad): call t.Helper() here when Travis set to use go 1.9.
    43  	anything, err := ptypes.MarshalAny(in)
    44  	if err != nil {
    45  		t.Fatalf("failed to marshal %v as 'any': err %v", in, err)
    46  	}
    47  	return anything
    48  }