forked from libp2p/go-openssl
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In order to replace timeouts with contexts in `Connect` instance creation (go-tarantool), I need a `DialContext` function. It accepts context, and cancels, if context is canceled by user. Part of tarantool/go-tarantool#136
- Loading branch information
Showing
3 changed files
with
196 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package openssl_test | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"io" | ||
"net" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/tarantool/go-openssl" | ||
) | ||
|
||
func sslConnect(t *testing.T, ssl_listener net.Listener) { | ||
for { | ||
var err error | ||
conn, err := ssl_listener.Accept() | ||
if err != nil { | ||
t.Errorf("failed accept: %s", err) | ||
continue | ||
} | ||
io.Copy(conn, io.LimitReader(rand.Reader, 1024)) | ||
break | ||
} | ||
} | ||
|
||
func TestDial(t *testing.T) { | ||
ctx := openssl.GetCtx(t) | ||
if err := ctx.SetCipherList("AES128-SHA"); err != nil { | ||
t.Fatal(err) | ||
} | ||
ssl_listener, err := openssl.Listen("tcp", "localhost:0", ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
sslConnect(t, ssl_listener) | ||
wg.Done() | ||
}() | ||
|
||
client, err := openssl.Dial(ssl_listener.Addr().Network(), | ||
ssl_listener.Addr().String(), ctx, openssl.InsecureSkipHostVerification) | ||
|
||
wg.Wait() | ||
|
||
if err != nil { | ||
t.Fatalf("unexpected err: %v", err) | ||
} | ||
n, err := io.Copy(io.Discard, io.LimitReader(client, 1024)) | ||
Check failure on line 53 in net_test.go GitHub Actions / All
|
||
if n != 1024 { | ||
if n == 0 { | ||
t.Fatal("client is closed after creation") | ||
} | ||
t.Fatalf("client lost some bytes, expected %d, got %d", 1024, n) | ||
} | ||
} | ||
|
||
func TestDialTimeout(t *testing.T) { | ||
ctx := openssl.GetCtx(t) | ||
if err := ctx.SetCipherList("AES128-SHA"); err != nil { | ||
t.Fatal(err) | ||
} | ||
ssl_listener, err := openssl.Listen("tcp", "localhost:0", ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
client, err := openssl.DialTimeout(ssl_listener.Addr().Network(), | ||
ssl_listener.Addr().String(), time.Nanosecond, ctx, 0) | ||
|
||
if client != nil || err == nil { | ||
t.Fatalf("expected error") | ||
} | ||
} | ||
|
||
func TestDialContext(t *testing.T) { | ||
ctx := openssl.GetCtx(t) | ||
if err := ctx.SetCipherList("AES128-SHA"); err != nil { | ||
t.Fatal(err) | ||
} | ||
ssl_listener, err := openssl.Listen("tcp", "localhost:0", ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cancelCtx, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
client, err := openssl.DialContext(cancelCtx, ssl_listener.Addr().Network(), | ||
ssl_listener.Addr().String(), ctx, 0) | ||
|
||
if client != nil || err == nil { | ||
t.Fatalf("expected error") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters