github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/binary_transparency/firmware/cmd/ft_witness/internal/ws/store_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 ws 16 17 import ( 18 "bytes" 19 "fmt" 20 "path/filepath" 21 "testing" 22 ) 23 24 const undefFp = "" 25 26 func TestRoundTrip(t *testing.T) { 27 for _, test := range []struct { 28 desc string 29 data []byte 30 }{ 31 { 32 desc: "initial checkpoint test", 33 data: []byte("some checkpoint"), 34 }, { 35 desc: "check over-write of checkpoint", 36 data: []byte("some more checkpoint"), 37 }, 38 } { 39 dbFP := filepath.Join(t.TempDir(), "db") 40 t.Run(test.desc, func(t *testing.T) { 41 store, err := NewStorage(dbFP) 42 if err != nil { 43 t.Error("failed to create storage", err) 44 } 45 want := test.data 46 if err := store.StoreCP(test.data); err != nil { 47 t.Error("failed to store into Witness Store", err) 48 } 49 got, err := store.RetrieveCP() 50 if err != nil { 51 t.Error("failed to retrieve from Witness Store", err) 52 } 53 if !bytes.Equal(got, want) { 54 t.Errorf("got '%s' want '%s'", got, want) 55 } 56 57 }) 58 } 59 } 60 61 func TestFailedStorage(t *testing.T) { 62 for _, test := range []struct { 63 desc string 64 wantError string 65 }{ 66 { 67 desc: "Handle Storage failure", 68 wantError: "failed to open file: open : no such file or directory", 69 }, 70 } { 71 t.Run(test.desc, func(t *testing.T) { 72 _, err := NewStorage(undefFp) 73 if err == nil { 74 t.Error("Unexpected success in storage creation", err) 75 } 76 fmt.Printf("Received Error = %s", err.Error()) 77 if err.Error() != test.wantError { 78 t.Error("Unexpected error message received", err) 79 } 80 }) 81 } 82 }