github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/binary_transparency/firmware/api/firmware_metadata_test.go (about)

     1  // Copyright 2020 Google LLC. 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 api_test holds blackbox tests for the api package.
    16  package api_test
    17  
    18  import (
    19  	"fmt"
    20  	"regexp"
    21  	"testing"
    22  
    23  	"github.com/google/trillian-examples/binary_transparency/firmware/api"
    24  )
    25  
    26  func TestFirmwareMetadataToString(t *testing.T) {
    27  	for _, test := range []struct {
    28  		desc string
    29  		fm   api.FirmwareMetadata
    30  		r    *regexp.Regexp
    31  	}{
    32  		{
    33  			desc: "one",
    34  			fm: api.FirmwareMetadata{
    35  				DeviceID:                    "device",
    36  				FirmwareRevision:            1,
    37  				FirmwareImageSHA512:         []byte("this is an image hash"),
    38  				ExpectedFirmwareMeasurement: []byte("this is a firmware measurement"),
    39  				BuildTimestamp:              "noon",
    40  			},
    41  			r: regexp.MustCompile(fmt.Sprintf("device.*v1.*noon.*%x", "this is an image hash")),
    42  		},
    43  		{
    44  			desc: "two",
    45  			fm: api.FirmwareMetadata{
    46  				DeviceID:                    "banana",
    47  				FirmwareRevision:            100,
    48  				FirmwareImageSHA512:         []byte("an image hash this is"),
    49  				ExpectedFirmwareMeasurement: []byte("a measurement this is"),
    50  				BuildTimestamp:              "ouch",
    51  			},
    52  			r: regexp.MustCompile(fmt.Sprintf("banana.*v100.*ouch.*%x", "an image hash this is")),
    53  		},
    54  	} {
    55  		t.Run(test.desc, func(t *testing.T) {
    56  			if got := test.fm.String(); !test.r.Match([]byte(got)) {
    57  				t.Fatalf("Failed to match output %q", got)
    58  			}
    59  		})
    60  	}
    61  }