noise/box/box_test.go
2014-07-06 09:45:39 -07:00

69 lines
1.4 KiB
Go

package box
import (
"crypto/rand"
"testing"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type S struct{}
var _ = Suite(&S{})
func (s *S) TestRoundtrip(c *C) {
enc, dec := newCrypters()
plain := []byte("yellow submarines")
padLen := 2
ciphertext, err := enc.EncryptBox(nil, nil, plain, padLen)
c.Assert(err, IsNil)
expectedLen := len(plain) + padLen + (2 * Noise255.DHLen()) + (2 * Noise255.MACLen()) + 4
c.Assert(ciphertext, HasLen, expectedLen, Commentf("expected: %d", expectedLen))
plaintext, err := dec.DecryptBox(ciphertext)
c.Assert(err, IsNil)
c.Assert(plaintext, DeepEquals, plain)
plain[0] = 'Y'
ciphertext, err = enc.EncryptBox(nil, nil, plain, 0)
c.Assert(err, IsNil)
plaintext, err = dec.DecryptBox(ciphertext)
c.Assert(err, IsNil)
c.Assert(plaintext, DeepEquals, plain)
}
func newCrypters() (*Crypter, *Crypter) {
recvKey, _ := Noise255.GenerateKey(rand.Reader)
sendKey, _ := Noise255.GenerateKey(rand.Reader)
enc := &Crypter{
Cipher: Noise255,
Key: sendKey,
PeerKey: recvKey,
}
enc.PeerKey.Private = nil
dec := &Crypter{
Cipher: Noise255,
Key: recvKey,
PeerKey: sendKey,
}
dec.PeerKey.Private = nil
return enc, dec
}
func BenchmarkEncryptBox(b *testing.B) {
enc, _ := newCrypters()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
enc.EncryptBox(nil, nil, []byte("yellow submarine"), 0)
}
}