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

     1  // Copyright 2021 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 client_test
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"net/url"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/google/go-cmp/cmp"
    26  	"github.com/google/trillian-examples/binary_transparency/firmware/api"
    27  	"github.com/google/trillian-examples/binary_transparency/firmware/internal/client"
    28  	"github.com/transparency-dev/formats/log"
    29  )
    30  
    31  func TestGetWitnessCheckpoint(t *testing.T) {
    32  	for _, test := range []struct {
    33  		desc    string
    34  		body    []byte
    35  		want    api.LogCheckpoint
    36  		wantErr bool
    37  	}{
    38  		{
    39  			desc: "valid 1",
    40  			body: mustSignCPNote(t, "Firmware Transparency Log\n1\nEjQ=\n123\n"),
    41  			want: api.LogCheckpoint{
    42  				Checkpoint: log.Checkpoint{
    43  					Origin: "Firmware Transparency Log",
    44  					Size:   1,
    45  					Hash:   []byte{0x12, 0x34},
    46  				},
    47  				TimestampNanos: 123,
    48  			},
    49  		}, {
    50  			desc: "valid 2",
    51  			body: mustSignCPNote(t, "Firmware Transparency Log\n10\nNBI=\n1230\n"),
    52  			want: api.LogCheckpoint{
    53  				Checkpoint: log.Checkpoint{
    54  					Origin: "Firmware Transparency Log",
    55  					Size:   10,
    56  					Hash:   []byte{0x34, 0x12},
    57  				},
    58  				TimestampNanos: 1230,
    59  			},
    60  		}, {
    61  			desc:    "garbage",
    62  			body:    []byte("garbage"),
    63  			wantErr: true,
    64  		},
    65  	} {
    66  		t.Run(test.desc, func(t *testing.T) {
    67  			ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    68  				if !strings.HasSuffix(r.URL.Path, api.WitnessGetCheckpoint) {
    69  					t.Fatalf("Got unexpected HTTP request on %q", r.URL.Path)
    70  				}
    71  				fmt.Fprint(w, string(test.body))
    72  			}))
    73  			defer ts.Close()
    74  
    75  			tsURL, err := url.Parse((ts.URL))
    76  			if err != nil {
    77  				t.Fatalf("Failed to parse test server URL: %v", err)
    78  			}
    79  			wc := client.WitnessClient{
    80  				URL:            tsURL,
    81  				LogSigVerifier: mustGetLogSigVerifier(t),
    82  			}
    83  			cp, err := wc.GetWitnessCheckpoint()
    84  			switch {
    85  			case err != nil && !test.wantErr:
    86  				t.Fatalf("Got unexpected error %q", err)
    87  			case err == nil && test.wantErr:
    88  				t.Fatal("Got no error, but wanted error")
    89  			case err != nil && test.wantErr:
    90  				// expected error
    91  			default:
    92  				// TODO(al): fix sigs here
    93  				cp.Envelope = nil
    94  
    95  				if d := cmp.Diff(*cp, test.want); len(d) != 0 {
    96  					t.Fatalf("Got checkpoint with diff: %s", d)
    97  				}
    98  			}
    99  		})
   100  	}
   101  }