github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/binary_transparency/firmware/cmd/ft_personality/internal/cas/sql_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 cas
    16  
    17  import (
    18  	"bytes"
    19  	"crypto/sha512"
    20  	"database/sql"
    21  	"testing"
    22  
    23  	_ "github.com/mattn/go-sqlite3" // Load drivers for sqlite3
    24  	"google.golang.org/grpc/codes"
    25  	"google.golang.org/grpc/status"
    26  )
    27  
    28  func TestRoundTrip(t *testing.T) {
    29  	for _, test := range []struct {
    30  		desc      string
    31  		image     []byte
    32  		extraRuns int
    33  	}{
    34  		{
    35  			desc:  "simple",
    36  			image: []byte("simple"),
    37  		}, {
    38  			desc:      "check no failure on inserting same data twice",
    39  			image:     []byte("simple"),
    40  			extraRuns: 1,
    41  		},
    42  	} {
    43  		t.Run(test.desc, func(t *testing.T) {
    44  			db, err := sql.Open("sqlite3", ":memory:")
    45  			if err != nil {
    46  				t.Error("failed to open temporary in-memory DB", err)
    47  			}
    48  			defer func() {
    49  				if err := db.Close(); err != nil {
    50  					t.Errorf("db.Close(): %v", err)
    51  				}
    52  			}()
    53  
    54  			store, err := NewBinaryStorage(db)
    55  			if err != nil {
    56  				t.Error("failed to create CAS", err)
    57  			}
    58  
    59  			keyBs := sha512.Sum512(test.image)
    60  			key, value := keyBs[:], test.image
    61  
    62  			for i := 0; i < test.extraRuns+1; i++ {
    63  				if err := store.Store(key, value); err != nil {
    64  					t.Error("failed to store into CAS", err)
    65  				}
    66  				got, err := store.Retrieve(key)
    67  				if err != nil {
    68  					t.Error("failed to retrieve from CAS", err)
    69  				}
    70  				if !bytes.Equal(got, value) {
    71  					t.Errorf("got != want (%x, %x)", got, value)
    72  				}
    73  			}
    74  		})
    75  	}
    76  }
    77  func TestUnknownHash(t *testing.T) {
    78  	db, err := sql.Open("sqlite3", ":memory:")
    79  	if err != nil {
    80  		t.Error("failed to open temporary in-memory DB", err)
    81  	}
    82  	defer func() {
    83  		if err := db.Close(); err != nil {
    84  			t.Errorf("db.Close(): %v", err)
    85  		}
    86  	}()
    87  
    88  	store, err := NewBinaryStorage(db)
    89  	if err != nil {
    90  		t.Error("failed to create CAS", err)
    91  	}
    92  
    93  	keyBs := sha512.Sum512([]byte("This doesn't exist"))
    94  
    95  	r, err := store.Retrieve(keyBs[:])
    96  	if err == nil {
    97  		t.Fatalf("want error, but got none, result: %v", r)
    98  	}
    99  	if got, want := status.Code(err), codes.NotFound; got != want {
   100  		t.Fatalf("got error code %s, want %s", got, want)
   101  	}
   102  }