github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/binary_transparency/firmware/cmd/ft_witness/internal/http/witness_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 http 16 17 import ( 18 "fmt" 19 "io" 20 "net/http" 21 "net/http/httptest" 22 "testing" 23 24 "github.com/google/trillian-examples/binary_transparency/firmware/internal/crypto" 25 "golang.org/x/mod/sumdb/note" 26 ) 27 28 const ( 29 dummyURL = "xyz" 30 dummyPollInterval = 5 31 ) 32 33 func TestGetWitnessCheckpoint(t *testing.T) { 34 testSigner, _ := note.NewSigner(crypto.TestFTPersonalityPriv) 35 testVerifier, _ := note.NewVerifier(crypto.TestFTPersonalityPub) 36 for _, test := range []struct { 37 desc string 38 wantBody string 39 }{ 40 { 41 desc: "Successful Witness Checkpoint retrieval", 42 wantBody: "Firmware Transparency Log\n1\nEjQ=\n123\n", 43 }, 44 } { 45 t.Run(test.desc, func(t *testing.T) { 46 n := ¬e.Note{ 47 Text: test.wantBody, 48 } 49 ns, err := note.Sign(n, testSigner) 50 if err != nil { 51 t.Fatalf("Failed to sign checkpoint: %v", err) 52 } 53 witness, err := NewWitness(&FakeStore{ns, true}, dummyURL, testVerifier, dummyPollInterval) 54 if err != nil { 55 t.Fatalf("error creating witness: %v", err) 56 } 57 ts := httptest.NewServer(http.HandlerFunc(witness.getCheckpoint)) 58 defer ts.Close() 59 60 client := ts.Client() 61 resp, err := client.Get(ts.URL) 62 if err != nil { 63 t.Errorf("error response: %v", err) 64 } 65 if resp.StatusCode != http.StatusOK { 66 t.Errorf("status code not OK: %v", resp.StatusCode) 67 } 68 body, err := io.ReadAll(resp.Body) 69 if err != nil { 70 t.Errorf("failed to read body: %v", err) 71 } 72 got, err := note.Open(body, note.VerifierList(testVerifier)) 73 if err != nil { 74 t.Fatalf("Failed to open returned body: %v :\n%s", err, body) 75 } 76 if got.Text != test.wantBody { 77 t.Errorf("got '%s' want '%s'", got.Text, test.wantBody) 78 } 79 }) 80 } 81 } 82 83 func TestFailedWitnessCreation(t *testing.T) { 84 testVerifier, _ := note.NewVerifier(crypto.TestFTPersonalityPub) 85 for _, test := range []struct { 86 desc string 87 wantError string 88 }{ 89 { 90 desc: "Failed Witness Creation", 91 wantError: "new witness failed due to storage retrieval: unable to access store", 92 }, 93 } { 94 t.Run(test.desc, func(t *testing.T) { 95 _, err := NewWitness(&FakeStore{[]byte{}, false}, dummyURL, testVerifier, dummyPollInterval) 96 if err == nil { 97 t.Errorf("error witness creation happened smoothly: %v", err) 98 } 99 fmt.Printf("Error received %s", err.Error()) 100 if err.Error() != test.wantError { 101 t.Error("Unexpected error message received", err) 102 } 103 }) 104 } 105 } 106 107 type FakeStore struct { 108 scp []byte 109 storeaccess bool 110 } 111 112 func (f *FakeStore) StoreCP(wcp []byte) error { 113 if !f.storeaccess { 114 return fmt.Errorf("unable to access store") 115 } 116 f.scp = wcp 117 return nil 118 } 119 120 func (f *FakeStore) RetrieveCP() ([]byte, error) { 121 if !f.storeaccess { 122 return nil, fmt.Errorf("unable to access store") 123 } 124 return f.scp, nil 125 }