-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
netx/{dialer,resolver}: actually add unit tests
They give us confidence that we can successfully log in case of failure. This is a side effect and I still don't want to make sure that the output does not change over time. Yet, it's important to know that the code can handle the format strings we use. Part of #359
- Loading branch information
1 parent
fc8cb14
commit 1bfe756
Showing
2 changed files
with
65 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package dialer_test | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"errors" | ||
"io" | ||
"testing" | ||
|
||
"github.com/apex/log" | ||
"github.com/ooni/probe-engine/netx/dialer" | ||
) | ||
|
||
func TestUnitLoggingDialerFailure(t *testing.T) { | ||
d := dialer.LoggingDialer{ | ||
Dialer: dialer.EOFDialer{}, | ||
Logger: log.Log, | ||
} | ||
conn, err := d.DialContext(context.Background(), "tcp", "www.google.com:443") | ||
if !errors.Is(err, io.EOF) { | ||
t.Fatal("not the error we expected") | ||
} | ||
if conn != nil { | ||
t.Fatal("expected nil conn here") | ||
} | ||
} | ||
|
||
func TestUnitLoggingTLSHandshakerFailure(t *testing.T) { | ||
h := dialer.LoggingTLSHandshaker{ | ||
TLSHandshaker: dialer.EOFTLSHandshaker{}, | ||
Logger: log.Log, | ||
} | ||
tlsconn, _, err := h.Handshake(context.Background(), dialer.EOFConn{}, &tls.Config{ | ||
ServerName: "www.google.com", | ||
}) | ||
if !errors.Is(err, io.EOF) { | ||
t.Fatal("not the error we expected") | ||
} | ||
if tlsconn != nil { | ||
t.Fatal("expected nil tlsconn here") | ||
} | ||
} |
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,23 @@ | ||
package resolver_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/apex/log" | ||
"github.com/ooni/probe-engine/netx/resolver" | ||
) | ||
|
||
func TestUnitLoggingResolver(t *testing.T) { | ||
r := resolver.LoggingResolver{ | ||
Logger: log.Log, | ||
Resolver: resolver.NewFakeResolverThatFails(), | ||
} | ||
addrs, err := r.LookupHost(context.Background(), "www.google.com") | ||
if err == nil { | ||
t.Fatal("expected an error here") | ||
} | ||
if addrs != nil { | ||
t.Fatal("expected nil addr here") | ||
} | ||
} |