1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00
v2fly/proxy/vmess/aead/encrypt_test.go

105 lines
2.5 KiB
Go
Raw Normal View History

2020-07-04 01:48:47 -04:00
package aead
import (
"bytes"
"fmt"
"io"
"testing"
"github.com/stretchr/testify/assert"
2020-07-04 01:48:47 -04:00
)
func TestOpenVMessAEADHeader(t *testing.T) {
TestHeader := []byte("Test Header")
key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
var keyw [16]byte
copy(keyw[:], key)
sealed := SealVMessAEADHeader(keyw, TestHeader)
2021-05-19 17:28:52 -04:00
AEADR := bytes.NewReader(sealed)
2020-07-04 01:48:47 -04:00
var authid [16]byte
io.ReadFull(AEADR, authid[:])
out, _, _, err := OpenVMessAEADHeader(keyw, authid, AEADR)
2020-07-04 01:48:47 -04:00
fmt.Println(string(out))
fmt.Println(err)
}
func TestOpenVMessAEADHeader2(t *testing.T) {
TestHeader := []byte("Test Header")
key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
var keyw [16]byte
copy(keyw[:], key)
sealed := SealVMessAEADHeader(keyw, TestHeader)
2021-05-19 17:28:52 -04:00
AEADR := bytes.NewReader(sealed)
2020-07-04 01:48:47 -04:00
var authid [16]byte
io.ReadFull(AEADR, authid[:])
out, _, readen, err := OpenVMessAEADHeader(keyw, authid, AEADR)
2020-07-04 01:48:47 -04:00
assert.Equal(t, len(sealed)-16-AEADR.Len(), readen)
assert.Equal(t, string(TestHeader), string(out))
assert.Nil(t, err)
}
func TestOpenVMessAEADHeader4(t *testing.T) {
for i := 0; i <= 60; i++ {
TestHeader := []byte("Test Header")
key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
var keyw [16]byte
copy(keyw[:], key)
sealed := SealVMessAEADHeader(keyw, TestHeader)
var sealedm [16]byte
copy(sealedm[:], sealed)
sealed[i] ^= 0xff
2021-05-19 17:28:52 -04:00
AEADR := bytes.NewReader(sealed)
2020-07-04 01:48:47 -04:00
var authid [16]byte
io.ReadFull(AEADR, authid[:])
out, drain, readen, err := OpenVMessAEADHeader(keyw, authid, AEADR)
2020-07-04 01:48:47 -04:00
assert.Equal(t, len(sealed)-16-AEADR.Len(), readen)
assert.Equal(t, true, drain)
assert.NotNil(t, err)
if err == nil {
fmt.Println(">")
}
assert.Nil(t, out)
}
}
func TestOpenVMessAEADHeader4Massive(t *testing.T) {
for j := 0; j < 1000; j++ {
for i := 0; i <= 60; i++ {
TestHeader := []byte("Test Header")
key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
var keyw [16]byte
copy(keyw[:], key)
sealed := SealVMessAEADHeader(keyw, TestHeader)
var sealedm [16]byte
copy(sealedm[:], sealed)
sealed[i] ^= 0xff
2021-05-19 17:28:52 -04:00
AEADR := bytes.NewReader(sealed)
2020-07-04 01:48:47 -04:00
var authid [16]byte
io.ReadFull(AEADR, authid[:])
out, drain, readen, err := OpenVMessAEADHeader(keyw, authid, AEADR)
2020-07-04 01:48:47 -04:00
assert.Equal(t, len(sealed)-16-AEADR.Len(), readen)
assert.Equal(t, true, drain)
assert.NotNil(t, err)
if err == nil {
fmt.Println(">")
}
assert.Nil(t, out)
}
}
}