Skip to content

Commit

Permalink
refactor!: rename "InitLocal" to "Init" (#354)
Browse files Browse the repository at this point in the history
* refactor!: rename "InitLocal" to "Init"

Straightforward find/replace:

    find . -name '*.go' | xargs sed -i 's/InitLocal/Init/g'

BREAKING CHANGE: the method signature of `Init()` has changed since the
last release to take in the raw `root.json` metadata.

Fixes #208.

Signed-off-by: Zachary Newman <z@znewman.net>

* test: remove now-obsolete duplicate test

Signed-off-by: Zachary Newman <z@znewman.net>
  • Loading branch information
znewman01 committed Aug 5, 2022
1 parent 8b2d2ab commit f3a48f7
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 33 deletions.
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ func NewClient(local LocalStore, remote RemoteStore) *Client {
}
}

// InitLocal initializes a local repository from root metadata.
// Init initializes a local repository from root metadata.
//
// The root's keys are extracted from the root and saved in local storage.
// Root expiration is not checked.
// It is expected that rootJSON was securely distributed with the software
// being updated.
func (c *Client) InitLocal(rootJSON []byte) error {
func (c *Client) Init(rootJSON []byte) error {
err := c.loadAndVerifyRootMeta(rootJSON, true /*ignoreExpiredCheck*/)
if err != nil {
return err
Expand Down
35 changes: 8 additions & 27 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (s *ClientSuite) rootMeta(c *C) []byte {
func (s *ClientSuite) newClient(c *C) *Client {
s.local = MemoryLocalStore()
client := NewClient(s.local, s.remote)
c.Assert(client.InitLocal(s.rootMeta(c)), IsNil)
c.Assert(client.Init(s.rootMeta(c)), IsNil)
return client
}

Expand Down Expand Up @@ -244,26 +244,7 @@ func (s *ClientSuite) assertErrExpired(c *C, err error, file string) {
c.Assert(expiredErr.Expired.Unix(), Equals, s.expiredTime.Round(time.Second).Unix())
}

func (s *ClientSuite) TestInit(c *C) {
client := NewClient(MemoryLocalStore(), s.remote)

// check invalid json
c.Assert(client.InitLocal(make([]byte, 0)), NotNil)
rootJson := `{ "signatures": [], "signed": {"version": "wrongtype"}, "spec_version": "1.0.0", "version": 1}`
err := client.InitLocal([]byte(rootJson))
c.Assert(err.Error(), Matches, "json: cannot unmarshal string.*")

// check Update() returns ErrNoRootKeys when uninitialized
_, err = client.Update()
c.Assert(err, Equals, ErrNoRootKeys)

// check Update() does not return ErrNoRootKeys after initialization
c.Assert(client.InitLocal(s.rootMeta(c)), IsNil)
_, err = client.Update()
c.Assert(err, IsNil)
}

func (s *ClientSuite) TestInitLocalAllowsExpired(c *C) {
func (s *ClientSuite) TestInitAllowsExpired(c *C) {
s.genKeyExpired(c, "targets")
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
Expand All @@ -273,11 +254,11 @@ func (s *ClientSuite) TestInitLocalAllowsExpired(c *C) {
bytes, err := io.ReadAll(s.remote.meta["root.json"])
c.Assert(err, IsNil)
s.withMetaExpired(func() {
c.Assert(client.InitLocal(bytes), IsNil)
c.Assert(client.Init(bytes), IsNil)
})
}

func (s *ClientSuite) TestInitLocal(c *C) {
func (s *ClientSuite) TestInit(c *C) {
client := NewClient(MemoryLocalStore(), s.remote)
bytes, err := io.ReadAll(s.remote.meta["root.json"])
c.Assert(err, IsNil)
Expand All @@ -290,7 +271,7 @@ func (s *ClientSuite) TestInitLocal(c *C) {
_, err = client.Update()
c.Assert(err, Equals, ErrNoRootKeys)

// check InitLocal() returns ErrInvalid when the root's signature is
// check Init() returns ErrInvalid when the root's signature is
// invalid
// modify root and marshal without regenerating signatures
root.Version = root.Version + 1
Expand All @@ -299,10 +280,10 @@ func (s *ClientSuite) TestInitLocal(c *C) {
dataSigned.Signed = rootBytes
dataBytes, err := json.Marshal(dataSigned)
c.Assert(err, IsNil)
c.Assert(client.InitLocal(dataBytes), Equals, verify.ErrInvalid)
c.Assert(client.Init(dataBytes), Equals, verify.ErrInvalid)

// check Update() does not return ErrNoRootKeys after initialization
c.Assert(client.InitLocal(bytes), IsNil)
c.Assert(client.Init(bytes), IsNil)
_, err = client.Update()
c.Assert(err, IsNil)
}
Expand Down Expand Up @@ -1042,7 +1023,7 @@ func (s *ClientSuite) TestUpdateHTTP(c *C) {
c.Assert(err, IsNil)
rootJsonBytes, err := json.Marshal(rootMeta)
c.Assert(err, IsNil)
c.Assert(client.InitLocal(rootJsonBytes), IsNil)
c.Assert(client.Init(rootJsonBytes), IsNil)

// check update is ok
targets, err := client.Update()
Expand Down
2 changes: 1 addition & 1 deletion client/delegations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func initTestDelegationClient(t *testing.T, dirPrefix string) (*Client, func() e
c := NewClient(MemoryLocalStore(), remote)
rawFile, err := ioutil.ReadFile(initialStateDir + "/" + "root.json")
assert.Nil(t, err)
assert.Nil(t, c.InitLocal(rawFile))
assert.Nil(t, c.Init(rawFile))
files, err := ioutil.ReadDir(initialStateDir)
assert.Nil(t, err)

Expand Down
2 changes: 1 addition & 1 deletion client/interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (t *testCase) runStep(c *C, stepName string) {
c.Assert(err, IsNil)
rootJsonBytes, err := io.ReadAll(ioReader)
c.Assert(err, IsNil)
c.Assert(client.InitLocal(rootJsonBytes), IsNil)
c.Assert(client.Init(rootJsonBytes), IsNil)

// check update returns the correct updated targets
files, err := client.Update()
Expand Down
2 changes: 1 addition & 1 deletion client/python_interop/python_interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (InteropSuite) TestGoClientPythonGenerated(c *C) {
client := client.NewClient(client.MemoryLocalStore(), remote)
rootJSON, err := ioutil.ReadFile(filepath.Join(testDataDir, dir, "repository", "metadata", "root.json"))
c.Assert(err, IsNil)
c.Assert(client.InitLocal(rootJSON), IsNil)
c.Assert(client.Init(rootJSON), IsNil)

// check update returns the correct updated targets
files, err := client.Update()
Expand Down
2 changes: 1 addition & 1 deletion cmd/tuf-client/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ func cmdInit(args *docopt.Args, client *tuf.Client) error {
if err != nil {
return err
}
return client.InitLocal(bytes)
return client.Init(bytes)
}

0 comments on commit f3a48f7

Please sign in to comment.