forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad1927e
commit 437a66b
Showing
4 changed files
with
246 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package aead | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"strconv" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCreateAuthID(t *testing.T) { | ||
key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test") | ||
authid := CreateAuthID(key, time.Now().Unix()) | ||
|
||
fmt.Println(key) | ||
fmt.Println(authid) | ||
} | ||
|
||
func TestCreateAuthIDAndDecode(t *testing.T) { | ||
key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test") | ||
authid := CreateAuthID(key, time.Now().Unix()) | ||
|
||
fmt.Println(key) | ||
fmt.Println(authid) | ||
|
||
AuthDecoder := NewAuthIDDecoderHolder() | ||
var keyw [16]byte | ||
copy(keyw[:], key) | ||
AuthDecoder.AddUser(keyw, "Demo User") | ||
res, err := AuthDecoder.Match(authid) | ||
fmt.Println(res) | ||
fmt.Println(err) | ||
assert.Equal(t, "Demo User", res) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestCreateAuthIDAndDecode2(t *testing.T) { | ||
key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test") | ||
authid := CreateAuthID(key, time.Now().Unix()) | ||
|
||
fmt.Println(key) | ||
fmt.Println(authid) | ||
|
||
AuthDecoder := NewAuthIDDecoderHolder() | ||
var keyw [16]byte | ||
copy(keyw[:], key) | ||
AuthDecoder.AddUser(keyw, "Demo User") | ||
res, err := AuthDecoder.Match(authid) | ||
fmt.Println(res) | ||
fmt.Println(err) | ||
assert.Equal(t, "Demo User", res) | ||
assert.Nil(t, err) | ||
|
||
key2 := KDF16([]byte("Demo Key for Auth ID Test2"), "Demo Path for Auth ID Test") | ||
authid2 := CreateAuthID(key2, time.Now().Unix()) | ||
|
||
res2, err2 := AuthDecoder.Match(authid2) | ||
assert.EqualError(t, err2, "user do not exist") | ||
assert.Nil(t, res2) | ||
|
||
} | ||
|
||
func TestCreateAuthIDAndDecodeMassive(t *testing.T) { | ||
key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test") | ||
authid := CreateAuthID(key, time.Now().Unix()) | ||
|
||
fmt.Println(key) | ||
fmt.Println(authid) | ||
|
||
AuthDecoder := NewAuthIDDecoderHolder() | ||
var keyw [16]byte | ||
copy(keyw[:], key) | ||
AuthDecoder.AddUser(keyw, "Demo User") | ||
res, err := AuthDecoder.Match(authid) | ||
fmt.Println(res) | ||
fmt.Println(err) | ||
assert.Equal(t, "Demo User", res) | ||
assert.Nil(t, err) | ||
|
||
for i := 0; i <= 10000; i++ { | ||
key2 := KDF16([]byte("Demo Key for Auth ID Test2"), "Demo Path for Auth ID Test", strconv.Itoa(i)) | ||
var keyw2 [16]byte | ||
copy(keyw2[:], key2) | ||
AuthDecoder.AddUser(keyw2, "Demo User"+strconv.Itoa(i)) | ||
} | ||
|
||
authid3 := CreateAuthID(key, time.Now().Unix()) | ||
|
||
res2, err2 := AuthDecoder.Match(authid3) | ||
assert.Equal(t, "Demo User", res2) | ||
assert.Nil(t, err2) | ||
|
||
} | ||
|
||
func TestCreateAuthIDAndDecodeSuperMassive(t *testing.T) { | ||
key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test") | ||
authid := CreateAuthID(key, time.Now().Unix()) | ||
|
||
fmt.Println(key) | ||
fmt.Println(authid) | ||
|
||
AuthDecoder := NewAuthIDDecoderHolder() | ||
var keyw [16]byte | ||
copy(keyw[:], key) | ||
AuthDecoder.AddUser(keyw, "Demo User") | ||
res, err := AuthDecoder.Match(authid) | ||
fmt.Println(res) | ||
fmt.Println(err) | ||
assert.Equal(t, "Demo User", res) | ||
assert.Nil(t, err) | ||
|
||
for i := 0; i <= 1000000; i++ { | ||
key2 := KDF16([]byte("Demo Key for Auth ID Test2"), "Demo Path for Auth ID Test", strconv.Itoa(i)) | ||
var keyw2 [16]byte | ||
copy(keyw2[:], key2) | ||
AuthDecoder.AddUser(keyw2, "Demo User"+strconv.Itoa(i)) | ||
} | ||
|
||
authid3 := CreateAuthID(key, time.Now().Unix()) | ||
|
||
before := time.Now() | ||
res2, err2 := AuthDecoder.Match(authid3) | ||
after := time.Now() | ||
assert.Equal(t, "Demo User", res2) | ||
assert.Nil(t, err2) | ||
|
||
fmt.Println(after.Sub(before).Seconds()) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package aead | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"testing" | ||
) | ||
|
||
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) | ||
|
||
var AEADR = bytes.NewReader(sealed) | ||
|
||
var authid [16]byte | ||
|
||
io.ReadFull(AEADR, authid[:]) | ||
|
||
out, _, err, _ := OpenVMessAEADHeader(keyw, authid, AEADR) | ||
|
||
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) | ||
|
||
var AEADR = bytes.NewReader(sealed) | ||
|
||
var authid [16]byte | ||
|
||
io.ReadFull(AEADR, authid[:]) | ||
|
||
out, _, err, readen := OpenVMessAEADHeader(keyw, authid, AEADR) | ||
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 | ||
var AEADR = bytes.NewReader(sealed) | ||
|
||
var authid [16]byte | ||
|
||
io.ReadFull(AEADR, authid[:]) | ||
|
||
out, drain, err, readen := OpenVMessAEADHeader(keyw, authid, AEADR) | ||
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 | ||
var AEADR = bytes.NewReader(sealed) | ||
|
||
var authid [16]byte | ||
|
||
io.ReadFull(AEADR, authid[:]) | ||
|
||
out, drain, err, readen := OpenVMessAEADHeader(keyw, authid, AEADR) | ||
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) | ||
} | ||
} | ||
} |