1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00
v2fly/common/crypto/auth_test.go

144 lines
3.3 KiB
Go
Raw Normal View History

2016-12-12 11:42:03 -05:00
package crypto_test
import (
2018-08-16 06:05:33 -04:00
"bytes"
2016-12-12 11:42:03 -05:00
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
"testing"
2017-02-06 07:31:36 -05:00
2019-02-10 09:02:28 -05:00
"github.com/google/go-cmp/cmp"
2018-07-13 08:36:09 -04:00
"v2ray.com/core/common"
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"
2016-12-12 11:42:03 -05:00
)
func TestAuthenticationReaderWriter(t *testing.T) {
key := make([]byte, 16)
rand.Read(key)
block, err := aes.NewCipher(key)
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-12-12 11:42:03 -05:00
aead, err := cipher.NewGCM(block)
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-12-12 11:42:03 -05:00
2018-03-16 05:22:22 -04:00
const payloadSize = 1024 * 80
rawPayload := make([]byte, payloadSize)
2017-04-23 07:30:08 -04:00
rand.Read(rawPayload)
2016-12-12 11:42:03 -05:00
2018-11-18 13:36:36 -05:00
payload := buf.MergeBytes(nil, rawPayload)
2016-12-12 15:49:04 -05:00
2018-08-16 06:05:33 -04:00
cache := bytes.NewBuffer(nil)
2016-12-12 15:49:04 -05:00
iv := make([]byte, 12)
rand.Read(iv)
writer := NewAuthenticationWriter(&AEADAuthenticator{
2018-04-14 07:10:12 -04:00
AEAD: aead,
NonceGenerator: GenerateStaticBytes(iv),
AdditionalDataGenerator: GenerateEmptyBytes(),
2018-07-07 09:42:24 -04:00
}, PlainChunkSizeParser{}, cache, protocol.TransferTypeStream, nil)
2017-02-06 08:06:41 -05:00
2019-02-10 09:02:28 -05:00
common.Must(writer.WriteMultiBuffer(payload))
if cache.Len() <= 1024*80 {
t.Error("cache len: ", cache.Len())
}
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{}))
2017-02-06 07:31:36 -05:00
2016-12-12 15:49:04 -05:00
reader := NewAuthenticationReader(&AEADAuthenticator{
2018-04-14 07:10:12 -04:00
AEAD: aead,
NonceGenerator: GenerateStaticBytes(iv),
AdditionalDataGenerator: GenerateEmptyBytes(),
2018-07-07 09:42:24 -04:00
}, PlainChunkSizeParser{}, cache, protocol.TransferTypeStream, nil)
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
2018-03-16 05:22:22 -04:00
for mb.Len() < payloadSize {
2017-11-09 16:33:15 -05:00
mb2, err := reader.ReadMultiBuffer()
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-04-27 07:31:09 -04:00
2018-11-17 03:12:20 -05:00
mb, _ = buf.MergeMulti(mb, mb2)
2017-04-27 07:31:09 -04:00
}
2017-02-06 08:06:41 -05:00
2019-02-10 09:02:28 -05:00
if mb.Len() != payloadSize {
t.Error("mb len: ", mb.Len())
}
2018-03-16 05:22:22 -04:00
mbContent := make([]byte, payloadSize)
2018-11-18 13:36:36 -05:00
buf.SplitBytes(mb, mbContent)
2019-02-10 09:02:28 -05:00
if r := cmp.Diff(mbContent, rawPayload); r != "" {
t.Error(r)
}
2016-12-12 15:49:04 -05:00
2017-11-09 16:33:15 -05:00
_, err = reader.ReadMultiBuffer()
2019-02-10 09:02:28 -05:00
if err != io.EOF {
t.Error("error: ", err)
}
2016-12-12 15:49:04 -05:00
}
2017-05-01 18:28:16 -04:00
func TestAuthenticationReaderWriterPacket(t *testing.T) {
key := make([]byte, 16)
2018-07-13 08:36:09 -04:00
common.Must2(rand.Read(key))
2017-05-01 18:28:16 -04:00
block, err := aes.NewCipher(key)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-05-01 18:28:16 -04:00
aead, err := cipher.NewGCM(block)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-05-01 18:28:16 -04:00
2018-08-16 06:05:33 -04:00
cache := buf.New()
2017-05-01 18:28:16 -04:00
iv := make([]byte, 12)
rand.Read(iv)
writer := NewAuthenticationWriter(&AEADAuthenticator{
2018-04-14 07:10:12 -04:00
AEAD: aead,
NonceGenerator: GenerateStaticBytes(iv),
AdditionalDataGenerator: GenerateEmptyBytes(),
2018-07-07 09:42:24 -04:00
}, PlainChunkSizeParser{}, cache, protocol.TransferTypePacket, nil)
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()
2018-04-19 16:56:55 -04:00
pb1.Write([]byte("abcd"))
2018-11-17 03:12:20 -05:00
payload = append(payload, pb1)
2017-05-01 18:28:16 -04:00
pb2 := buf.New()
2018-04-19 16:56:55 -04:00
pb2.Write([]byte("efgh"))
2018-11-17 03:12:20 -05:00
payload = append(payload, pb2)
2017-05-01 18:28:16 -04:00
2019-02-10 09:02:28 -05:00
common.Must(writer.WriteMultiBuffer(payload))
if cache.Len() == 0 {
t.Error("cache len: ", cache.Len())
}
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{}))
2017-05-01 18:28:16 -04:00
reader := NewAuthenticationReader(&AEADAuthenticator{
2018-04-14 07:10:12 -04:00
AEAD: aead,
NonceGenerator: GenerateStaticBytes(iv),
AdditionalDataGenerator: GenerateEmptyBytes(),
2018-07-07 09:42:24 -04:00
}, PlainChunkSizeParser{}, cache, protocol.TransferTypePacket, nil)
2017-05-01 18:28:16 -04:00
2017-11-09 16:33:15 -05:00
mb, err := reader.ReadMultiBuffer()
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-05-01 18:28:16 -04:00
2018-11-18 13:57:29 -05:00
mb, b1 := buf.SplitFirst(mb)
2019-02-10 09:02:28 -05:00
if b1.String() != "abcd" {
t.Error("b1: ", b1.String())
}
2017-12-03 07:23:24 -05:00
2018-11-18 13:57:29 -05:00
mb, b2 := buf.SplitFirst(mb)
2019-02-10 09:02:28 -05:00
if b2.String() != "efgh" {
t.Error("b2: ", b2.String())
}
2018-04-01 17:31:53 -04:00
2019-02-10 09:02:28 -05:00
if !mb.IsEmpty() {
t.Error("not empty")
}
2017-05-01 18:28:16 -04:00
2017-11-09 16:33:15 -05:00
_, err = reader.ReadMultiBuffer()
2019-02-10 09:02:28 -05:00
if err != io.EOF {
t.Error("error: ", err)
}
2017-05-01 18:28:16 -04:00
}