Skip to content

Commit

Permalink
设置测试时间防止由于证书过期导致测试不通过
Browse files Browse the repository at this point in the history
  • Loading branch information
Trisia committed Aug 30, 2023
1 parent e2c0acc commit c9f7412
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
18 changes: 13 additions & 5 deletions tlcp/handshake_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func Test_clientHandshake_auth_server(t *testing.T) {
pool.AddCert(zjcaRoot)

time.Sleep(time.Millisecond * 300)
config := &Config{RootCAs: pool}
config := &Config{RootCAs: pool, Time: runtimeTime}
testClientHandshake(t, config, "127.0.0.1:8445")
}

Expand All @@ -115,7 +115,7 @@ func Test_clientHandshake_client_auth(t *testing.T) {

time.Sleep(time.Millisecond * 300)

config := &Config{RootCAs: pool, Certificates: []Certificate{authCert}}
config := &Config{RootCAs: pool, Certificates: []Certificate{authCert}, Time: runtimeTime}
testClientHandshake(t, config, "127.0.0.1:8446")
}

Expand Down Expand Up @@ -155,7 +155,7 @@ func Test_clientHandshake_client_nocert(t *testing.T) {

time.Sleep(time.Millisecond * 300)

config := &Config{RootCAs: pool}
config := &Config{RootCAs: pool, Time: runtimeTime}
conn, err := Dial("tcp", "127.0.0.1:8449", config)
if err != nil && err.Error() != "remote error: tlcp: bad certificate" {
t.Fatal(err)
Expand All @@ -176,7 +176,7 @@ func Test_resumedSession(t *testing.T) {
pool := smx509.NewCertPool()
pool.AddCert(root1)
time.Sleep(time.Millisecond * 300)
config := &Config{RootCAs: pool, SessionCache: NewLRUSessionCache(2)}
config := &Config{RootCAs: pool, SessionCache: NewLRUSessionCache(2), Time: runtimeTime}

buff := make([]byte, 1024)
for i := 0; i < 2; i++ {
Expand Down Expand Up @@ -215,6 +215,7 @@ func Test_clientHandshake_ECDHE(t *testing.T) {
RootCAs: pool,
Certificates: []Certificate{authCert, authCert},
CipherSuites: []uint16{ECDHE_SM4_GCM_SM3, ECDHE_SM4_CBC_SM3},
Time: runtimeTime,
}
testClientHandshake(t, config, "127.0.0.1:8451")

Expand Down Expand Up @@ -248,6 +249,7 @@ func Test_NotResumedSession(t *testing.T) {
ClientCAs: pool,
ClientAuth: RequireAndVerifyClientCert,
Certificates: []Certificate{sigCert, encCert},
Time: runtimeTime,
}
for {
conn, err := tcpLn.Accept()
Expand All @@ -266,7 +268,12 @@ func Test_NotResumedSession(t *testing.T) {
}()

time.Sleep(time.Millisecond * 300)
config := &Config{RootCAs: pool, Certificates: []Certificate{authCert}, SessionCache: NewLRUSessionCache(2)}
config := &Config{
RootCAs: pool,
Certificates: []Certificate{authCert},
SessionCache: NewLRUSessionCache(2),
Time: runtimeTime,
}

for i := 0; i < 2; i++ {
conn, err := Dial("tcp", "127.0.0.1:20099", config)
Expand Down Expand Up @@ -306,6 +313,7 @@ func Test_clientHandshake_ECCWithEncCert(t *testing.T) {
RootCAs: pool,
Certificates: []Certificate{authCert, authCert},
CipherSuites: []uint16{ECC_SM4_GCM_SM3, ECC_SM4_CBC_SM3},
Time: runtimeTime,
}
testClientHandshake(t, config, "127.0.0.1:8452")
}
16 changes: 15 additions & 1 deletion tlcp/handshake_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ func init() {
simplePool.AddCert(root1)
}

// 测试时服务器时间,防止证书过期
func runtimeTime() time.Time {
res, _ := time.Parse("2006-01-02 15:04:05", "2022-12-15 00:00:00")
return res
}

/*
func Test_serverHandshake(t *testing.T) {
err := server(8443)
Expand Down Expand Up @@ -145,6 +151,7 @@ func server(port int) error {
defer tcpLn.Close()
config := &Config{
Certificates: []Certificate{sigCert, encCert},
Time: runtimeTime,
}
var conn net.Conn
for {
Expand Down Expand Up @@ -175,6 +182,7 @@ func serverNeedAuth(port int) error {
Certificates: []Certificate{sigCert, encCert},
ClientAuth: RequireAndVerifyClientCert,
ClientCAs: simplePool,
Time: runtimeTime,
}
var conn net.Conn
for {
Expand Down Expand Up @@ -246,6 +254,7 @@ func Test_ResumedSession(t *testing.T) {
ClientAuth: RequireAndVerifyClientCert,
Certificates: []Certificate{sigCert, encCert},
SessionCache: NewLRUSessionCache(10),
Time: runtimeTime,
}
first := true
for {
Expand Down Expand Up @@ -279,7 +288,12 @@ func Test_ResumedSession(t *testing.T) {
}()

time.Sleep(time.Millisecond * 300)
config := &Config{RootCAs: pool, Certificates: []Certificate{authCert}, SessionCache: NewLRUSessionCache(2)}
config := &Config{
RootCAs: pool,
Certificates: []Certificate{authCert},
SessionCache: NewLRUSessionCache(2),
Time: runtimeTime,
}

for i := 0; i < 2; i++ {
conn, err := Dial("tcp", "127.0.0.1:20100", config)
Expand Down

0 comments on commit c9f7412

Please sign in to comment.