ooni-probe-cli/internal/model/mocks/trace_test.go
Simone Basso 5ebdeb56ca
feat: tlsping and tcpping using step-by-step (#815)
## Checklist

- [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request: https://github.com/ooni/probe/issues/2158
- [x] if you changed anything related how experiments work and you need to reflect these changes in the ooni/spec repository, please link to the related ooni/spec pull request: https://github.com/ooni/spec/pull/250

## Description

This diff refactors the codebase to reimplement tlsping and tcpping
to use the step-by-step measurements style.

See docs/design/dd-003-step-by-step.md for more information on the
step-by-step measurement style.
2022-07-01 12:22:22 +02:00

75 lines
1.4 KiB
Go

package mocks
import (
"crypto/tls"
"testing"
"time"
)
func TestTrace(t *testing.T) {
t.Run("TimeNow", func(t *testing.T) {
now := time.Now()
tx := &Trace{
MockTimeNow: func() time.Time {
return now
},
}
if !tx.TimeNow().Equal(now) {
t.Fatal("not working as intended")
}
})
t.Run("OnConnectDone", func(t *testing.T) {
var called bool
tx := &Trace{
MockOnConnectDone: func(started time.Time, network, domain, remoteAddr string, err error, finished time.Time) {
called = true
},
}
tx.OnConnectDone(
time.Now(),
"tcp",
"dns.google",
"8.8.8.8:443",
nil,
time.Now(),
)
if !called {
t.Fatal("not called")
}
})
t.Run("OnTLSHandshakeStart", func(t *testing.T) {
var called bool
tx := &Trace{
MockOnTLSHandshakeStart: func(now time.Time, remoteAddr string, config *tls.Config) {
called = true
},
}
tx.OnTLSHandshakeStart(time.Now(), "8.8.8.8:443", &tls.Config{})
if !called {
t.Fatal("not called")
}
})
t.Run("OnTLSHandshakeDone", func(t *testing.T) {
var called bool
tx := &Trace{
MockOnTLSHandshakeDone: func(started time.Time, remoteAddr string, config *tls.Config, state tls.ConnectionState, err error, finished time.Time) {
called = true
},
}
tx.OnTLSHandshakeDone(
time.Now(),
"8.8.8.8:443",
&tls.Config{},
tls.ConnectionState{},
nil,
time.Now(),
)
if !called {
t.Fatal("not called")
}
})
}