2021-02-02 12:05:47 +01:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2021-03-29 19:37:32 +02:00
|
|
|
"io/fs"
|
2021-02-02 12:05:47 +01:00
|
|
|
"os"
|
2021-12-03 15:30:56 +01:00
|
|
|
"strings"
|
2021-02-02 12:05:47 +01:00
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
2021-03-29 19:19:06 +02:00
|
|
|
"github.com/apex/log"
|
2021-03-29 18:46:26 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/kvstore"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2021-03-29 19:19:06 +02:00
|
|
|
func TestInputLoaderInputNoneWithStaticInputs(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
2021-03-29 19:19:06 +02:00
|
|
|
StaticInputs: []string{"https://www.google.com/"},
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputNone,
|
2021-03-29 20:00:50 +02:00
|
|
|
}
|
2021-03-29 19:19:06 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if !errors.Is(err, ErrNoInputExpected) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("not the output we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputNoneWithFilesInputs(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
2021-03-29 19:19:06 +02:00
|
|
|
SourceFiles: []string{
|
|
|
|
"testdata/inputloader1.txt",
|
|
|
|
"testdata/inputloader2.txt",
|
|
|
|
},
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputNone,
|
2021-03-29 20:00:50 +02:00
|
|
|
}
|
2021-03-29 19:19:06 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if !errors.Is(err, ErrNoInputExpected) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("not the output we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputNoneWithBothInputs(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
2021-03-29 19:19:06 +02:00
|
|
|
StaticInputs: []string{"https://www.google.com/"},
|
|
|
|
SourceFiles: []string{
|
|
|
|
"testdata/inputloader1.txt",
|
|
|
|
"testdata/inputloader2.txt",
|
|
|
|
},
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputNone,
|
2021-03-29 20:00:50 +02:00
|
|
|
}
|
2021-03-29 19:19:06 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if !errors.Is(err, ErrNoInputExpected) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("not the output we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputNoneWithNoInput(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputNone,
|
2021-03-29 20:00:50 +02:00
|
|
|
}
|
2021-03-29 19:19:06 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(out) != 1 || out[0].URL != "" {
|
|
|
|
t.Fatal("not the output we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputOptionalWithNoInput(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputOptional,
|
2021-03-29 20:00:50 +02:00
|
|
|
}
|
2021-03-29 19:19:06 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(out) != 1 || out[0].URL != "" {
|
|
|
|
t.Fatal("not the output we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputOptionalWithInput(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
2021-03-29 19:19:06 +02:00
|
|
|
StaticInputs: []string{"https://www.google.com/"},
|
|
|
|
SourceFiles: []string{
|
|
|
|
"testdata/inputloader1.txt",
|
|
|
|
"testdata/inputloader2.txt",
|
|
|
|
},
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputOptional,
|
2021-03-29 20:00:50 +02:00
|
|
|
}
|
2021-03-29 19:19:06 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(out) != 5 {
|
|
|
|
t.Fatal("not the output length we expected")
|
|
|
|
}
|
2022-01-03 13:53:23 +01:00
|
|
|
expect := []model.OOAPIURLInfo{
|
2021-03-29 19:19:06 +02:00
|
|
|
{URL: "https://www.google.com/"},
|
|
|
|
{URL: "https://www.x.org/"},
|
|
|
|
{URL: "https://www.slashdot.org/"},
|
|
|
|
{URL: "https://abc.xyz/"},
|
|
|
|
{URL: "https://run.ooni.io/"},
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(out, expect); diff != "" {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputOptionalNonexistentFile(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
2021-03-29 19:19:06 +02:00
|
|
|
StaticInputs: []string{"https://www.google.com/"},
|
|
|
|
SourceFiles: []string{
|
|
|
|
"testdata/inputloader1.txt",
|
|
|
|
"/nonexistent",
|
|
|
|
"testdata/inputloader2.txt",
|
|
|
|
},
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputOptional,
|
2021-03-29 20:00:50 +02:00
|
|
|
}
|
2021-03-29 19:19:06 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if !errors.Is(err, syscall.ENOENT) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("not the output we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputStrictlyRequiredWithInput(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
2021-03-29 19:19:06 +02:00
|
|
|
StaticInputs: []string{"https://www.google.com/"},
|
|
|
|
SourceFiles: []string{
|
|
|
|
"testdata/inputloader1.txt",
|
|
|
|
"testdata/inputloader2.txt",
|
|
|
|
},
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputStrictlyRequired,
|
2021-03-29 20:00:50 +02:00
|
|
|
}
|
2021-03-29 19:19:06 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(out) != 5 {
|
|
|
|
t.Fatal("not the output length we expected")
|
|
|
|
}
|
2022-01-03 13:53:23 +01:00
|
|
|
expect := []model.OOAPIURLInfo{
|
2021-03-29 19:19:06 +02:00
|
|
|
{URL: "https://www.google.com/"},
|
|
|
|
{URL: "https://www.x.org/"},
|
|
|
|
{URL: "https://www.slashdot.org/"},
|
|
|
|
{URL: "https://abc.xyz/"},
|
|
|
|
{URL: "https://run.ooni.io/"},
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(out, expect); diff != "" {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputStrictlyRequiredWithoutInput(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputStrictlyRequired,
|
2021-03-29 20:00:50 +02:00
|
|
|
}
|
2021-03-29 19:19:06 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if !errors.Is(err, ErrInputRequired) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("not the output we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputStrictlyRequiredWithEmptyFile(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputStrictlyRequired,
|
2021-03-29 19:19:06 +02:00
|
|
|
SourceFiles: []string{
|
|
|
|
"testdata/inputloader1.txt",
|
|
|
|
"testdata/inputloader3.txt", // we want it before inputloader2.txt
|
|
|
|
"testdata/inputloader2.txt",
|
|
|
|
},
|
2021-03-29 20:00:50 +02:00
|
|
|
}
|
2021-03-29 19:19:06 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if !errors.Is(err, ErrDetectedEmptyFile) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("not the output we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 15:30:56 +01:00
|
|
|
func TestInputLoaderInputOrStaticDefaultWithInput(t *testing.T) {
|
|
|
|
il := &InputLoader{
|
|
|
|
ExperimentName: "dnscheck",
|
|
|
|
StaticInputs: []string{"https://www.google.com/"},
|
|
|
|
SourceFiles: []string{
|
|
|
|
"testdata/inputloader1.txt",
|
|
|
|
"testdata/inputloader2.txt",
|
|
|
|
},
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputOrStaticDefault,
|
2021-12-03 15:30:56 +01:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(out) != 5 {
|
|
|
|
t.Fatal("not the output length we expected")
|
|
|
|
}
|
2022-01-03 13:53:23 +01:00
|
|
|
expect := []model.OOAPIURLInfo{
|
2021-12-03 15:30:56 +01:00
|
|
|
{URL: "https://www.google.com/"},
|
|
|
|
{URL: "https://www.x.org/"},
|
|
|
|
{URL: "https://www.slashdot.org/"},
|
|
|
|
{URL: "https://abc.xyz/"},
|
|
|
|
{URL: "https://run.ooni.io/"},
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(out, expect); diff != "" {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputOrStaticDefaultWithEmptyFile(t *testing.T) {
|
|
|
|
il := &InputLoader{
|
|
|
|
ExperimentName: "dnscheck",
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputOrStaticDefault,
|
2021-12-03 15:30:56 +01:00
|
|
|
SourceFiles: []string{
|
|
|
|
"testdata/inputloader1.txt",
|
|
|
|
"testdata/inputloader3.txt", // we want it before inputloader2.txt
|
|
|
|
"testdata/inputloader2.txt",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if !errors.Is(err, ErrDetectedEmptyFile) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("not the output we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputOrStaticDefaultWithoutInputDNSCheck(t *testing.T) {
|
|
|
|
il := &InputLoader{
|
|
|
|
ExperimentName: "dnscheck",
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputOrStaticDefault,
|
2021-12-03 15:30:56 +01:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(out) != len(dnsCheckDefaultInput) {
|
|
|
|
t.Fatal("invalid output length")
|
|
|
|
}
|
|
|
|
for idx := 0; idx < len(dnsCheckDefaultInput); idx++ {
|
|
|
|
e := out[idx]
|
|
|
|
if e.CategoryCode != "MISC" {
|
|
|
|
t.Fatal("invalid category code")
|
|
|
|
}
|
|
|
|
if e.CountryCode != "XX" {
|
|
|
|
t.Fatal("invalid country code")
|
|
|
|
}
|
|
|
|
if e.URL != dnsCheckDefaultInput[idx] {
|
|
|
|
t.Fatal("invalid URL")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputOrStaticDefaultWithoutInputStunReachability(t *testing.T) {
|
|
|
|
il := &InputLoader{
|
|
|
|
ExperimentName: "stunreachability",
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputOrStaticDefault,
|
2021-12-03 15:30:56 +01:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(out) != len(stunReachabilityDefaultInput) {
|
|
|
|
t.Fatal("invalid output length")
|
|
|
|
}
|
|
|
|
for idx := 0; idx < len(stunReachabilityDefaultInput); idx++ {
|
|
|
|
e := out[idx]
|
|
|
|
if e.CategoryCode != "MISC" {
|
|
|
|
t.Fatal("invalid category code")
|
|
|
|
}
|
|
|
|
if e.CountryCode != "XX" {
|
|
|
|
t.Fatal("invalid country code")
|
|
|
|
}
|
|
|
|
if e.URL != stunReachabilityDefaultInput[idx] {
|
|
|
|
t.Fatal("invalid URL")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStaticBareInputForExperimentWorksWithNonCanonicalNames(t *testing.T) {
|
|
|
|
names := []string{"DNSCheck", "STUNReachability"}
|
|
|
|
for _, name := range names {
|
|
|
|
if _, err := staticInputForExperiment(name); err != nil {
|
|
|
|
t.Fatal("failure for", name, ":", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputOrStaticDefaultWithoutInputOtherName(t *testing.T) {
|
|
|
|
il := &InputLoader{
|
|
|
|
ExperimentName: "xx",
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputOrStaticDefault,
|
2021-12-03 15:30:56 +01:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if !errors.Is(err, ErrNoStaticInput) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("expected nil result here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 19:19:06 +02:00
|
|
|
func TestInputLoaderInputOrQueryBackendWithInput(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
2021-03-29 19:19:06 +02:00
|
|
|
StaticInputs: []string{"https://www.google.com/"},
|
|
|
|
SourceFiles: []string{
|
|
|
|
"testdata/inputloader1.txt",
|
|
|
|
"testdata/inputloader2.txt",
|
|
|
|
},
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputOrQueryBackend,
|
2021-03-29 20:00:50 +02:00
|
|
|
}
|
2021-03-29 19:19:06 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(out) != 5 {
|
|
|
|
t.Fatal("not the output length we expected")
|
|
|
|
}
|
2022-01-03 13:53:23 +01:00
|
|
|
expect := []model.OOAPIURLInfo{
|
2021-03-29 19:19:06 +02:00
|
|
|
{URL: "https://www.google.com/"},
|
|
|
|
{URL: "https://www.x.org/"},
|
|
|
|
{URL: "https://www.slashdot.org/"},
|
|
|
|
{URL: "https://abc.xyz/"},
|
|
|
|
{URL: "https://run.ooni.io/"},
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(out, expect); diff != "" {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputOrQueryBackendWithNoInputAndCancelledContext(t *testing.T) {
|
2021-04-05 15:28:13 +02:00
|
|
|
sess, err := NewSession(context.Background(), SessionConfig{
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
KVStore: &kvstore.Memory{},
|
2021-03-29 19:19:06 +02:00
|
|
|
Logger: log.Log,
|
|
|
|
SoftwareName: "miniooni",
|
|
|
|
SoftwareVersion: "0.1.0-dev",
|
|
|
|
TempDir: "testdata",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer sess.Close()
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputOrQueryBackend,
|
2021-03-29 19:19:06 +02:00
|
|
|
Session: sess,
|
2021-03-29 20:00:50 +02:00
|
|
|
}
|
2021-03-29 19:19:06 +02:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel() // fail immediately
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("not the output we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderInputOrQueryBackendWithEmptyFile(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
2022-08-17 10:57:03 +02:00
|
|
|
InputPolicy: model.InputOrQueryBackend,
|
2021-03-29 19:19:06 +02:00
|
|
|
SourceFiles: []string{
|
|
|
|
"testdata/inputloader1.txt",
|
|
|
|
"testdata/inputloader3.txt", // we want it before inputloader2.txt
|
|
|
|
"testdata/inputloader2.txt",
|
|
|
|
},
|
2021-03-29 20:00:50 +02:00
|
|
|
}
|
2021-03-29 19:19:06 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
out, err := il.Load(ctx)
|
|
|
|
if !errors.Is(err, ErrDetectedEmptyFile) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("not the output we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
type InputLoaderBrokenFS struct{}
|
|
|
|
|
2021-03-29 19:37:32 +02:00
|
|
|
func (InputLoaderBrokenFS) Open(filepath string) (fs.File, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return InputLoaderBrokenFile{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type InputLoaderBrokenFile struct{}
|
|
|
|
|
|
|
|
func (InputLoaderBrokenFile) Stat() (os.FileInfo, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (InputLoaderBrokenFile) Read([]byte) (int, error) {
|
|
|
|
return 0, syscall.EFAULT
|
|
|
|
}
|
|
|
|
|
|
|
|
func (InputLoaderBrokenFile) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputLoaderReadfileScannerFailure(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{}
|
2021-02-02 12:05:47 +01:00
|
|
|
out, err := il.readfile("", InputLoaderBrokenFS{}.Open)
|
|
|
|
if !errors.Is(err, syscall.EFAULT) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("not the output we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 18:46:26 +02:00
|
|
|
// InputLoaderMockableSession is a mockable session
|
|
|
|
// used by InputLoader tests.
|
|
|
|
type InputLoaderMockableSession struct {
|
|
|
|
// Output contains the output of CheckIn. It should
|
|
|
|
// be nil when Error is not-nil.
|
2022-01-03 13:53:23 +01:00
|
|
|
Output *model.OOAPICheckInInfo
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2021-03-29 18:46:26 +02:00
|
|
|
// Error is the error to be returned by CheckIn. It
|
|
|
|
// should be nil when Output is not-nil.
|
|
|
|
Error error
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-03-29 18:46:26 +02:00
|
|
|
// CheckIn implements InputLoaderSession.CheckIn.
|
|
|
|
func (sess *InputLoaderMockableSession) CheckIn(
|
2022-01-03 13:53:23 +01:00
|
|
|
ctx context.Context, config *model.OOAPICheckInConfig) (*model.OOAPICheckInInfo, error) {
|
2021-03-29 18:46:26 +02:00
|
|
|
if sess.Output == nil && sess.Error == nil {
|
|
|
|
return nil, errors.New("both Output and Error are nil")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-03-29 18:46:26 +02:00
|
|
|
return sess.Output, sess.Error
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-03-29 18:46:26 +02:00
|
|
|
func TestInputLoaderCheckInFailure(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
|
|
|
Session: &InputLoaderMockableSession{
|
2021-03-29 18:46:26 +02:00
|
|
|
Error: io.EOF,
|
|
|
|
},
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-03-29 20:00:50 +02:00
|
|
|
out, err := il.loadRemote(context.Background())
|
2021-02-02 12:05:47 +01:00
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("expected nil output here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 18:46:26 +02:00
|
|
|
func TestInputLoaderCheckInSuccessWithNilWebConnectivity(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
|
|
|
Session: &InputLoaderMockableSession{
|
2022-01-03 13:53:23 +01:00
|
|
|
Output: &model.OOAPICheckInInfo{},
|
2021-03-29 18:46:26 +02:00
|
|
|
},
|
|
|
|
}
|
2021-03-29 20:00:50 +02:00
|
|
|
out, err := il.loadRemote(context.Background())
|
2021-03-29 18:46:26 +02:00
|
|
|
if !errors.Is(err, ErrNoURLsReturned) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("expected nil output here")
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-03-29 18:46:26 +02:00
|
|
|
func TestInputLoaderCheckInSuccessWithNoURLs(t *testing.T) {
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
|
|
|
Session: &InputLoaderMockableSession{
|
2022-01-03 13:53:23 +01:00
|
|
|
Output: &model.OOAPICheckInInfo{
|
|
|
|
WebConnectivity: &model.OOAPICheckInInfoWebConnectivity{},
|
2021-03-29 18:46:26 +02:00
|
|
|
},
|
2021-02-02 12:05:47 +01:00
|
|
|
},
|
|
|
|
}
|
2021-03-29 20:00:50 +02:00
|
|
|
out, err := il.loadRemote(context.Background())
|
2021-03-29 18:46:26 +02:00
|
|
|
if !errors.Is(err, ErrNoURLsReturned) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("expected nil output here")
|
|
|
|
}
|
|
|
|
}
|
2021-03-29 18:46:26 +02:00
|
|
|
|
|
|
|
func TestInputLoaderCheckInSuccessWithSomeURLs(t *testing.T) {
|
2022-01-03 13:53:23 +01:00
|
|
|
expect := []model.OOAPIURLInfo{{
|
2021-03-29 18:46:26 +02:00
|
|
|
CategoryCode: "NEWS",
|
|
|
|
CountryCode: "IT",
|
|
|
|
URL: "https://repubblica.it",
|
|
|
|
}, {
|
|
|
|
CategoryCode: "NEWS",
|
|
|
|
CountryCode: "IT",
|
|
|
|
URL: "https://corriere.it",
|
|
|
|
}}
|
2021-03-29 20:00:50 +02:00
|
|
|
il := &InputLoader{
|
|
|
|
Session: &InputLoaderMockableSession{
|
2022-01-03 13:53:23 +01:00
|
|
|
Output: &model.OOAPICheckInInfo{
|
|
|
|
WebConnectivity: &model.OOAPICheckInInfoWebConnectivity{
|
2021-03-29 18:46:26 +02:00
|
|
|
URLs: expect,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-03-29 20:00:50 +02:00
|
|
|
out, err := il.loadRemote(context.Background())
|
2021-03-29 18:46:26 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(expect, out); diff != "" {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 14:14:25 +02:00
|
|
|
|
|
|
|
func TestPreventMistakesWithCategories(t *testing.T) {
|
2022-01-03 13:53:23 +01:00
|
|
|
input := []model.OOAPIURLInfo{{
|
2021-04-07 14:14:25 +02:00
|
|
|
CategoryCode: "NEWS",
|
|
|
|
URL: "https://repubblica.it/",
|
|
|
|
CountryCode: "IT",
|
|
|
|
}, {
|
|
|
|
CategoryCode: "HACK",
|
|
|
|
URL: "https://2600.com",
|
|
|
|
CountryCode: "XX",
|
|
|
|
}, {
|
|
|
|
CategoryCode: "FILE",
|
|
|
|
URL: "https://addons.mozilla.org/",
|
|
|
|
CountryCode: "XX",
|
|
|
|
}}
|
2022-01-03 13:53:23 +01:00
|
|
|
desired := []model.OOAPIURLInfo{{
|
2021-04-07 14:14:25 +02:00
|
|
|
CategoryCode: "NEWS",
|
|
|
|
URL: "https://repubblica.it/",
|
|
|
|
CountryCode: "IT",
|
|
|
|
}, {
|
|
|
|
CategoryCode: "FILE",
|
|
|
|
URL: "https://addons.mozilla.org/",
|
|
|
|
CountryCode: "XX",
|
|
|
|
}}
|
|
|
|
il := &InputLoader{}
|
|
|
|
output := il.preventMistakes(input, []string{"NEWS", "FILE"})
|
|
|
|
if diff := cmp.Diff(desired, output); diff != "" {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPreventMistakesWithoutCategoriesAndNil(t *testing.T) {
|
2022-01-03 13:53:23 +01:00
|
|
|
input := []model.OOAPIURLInfo{{
|
2021-04-07 14:14:25 +02:00
|
|
|
CategoryCode: "NEWS",
|
|
|
|
URL: "https://repubblica.it/",
|
|
|
|
CountryCode: "IT",
|
|
|
|
}, {
|
|
|
|
CategoryCode: "HACK",
|
|
|
|
URL: "https://2600.com",
|
|
|
|
CountryCode: "XX",
|
|
|
|
}, {
|
|
|
|
CategoryCode: "FILE",
|
|
|
|
URL: "https://addons.mozilla.org/",
|
|
|
|
CountryCode: "XX",
|
|
|
|
}}
|
|
|
|
il := &InputLoader{}
|
|
|
|
output := il.preventMistakes(input, nil)
|
|
|
|
if diff := cmp.Diff(input, output); diff != "" {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPreventMistakesWithoutCategoriesAndEmpty(t *testing.T) {
|
2022-01-03 13:53:23 +01:00
|
|
|
input := []model.OOAPIURLInfo{{
|
2021-04-07 14:14:25 +02:00
|
|
|
CategoryCode: "NEWS",
|
|
|
|
URL: "https://repubblica.it/",
|
|
|
|
CountryCode: "IT",
|
|
|
|
}, {
|
|
|
|
CategoryCode: "HACK",
|
|
|
|
URL: "https://2600.com",
|
|
|
|
CountryCode: "XX",
|
|
|
|
}, {
|
|
|
|
CategoryCode: "FILE",
|
|
|
|
URL: "https://addons.mozilla.org/",
|
|
|
|
CountryCode: "XX",
|
|
|
|
}}
|
|
|
|
il := &InputLoader{}
|
|
|
|
output := il.preventMistakes(input, []string{})
|
|
|
|
if diff := cmp.Diff(input, output); diff != "" {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InputLoaderFakeLogger is a fake InputLoaderLogger.
|
|
|
|
type InputLoaderFakeLogger struct{}
|
|
|
|
|
|
|
|
// Warnf implements InputLoaderLogger.Warnf
|
|
|
|
func (ilfl *InputLoaderFakeLogger) Warnf(format string, v ...interface{}) {}
|
|
|
|
|
|
|
|
func TestInputLoaderLoggerWorksAsIntended(t *testing.T) {
|
|
|
|
logger := &InputLoaderFakeLogger{}
|
|
|
|
inputLoader := &InputLoader{Logger: logger}
|
|
|
|
out := inputLoader.logger()
|
|
|
|
if out != logger {
|
|
|
|
t.Fatal("logger not working as intended")
|
|
|
|
}
|
|
|
|
}
|
2021-12-03 15:30:56 +01:00
|
|
|
|
|
|
|
func TestStringListToModelURLInfoWithValidInput(t *testing.T) {
|
|
|
|
input := []string{
|
|
|
|
"stun://stun.voip.blackberry.com:3478",
|
|
|
|
"stun://stun.altar.com.pl:3478",
|
|
|
|
}
|
|
|
|
output, err := stringListToModelURLInfo(input, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(input) != len(output) {
|
|
|
|
t.Fatal("unexpected output length")
|
|
|
|
}
|
|
|
|
for idx := 0; idx < len(input); idx++ {
|
|
|
|
if input[idx] != output[idx].URL {
|
|
|
|
t.Fatal("unexpected entry")
|
|
|
|
}
|
|
|
|
if output[idx].CategoryCode != "MISC" {
|
|
|
|
t.Fatal("unexpected category")
|
|
|
|
}
|
|
|
|
if output[idx].CountryCode != "XX" {
|
|
|
|
t.Fatal("unexpected country")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringListToModelURLInfoWithInvalidInput(t *testing.T) {
|
|
|
|
input := []string{
|
|
|
|
"stun://stun.voip.blackberry.com:3478",
|
|
|
|
"\t", // <- not a valid URL
|
|
|
|
"stun://stun.altar.com.pl:3478",
|
|
|
|
}
|
|
|
|
output, err := stringListToModelURLInfo(input, nil)
|
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "invalid control character in URL") {
|
|
|
|
t.Fatal("no the error we expected", err)
|
|
|
|
}
|
|
|
|
if output != nil {
|
|
|
|
t.Fatal("unexpected nil output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringListToModelURLInfoWithError(t *testing.T) {
|
|
|
|
input := []string{
|
|
|
|
"stun://stun.voip.blackberry.com:3478",
|
|
|
|
"\t",
|
|
|
|
"stun://stun.altar.com.pl:3478",
|
|
|
|
}
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
output, err := stringListToModelURLInfo(input, expected)
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("no the error we expected", err)
|
|
|
|
}
|
|
|
|
if output != nil {
|
|
|
|
t.Fatal("unexpected nil output")
|
|
|
|
}
|
|
|
|
}
|