Skip to content

Commit

Permalink
increase test coverage #6 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
u5surf authored and blaskovicz committed Sep 30, 2019
1 parent da618cd commit 736b856
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cryptkeeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ func TestCryptSetup(t *testing.T) {

func TestCryptString(t *testing.T) {

t.Run("Invalid without SetCryptKey", func(t *testing.T) {
t.Run("Invalid Encrypt", func(t *testing.T) {
_, err := Encrypt("abc")
if err == nil {
t.Fatalf("Encrypt without SetCryptKey should have errored")
}
})
t.Run("Invalid Decrypt", func(t *testing.T) {
_, err := Decrypt("2tHq4GL8r7tTvfk6l2TS8d5nVDXY6ztqz6WTmbmq8ZOJ")
if err == nil {
t.Fatalf("Decrypt without SetCryptKey should have errored")
}
})
})
t.Run("Encrypt/Valid", func(t *testing.T) {
err := SetCryptKey([]byte("12345678901234567890123456789012"))
if err != nil {
Expand Down Expand Up @@ -129,6 +143,13 @@ func TestCryptString(t *testing.T) {
if err != nil || cs.String != "another secret text" {
t.Fatalf("UnmarshalJSON failed to provide original value")
}

badJsonBytes := append(jsonBytes, '}')

err = cs.UnmarshalJSON(badJsonBytes)
if err == nil {
t.Fatalf("UnmarshalJSON with bad json bytes should have errored")
}
})
t.Run("Scan", func(t *testing.T) {
scannable := "2tHq4GL8r7tTvfk6l2TS8d5nVDXY6ztqz6WTmbmq8ZOJ"
Expand All @@ -150,6 +171,12 @@ func TestCryptString(t *testing.T) {
t.Fatalf("Scan of []byte should have matched 'how are you doing', got: '%s'", csByte.String)
}

badScannable := "@tHq4GL8r7tTvfk6l2TS8d5nVDXY6ztqz6WTmbmq8ZOJ"
err = csString.Scan(interface{}(badScannable))
if err == nil {
t.Fatalf("Scan of malformed encrypted string should have errored")
}

var csGoofyType CryptString
err = csGoofyType.Scan(interface{}(12345))
if err == nil {
Expand Down Expand Up @@ -306,6 +333,13 @@ func TestCryptBytes(t *testing.T) {
if !bytes.Equal(originalBytes, cb.Bytes) {
t.Fatalf("UnmarshalJSON should have matched '%s', got: '%s'", originalBytes, cb.Bytes)
}

badJsonBytes := append(jsonBytes, '}')

err = cb.UnmarshalJSON(badJsonBytes)
if err == nil {
t.Fatalf("UnmarshalJSON with bad json bytes should have errored")
}
})
t.Run("Scan", func(t *testing.T) {
scannable := "2tHq4GL8r7tTvfk6l2TS8d5nVDXY6ztqz6WTmbmq8ZOJ"
Expand Down

0 comments on commit 736b856

Please # to comment.