github.com/imannamdari/v2ray-core/v5@v5.0.5/proxy/vmess/aead/encrypt_test.go (about)

     1  package aead
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestOpenVMessAEADHeader(t *testing.T) {
    13  	TestHeader := []byte("Test Header")
    14  	key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
    15  	var keyw [16]byte
    16  	copy(keyw[:], key)
    17  	sealed := SealVMessAEADHeader(keyw, TestHeader)
    18  
    19  	AEADR := bytes.NewReader(sealed)
    20  
    21  	var authid [16]byte
    22  
    23  	io.ReadFull(AEADR, authid[:])
    24  
    25  	out, _, _, err := OpenVMessAEADHeader(keyw, authid, AEADR)
    26  
    27  	fmt.Println(string(out))
    28  	fmt.Println(err)
    29  }
    30  
    31  func TestOpenVMessAEADHeader2(t *testing.T) {
    32  	TestHeader := []byte("Test Header")
    33  	key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
    34  	var keyw [16]byte
    35  	copy(keyw[:], key)
    36  	sealed := SealVMessAEADHeader(keyw, TestHeader)
    37  
    38  	AEADR := bytes.NewReader(sealed)
    39  
    40  	var authid [16]byte
    41  
    42  	io.ReadFull(AEADR, authid[:])
    43  
    44  	out, _, readen, err := OpenVMessAEADHeader(keyw, authid, AEADR)
    45  	assert.Equal(t, len(sealed)-16-AEADR.Len(), readen)
    46  	assert.Equal(t, string(TestHeader), string(out))
    47  	assert.Nil(t, err)
    48  }
    49  
    50  func TestOpenVMessAEADHeader4(t *testing.T) {
    51  	for i := 0; i <= 60; i++ {
    52  		TestHeader := []byte("Test Header")
    53  		key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
    54  		var keyw [16]byte
    55  		copy(keyw[:], key)
    56  		sealed := SealVMessAEADHeader(keyw, TestHeader)
    57  		var sealedm [16]byte
    58  		copy(sealedm[:], sealed)
    59  		sealed[i] ^= 0xff
    60  		AEADR := bytes.NewReader(sealed)
    61  
    62  		var authid [16]byte
    63  
    64  		io.ReadFull(AEADR, authid[:])
    65  
    66  		out, drain, readen, err := OpenVMessAEADHeader(keyw, authid, AEADR)
    67  		assert.Equal(t, len(sealed)-16-AEADR.Len(), readen)
    68  		assert.Equal(t, true, drain)
    69  		assert.NotNil(t, err)
    70  		if err == nil {
    71  			fmt.Println(">")
    72  		}
    73  		assert.Nil(t, out)
    74  	}
    75  }
    76  
    77  func TestOpenVMessAEADHeader4Massive(t *testing.T) {
    78  	for j := 0; j < 1000; j++ {
    79  		for i := 0; i <= 60; i++ {
    80  			TestHeader := []byte("Test Header")
    81  			key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
    82  			var keyw [16]byte
    83  			copy(keyw[:], key)
    84  			sealed := SealVMessAEADHeader(keyw, TestHeader)
    85  			var sealedm [16]byte
    86  			copy(sealedm[:], sealed)
    87  			sealed[i] ^= 0xff
    88  			AEADR := bytes.NewReader(sealed)
    89  
    90  			var authid [16]byte
    91  
    92  			io.ReadFull(AEADR, authid[:])
    93  
    94  			out, drain, readen, err := OpenVMessAEADHeader(keyw, authid, AEADR)
    95  			assert.Equal(t, len(sealed)-16-AEADR.Len(), readen)
    96  			assert.Equal(t, true, drain)
    97  			assert.NotNil(t, err)
    98  			if err == nil {
    99  				fmt.Println(">")
   100  			}
   101  			assert.Nil(t, out)
   102  		}
   103  	}
   104  }