Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
xxf098 committed Aug 20, 2023
1 parent b4edcb7 commit 8e54e42
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 0 deletions.
19 changes: 19 additions & 0 deletions outbound/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package outbound

import (
"fmt"
"testing"

C "github.com/xxf098/lite-proxy/constant"
)

func TestSerializesSocksAddr(t *testing.T) {
metadata := C.Metadata{
NetWork: 3,
Type: 7,
DstPort: "443",
Host: "tj.trojanfree.com",
}
buf := serializesSocksAddr(&metadata)
fmt.Println(buf)
}
63 changes: 63 additions & 0 deletions transport/ssr/protocol/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package protocol

import (
"crypto/rc4"
"fmt"
"hash/adler32"
"hash/crc32"
"testing"

"github.com/xxf098/lite-proxy/transport/ssr/tools"
)

func TestAuthAes128Sha1Encode(t *testing.T) {
// base := &Base{
// Key: []byte{253, 53, 153, 216, 16, 234, 129, 68, 247, 183, 217, 78, 176, 51, 247, 223, 201, 115, 166, 180, 163, 135, 75, 165, 113, 87, 160, 116, 106, 70, 47, 122},
// Overhead: 14,
// Param: "29220:k5A4Ni",
// }
// auth := newAuthAES128SHA1(base)

// data := []byte{3, 19, 99, 108, 105, 101, 110, 116, 115, 51, 46, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 0, 80}
// buf := tools.BufPool.Get().(*bytes.Buffer)
// auth.Encode(buf, data)
// fmt.Println(buf.Bytes())
}

func TestAuthAes128Sha1Decode(t *testing.T) {
}

func TestCrc32(t *testing.T) {
buf := []byte{3, 19, 99, 108, 105, 101, 110, 116, 115, 51, 46, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 0, 80}
h := 0xffffffff - crc32.ChecksumIEEE(buf)
fmt.Println(h)
}

func TestAdler32(t *testing.T) {
buf := []byte{3, 19, 99, 108, 105, 101, 110, 116, 115, 51, 46, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 0, 80}
h := adler32.Checksum(buf)
fmt.Println(h)
}

func TestGetRandLength(t *testing.T) {
lastHash := []byte{36, 253, 199, 121, 90, 151, 29, 181, 227, 115, 216, 51, 91, 188, 55, 64}
random := tools.XorShift128Plus{}
a := authChainA{}
length := a.getRandLength(23, lastHash, &random)
fmt.Println(length)
}

func TestRc4(t *testing.T) {
key := []byte{107, 41, 179, 39, 181, 176, 111, 92, 103, 186, 17, 249, 183, 131, 181, 124}
c, _ := rc4.NewCipher(key)
src := []byte{3, 19, 99, 108, 105, 101, 110, 116, 115, 51, 46, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 0, 80}
// dst := []byte{95, 186, 186, 30, 52, 206, 58, 189, 157, 89, 32, 71, 153, 164, 56, 109, 229, 110, 149, 218, 97, 172, 179}
c.XORKeyStream(src, src)
fmt.Println(src)
}

func TestAuthAES128(t *testing.T) {
item := []byte("Q2dECE")
usekey := tools.SHA1Sum(item)
fmt.Printf("%x", usekey)
}
20 changes: 20 additions & 0 deletions transport/ssr/protocol/protocol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package protocol

import (
"fmt"
"testing"
)

func TestGetDataLength(t *testing.T) {
b := []byte{1, 2, 3, 3, 4, 5, 6, 5, 7, 7, 7, 7, 7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
// b := []byte{1, 2, 3, 3, 4, 5, 6}
l := getDataLength(b)
fmt.Println(l)
}

func TestTrapezoidRandom(t *testing.T) {
max := 1339
d := -0.3
r := trapezoidRandom(max, d)
fmt.Println(r)
}
32 changes: 32 additions & 0 deletions transport/ssr/tools/crypto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package tools

import (
"fmt"
"testing"
)

func TestHmacSHA1(t *testing.T) {
key := []byte{253, 53, 153, 216, 16, 234, 129, 68, 247, 183, 217, 78, 176, 51, 247, 223, 201, 115, 166, 180, 163, 135, 75, 165, 113, 87, 160, 116, 106, 70, 47, 122}
data := []byte{253, 53, 153, 216, 16, 234, 129, 68, 247, 183, 217, 78, 176, 51, 247, 223, 201, 115, 166, 180, 163, 135, 75, 165, 113, 87, 160, 116, 106, 70, 47, 122, 8, 121, 181, 140, 195, 174, 254, 149, 50, 21, 116, 215, 125, 92, 142, 3, 70, 234, 78, 158, 240, 92, 27, 40, 250, 109, 71, 107, 238, 82, 91, 193}
hashData := HmacSHA1(key, data)
fmt.Println(hashData)
}

func TestHmacMD5(t *testing.T) {
key := []byte{253, 53, 153, 216, 16, 234, 129, 68, 247, 183, 217, 78, 176, 51, 247, 223, 201, 115, 166, 180, 163, 135, 75, 165, 113, 87, 160, 116, 106, 70, 47, 122}
data := []byte{253, 53, 153, 216, 16, 234, 129, 68, 247, 183, 217, 78, 176, 51, 247, 223, 201, 115, 166, 180, 163, 135, 75, 165, 113, 87, 160, 116, 106, 70, 47, 122, 8, 121, 181, 140, 195, 174, 254, 149, 50, 21, 116, 215, 125, 92, 142, 3, 70, 234, 78, 158, 240, 92, 27, 40, 250, 109, 71, 107, 238, 82, 91, 193}
hashData := HmacMD5(key, data)
fmt.Println(hashData)
}

func TestMD5Sum(t *testing.T) {
data := []byte{253, 53, 153, 216, 16, 234, 129, 68, 247, 183, 217, 78, 176, 51, 247, 223, 201, 115, 166, 180, 163, 135, 75, 165, 113, 87, 160, 116, 106, 70, 47, 122, 8, 121, 181, 140, 195, 174, 254, 149, 50, 21, 116, 215, 125, 92, 142, 3, 70, 234, 78, 158, 240, 92, 27, 40, 250, 109, 71, 107, 238, 82, 91, 193}
hashData := MD5Sum(data)
fmt.Println(hashData)
}

func TestSHa1(t *testing.T) {
key := []byte{253, 53, 153, 216, 16, 234, 129, 68, 247, 183, 217, 78, 176, 51, 247, 223, 201, 115, 166, 180, 163, 135, 75, 165, 113, 87, 160, 116, 106, 70, 47, 122}
data := SHA1Sum(key)
fmt.Println(data)
}
11 changes: 11 additions & 0 deletions transport/trojan/trojan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package trojan

import (
"fmt"
"testing"
)

func TestHexSha224(t *testing.T) {
h := hexSha224([]byte("123"))
fmt.Println(h)
}
115 changes: 115 additions & 0 deletions transport/vmess/user_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package vmess

import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"encoding/binary"
"fmt"
"net/url"
"testing"
"time"

"github.com/gofrs/uuid"
)
Expand All @@ -19,3 +27,110 @@ func TestNewID(t *testing.T) {
fmt.Println(id.UUID.String())
}
}

func hashTimestamp1(t uint64) []byte {
md5hash := md5.New()
ts := make([]byte, 8)
binary.BigEndian.PutUint64(ts, t)
md5hash.Write(ts)
md5hash.Write(ts)
md5hash.Write(ts)
md5hash.Write(ts)
return md5hash.Sum(nil)
}

func TestHashTimestamp(t *testing.T) {
buf := hashTimestamp1(1633248908)
fmt.Println(buf)
}

func TestCipher(t *testing.T) {
uid, err := uuid.FromString("db538344-45db-4713-a317-2f7fd1e4c29d")
if err != nil {
t.Error(err)
}
id := newID(&uid)
block, err := aes.NewCipher(id.CmdKey)
if err != nil {
t.Error(err)
}

// timestamp := time.Now()
stream := cipher.NewCFBEncrypter(block, hashTimestamp1(1633248908))
// buf := []byte{1, 1, 1, 1}
// stream.XORKeyStream(buf, buf)
// fmt.Println(buf)
buf := []byte("github.com/xxf098")
stream.XORKeyStream(buf, buf)
fmt.Println(buf)
}

func TestKdf(t *testing.T) {
key := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}
// kdfSaltConstAuthIDEncryptionKey
result := kdf(key, kdfSaltConstAuthIDEncryptionKey)
// [93 153 120 238 109 19 174 100 222 74 202 189 241 233 7 82 23 89 126 149 192 95 66 176 111 202 233 12 181 188 228 205]
fmt.Println(result)
}

func TestSha256(t *testing.T) {
buf := sha256.Sum256([]byte("rust"))
fmt.Println(buf)
}

func TestHmac(t *testing.T) {
key := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}
h := hmac.New(sha256.New, key)
h.Write([]byte("rust"))
buf := h.Sum(nil)
fmt.Println(buf)
}

func TestAes(t *testing.T) {
key := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}
aesBlock, _ := aes.NewCipher(key)
var result [16]byte
buf := []byte("rustrustrustrust")
aesBlock.Encrypt(result[:], buf)
fmt.Println(result)
}

func TestCreateAuthID(t *testing.T) {
ti := time.Now()
key := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}
tu := ti.Unix()
fmt.Println(tu)
id := createAuthID(key, tu)
fmt.Println(id)
}

func TestGCM(t *testing.T) {
payloadHeaderAEADKey := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}
payloadHeaderAEADNonce := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}
generatedAuthID := []byte{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8}

data := []byte("rust")
aeadPayloadLengthSerializedByte := make([]byte, 2)
binary.BigEndian.PutUint16(aeadPayloadLengthSerializedByte, uint16(len(data)))

payloadHeaderAEADAESBlock, _ := aes.NewCipher(payloadHeaderAEADKey)
payloadHeaderAEAD, _ := cipher.NewGCM(payloadHeaderAEADAESBlock)
payloadHeaderAEADEncrypted := payloadHeaderAEAD.Seal(nil, payloadHeaderAEADNonce, data, generatedAuthID[:])
fmt.Println(payloadHeaderAEADEncrypted)
}

func TestSealVMessAEADHeader(t *testing.T) {
data := []byte("rust")
key := [16]byte{111, 135, 225, 177, 159, 141, 162, 93, 247, 21, 214, 85, 174, 154, 23, 100}
buf := sealVMessAEADHeader(key, data, time.Now())
fmt.Println(buf)
}

func TestUrl(t *testing.T) {
urlStr := "ws://zhk3.greenyun.tk:11110/onefall"
u, err := url.Parse(urlStr)
if err != nil {
t.Error(err)
}
fmt.Println(u.Host)
}

0 comments on commit 8e54e42

Please sign in to comment.