github.com/v2fly/v2ray-core/v4@v4.45.2/common/crypto/auth_test.go (about)

     1  package crypto_test
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/aes"
     6  	"crypto/cipher"
     7  	"crypto/rand"
     8  	"io"
     9  	"testing"
    10  
    11  	"github.com/google/go-cmp/cmp"
    12  
    13  	"github.com/v2fly/v2ray-core/v4/common"
    14  	"github.com/v2fly/v2ray-core/v4/common/buf"
    15  	. "github.com/v2fly/v2ray-core/v4/common/crypto"
    16  	"github.com/v2fly/v2ray-core/v4/common/protocol"
    17  )
    18  
    19  func TestAuthenticationReaderWriter(t *testing.T) {
    20  	key := make([]byte, 16)
    21  	rand.Read(key)
    22  	block, err := aes.NewCipher(key)
    23  	common.Must(err)
    24  
    25  	aead, err := cipher.NewGCM(block)
    26  	common.Must(err)
    27  
    28  	const payloadSize = 1024 * 80
    29  	rawPayload := make([]byte, payloadSize)
    30  	rand.Read(rawPayload)
    31  
    32  	payload := buf.MergeBytes(nil, rawPayload)
    33  
    34  	cache := bytes.NewBuffer(nil)
    35  	iv := make([]byte, 12)
    36  	rand.Read(iv)
    37  
    38  	writer := NewAuthenticationWriter(&AEADAuthenticator{
    39  		AEAD:                    aead,
    40  		NonceGenerator:          GenerateStaticBytes(iv),
    41  		AdditionalDataGenerator: GenerateEmptyBytes(),
    42  	}, PlainChunkSizeParser{}, cache, protocol.TransferTypeStream, nil)
    43  
    44  	common.Must(writer.WriteMultiBuffer(payload))
    45  	if cache.Len() <= 1024*80 {
    46  		t.Error("cache len: ", cache.Len())
    47  	}
    48  	common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{}))
    49  
    50  	reader := NewAuthenticationReader(&AEADAuthenticator{
    51  		AEAD:                    aead,
    52  		NonceGenerator:          GenerateStaticBytes(iv),
    53  		AdditionalDataGenerator: GenerateEmptyBytes(),
    54  	}, PlainChunkSizeParser{}, cache, protocol.TransferTypeStream, nil)
    55  
    56  	var mb buf.MultiBuffer
    57  
    58  	for mb.Len() < payloadSize {
    59  		mb2, err := reader.ReadMultiBuffer()
    60  		common.Must(err)
    61  
    62  		mb, _ = buf.MergeMulti(mb, mb2)
    63  	}
    64  
    65  	if mb.Len() != payloadSize {
    66  		t.Error("mb len: ", mb.Len())
    67  	}
    68  
    69  	mbContent := make([]byte, payloadSize)
    70  	buf.SplitBytes(mb, mbContent)
    71  	if r := cmp.Diff(mbContent, rawPayload); r != "" {
    72  		t.Error(r)
    73  	}
    74  
    75  	_, err = reader.ReadMultiBuffer()
    76  	if err != io.EOF {
    77  		t.Error("error: ", err)
    78  	}
    79  }
    80  
    81  func TestAuthenticationReaderWriterPacket(t *testing.T) {
    82  	key := make([]byte, 16)
    83  	common.Must2(rand.Read(key))
    84  	block, err := aes.NewCipher(key)
    85  	common.Must(err)
    86  
    87  	aead, err := cipher.NewGCM(block)
    88  	common.Must(err)
    89  
    90  	cache := buf.New()
    91  	iv := make([]byte, 12)
    92  	rand.Read(iv)
    93  
    94  	writer := NewAuthenticationWriter(&AEADAuthenticator{
    95  		AEAD:                    aead,
    96  		NonceGenerator:          GenerateStaticBytes(iv),
    97  		AdditionalDataGenerator: GenerateEmptyBytes(),
    98  	}, PlainChunkSizeParser{}, cache, protocol.TransferTypePacket, nil)
    99  
   100  	var payload buf.MultiBuffer
   101  	pb1 := buf.New()
   102  	pb1.Write([]byte("abcd"))
   103  	payload = append(payload, pb1)
   104  
   105  	pb2 := buf.New()
   106  	pb2.Write([]byte("efgh"))
   107  	payload = append(payload, pb2)
   108  
   109  	common.Must(writer.WriteMultiBuffer(payload))
   110  	if cache.Len() == 0 {
   111  		t.Error("cache len: ", cache.Len())
   112  	}
   113  
   114  	common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{}))
   115  
   116  	reader := NewAuthenticationReader(&AEADAuthenticator{
   117  		AEAD:                    aead,
   118  		NonceGenerator:          GenerateStaticBytes(iv),
   119  		AdditionalDataGenerator: GenerateEmptyBytes(),
   120  	}, PlainChunkSizeParser{}, cache, protocol.TransferTypePacket, nil)
   121  
   122  	mb, err := reader.ReadMultiBuffer()
   123  	common.Must(err)
   124  
   125  	mb, b1 := buf.SplitFirst(mb)
   126  	if b1.String() != "abcd" {
   127  		t.Error("b1: ", b1.String())
   128  	}
   129  
   130  	mb, b2 := buf.SplitFirst(mb)
   131  	if b2.String() != "efgh" {
   132  		t.Error("b2: ", b2.String())
   133  	}
   134  
   135  	if !mb.IsEmpty() {
   136  		t.Error("not empty")
   137  	}
   138  
   139  	_, err = reader.ReadMultiBuffer()
   140  	if err != io.EOF {
   141  		t.Error("error: ", err)
   142  	}
   143  }