From a2891d6cbc0b70416ce3e59ac205ab3991a49945 Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Wed, 25 Jul 2018 14:34:07 +0800 Subject: [PATCH] use t.Log instead of log.Print --- certificate.go | 2 +- certificate_test.go | 21 ++++++++------- hash_test.go | 3 +-- tls_test.go | 62 ++++++++++++++++++++++----------------------- 4 files changed, 43 insertions(+), 45 deletions(-) diff --git a/certificate.go b/certificate.go index 36791ad..e5b17f8 100644 --- a/certificate.go +++ b/certificate.go @@ -33,7 +33,7 @@ func (c *Certificate) Free() { } func (c *Certificate) free() { - log.Println("free certificate") + //log.Println("free certificate") c.Free() } diff --git a/certificate_test.go b/certificate_test.go index f58fb7e..14990b5 100644 --- a/certificate_test.go +++ b/certificate_test.go @@ -1,7 +1,6 @@ package gnutls import ( - "log" "runtime" "testing" "time" @@ -12,16 +11,16 @@ func TestGetAltname(t *testing.T) { if err != nil { t.Fatal(err) } - log.Printf("%+v", cert) - log.Println(cert.GetAltName()) - log.Println(cert.GetCertString()) - log.Println(cert.GetDN()) - log.Println(cert.GetIssuerDN()) - log.Println("CN ", cert.CommonName()) - //log.Println("flag 0: ", cert.getCertString(0, 0)) - //log.Println("flag 1: ", cert.getCertString(0, 1)) - //log.Println("flag 2: ", cert.getCertString(0, 2)) - //log.Println("flag 3: ", cert.getCertString(0, 3)) + t.Logf("%+v", cert) + t.Log(cert.GetAltName()) + t.Log(cert.GetCertString()) + t.Log(cert.GetDN()) + t.Log(cert.GetIssuerDN()) + t.Log("CN ", cert.CommonName()) + //t.Log("flag 0: ", cert.getCertString(0, 0)) + //t.Log("flag 1: ", cert.getCertString(0, 1)) + //t.Log("flag 2: ", cert.getCertString(0, 2)) + //t.Log("flag 3: ", cert.getCertString(0, 3)) cert.Free() } diff --git a/hash_test.go b/hash_test.go index 3d9ee1a..288ba03 100644 --- a/hash_test.go +++ b/hash_test.go @@ -5,7 +5,6 @@ import ( "crypto/rand" "crypto/sha512" "encoding/hex" - "log" "runtime" "testing" "time" @@ -23,7 +22,7 @@ func TestHashSHA(t *testing.T) { h3.Write(data) h4 := h3.Sum(nil) if !bytes.Equal(h4[:], h1) { - log.Printf("\n%s\n%s", hex.EncodeToString(h4[:]), hex.EncodeToString(h1)) + t.Logf("\n%s\n%s", hex.EncodeToString(h4[:]), hex.EncodeToString(h1)) t.Fatal("hash not equal") } runtime.GC() diff --git a/tls_test.go b/tls_test.go index 10638cc..1f9ce0f 100644 --- a/tls_test.go +++ b/tls_test.go @@ -6,10 +6,8 @@ import ( "crypto/tls" "fmt" "io" - "log" "net" "net/http" - "os" "runtime" "testing" "time" @@ -28,21 +26,21 @@ func TestTLSClient(t *testing.T) { } defer l.Close() addr := l.Addr().String() - log.Println("test server listen on ", addr) + t.Log("test server listen on ", addr) go func() { for { c, err := l.Accept() if err != nil { break } - log.Printf("accept connection from %s", c.RemoteAddr()) + t.Logf("accept connection from %s", c.RemoteAddr()) go func(c net.Conn) { defer c.Close() for { buf := make([]byte, 4096) n, err := c.Read(buf) if err != nil { - log.Println("connection closed") + t.Log("connection closed") break } if _, err = c.Write(buf[:n]); err != nil { @@ -85,32 +83,32 @@ func TestTLSServer(t *testing.T) { t.Fatal("gnutls listen ", err) } addr := l.Addr().String() - log.Println("test server listen on ", addr) + t.Log("test server listen on ", addr) defer l.Close() go func() { for { c, err := l.Accept() if err != nil { - log.Println("gnutls accept ", err) + t.Log("gnutls accept ", err) break } - log.Println("accept connection from ", c.RemoteAddr()) + t.Log("accept connection from ", c.RemoteAddr()) go func(c net.Conn) { defer c.Close() tlsconn := c.(*Conn) if err := tlsconn.Handshake(); err != nil { - log.Println(err) + t.Log(err) return } buf := make([]byte, 4096) for { n, err := c.Read(buf[0:]) if err != nil { - log.Println("gnutls read ", err) + t.Log("gnutls read ", err) break } if _, err := c.Write(buf[:n]); err != nil { - log.Println("gnutls write ", err) + t.Log("gnutls write ", err) break } } @@ -156,34 +154,34 @@ func TestTLSALPNServer(t *testing.T) { t.Fatal("gnutls listen ", err) } addr := l.Addr().String() - log.Println("test server listen on ", addr) + t.Log("test server listen on ", addr) defer l.Close() go func() { for { c, err := l.Accept() if err != nil { - log.Println("gnutls accept ", err) + t.Log("gnutls accept ", err) break } - log.Println("accept connection from ", c.RemoteAddr()) + t.Log("accept connection from ", c.RemoteAddr()) go func(c net.Conn) { defer c.Close() tlsConn := c.(*Conn) if err := tlsConn.Handshake(); err != nil { - log.Println(err) + t.Log(err) return } connState := tlsConn.ConnectionState() - log.Printf("%+v", connState) + t.Logf("%+v", connState) buf := make([]byte, 4096) for { n, err := c.Read(buf[0:]) if err != nil { - log.Println("gnutls read ", err) + t.Log("gnutls read ", err) break } if _, err := c.Write(buf[:n]); err != nil { - log.Println("gnutls write ", err) + t.Log("gnutls write ", err) break } } @@ -205,7 +203,7 @@ func TestTLSALPNServer(t *testing.T) { t.Fatal(err) } connState := c.ConnectionState() - log.Printf("%+v", connState) + t.Logf("%+v", connState) if connState.NegotiatedProtocol != expectedAlpn { t.Errorf("expected alpn %s, got %s", @@ -247,34 +245,34 @@ func TestTLSALPNClient(t *testing.T) { t.Fatal("tls listen ", err) } addr := l.Addr().String() - log.Println("test server listen on ", addr) + t.Log("test server listen on ", addr) defer l.Close() go func() { for { c, err := l.Accept() if err != nil { - log.Println("gnutls accept ", err) + t.Log("gnutls accept ", err) break } - log.Println("accept connection from ", c.RemoteAddr()) + t.Log("accept connection from ", c.RemoteAddr()) go func(c net.Conn) { defer c.Close() tlsConn := c.(*tls.Conn) if err := tlsConn.Handshake(); err != nil { - log.Println(err) + t.Log(err) return } connState := tlsConn.ConnectionState() - log.Printf("%+v", connState) + t.Logf("%+v", connState) buf := make([]byte, 4096) for { n, err := c.Read(buf[0:]) if err != nil { - log.Println("tls read ", err) + t.Log("tls read ", err) break } if _, err := c.Write(buf[:n]); err != nil { - log.Println("tls write ", err) + t.Log("tls write ", err) break } } @@ -295,7 +293,7 @@ func TestTLSALPNClient(t *testing.T) { t.Fatal(err) } connState := c.ConnectionState() - log.Printf("%+v", connState) + t.Logf("%+v", connState) if connState.NegotiatedProtocol != expectedAlpn { t.Errorf("expected alpn %s, got %s", @@ -350,14 +348,14 @@ func TestTLSServerSNI(t *testing.T) { for { c, err := l.Accept() if err != nil { - log.Println(err) + t.Log(err) break } go func(c net.Conn) { defer c.Close() tlsconn := c.(*Conn) if err := tlsconn.Handshake(); err != nil { - log.Println(err) + t.Log(err) return } state := tlsconn.ConnectionState() @@ -415,7 +413,7 @@ func TestTLSGetPeerCert(t *testing.T) { } state := conn.ConnectionState() for i := 0; i < int(state.PeerCertificate.certSize); i++ { - log.Println(state.PeerCertificate.getCertString(i, 1)) + t.Log(state.PeerCertificate.getCertString(i, 1)) } req, _ := http.NewRequest("GET", "https://www.ratafee.nl/httpbin/ip", nil) @@ -425,7 +423,9 @@ func TestTLSGetPeerCert(t *testing.T) { if err != nil { t.Error(err) } - resp.Write(os.Stdout) + var buf = new(bytes.Buffer) + resp.Write(buf) + t.Logf("%s", string(buf.Bytes())) runtime.GC() time.Sleep(1 * time.Second) }