ooni-probe-cli/internal/engine/geolocate/stun_test.go
Simone Basso d57c78bc71
chore: merge probe-engine into probe-cli (#201)
This is how I did it:

1. `git clone https://github.com/ooni/probe-engine internal/engine`

2. ```
(cd internal/engine && git describe --tags)
v0.23.0
```

3. `nvim go.mod` (merging `go.mod` with `internal/engine/go.mod`

4. `rm -rf internal/.git internal/engine/go.{mod,sum}`

5. `git add internal/engine`

6. `find . -type f -name \*.go -exec sed -i 's@/ooni/probe-engine@/ooni/probe-cli/v3/internal/engine@g' {} \;`

7. `go build ./...` (passes)

8. `go test -race ./...` (temporary failure on RiseupVPN)

9. `go mod tidy`

10. this commit message

Once this piece of work is done, we can build a new version of `ooniprobe` that
is using `internal/engine` directly. We need to do more work to ensure all the
other functionality in `probe-engine` (e.g. making mobile packages) are still WAI.

Part of https://github.com/ooni/probe/issues/1335
2021-02-02 12:05:47 +01:00

155 lines
3.5 KiB
Go

package geolocate
import (
"context"
"errors"
"net"
"net/http"
"testing"
"time"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/internal/httpheader"
"github.com/pion/stun"
)
func TestSTUNIPLookupCanceledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // stop immediately
ip, err := stunIPLookup(ctx, stunConfig{
Endpoint: "stun.ekiga.net:3478",
Logger: log.Log,
})
if !errors.Is(err, context.Canceled) {
t.Fatalf("not the error we expected: %+v", err)
}
if ip != DefaultProbeIP {
t.Fatalf("not the IP address we expected: %+v", ip)
}
}
func TestSTUNIPLookupDialFailure(t *testing.T) {
expected := errors.New("mocked error")
ctx := context.Background()
ip, err := stunIPLookup(ctx, stunConfig{
Dial: func(network, address string) (stunClient, error) {
return nil, expected
},
Endpoint: "stun.ekiga.net:3478",
Logger: log.Log,
})
if !errors.Is(err, expected) {
t.Fatalf("not the error we expected: %+v", err)
}
if ip != DefaultProbeIP {
t.Fatalf("not the IP address we expected: %+v", ip)
}
}
type MockableSTUNClient struct {
StartErr error
Event stun.Event
}
func (c MockableSTUNClient) Close() error {
return nil
}
func (c MockableSTUNClient) Start(m *stun.Message, h stun.Handler) error {
if c.StartErr != nil {
return c.StartErr
}
go func() {
<-time.After(100 * time.Millisecond)
h(c.Event)
}()
return nil
}
func TestSTUNIPLookupStartReturnsError(t *testing.T) {
expected := errors.New("mocked error")
ctx := context.Background()
ip, err := stunIPLookup(ctx, stunConfig{
Dial: func(network, address string) (stunClient, error) {
return MockableSTUNClient{StartErr: expected}, nil
},
Endpoint: "stun.ekiga.net:3478",
Logger: log.Log,
})
if !errors.Is(err, expected) {
t.Fatalf("not the error we expected: %+v", err)
}
if ip != DefaultProbeIP {
t.Fatalf("not the IP address we expected: %+v", ip)
}
}
func TestSTUNIPLookupStunEventContainsError(t *testing.T) {
expected := errors.New("mocked error")
ctx := context.Background()
ip, err := stunIPLookup(ctx, stunConfig{
Dial: func(network, address string) (stunClient, error) {
return MockableSTUNClient{Event: stun.Event{
Error: expected,
}}, nil
},
Endpoint: "stun.ekiga.net:3478",
Logger: log.Log,
})
if !errors.Is(err, expected) {
t.Fatalf("not the error we expected: %+v", err)
}
if ip != DefaultProbeIP {
t.Fatalf("not the IP address we expected: %+v", ip)
}
}
func TestSTUNIPLookupCannotDecodeMessage(t *testing.T) {
ctx := context.Background()
ip, err := stunIPLookup(ctx, stunConfig{
Dial: func(network, address string) (stunClient, error) {
return MockableSTUNClient{Event: stun.Event{
Message: &stun.Message{},
}}, nil
},
Endpoint: "stun.ekiga.net:3478",
Logger: log.Log,
})
if !errors.Is(err, stun.ErrAttributeNotFound) {
t.Fatalf("not the error we expected: %+v", err)
}
if ip != DefaultProbeIP {
t.Fatalf("not the IP address we expected: %+v", ip)
}
}
func TestIPLookupWorksUsingSTUNEkiga(t *testing.T) {
ip, err := stunEkigaIPLookup(
context.Background(),
http.DefaultClient,
log.Log,
httpheader.UserAgent(),
)
if err != nil {
t.Fatal(err)
}
if net.ParseIP(ip) == nil {
t.Fatalf("not an IP address: '%s'", ip)
}
}
func TestIPLookupWorksUsingSTUNGoogle(t *testing.T) {
ip, err := stunGoogleIPLookup(
context.Background(),
http.DefaultClient,
log.Log,
httpheader.UserAgent(),
)
if err != nil {
t.Fatal(err)
}
if net.ParseIP(ip) == nil {
t.Fatalf("not an IP address: '%s'", ip)
}
}