Fix tests

This commit is contained in:
Andy Wang 2020-04-09 22:21:02 +01:00
parent 444182f5bb
commit 254b7152b6
7 changed files with 116 additions and 71 deletions

View File

@ -0,0 +1,52 @@
package main
import "testing"
func TestParseBindAddr(t *testing.T) {
t.Run("port only", func(t *testing.T) {
addrs, err := parseBindAddr([]string{":443"})
if err != nil {
t.Error(err)
return
}
if addrs[0].String() != ":443" {
t.Errorf("expected %v got %v", ":443", addrs[0].String())
}
})
t.Run("specific address", func(t *testing.T) {
addrs, err := parseBindAddr([]string{"192.168.1.123:443"})
if err != nil {
t.Error(err)
return
}
if addrs[0].String() != "192.168.1.123:443" {
t.Errorf("expected %v got %v", "192.168.1.123:443", addrs[0].String())
}
})
t.Run("ipv6", func(t *testing.T) {
addrs, err := parseBindAddr([]string{"[::]:443"})
if err != nil {
t.Error(err)
return
}
if addrs[0].String() != "[::]:443" {
t.Errorf("expected %v got %v", "[::]:443", addrs[0].String())
}
})
t.Run("mixed", func(t *testing.T) {
addrs, err := parseBindAddr([]string{":80", "[::]:443"})
if err != nil {
t.Error(err)
return
}
if addrs[0].String() != ":80" {
t.Errorf("expected %v got %v", ":80", addrs[0].String())
}
if addrs[1].String() != "[::]:443" {
t.Errorf("expected %v got %v", "[::]:443", addrs[1].String())
}
})
}

View File

@ -2,8 +2,8 @@ package client
import ( import (
"bytes" "bytes"
"github.com/cbeuw/Cloak/internal/common"
"github.com/cbeuw/Cloak/internal/multiplex" "github.com/cbeuw/Cloak/internal/multiplex"
"io"
"testing" "testing"
"time" "time"
) )
@ -11,8 +11,6 @@ import (
func TestMakeAuthenticationPayload(t *testing.T) { func TestMakeAuthenticationPayload(t *testing.T) {
tests := []struct { tests := []struct {
authInfo authInfo authInfo authInfo
seed io.Reader
time time.Time
expPayload authenticationPayload expPayload authenticationPayload
expSecret [32]byte expSecret [32]byte
}{ }{
@ -31,13 +29,15 @@ func TestMakeAuthenticationPayload(t *testing.T) {
ProxyMethod: "shadowsocks", ProxyMethod: "shadowsocks",
EncryptionMethod: multiplex.E_METHOD_PLAIN, EncryptionMethod: multiplex.E_METHOD_PLAIN,
MockDomain: "d2jkinvisak5y9.cloudfront.net", MockDomain: "d2jkinvisak5y9.cloudfront.net",
WorldState: common.WorldState{
Rand: bytes.NewBuffer([]byte{
0xf1, 0x1e, 0x42, 0xe1, 0x84, 0x22, 0x07, 0xc5,
0xc3, 0x5c, 0x0f, 0x7b, 0x01, 0xf3, 0x65, 0x2d,
0xd7, 0x9b, 0xad, 0xb0, 0xb2, 0x77, 0xa2, 0x06,
0x6b, 0x78, 0x1b, 0x74, 0x1f, 0x43, 0xc9, 0x80}),
Now: func() time.Time { return time.Unix(1579908372, 0) },
},
}, },
bytes.NewBuffer([]byte{
0xf1, 0x1e, 0x42, 0xe1, 0x84, 0x22, 0x07, 0xc5,
0xc3, 0x5c, 0x0f, 0x7b, 0x01, 0xf3, 0x65, 0x2d,
0xd7, 0x9b, 0xad, 0xb0, 0xb2, 0x77, 0xa2, 0x06,
0x6b, 0x78, 0x1b, 0x74, 0x1f, 0x43, 0xc9, 0x80}),
time.Unix(1579908372, 0),
authenticationPayload{ authenticationPayload{
randPubKey: [32]byte{ randPubKey: [32]byte{
0xee, 0x9e, 0x41, 0x4e, 0xb3, 0x3b, 0x85, 0x03, 0xee, 0x9e, 0x41, 0x4e, 0xb3, 0x3b, 0x85, 0x03,
@ -63,7 +63,7 @@ func TestMakeAuthenticationPayload(t *testing.T) {
} }
for _, tc := range tests { for _, tc := range tests {
func() { func() {
payload, sharedSecret := makeAuthenticationPayload(tc.authInfo, tc.seed, tc.time) payload, sharedSecret := makeAuthenticationPayload(tc.authInfo)
if payload != tc.expPayload { if payload != tc.expPayload {
t.Errorf("payload doesn't match:\nexp %v\ngot %v", tc.expPayload, payload) t.Errorf("payload doesn't match:\nexp %v\ngot %v", tc.expPayload, payload)
} }

View File

@ -15,3 +15,10 @@ type WorldState struct {
Rand io.Reader Rand io.Reader
Now func() time.Time Now func() time.Time
} }
func WorldOfTime(t time.Time) WorldState {
return WorldState{
Rand: rand.Reader,
Now: func() time.Time { return t },
}
}

View File

@ -0,0 +1,36 @@
package integration_test
import (
"encoding/base64"
"github.com/cbeuw/Cloak/internal/client"
"github.com/cbeuw/Cloak/internal/server"
)
var bypassUID = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
var publicKey, _ = base64.StdEncoding.DecodeString("7f7TuKrs264VNSgMno8PkDlyhGhVuOSR8JHLE6H4Ljc=")
var privateKey, _ = base64.StdEncoding.DecodeString("SMWeC6VuZF8S/id65VuFQFlfa7hTEJBpL6wWhqPP100=")
var clientConfig = client.RawConfig{
ServerName: "www.example.com",
ProxyMethod: "test",
EncryptionMethod: "plain",
UID: bypassUID,
PublicKey: publicKey,
NumConn: 3,
UDP: false,
BrowserSig: "chrome",
Transport: "direct",
}
var serverState = server.State{
ProxyBook: nil,
ProxyDialer: nil,
AdminUID: nil,
Timeout: 0,
BypassUID: nil,
RedirHost: nil,
RedirPort: "",
RedirDialer: nil,
Panel: nil,
LocalAPIRouter: nil,
}

View File

@ -4,6 +4,7 @@ import (
"crypto" "crypto"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"github.com/cbeuw/Cloak/internal/common"
"github.com/cbeuw/Cloak/internal/ecdh" "github.com/cbeuw/Cloak/internal/ecdh"
"testing" "testing"
"time" "time"
@ -23,7 +24,7 @@ func TestTouchStone(t *testing.T) {
return return
} }
nineSixSix := func() time.Time { return time.Unix(1565998966, 0) } nineSixSix := time.Unix(1565998966, 0)
cinfo, err := decryptClientInfo(ai, nineSixSix) cinfo, err := decryptClientInfo(ai, nineSixSix)
if err != nil { if err != nil {
t.Errorf("expecting no error, got %v", err) t.Errorf("expecting no error, got %v", err)
@ -42,13 +43,13 @@ func TestTouchStone(t *testing.T) {
return return
} }
nineSixSixP50 := func() time.Time { return time.Unix(1565998966, 0).Add(50) } nineSixSixP50 := time.Unix(1565998966, 0).Add(50)
_, err = decryptClientInfo(ai, nineSixSixP50) _, err = decryptClientInfo(ai, nineSixSixP50)
if err != nil { if err != nil {
t.Errorf("expecting no error, got %v", err) t.Errorf("expecting no error, got %v", err)
return return
} }
nineSixSixM50 := func() time.Time { return time.Unix(1565998966, 0).Truncate(50) } nineSixSixM50 := time.Unix(1565998966, 0).Truncate(50)
_, err = decryptClientInfo(ai, nineSixSixM50) _, err = decryptClientInfo(ai, nineSixSixM50)
if err != nil { if err != nil {
t.Errorf("expecting no error, got %v", err) t.Errorf("expecting no error, got %v", err)
@ -65,7 +66,7 @@ func TestTouchStone(t *testing.T) {
return return
} }
nineSixSixOver := func() time.Time { return time.Unix(1565998966, 0).Add(TIMESTAMP_TOLERANCE + 10) } nineSixSixOver := time.Unix(1565998966, 0).Add(TIMESTAMP_TOLERANCE + 10)
_, err = decryptClientInfo(ai, nineSixSixOver) _, err = decryptClientInfo(ai, nineSixSixOver)
if err == nil { if err == nil {
t.Errorf("expecting %v, got %v", ErrTimestampOutOfWindow, err) t.Errorf("expecting %v, got %v", ErrTimestampOutOfWindow, err)
@ -81,7 +82,7 @@ func TestTouchStone(t *testing.T) {
return return
} }
nineSixSixUnder := func() time.Time { return time.Unix(1565998966, 0).Add(TIMESTAMP_TOLERANCE - 10) } nineSixSixUnder := time.Unix(1565998966, 0).Add(TIMESTAMP_TOLERANCE - 10)
_, err = decryptClientInfo(ai, nineSixSixUnder) _, err = decryptClientInfo(ai, nineSixSixUnder)
if err == nil { if err == nil {
t.Errorf("expecting %v, got %v", ErrTimestampOutOfWindow, err) t.Errorf("expecting %v, got %v", ErrTimestampOutOfWindow, err)
@ -97,7 +98,7 @@ func TestTouchStone(t *testing.T) {
return return
} }
fiveOSix := func() time.Time { return time.Unix(1565999506, 0) } fiveOSix := time.Unix(1565999506, 0)
cinfo, err := decryptClientInfo(ai, fiveOSix) cinfo, err := decryptClientInfo(ai, fiveOSix)
if err == nil { if err == nil {
t.Errorf("not a cloak, got nil error and cinfo %v", cinfo) t.Errorf("not a cloak, got nil error and cinfo %v", cinfo)
@ -113,7 +114,7 @@ func TestTouchStone(t *testing.T) {
return return
} }
sixOneFive := func() time.Time { return time.Unix(1565999615, 0) } sixOneFive := time.Unix(1565999615, 0)
cinfo, err := decryptClientInfo(ai, sixOneFive) cinfo, err := decryptClientInfo(ai, sixOneFive)
if err == nil { if err == nil {
t.Errorf("not a cloak, got nil error and cinfo %v", cinfo) t.Errorf("not a cloak, got nil error and cinfo %v", cinfo)
@ -123,13 +124,12 @@ func TestTouchStone(t *testing.T) {
} }
func TestPrepareConnection(t *testing.T) { func TestAuthFirstPacket(t *testing.T) {
nineSixSix := func() time.Time { return time.Unix(1565998966, 0) }
pvBytes, _ := hex.DecodeString("10de5a3c4a4d04efafc3e06d1506363a72bd6d053baef123e6a9a79a0c04b547") pvBytes, _ := hex.DecodeString("10de5a3c4a4d04efafc3e06d1506363a72bd6d053baef123e6a9a79a0c04b547")
p, _ := ecdh.Unmarshal(pvBytes) p, _ := ecdh.Unmarshal(pvBytes)
getNewState := func() *State { getNewState := func() *State {
sta, _ := InitState(nineSixSix) sta, _ := InitState(RawConfig{}, common.WorldOfTime(time.Unix(1565998966, 0)))
sta.staticPv = p.(crypto.PrivateKey) sta.staticPv = p.(crypto.PrivateKey)
sta.ProxyBook["shadowsocks"] = nil sta.ProxyBook["shadowsocks"] = nil
return sta return sta
@ -167,7 +167,7 @@ func TestPrepareConnection(t *testing.T) {
} }
}) })
t.Run("Websocket correct", func(t *testing.T) { t.Run("Websocket correct", func(t *testing.T) {
sta, _ := InitState(func() time.Time { return time.Unix(1584358419, 0) }) sta, _ := InitState(RawConfig{}, common.WorldOfTime(time.Unix(1584358419, 0)))
sta.staticPv = p.(crypto.PrivateKey) sta.staticPv = p.(crypto.PrivateKey)
sta.ProxyBook["shadowsocks"] = nil sta.ProxyBook["shadowsocks"] = nil

View File

@ -222,7 +222,6 @@ func (sta *State) UsedRandomCleaner() {
time.Sleep(CACHE_CLEAN_INTERVAL) time.Sleep(CACHE_CLEAN_INTERVAL)
sta.usedRandomM.Lock() sta.usedRandomM.Lock()
for key, t := range sta.usedRandom { for key, t := range sta.usedRandom {
// todo: inpure time
if time.Unix(t, 0).Before(sta.WorldState.Now().Add(TIMESTAMP_TOLERANCE)) { if time.Unix(t, 0).Before(sta.WorldState.Now().Add(TIMESTAMP_TOLERANCE)) {
delete(sta.usedRandom, key) delete(sta.usedRandom, key)
} }

View File

@ -106,52 +106,3 @@ func TestParseRedirAddr(t *testing.T) {
} }
}) })
} }
func TestParseBindAddr(t *testing.T) {
t.Run("port only", func(t *testing.T) {
addrs, err := parseBindAddr([]string{":443"})
if err != nil {
t.Error(err)
return
}
if addrs[0].String() != ":443" {
t.Errorf("expected %v got %v", ":443", addrs[0].String())
}
})
t.Run("specific address", func(t *testing.T) {
addrs, err := parseBindAddr([]string{"192.168.1.123:443"})
if err != nil {
t.Error(err)
return
}
if addrs[0].String() != "192.168.1.123:443" {
t.Errorf("expected %v got %v", "192.168.1.123:443", addrs[0].String())
}
})
t.Run("ipv6", func(t *testing.T) {
addrs, err := parseBindAddr([]string{"[::]:443"})
if err != nil {
t.Error(err)
return
}
if addrs[0].String() != "[::]:443" {
t.Errorf("expected %v got %v", "[::]:443", addrs[0].String())
}
})
t.Run("mixed", func(t *testing.T) {
addrs, err := parseBindAddr([]string{":80", "[::]:443"})
if err != nil {
t.Error(err)
return
}
if addrs[0].String() != ":80" {
t.Errorf("expected %v got %v", ":80", addrs[0].String())
}
if addrs[1].String() != "[::]:443" {
t.Errorf("expected %v got %v", "[::]:443", addrs[1].String())
}
})
}