From 254b7152b6d0c39399e7975790e5f2cb1668c8c9 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Thu, 9 Apr 2020 22:21:02 +0100 Subject: [PATCH] Fix tests --- cmd/ck-server/ck-server_test.go | 52 +++++++++++++++++++ internal/client/auth_test.go | 20 +++---- internal/common/worldstate.go | 7 +++ internal/integration_test/integration_test.go | 36 +++++++++++++ internal/server/auth_test.go | 22 ++++---- internal/server/state.go | 1 - internal/server/state_test.go | 49 ----------------- 7 files changed, 116 insertions(+), 71 deletions(-) create mode 100644 cmd/ck-server/ck-server_test.go create mode 100644 internal/integration_test/integration_test.go diff --git a/cmd/ck-server/ck-server_test.go b/cmd/ck-server/ck-server_test.go new file mode 100644 index 0000000..f4067f5 --- /dev/null +++ b/cmd/ck-server/ck-server_test.go @@ -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()) + } + }) +} diff --git a/internal/client/auth_test.go b/internal/client/auth_test.go index 56d0c53..6933b68 100644 --- a/internal/client/auth_test.go +++ b/internal/client/auth_test.go @@ -2,8 +2,8 @@ package client import ( "bytes" + "github.com/cbeuw/Cloak/internal/common" "github.com/cbeuw/Cloak/internal/multiplex" - "io" "testing" "time" ) @@ -11,8 +11,6 @@ import ( func TestMakeAuthenticationPayload(t *testing.T) { tests := []struct { authInfo authInfo - seed io.Reader - time time.Time expPayload authenticationPayload expSecret [32]byte }{ @@ -31,13 +29,15 @@ func TestMakeAuthenticationPayload(t *testing.T) { ProxyMethod: "shadowsocks", EncryptionMethod: multiplex.E_METHOD_PLAIN, 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{ randPubKey: [32]byte{ 0xee, 0x9e, 0x41, 0x4e, 0xb3, 0x3b, 0x85, 0x03, @@ -63,7 +63,7 @@ func TestMakeAuthenticationPayload(t *testing.T) { } for _, tc := range tests { func() { - payload, sharedSecret := makeAuthenticationPayload(tc.authInfo, tc.seed, tc.time) + payload, sharedSecret := makeAuthenticationPayload(tc.authInfo) if payload != tc.expPayload { t.Errorf("payload doesn't match:\nexp %v\ngot %v", tc.expPayload, payload) } diff --git a/internal/common/worldstate.go b/internal/common/worldstate.go index 6443472..95d330d 100644 --- a/internal/common/worldstate.go +++ b/internal/common/worldstate.go @@ -15,3 +15,10 @@ type WorldState struct { Rand io.Reader Now func() time.Time } + +func WorldOfTime(t time.Time) WorldState { + return WorldState{ + Rand: rand.Reader, + Now: func() time.Time { return t }, + } +} diff --git a/internal/integration_test/integration_test.go b/internal/integration_test/integration_test.go new file mode 100644 index 0000000..28409ea --- /dev/null +++ b/internal/integration_test/integration_test.go @@ -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, +} diff --git a/internal/server/auth_test.go b/internal/server/auth_test.go index b95d8a0..7eb5775 100644 --- a/internal/server/auth_test.go +++ b/internal/server/auth_test.go @@ -4,6 +4,7 @@ import ( "crypto" "encoding/hex" "fmt" + "github.com/cbeuw/Cloak/internal/common" "github.com/cbeuw/Cloak/internal/ecdh" "testing" "time" @@ -23,7 +24,7 @@ func TestTouchStone(t *testing.T) { return } - nineSixSix := func() time.Time { return time.Unix(1565998966, 0) } + nineSixSix := time.Unix(1565998966, 0) cinfo, err := decryptClientInfo(ai, nineSixSix) if err != nil { t.Errorf("expecting no error, got %v", err) @@ -42,13 +43,13 @@ func TestTouchStone(t *testing.T) { return } - nineSixSixP50 := func() time.Time { return time.Unix(1565998966, 0).Add(50) } + nineSixSixP50 := time.Unix(1565998966, 0).Add(50) _, err = decryptClientInfo(ai, nineSixSixP50) if err != nil { t.Errorf("expecting no error, got %v", err) return } - nineSixSixM50 := func() time.Time { return time.Unix(1565998966, 0).Truncate(50) } + nineSixSixM50 := time.Unix(1565998966, 0).Truncate(50) _, err = decryptClientInfo(ai, nineSixSixM50) if err != nil { t.Errorf("expecting no error, got %v", err) @@ -65,7 +66,7 @@ func TestTouchStone(t *testing.T) { 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) if err == nil { t.Errorf("expecting %v, got %v", ErrTimestampOutOfWindow, err) @@ -81,7 +82,7 @@ func TestTouchStone(t *testing.T) { 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) if err == nil { t.Errorf("expecting %v, got %v", ErrTimestampOutOfWindow, err) @@ -97,7 +98,7 @@ func TestTouchStone(t *testing.T) { return } - fiveOSix := func() time.Time { return time.Unix(1565999506, 0) } + fiveOSix := time.Unix(1565999506, 0) cinfo, err := decryptClientInfo(ai, fiveOSix) if err == nil { t.Errorf("not a cloak, got nil error and cinfo %v", cinfo) @@ -113,7 +114,7 @@ func TestTouchStone(t *testing.T) { return } - sixOneFive := func() time.Time { return time.Unix(1565999615, 0) } + sixOneFive := time.Unix(1565999615, 0) cinfo, err := decryptClientInfo(ai, sixOneFive) if err == nil { 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) { - nineSixSix := func() time.Time { return time.Unix(1565998966, 0) } +func TestAuthFirstPacket(t *testing.T) { pvBytes, _ := hex.DecodeString("10de5a3c4a4d04efafc3e06d1506363a72bd6d053baef123e6a9a79a0c04b547") p, _ := ecdh.Unmarshal(pvBytes) getNewState := func() *State { - sta, _ := InitState(nineSixSix) + sta, _ := InitState(RawConfig{}, common.WorldOfTime(time.Unix(1565998966, 0))) sta.staticPv = p.(crypto.PrivateKey) sta.ProxyBook["shadowsocks"] = nil return sta @@ -167,7 +167,7 @@ func TestPrepareConnection(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.ProxyBook["shadowsocks"] = nil diff --git a/internal/server/state.go b/internal/server/state.go index 77b5acc..e26ad02 100644 --- a/internal/server/state.go +++ b/internal/server/state.go @@ -222,7 +222,6 @@ func (sta *State) UsedRandomCleaner() { time.Sleep(CACHE_CLEAN_INTERVAL) sta.usedRandomM.Lock() for key, t := range sta.usedRandom { - // todo: inpure time if time.Unix(t, 0).Before(sta.WorldState.Now().Add(TIMESTAMP_TOLERANCE)) { delete(sta.usedRandom, key) } diff --git a/internal/server/state_test.go b/internal/server/state_test.go index ce9d40d..7226fe4 100644 --- a/internal/server/state_test.go +++ b/internal/server/state_test.go @@ -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()) - } - }) -}