1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/common/crypto/auth_test.go

132 lines
3.0 KiB
Go
Raw Normal View History

2016-12-12 11:42:03 -05:00
package crypto_test
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
"testing"
2017-02-06 07:31:36 -05:00
2016-12-12 11:42:03 -05:00
"v2ray.com/core/common/buf"
. "v2ray.com/core/common/crypto"
2017-05-02 16:23:07 -04:00
"v2ray.com/core/common/protocol"
2017-10-24 10:15:35 -04:00
. "v2ray.com/ext/assert"
2016-12-12 11:42:03 -05:00
)
func TestAuthenticationReaderWriter(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-12-12 11:42:03 -05:00
key := make([]byte, 16)
rand.Read(key)
block, err := aes.NewCipher(key)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2016-12-12 11:42:03 -05:00
aead, err := cipher.NewGCM(block)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2016-12-12 11:42:03 -05:00
2017-04-27 07:31:09 -04:00
rawPayload := make([]byte, 8192*10)
2017-04-23 07:30:08 -04:00
rand.Read(rawPayload)
2016-12-12 11:42:03 -05:00
2017-04-27 07:31:09 -04:00
payload := buf.NewLocal(8192 * 10)
2017-04-23 07:30:08 -04:00
payload.Append(rawPayload)
2016-12-12 15:49:04 -05:00
2017-04-27 07:31:09 -04:00
cache := buf.NewLocal(160 * 1024)
2016-12-12 15:49:04 -05:00
iv := make([]byte, 12)
rand.Read(iv)
writer := NewAuthenticationWriter(&AEADAuthenticator{
AEAD: aead,
NonceGenerator: &StaticBytesGenerator{
Content: iv,
},
AdditionalDataGenerator: &NoOpBytesGenerator{},
2017-05-02 16:23:07 -04:00
}, PlainChunkSizeParser{}, cache, protocol.TransferTypeStream)
2017-02-06 08:06:41 -05:00
2017-11-09 16:33:15 -05:00
assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(payload)), IsNil)
2017-11-21 16:03:43 -05:00
assert(cache.Len(), Equals, 82658)
2017-11-09 16:33:15 -05:00
assert(writer.WriteMultiBuffer(buf.MultiBuffer{}), IsNil)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-02-06 07:31:36 -05:00
2016-12-12 15:49:04 -05:00
reader := NewAuthenticationReader(&AEADAuthenticator{
AEAD: aead,
NonceGenerator: &StaticBytesGenerator{
Content: iv,
},
AdditionalDataGenerator: &NoOpBytesGenerator{},
2017-05-02 16:23:07 -04:00
}, PlainChunkSizeParser{}, cache, protocol.TransferTypeStream)
2016-12-12 15:49:04 -05:00
2017-11-08 18:55:28 -05:00
var mb buf.MultiBuffer
2017-04-27 07:31:09 -04:00
for mb.Len() < len(rawPayload) {
2017-11-09 16:33:15 -05:00
mb2, err := reader.ReadMultiBuffer()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-04-27 07:31:09 -04:00
mb.AppendMulti(mb2)
}
2017-02-06 08:06:41 -05:00
2017-04-27 07:31:09 -04:00
mbContent := make([]byte, 8192*10)
2017-04-23 07:30:08 -04:00
mb.Read(mbContent)
2017-10-24 10:15:35 -04:00
assert(mbContent, Equals, rawPayload)
2016-12-12 15:49:04 -05:00
2017-11-09 16:33:15 -05:00
_, err = reader.ReadMultiBuffer()
2017-10-24 10:15:35 -04:00
assert(err, Equals, io.EOF)
2016-12-12 15:49:04 -05:00
}
2017-05-01 18:28:16 -04:00
func TestAuthenticationReaderWriterPacket(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-05-01 18:28:16 -04:00
key := make([]byte, 16)
rand.Read(key)
block, err := aes.NewCipher(key)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-05-01 18:28:16 -04:00
aead, err := cipher.NewGCM(block)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-05-01 18:28:16 -04:00
cache := buf.NewLocal(1024)
iv := make([]byte, 12)
rand.Read(iv)
writer := NewAuthenticationWriter(&AEADAuthenticator{
AEAD: aead,
NonceGenerator: &StaticBytesGenerator{
Content: iv,
},
AdditionalDataGenerator: &NoOpBytesGenerator{},
2017-05-02 16:23:07 -04:00
}, PlainChunkSizeParser{}, cache, protocol.TransferTypePacket)
2017-05-01 18:28:16 -04:00
2017-11-08 18:55:28 -05:00
var payload buf.MultiBuffer
2017-05-01 18:28:16 -04:00
pb1 := buf.New()
pb1.Append([]byte("abcd"))
payload.Append(pb1)
pb2 := buf.New()
pb2.Append([]byte("efgh"))
payload.Append(pb2)
2017-11-09 16:33:15 -05:00
assert(writer.WriteMultiBuffer(payload), IsNil)
2017-10-24 10:15:35 -04:00
assert(cache.Len(), GreaterThan, 0)
2017-11-09 16:33:15 -05:00
assert(writer.WriteMultiBuffer(buf.MultiBuffer{}), IsNil)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-05-01 18:28:16 -04:00
reader := NewAuthenticationReader(&AEADAuthenticator{
AEAD: aead,
NonceGenerator: &StaticBytesGenerator{
Content: iv,
},
AdditionalDataGenerator: &NoOpBytesGenerator{},
2017-05-02 16:23:07 -04:00
}, PlainChunkSizeParser{}, cache, protocol.TransferTypePacket)
2017-05-01 18:28:16 -04:00
2017-11-09 16:33:15 -05:00
mb, err := reader.ReadMultiBuffer()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-05-01 18:28:16 -04:00
b1 := mb.SplitFirst()
2017-10-24 10:15:35 -04:00
assert(b1.String(), Equals, "abcd")
2017-05-01 18:28:16 -04:00
b2 := mb.SplitFirst()
2017-10-24 10:15:35 -04:00
assert(b2.String(), Equals, "efgh")
assert(mb.IsEmpty(), IsTrue)
2017-05-01 18:28:16 -04:00
2017-11-09 16:33:15 -05:00
_, err = reader.ReadMultiBuffer()
2017-10-24 10:15:35 -04:00
assert(err, Equals, io.EOF)
2017-05-01 18:28:16 -04:00
}