ooni-probe-cli/internal/engine/probeservices/statefile_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

154 lines
3.7 KiB
Go

package probeservices_test
import (
"encoding/json"
"errors"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/engine/internal/kvstore"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
)
func TestStateAuth(t *testing.T) {
t.Run("with no Token", func(t *testing.T) {
state := probeservices.State{Expire: time.Now().Add(10 * time.Hour)}
if state.Auth() != nil {
t.Fatal("expected nil here")
}
})
t.Run("with expired Token", func(t *testing.T) {
state := probeservices.State{
Expire: time.Now().Add(-1 * time.Hour),
Token: "xx-x-xxx-xx",
}
if state.Auth() != nil {
t.Fatal("expected nil here")
}
})
t.Run("with good Token", func(t *testing.T) {
state := probeservices.State{
Expire: time.Now().Add(10 * time.Hour),
Token: "xx-x-xxx-xx",
}
if state.Auth() == nil {
t.Fatal("expected valid auth here")
}
})
}
func TestStateCredentials(t *testing.T) {
t.Run("with no ClientID", func(t *testing.T) {
state := probeservices.State{}
if state.Credentials() != nil {
t.Fatal("expected nil here")
}
})
t.Run("with no Password", func(t *testing.T) {
state := probeservices.State{
ClientID: "xx-x-xxx-xx",
}
if state.Credentials() != nil {
t.Fatal("expected nil here")
}
})
t.Run("with all good", func(t *testing.T) {
state := probeservices.State{
ClientID: "xx-x-xxx-xx",
Password: "xx",
}
if state.Credentials() == nil {
t.Fatal("expected valid auth here")
}
})
}
func TestStateFileMemoryIntegration(t *testing.T) {
// Does the StateFile have the property that we can write
// values into it and then read again the same files?
sf := probeservices.NewStateFile(kvstore.NewMemoryKeyValueStore())
s := probeservices.State{
Expire: time.Now(),
Password: "xy",
Token: "abc",
ClientID: "xx",
}
if err := sf.Set(s); err != nil {
t.Fatal(err)
}
os := sf.Get()
diff := cmp.Diff(s, os)
if diff != "" {
t.Fatal(diff)
}
}
func TestStateFileSetMarshalError(t *testing.T) {
sf := probeservices.NewStateFile(kvstore.NewMemoryKeyValueStore())
s := probeservices.State{
Expire: time.Now(),
Password: "xy",
Token: "abc",
ClientID: "xx",
}
expected := errors.New("mocked error")
failingfunc := func(v interface{}) ([]byte, error) {
return nil, expected
}
if err := sf.SetMockable(s, failingfunc); !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
}
func TestStateFileGetKVStoreGetError(t *testing.T) {
sf := probeservices.NewStateFile(kvstore.NewMemoryKeyValueStore())
expected := errors.New("mocked error")
failingfunc := func(string) ([]byte, error) {
return nil, expected
}
s, err := sf.GetMockable(failingfunc, json.Unmarshal)
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if s.ClientID != "" {
t.Fatal("unexpected ClientID field")
}
if !s.Expire.IsZero() {
t.Fatal("unexpected Expire field")
}
if s.Password != "" {
t.Fatal("unexpected Password field")
}
if s.Token != "" {
t.Fatal("unexpected Token field")
}
}
func TestStateFileGetUnmarshalError(t *testing.T) {
sf := probeservices.NewStateFile(kvstore.NewMemoryKeyValueStore())
if err := sf.Set(probeservices.State{}); err != nil {
t.Fatal(err)
}
expected := errors.New("mocked error")
failingfunc := func([]byte, interface{}) error {
return expected
}
s, err := sf.GetMockable(sf.Store.Get, failingfunc)
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if s.ClientID != "" {
t.Fatal("unexpected ClientID field")
}
if !s.Expire.IsZero() {
t.Fatal("unexpected Expire field")
}
if s.Password != "" {
t.Fatal("unexpected Password field")
}
if s.Token != "" {
t.Fatal("unexpected Token field")
}
}