Skip to content

Commit

Permalink
Try strongbox.TestKeyLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
DTLP committed Sep 20, 2023
1 parent f7a2007 commit 9bf6c37
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 11 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ require (
require github.com/uw-labs/strongbox/strongbox-lib v0.0.0-00010101000000-000000000000

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
18 changes: 14 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jacobsa/crypto v0.0.0-20190317225127-9f44e2d11115 h1:YuDUUFNM21CAbyPOpOP8BicaTD/0klJEKt5p8yuw+uY=
github.com/jacobsa/crypto v0.0.0-20190317225127-9f44e2d11115/go.mod h1:LadVJg0XuawGk+8L1rYnIED8451UyNxEMdTWCEt5kmU=
github.com/jacobsa/oglematchers v0.0.0-20150720000706-141901ea67cd h1:9GCSedGjMcLZCrusBZuo4tyKLpKUPenUUqi34AkuFmA=
Expand All @@ -10,17 +12,25 @@ github.com/jacobsa/ogletest v0.0.0-20170503003838-80d50a735a11 h1:BMb8s3ENQLt5ul
github.com/jacobsa/ogletest v0.0.0-20170503003838-80d50a735a11/go.mod h1:+DBdDyfoO2McrOyDemRBq0q9CMEByef7sYl7JH5Q3BI=
github.com/jacobsa/reqtrace v0.0.0-20150505043853-245c9e0234cb h1:uSWBjJdMf47kQlXMwWEfmc864bA1wAC+Kl3ApryuG9Y=
github.com/jacobsa/reqtrace v0.0.0-20150505043853-245c9e0234cb/go.mod h1:ivcmUvxXWjb27NsPEaiYK7AidlZXS7oQ5PowUS9z3I4=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
119 changes: 119 additions & 0 deletions strongbox-lib/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package strongbox

import (
"bytes"
"crypto/rand"
"crypto/sha256"
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

var (
priv, pub []byte
plain = []byte("hello world. this is some plain text for testing")
)

type mockKeyRing struct{}

func (m *mockKeyRing) Load() error {
return nil
}

func (m *mockKeyRing) Save() error {
return nil
}

func (m *mockKeyRing) AddKey(name string, keyID []byte, key []byte) {
}

func (m *mockKeyRing) Key(keyID []byte) ([]byte, error) {
return priv, nil
}

func TestMain(m *testing.M) {
var err error

priv = make([]byte, 32)
_, err = rand.Read(priv)
if err != nil {
panic(err)
}

keyID := sha256.Sum256(priv)
pub = keyID[:]

keyLoader = TestKeyLoader
kr = &mockKeyRing{}

os.Exit(m.Run())
}

func TestKeyLoader(string) ([]byte, error) {
fmt.Println("\nfunc strongbox.TestKeyLoader()")
return priv, nil
}

func TestMultipleClean(t *testing.T) {
fmt.Println("func TestMultipleClean()")
assert := assert.New(t)

var cleaned bytes.Buffer
fmt.Println("cleaned: ", cleaned)
fmt.Println("bytes.NewReader: ", bytes.NewReader(plain))
Clean(bytes.NewReader(plain), &cleaned, "")

var doubleCleaned bytes.Buffer
Clean(bytes.NewReader(cleaned.Bytes()), &doubleCleaned, "")

assert.Equal(cleaned.String(), doubleCleaned.String())
}

func TestSmudgeAlreadyPlaintext(t *testing.T) {
assert := assert.New(t)

var smudged bytes.Buffer
Smudge(bytes.NewReader(plain), &smudged, "")

assert.Equal(string(plain), smudged.String())
}

func TestRoundTrip(t *testing.T) {
assert := assert.New(t)

var cleaned bytes.Buffer
Clean(bytes.NewReader(plain), &cleaned, "")

fmt.Printf("%s", string(cleaned.String()))

assert.NotEqual(plain, cleaned.Bytes())

var smudged bytes.Buffer
Smudge(bytes.NewReader(cleaned.Bytes()), &smudged, "")

assert.Equal(string(plain), smudged.String())
}

func TestDeterministic(t *testing.T) {
assert := assert.New(t)

var cleaned1 bytes.Buffer
Clean(bytes.NewReader(plain), &cleaned1, "")

var cleaned2 bytes.Buffer
Clean(bytes.NewReader(plain), &cleaned2, "")

assert.Equal(cleaned1.String(), cleaned2.String())
}

func BenchmarkRoundTripPlain(b *testing.B) {
for n := 0; n < b.N; n++ {
var cleaned bytes.Buffer
Clean(bytes.NewReader(plain), &cleaned, "")

var smudged bytes.Buffer
Smudge(bytes.NewReader(cleaned.Bytes()), &smudged, "")
}
}
3 changes: 2 additions & 1 deletion strongbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var (
date = "unknown"
builtBy = "unknown"

keyLoader = strongbox.Key
// keyLoader = strongbox.Key
keyLoader = strongbox.TestKeyLoader
kr keyRing
prefix = []byte("# STRONGBOX ENCRYPTED RESOURCE ;")
defaultPrefix = []byte("# STRONGBOX ENCRYPTED RESOURCE ; See https://github.com/uw-labs/strongbox\n")
Expand Down
14 changes: 10 additions & 4 deletions strongbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

var (
// keyLoader = strongbox.TestKeyLoader
priv, pub []byte
plain = []byte("hello world. this is some plain text for testing")
)
Expand All @@ -35,6 +36,7 @@ func (m *mockKeyRing) Key(keyID []byte) ([]byte, error) {
}

func TestMain(m *testing.M) {
fmt.Println("\nfunc TestMain")
var err error

priv = make([]byte, 32)
Expand All @@ -46,18 +48,22 @@ func TestMain(m *testing.M) {
keyID := sha256.Sum256(priv)
pub = keyID[:]

keyLoader = testKeyLoader
keyLoader = strongbox.TestKeyLoader
// keyLoader = testKeyLoader
kr = &mockKeyRing{}

os.Exit(m.Run())
}

func testKeyLoader(string) ([]byte, error) {
return priv, nil
}
// func testKeyLoader(string) ([]byte, error) {
// fmt.Println("\nfunc testKeyLoader()")
// keyLoader = strongbox.TestKeyLoader
// return priv, nil
// }

func TestMultipleClean(t *testing.T) {
fmt.Println("func TestMultipleClean()")

assert := assert.New(t)

var cleaned bytes.Buffer
Expand Down

0 comments on commit 9bf6c37

Please sign in to comment.