...

Source file src/github.com/theupdateframework/go-tuf/client/leveldbstore/leveldbstore_test.go

Documentation: github.com/theupdateframework/go-tuf/client/leveldbstore

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	. "gopkg.in/check.v1"
     9  	"os"
    10  )
    11  
    12  type LocalStoreSuite struct{}
    13  
    14  var _ = Suite(&LocalStoreSuite{})
    15  
    16  // Hook up gocheck into the "go test" runner.
    17  func Test(t *testing.T) { TestingT(t) }
    18  
    19  func (LocalStoreSuite) TestFileLocalStore(c *C) {
    20  	tmp := c.MkDir()
    21  	path := filepath.Join(tmp, "tuf.db")
    22  	store, err := FileLocalStore(path)
    23  	c.Assert(err, IsNil)
    24  	defer store.Close()
    25  
    26  	type meta map[string]json.RawMessage
    27  
    28  	assertGet := func(expected meta) {
    29  		actual, err := store.GetMeta()
    30  		c.Assert(err, IsNil)
    31  		c.Assert(meta(actual), DeepEquals, expected)
    32  	}
    33  
    34  	// initial GetMeta should return empty meta
    35  	assertGet(meta{})
    36  
    37  	// SetMeta should persist
    38  	rootJSON := []byte(`{"_type":"Root"}`)
    39  	c.Assert(store.SetMeta("root.json", rootJSON), IsNil)
    40  	assertGet(meta{"root.json": rootJSON})
    41  
    42  	// SetMeta should add to existing meta
    43  	targetsJSON := []byte(`{"_type":"Target"}`)
    44  	c.Assert(store.SetMeta("targets.json", targetsJSON), IsNil)
    45  	assertGet(meta{"root.json": rootJSON, "targets.json": targetsJSON})
    46  
    47  	// a new store should get the same meta
    48  	c.Assert(store.Close(), IsNil)
    49  	store, err = FileLocalStore(path)
    50  	c.Assert(err, IsNil)
    51  	defer func() {
    52  		c.Assert(store.Close(), IsNil)
    53  	}()
    54  	assertGet(meta{"root.json": rootJSON, "targets.json": targetsJSON})
    55  }
    56  
    57  func (LocalStoreSuite) TestDeleteMeta(c *C) {
    58  	tmp := c.MkDir()
    59  	path := filepath.Join(tmp, "tuf.db")
    60  	store, err := FileLocalStore(path)
    61  	c.Assert(err, IsNil)
    62  
    63  	type meta map[string]json.RawMessage
    64  
    65  	assertGet := func(expected meta) {
    66  		actual, err := store.GetMeta()
    67  		c.Assert(err, IsNil)
    68  		c.Assert(meta(actual), DeepEquals, expected)
    69  	}
    70  
    71  	// initial GetMeta should return empty meta
    72  	assertGet(meta{})
    73  
    74  	// SetMeta should persist
    75  	rootJSON := []byte(`{"_type":"Root"}`)
    76  	c.Assert(store.SetMeta("root.json", rootJSON), IsNil)
    77  	assertGet(meta{"root.json": rootJSON})
    78  
    79  	store.DeleteMeta("root.json")
    80  	m, _ := store.GetMeta()
    81  	if _, ok := m["root.json"]; ok {
    82  		c.Fatalf("Metadata is not deleted!")
    83  	}
    84  }
    85  
    86  func (LocalStoreSuite) TestCorruptManifest(c *C) {
    87  	tmp := c.MkDir()
    88  	path := filepath.Join(tmp, "tuf.db")
    89  
    90  	store, err := FileLocalStore(path)
    91  	c.Assert(err, IsNil)
    92  
    93  	// now break the manifest file
    94  	err = os.Truncate(filepath.Join(path, "MANIFEST-000000"), 1)
    95  	c.Assert(err, IsNil)
    96  	err = store.Close()
    97  	c.Assert(err, IsNil)
    98  
    99  	store, err = FileLocalStore(path)
   100  	c.Assert(err, IsNil)
   101  
   102  	type meta map[string]json.RawMessage
   103  
   104  	assertGet := func(expected meta) {
   105  		actual, err := store.GetMeta()
   106  		c.Assert(err, IsNil)
   107  		c.Assert(meta(actual), DeepEquals, expected)
   108  	}
   109  
   110  	// initial GetMeta should return empty meta
   111  	assertGet(meta{})
   112  
   113  	// SetMeta should persist
   114  	rootJSON := []byte(`{"_type":"Root"}`)
   115  	c.Assert(store.SetMeta("root.json", rootJSON), IsNil)
   116  	assertGet(meta{"root.json": rootJSON})
   117  
   118  	store.DeleteMeta("root.json")
   119  	m, _ := store.GetMeta()
   120  	if _, ok := m["root.json"]; ok {
   121  		c.Fatalf("Metadata is not deleted!")
   122  	}
   123  }
   124  

View as plain text