ooni-probe-cli/internal/runtimex/runtimex_test.go
Simone Basso b9a844ecee
feat: run ~always netxlite integration tests (#522)
* feat: run ~always netxlite integration tests

This diff ensures that we check on windows, linux, macos that our
fundamental networking library (netxlite) works.

We combine unit and integration tests.

This work is part of https://github.com/ooni/probe/issues/1733, where
I want to have more strong guarantees about the foundations.

* fix(filtering/tls_test.go): make portable on Windows

The trick here is to use the wrapped error so to normalize the
different errors messages we see on Windows.

* fix(netxlite/quic_test.go): make portable on windows

Rather than using the zero port, use the `x` port which fails
when the stdlib is parsing the address.

The zero port seems to work on Windows while it does not on Unix.

* fix(serialresolver_test.go): make error more timeout than before

This seems enough to convince Go on Windows about this error
being really a timeout timeouty timeouted thingie.
2021-09-29 16:04:26 +02:00

74 lines
1.6 KiB
Go

package runtimex_test
import (
"errors"
"testing"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
func TestPanicOnError(t *testing.T) {
badfunc := func(in error) (out error) {
defer func() {
out = recover().(error)
}()
runtimex.PanicOnError(in, "antani failed")
return
}
t.Run("error is nil", func(t *testing.T) {
runtimex.PanicOnError(nil, "antani failed")
})
t.Run("error is not nil", func(t *testing.T) {
expected := errors.New("mocked error")
if !errors.Is(badfunc(expected), expected) {
t.Fatal("not the error we expected")
}
})
}
func TestPanicIfFalse(t *testing.T) {
badfunc := func(in bool, message string) (out error) {
defer func() {
out = errors.New(recover().(string))
}()
runtimex.PanicIfFalse(in, message)
return
}
t.Run("assertion is true", func(t *testing.T) {
runtimex.PanicIfFalse(true, "antani failed")
})
t.Run("assertion is false", func(t *testing.T) {
message := "mocked error"
err := badfunc(false, message)
if err == nil || err.Error() != message {
t.Fatal("not the error we expected", err)
}
})
}
func TestPanicIfTrue(t *testing.T) {
badfunc := func(in bool, message string) (out error) {
defer func() {
out = errors.New(recover().(string))
}()
runtimex.PanicIfTrue(in, message)
return
}
t.Run("assertion is false", func(t *testing.T) {
runtimex.PanicIfTrue(false, "antani failed")
})
t.Run("assertion is true", func(t *testing.T) {
message := "mocked error"
err := badfunc(true, message)
if err == nil || err.Error() != message {
t.Fatal("not the error we expected", err)
}
})
}