github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/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  	var 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  	var AEADR = bytes.NewReader(sealed)
    39  
    40  	var authid [16]byte
    41  
    42  	io.ReadFull(AEADR, authid[:])
    43  
    44  	out, _, err, readen := 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  		var AEADR = bytes.NewReader(sealed)
    61  
    62  		var authid [16]byte
    63  
    64  		io.ReadFull(AEADR, authid[:])
    65  
    66  		out, drain, err, readen := 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  
    78  func TestOpenVMessAEADHeader4Massive(t *testing.T) {
    79  	for j := 0; j < 1000; j++ {
    80  
    81  		for i := 0; i <= 60; i++ {
    82  			TestHeader := []byte("Test Header")
    83  			key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
    84  			var keyw [16]byte
    85  			copy(keyw[:], key)
    86  			sealed := SealVMessAEADHeader(keyw, TestHeader)
    87  			var sealedm [16]byte
    88  			copy(sealedm[:], sealed)
    89  			sealed[i] ^= 0xff
    90  			var AEADR = bytes.NewReader(sealed)
    91  
    92  			var authid [16]byte
    93  
    94  			io.ReadFull(AEADR, authid[:])
    95  
    96  			out, drain, err, readen := OpenVMessAEADHeader(keyw, authid, AEADR)
    97  			assert.Equal(t, len(sealed)-16-AEADR.Len(), readen)
    98  			assert.Equal(t, true, drain)
    99  			assert.NotNil(t, err)
   100  			if err == nil {
   101  				fmt.Println(">")
   102  			}
   103  			assert.Nil(t, out)
   104  		}
   105  	}
   106  }