mirror of https://github.com/v2ray/v2ray-core
V2Ray
9 years ago
2 changed files with 90 additions and 90 deletions
@ -1,67 +1,67 @@ |
|||||||
package vmess |
package vmess |
||||||
|
|
||||||
import ( |
import ( |
||||||
"bytes" |
"bytes" |
||||||
"crypto/aes" |
"crypto/aes" |
||||||
"crypto/rand" |
"crypto/rand" |
||||||
mrand "math/rand" |
mrand "math/rand" |
||||||
"testing" |
"testing" |
||||||
) |
) |
||||||
|
|
||||||
func randomBytes(p []byte, t *testing.T) { |
func randomBytes(p []byte, t *testing.T) { |
||||||
nBytes, err := rand.Read(p) |
nBytes, err := rand.Read(p) |
||||||
if err != nil { |
if err != nil { |
||||||
t.Fatal(err) |
t.Fatal(err) |
||||||
} |
} |
||||||
if nBytes != len(p) { |
if nBytes != len(p) { |
||||||
t.Error("Unable to generate %d bytes of random buffer", len(p)) |
t.Error("Unable to generate %d bytes of random buffer", len(p)) |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
func TestNormalReading(t *testing.T) { |
func TestNormalReading(t *testing.T) { |
||||||
testSize := 256 |
testSize := 256 |
||||||
plaintext := make([]byte, testSize) |
plaintext := make([]byte, testSize) |
||||||
randomBytes(plaintext, t) |
randomBytes(plaintext, t) |
||||||
|
|
||||||
keySize := 16 |
keySize := 16 |
||||||
key := make([]byte, keySize) |
key := make([]byte, keySize) |
||||||
randomBytes(key, t) |
randomBytes(key, t) |
||||||
|
|
||||||
cipher, err := aes.NewCipher(key) |
cipher, err := aes.NewCipher(key) |
||||||
if err != nil { |
if err != nil { |
||||||
t.Fatal(err) |
t.Fatal(err) |
||||||
} |
} |
||||||
|
|
||||||
ciphertext := make([]byte, testSize) |
ciphertext := make([]byte, testSize) |
||||||
for encryptSize := 0; encryptSize < testSize; encryptSize += blockSize { |
for encryptSize := 0; encryptSize < testSize; encryptSize += blockSize { |
||||||
cipher.Encrypt(ciphertext[encryptSize:], plaintext[encryptSize:]) |
cipher.Encrypt(ciphertext[encryptSize:], plaintext[encryptSize:]) |
||||||
} |
} |
||||||
|
|
||||||
ciphertextcopy := make([]byte, testSize) |
ciphertextcopy := make([]byte, testSize) |
||||||
copy(ciphertextcopy, ciphertext) |
copy(ciphertextcopy, ciphertext) |
||||||
|
|
||||||
reader, err := NewDecryptionReader(bytes.NewReader(ciphertextcopy), key) |
reader, err := NewDecryptionReader(bytes.NewReader(ciphertextcopy), key) |
||||||
if err != nil { |
if err != nil { |
||||||
t.Fatal(err) |
t.Fatal(err) |
||||||
} |
} |
||||||
|
|
||||||
readtext := make([]byte, testSize) |
readtext := make([]byte, testSize) |
||||||
readSize := 0 |
readSize := 0 |
||||||
for readSize < testSize { |
for readSize < testSize { |
||||||
nBytes := mrand.Intn(16) + 1 |
nBytes := mrand.Intn(16) + 1 |
||||||
if nBytes > testSize - readSize { |
if nBytes > testSize-readSize { |
||||||
nBytes = testSize - readSize |
nBytes = testSize - readSize |
||||||
} |
} |
||||||
bytesRead, err := reader.Read(readtext[readSize:readSize + nBytes]) |
bytesRead, err := reader.Read(readtext[readSize : readSize+nBytes]) |
||||||
if err != nil { |
if err != nil { |
||||||
t.Fatal(err) |
t.Fatal(err) |
||||||
} |
} |
||||||
if bytesRead != nBytes { |
if bytesRead != nBytes { |
||||||
t.Errorf("Expected to read %d bytes, but only read %d bytes", nBytes, bytesRead) |
t.Errorf("Expected to read %d bytes, but only read %d bytes", nBytes, bytesRead) |
||||||
} |
} |
||||||
readSize += nBytes |
readSize += nBytes |
||||||
} |
} |
||||||
if ! bytes.Equal(readtext, plaintext) { |
if !bytes.Equal(readtext, plaintext) { |
||||||
t.Errorf("Expected plaintext %v, but got %v", plaintext, readtext) |
t.Errorf("Expected plaintext %v, but got %v", plaintext, readtext) |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue