Skip to content

Commit

Permalink
Run both tests, fix remaining tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Ville Aikas <vaikas@chainguard.dev>
  • Loading branch information
vaikas committed Sep 26, 2022
1 parent 0e5f14b commit 3f75002
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,8 @@ type Destination interface {
// - The target does not exist in any targets
// - Metadata cannot be generated for the downloaded data
// - Generated metadata does not match local metadata for the given file
// - Size of the download does not match if the reported size is known and
// incorrect
func (c *Client) Download(name string, dest Destination) (err error) {
// delete dest if there is an error
defer func() {
Expand Down
54 changes: 25 additions & 29 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ import (
func Test(t *testing.T) { TestingT(t) }

type ClientSuite struct {
store tuf.LocalStore
repo *tuf.Repo
local LocalStore
remote RemoteStore
expiredTime time.Time
keyIDs map[string][]string
store tuf.LocalStore
repo *tuf.Repo
local LocalStore
remote RemoteStore
expiredTime time.Time
keyIDs map[string][]string
useFileStore bool
}

var _ = Suite(&ClientSuite{})
var _ = Suite(&ClientSuite{useFileStore: false})
var _ = Suite(&ClientSuite{useFileStore: true})

func newFakeRemoteStore() *fakeRemoteStore {
return &fakeRemoteStore{
Expand Down Expand Up @@ -72,7 +74,7 @@ func (f *fakeRemoteStore) get(name string, store map[string]*fakeFile) (io.ReadC
// These are helper methods for manipulating the internals of the Stores
// because the set/delete methods are not part of the Interface, we need to
// switch on the underlying implementation.
// Also read* methods are convenience for ease of testing.
// Also readMeta method is convenience for ease of testing.
func (s *ClientSuite) setMeta(path string, data []byte) error {
switch impl := s.remote.(type) {
case *fakeRemoteStore:
Expand Down Expand Up @@ -178,11 +180,13 @@ func (s *ClientSuite) SetUpTest(c *C) {
c.Assert(s.repo.Commit(), IsNil)

// create a remote store containing valid repo files
// TODO(vaikas): How do we run these both???
//s.remote = newFakeRemoteStore()
s.remote, err = newTestFileStore()
if err != nil {
c.Fatalf("failed to create new FileStore: %v", err)
if s.useFileStore {
s.remote, err = newTestFileStore()
if err != nil {
c.Fatalf("failed to create new FileStore: %v", err)
}
} else {
s.remote = newFakeRemoteStore()
}
s.syncRemote(c)
for path, data := range targetFiles {
Expand Down Expand Up @@ -229,7 +233,7 @@ func (s *ClientSuite) syncRemote(c *C) {
c.Assert(err, IsNil)
for name, data := range meta {
if err := s.setMeta(name, data); err != nil {
panic(err)
panic(fmt.Sprintf("setMetadata failed: %v", err))
}
}
}
Expand Down Expand Up @@ -1182,47 +1186,39 @@ func (s *ClientSuite) TestDownloadOK(c *C) {
}
}

/* TODO(VAIKAS): FIX THESE
func (s *ClientSuite) TestDownloadWrongSize(c *C) {
client := s.updatedClient(c)
remoteFile := &fakeFile{buf: bytes.NewReader([]byte("wrong-size")), size: 10}
// Update with a file that's incorrect size.
s.setTarget("foo.txt", []byte("wrong-size"))
var dest testDestination
c.Assert(client.Download("foo.txt", &dest), DeepEquals, ErrWrongSize{"foo.txt", 10, 3})
c.Assert(remoteFile.bytesRead, Equals, 0)
c.Assert(dest.deleted, Equals, true)
}

func (s *ClientSuite) TestDownloadTargetTooLong(c *C) {
client := s.updatedClient(c)
remoteFile := s.remote.targets["foo.txt"]
remoteFile.buf = bytes.NewReader([]byte("foo-ooo"))
s.setTarget("foo.txt", []byte("foo-ooo"))
var dest testDestination
c.Assert(client.Download("foo.txt", &dest), IsNil)
c.Assert(remoteFile.bytesRead, Equals, 3)
c.Assert(dest.deleted, Equals, false)
c.Assert(dest.String(), Equals, "foo")
c.Assert(client.Download("foo.txt", &dest), DeepEquals, ErrWrongSize{"foo.txt", 7, 3})
c.Assert(dest.deleted, Equals, true)
}

func (s *ClientSuite) TestDownloadTargetTooShort(c *C) {
client := s.updatedClient(c)
remoteFile := s.remote.targets["foo.txt"]
remoteFile.buf = bytes.NewReader([]byte("fo"))
s.setTarget("foo.txt", []byte("fo"))
var dest testDestination
c.Assert(client.Download("foo.txt", &dest), DeepEquals, ErrWrongSize{"foo.txt", 2, 3})
c.Assert(dest.deleted, Equals, true)
}

func (s *ClientSuite) TestDownloadTargetCorruptData(c *C) {
client := s.updatedClient(c)
remoteFile := s.remote.targets["foo.txt"]
remoteFile.buf = bytes.NewReader([]byte("corrupt"))
s.setTarget("foo.txt", []byte("ooo"))
var dest testDestination
assertWrongHash(c, client.Download("foo.txt", &dest))
c.Assert(dest.deleted, Equals, true)
}

*/

func (s *ClientSuite) TestAvailableTargets(c *C) {
client := s.updatedClient(c)
files, err := client.Targets()
Expand Down

0 comments on commit 3f75002

Please sign in to comment.