Skip to content

Commit

Permalink
netx/{dialer,resolver}: actually add unit tests
Browse files Browse the repository at this point in the history
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
bassosimone committed Apr 5, 2020
1 parent fc8cb14 commit 1bfe756
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
42 changes: 42 additions & 0 deletions netx/dialer/logging_test.go
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")
}
}
23 changes: 23 additions & 0 deletions netx/resolver/logging_test.go
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")
}
}

0 comments on commit 1bfe756

Please sign in to comment.