Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Increase test coverage (gopasspw#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikschulz authored Jan 4, 2018
1 parent 15b5261 commit 72000bd
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 79 deletions.
15 changes: 15 additions & 0 deletions store/root/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,19 @@ func TestTemplate(t *testing.T) {
tt, err := rs.TemplateTree()
assert.NoError(t, err)
assert.Equal(t, "gopass\n", tt.Format(0))

assert.Equal(t, false, rs.HasTemplate(ctx, "foo"))
_, err = rs.GetTemplate(ctx, "foo")
assert.Error(t, err)
assert.Error(t, rs.RemoveTemplate(ctx, "foo"))

assert.NoError(t, rs.SetTemplate(ctx, "foo", []byte("foobar")))
assert.Equal(t, true, rs.HasTemplate(ctx, "foo"))
b, err := rs.GetTemplate(ctx, "foo")
assert.NoError(t, err)
assert.Equal(t, "foobar", string(b))
b, found := rs.LookupTemplate(ctx, "foo/bar")
assert.Equal(t, true, found)
assert.Equal(t, "foobar", string(b))
assert.NoError(t, rs.RemoveTemplate(ctx, "foo"))
}
10 changes: 8 additions & 2 deletions store/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package secret

import (
"bytes"
"fmt"
"strings"
"sync"

Expand Down Expand Up @@ -57,14 +58,19 @@ func (s *Secret) decodeYAML() (bool, error) {
return true, nil
}

func (s *Secret) encodeYAML() error {
func (s *Secret) encodeYAML() (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %s", r)
}
}()
// update body
yb, err := yaml.Marshal(s.data)
if err != nil {
return err
}
s.body = "---\n" + string(yb)
return nil
return err
}

// Bytes encodes an secret
Expand Down
27 changes: 24 additions & 3 deletions store/secret/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/justwatchcom/gopass/store"
"github.com/stretchr/testify/assert"
)

const (
Expand All @@ -18,10 +19,30 @@ func TestYAMLKeyFromEmptySecret(t *testing.T) {
t.Logf("Get Key from empty Secret")
s := &Secret{}
_, err := s.Value(yamlKey)
if err == nil {
t.Errorf("Should complain about missing YAML marker")
}
assert.Error(t, err)
}

type inlineB struct {
B int
inlineC `yaml:",inline"`
}

type inlineC struct {
C int
}

func TestYAMLEncodingError(t *testing.T) {
s := &Secret{
data: map[string]interface{}{
"foo": &struct {
B int
inlineB `yaml:",inline"`
}{1, inlineB{2, inlineC{3}}},
},
}
assert.Error(t, s.encodeYAML())
}

func TestYAMLKeyToEmptySecret(t *testing.T) {
t.Logf("Set Key to empty Secret")
s := &Secret{}
Expand Down
2 changes: 1 addition & 1 deletion store/sub/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func WithReason(ctx context.Context, msg string) context.Context {

// HasReason returns true if a value for reason has been set in this context
func HasReason(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyReason).(bool)
_, ok := ctx.Value(ctxKeyReason).(string)
return ok
}

Expand Down
9 changes: 9 additions & 0 deletions store/sub/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestFsckCheck(t *testing.T) {
assert.Equal(t, false, IsFsckCheck(ctx))
assert.Equal(t, true, IsFsckCheck(WithFsckCheck(ctx, true)))
assert.Equal(t, false, IsFsckCheck(WithFsckCheck(ctx, false)))
assert.Equal(t, true, HasFsckCheck(WithFsckCheck(ctx, true)))
}

func TestFsckForce(t *testing.T) {
Expand All @@ -21,6 +22,7 @@ func TestFsckForce(t *testing.T) {
assert.Equal(t, false, IsFsckForce(ctx))
assert.Equal(t, true, IsFsckForce(WithFsckForce(ctx, true)))
assert.Equal(t, false, IsFsckForce(WithFsckForce(ctx, false)))
assert.Equal(t, true, HasFsckForce(WithFsckForce(ctx, true)))
}

func TestAutoSync(t *testing.T) {
Expand All @@ -29,13 +31,15 @@ func TestAutoSync(t *testing.T) {
assert.Equal(t, true, IsAutoSync(ctx))
assert.Equal(t, true, IsAutoSync(WithAutoSync(ctx, true)))
assert.Equal(t, false, IsAutoSync(WithAutoSync(ctx, false)))
assert.Equal(t, true, HasAutoSync(WithAutoSync(ctx, true)))
}

func TestReason(t *testing.T) {
ctx := context.Background()

assert.Equal(t, "", GetReason(ctx))
assert.Equal(t, "foobar", GetReason(WithReason(ctx, "foobar")))
assert.Equal(t, true, HasReason(WithReason(ctx, "foobar")))
}

func TestImportFunc(t *testing.T) {
Expand All @@ -46,6 +50,8 @@ func TestImportFunc(t *testing.T) {
}
assert.NotNil(t, GetImportFunc(ctx))
assert.Equal(t, true, GetImportFunc(WithImportFunc(ctx, ifunc))(ctx, "", nil))
assert.Equal(t, true, HasImportFunc(WithImportFunc(ctx, ifunc)))
assert.Equal(t, true, GetImportFunc(WithImportFunc(ctx, nil))(ctx, "", nil))
}

func TestRecipientFunc(t *testing.T) {
Expand All @@ -57,6 +63,7 @@ func TestRecipientFunc(t *testing.T) {
assert.NotNil(t, GetRecipientFunc(ctx))
_, err := GetRecipientFunc(WithRecipientFunc(ctx, rfunc))(ctx, "", nil)
assert.NoError(t, err)
assert.Equal(t, true, HasRecipientFunc(WithRecipientFunc(ctx, rfunc)))
}

func TestFsckFunc(t *testing.T) {
Expand All @@ -67,6 +74,7 @@ func TestFsckFunc(t *testing.T) {
}
assert.NotNil(t, GetFsckFunc(ctx))
assert.Equal(t, true, GetFsckFunc(WithFsckFunc(ctx, ffunc))(ctx, ""))
assert.Equal(t, true, HasFsckFunc(WithFsckFunc(ctx, ffunc)))
}

func TestCheckRecipients(t *testing.T) {
Expand All @@ -75,4 +83,5 @@ func TestCheckRecipients(t *testing.T) {
assert.Equal(t, false, IsCheckRecipients(ctx))
assert.Equal(t, true, IsCheckRecipients(WithCheckRecipients(ctx, true)))
assert.Equal(t, false, IsCheckRecipients(WithCheckRecipients(ctx, false)))
assert.Equal(t, true, HasCheckRecipients(WithCheckRecipients(ctx, true)))
}
3 changes: 2 additions & 1 deletion store/sub/gpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func (s *Store) exportPublicKey(ctx context.Context, r string) (string, error) {
return "", err
}

if fi.Size() < 1024 {
// ECC keys are at least 700 byte, RSA should be a lot bigger
if fi.Size() < 256 {
return "", errors.New("exported key too small")
}

Expand Down
83 changes: 54 additions & 29 deletions store/sub/move_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,55 @@ func TestCopy(t *testing.T) {
name: "Empty store",
tf: func(s *Store) func(t *testing.T) {
return func(t *testing.T) {
if err := s.Copy(ctx, "foo", "bar"); err == nil {
t.Errorf("Should fail to copy non-existing entries in empty store")
}
assert.Error(t, s.Copy(ctx, "foo", "bar"))
}
},
},
{
name: "Single entry",
tf: func(s *Store) func(t *testing.T) {
return func(t *testing.T) {
if err := s.Set(ctx, "foo", secret.New("bar", "")); err != nil {
t.Fatalf("Failed to insert test data: %s", err)
}
if err := s.Copy(ctx, "foo", "bar"); err != nil {
t.Errorf("Failed to copy 'foo' to 'bar': %s", err)
}
assert.NoError(t, s.Set(ctx, "foo", secret.New("bar", "")))
assert.NoError(t, s.Copy(ctx, "foo", "bar"))
sec, err := s.Get(ctx, "foo")
if err != nil {
t.Fatalf("Failed to get 'foo': %s", err)
}
if sec.Password() != "bar" {
t.Errorf("Wrong content in 'foo'")
}
assert.NoError(t, err)
assert.Equal(t, "bar", sec.Password())
sec, err = s.Get(ctx, "bar")
if err != nil {
t.Fatalf("Failed to get 'bar': %s", err)
}
if sec.Password() != "bar" {
t.Errorf("Wrong content in 'bar'")
}
assert.NoError(t, err)
assert.Equal(t, "bar", sec.Password())
}
},
},
{
name: "Recursive",
tf: func(s *Store) func(t *testing.T) {
return func(t *testing.T) {
assert.NoError(t, s.Set(ctx, "foo/bar/baz", secret.New("baz", "")))
assert.NoError(t, s.Set(ctx, "foo/bar/zab", secret.New("zab", "")))
assert.NoError(t, s.Copy(ctx, "foo", "bar"))

sec, err := s.Get(ctx, "bar/bar/baz")
assert.NoError(t, err)
assert.Equal(t, "baz", sec.Password())

sec, err = s.Get(ctx, "bar/bar/zab")
assert.NoError(t, err)
assert.Equal(t, "zab", sec.Password())

sec, err = s.Get(ctx, "foo/bar/baz")
assert.NoError(t, err)
assert.Equal(t, "baz", sec.Password())

sec, err = s.Get(ctx, "foo/bar/zab")
assert.NoError(t, err)
assert.Equal(t, "zab", sec.Password())
}
},
},
} {
// common setup
tempdir, err := ioutil.TempDir("", "gopass-")
if err != nil {
t.Fatalf("Failed to create tempdir: %s", err)
}
assert.NoError(t, err)

s := &Store{
alias: "",
Expand All @@ -78,8 +88,7 @@ func TestCopy(t *testing.T) {
git: gitmock.New(),
}

err = s.saveRecipients(ctx, []string{"john.doe"}, "test", false)
assert.NoError(t, err)
assert.NoError(t, s.saveRecipients(ctx, []string{"john.doe"}, "test", false))

// run test case
t.Run(tc.name, tc.tf(s))
Expand Down Expand Up @@ -107,9 +116,7 @@ func TestMove(t *testing.T) {
name: "Empty store",
tf: func(s *Store) func(t *testing.T) {
return func(t *testing.T) {
if err := s.Move(ctx, "foo", "bar"); err == nil {
t.Errorf("Should fail to move non-existing entries in empty store")
}
assert.Error(t, s.Move(ctx, "foo", "bar"))
}
},
},
Expand Down Expand Up @@ -137,6 +144,24 @@ func TestMove(t *testing.T) {
}
},
},
{
name: "Recursive",
tf: func(s *Store) func(t *testing.T) {
return func(t *testing.T) {
assert.NoError(t, s.Set(ctx, "foo/bar/baz", secret.New("baz", "")))
assert.NoError(t, s.Set(ctx, "foo/bar/zab", secret.New("zab", "")))
assert.NoError(t, s.Move(ctx, "foo", "bar"))

sec, err := s.Get(ctx, "bar/bar/baz")
assert.NoError(t, err)
assert.Equal(t, "baz", sec.Password())

sec, err = s.Get(ctx, "bar/bar/zab")
assert.NoError(t, err)
assert.Equal(t, "zab", sec.Password())
}
},
},
} {
// common setup
tempdir, err := ioutil.TempDir("", "gopass-")
Expand Down
1 change: 1 addition & 0 deletions store/sub/recipients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestGetRecipientsDefault(t *testing.T) {
git: gitmock.New(),
}

assert.Equal(t, genRecs, s.Recipients(ctx))
recs, err := s.GetRecipients(ctx, "")
assert.NoError(t, err)
assert.Equal(t, genRecs, recs)
Expand Down
37 changes: 37 additions & 0 deletions store/sub/store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sub

import (
"context"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -9,6 +10,7 @@ import (
"testing"

gpgmock "github.com/justwatchcom/gopass/backend/gpg/mock"
"github.com/justwatchcom/gopass/store/secret"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -90,3 +92,38 @@ func TestStore(t *testing.T) {
t.Errorf("Should be equal to myself")
}
}

func TestIdFile(t *testing.T) {
ctx := context.Background()

tempdir, err := ioutil.TempDir("", "gopass-")
if err != nil {
t.Fatalf("Failed to create tempdir: %s", err)
}
defer func() {
_ = os.RemoveAll(tempdir)
}()

s, err := createSubStore(tempdir)
assert.NoError(t, err)

// test sub-id
secName := "a"
for i := 0; i < 99; i++ {
secName += "/a"
}
assert.NoError(t, s.Set(ctx, secName, secret.New("foo", "bar")))
assert.NoError(t, ioutil.WriteFile(filepath.Join(tempdir, "sub", "a", GPGID), []byte("foobar"), 0600))
assert.Equal(t, filepath.Join(tempdir, "sub", "a", GPGID), s.idFile(secName))

assert.Equal(t, true, s.Exists(secName))

// test abort condition
secName = "a"
for i := 0; i < 100; i++ {
secName += "/a"
}
assert.NoError(t, s.Set(ctx, secName, secret.New("foo", "bar")))
assert.NoError(t, ioutil.WriteFile(filepath.Join(tempdir, "sub", "a", GPGID), []byte("foobar"), 0600))
assert.Equal(t, filepath.Join(tempdir, "sub", GPGID), s.idFile(secName))
}
Loading

0 comments on commit 72000bd

Please sign in to comment.