From 9bf6c376cff7074f7908c36acbd73380d26f008c Mon Sep 17 00:00:00 2001 From: DTLP Date: Wed, 20 Sep 2023 16:38:14 +0100 Subject: [PATCH] Try strongbox.TestKeyLoader --- go.mod | 6 ++- go.sum | 18 +++++-- strongbox-lib/test.go | 119 ++++++++++++++++++++++++++++++++++++++++++ strongbox.go | 3 +- strongbox_test.go | 14 +++-- 5 files changed, 149 insertions(+), 11 deletions(-) create mode 100644 strongbox-lib/test.go diff --git a/go.mod b/go.mod index e6cb00b..1c3bfcf 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 1a75760..63171fa 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/strongbox-lib/test.go b/strongbox-lib/test.go new file mode 100644 index 0000000..c08b304 --- /dev/null +++ b/strongbox-lib/test.go @@ -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, "") + } +} diff --git a/strongbox.go b/strongbox.go index 1f12f4d..0023e35 100644 --- a/strongbox.go +++ b/strongbox.go @@ -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") diff --git a/strongbox_test.go b/strongbox_test.go index dd754b9..30f922d 100644 --- a/strongbox_test.go +++ b/strongbox_test.go @@ -13,6 +13,7 @@ import ( ) var ( + // keyLoader = strongbox.TestKeyLoader priv, pub []byte plain = []byte("hello world. this is some plain text for testing") ) @@ -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) @@ -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