From 7396c4ef91d1e2c4c98c293f39ca773f4705827e Mon Sep 17 00:00:00 2001 From: jsing Date: Tue, 13 Oct 2015 13:59:45 +0000 Subject: [PATCH] Single byte read/write tests. --- regress/lib/libtls/gotls/tls_test.go | 67 ++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/regress/lib/libtls/gotls/tls_test.go b/regress/lib/libtls/gotls/tls_test.go index 2331ec0be62..3a430924a72 100644 --- a/regress/lib/libtls/gotls/tls_test.go +++ b/regress/lib/libtls/gotls/tls_test.go @@ -118,6 +118,73 @@ func TestTLSBasic(t *testing.T) { } } +func TestTLSSingleByteReadWrite(t *testing.T) { + ts, u, caFile, err := newTestServer() + if err != nil { + t.Fatalf("Failed to start test server: %v", err) + } + defer os.Remove(caFile) + defer ts.Close() + + if err := Init(); err != nil { + t.Fatal(err) + } + + cfg, err := NewConfig() + if err != nil { + t.Fatal(err) + } + defer cfg.Free() + cfg.SetCAFile(caFile) + + tls, err := NewClient(cfg) + if err != nil { + t.Fatal(err) + } + defer tls.Free() + + t.Logf("Connecting to %s", u.Host) + + if err := tls.Connect(u.Host, ""); err != nil { + t.Fatal(err) + } + defer func() { + if err := tls.Close(); err != nil { + t.Fatalf("Close failed: %v", err) + } + }() + + for _, b := range []byte("GET / HTTP/1.0\n\n") { + n, err := tls.Write([]byte{b}) + if err != nil { + t.Fatal(err) + } + if n != 1 { + t.Fatalf("Wrote byte %v, got length %d, want 1", b, n) + } + } + + var body []byte + for { + buf := make([]byte, 1) + n, err := tls.Read(buf) + if err != nil { + t.Fatal(err) + } + if n == 0 { + break + } + if n != 1 { + t.Fatalf("Read single byte, got length %d, want 1", n) + } + body = append(body, buf...) + } + + if !strings.Contains(string(body), httpContent) { + t.Errorf("Response does not contain %q", httpContent) + } +} + func TestTLSInfo(t *testing.T) { ts, u, caFile, err := newTestServer() if err != nil { -- 2.20.1