-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
149 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters