From ed9217bf20dfd30b96537ff8b8b6e3234f9f5220 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Mon, 12 Dec 2016 21:49:04 +0100 Subject: [PATCH] test partial read --- common/crypto/auth_test.go | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/common/crypto/auth_test.go b/common/crypto/auth_test.go index 4648320f..0180abff 100644 --- a/common/crypto/auth_test.go +++ b/common/crypto/auth_test.go @@ -63,6 +63,62 @@ func TestAuthenticationReaderWriter(t *testing.T) { assert.Error(err).Equals(io.EOF) } +func TestAuthenticationReaderWriterPartial(t *testing.T) { + assert := assert.On(t) + + key := make([]byte, 16) + rand.Read(key) + block, err := aes.NewCipher(key) + assert.Error(err).IsNil() + + aead, err := cipher.NewGCM(block) + assert.Error(err).IsNil() + + payload := make([]byte, 8*1024) + rand.Read(payload) + + cache := buf.NewLocal(16 * 1024) + iv := make([]byte, 12) + rand.Read(iv) + + writer := NewAuthenticationWriter(&AEADAuthenticator{ + AEAD: aead, + NonceGenerator: &StaticBytesGenerator{ + Content: iv, + }, + AdditionalDataGenerator: &NoOpBytesGenerator{}, + }, cache) + + nBytes, err := writer.Write(payload) + assert.Error(err).IsNil() + assert.Int(nBytes).Equals(len(payload)) + assert.Int(cache.Len()).GreaterThan(0) + _, err = writer.Write([]byte{}) + assert.Error(err).IsNil() + + reader := NewAuthenticationReader(&AEADAuthenticator{ + AEAD: aead, + NonceGenerator: &StaticBytesGenerator{ + Content: iv, + }, + AdditionalDataGenerator: &NoOpBytesGenerator{}, + }, cache, false) + + actualPayload := make([]byte, 7*1024) + nBytes, err = reader.Read(actualPayload) + assert.Error(err).IsNil() + assert.Int(nBytes).Equals(len(actualPayload)) + assert.Bytes(actualPayload[:nBytes]).Equals(payload[:nBytes]) + + nBytes, err = reader.Read(actualPayload) + assert.Error(err).IsNil() + assert.Int(nBytes).Equals(len(payload) - len(actualPayload)) + assert.Bytes(actualPayload[:nBytes]).Equals(payload[7*1024:]) + + _, err = reader.Read(actualPayload) + assert.Error(err).Equals(io.EOF) +} + func TestAuthenticationReaderWriterAggressive(t *testing.T) { assert := assert.On(t)