2019-12-02 16:57:55 +01:00
|
|
|
package nettests
|
|
|
|
|
|
|
|
import (
|
2021-04-05 15:28:13 +02:00
|
|
|
"context"
|
2019-12-02 16:57:55 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-02 10:32:46 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database"
|
|
|
|
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/ooni"
|
2019-12-02 16:57:55 +01:00
|
|
|
)
|
|
|
|
|
2021-04-29 15:59:53 +02:00
|
|
|
func copyfile(source, dest string) error {
|
|
|
|
data, err := ioutil.ReadFile(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(dest, data, 0600)
|
|
|
|
}
|
|
|
|
|
2020-11-13 19:01:06 +01:00
|
|
|
func newOONIProbe(t *testing.T) *ooni.Probe {
|
2019-12-02 16:57:55 +01:00
|
|
|
homePath, err := ioutil.TempDir("", "ooniprobetests")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
configPath := path.Join(homePath, "config.json")
|
2020-11-13 17:14:26 +01:00
|
|
|
testingConfig := path.Join("..", "..", "testdata", "testing-config.json")
|
2021-04-29 15:59:53 +02:00
|
|
|
if err := copyfile(testingConfig, configPath); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-13 19:01:06 +01:00
|
|
|
probe := ooni.NewProbe(configPath, homePath)
|
2019-12-29 14:07:57 +01:00
|
|
|
swName := "ooniprobe-cli-tests"
|
|
|
|
swVersion := "3.0.0-alpha"
|
2020-11-13 19:01:06 +01:00
|
|
|
err = probe.Init(swName, swVersion)
|
2019-12-02 16:57:55 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-13 19:01:06 +01:00
|
|
|
return probe
|
2019-12-02 16:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateContext(t *testing.T) {
|
2020-11-13 19:01:06 +01:00
|
|
|
newOONIProbe(t)
|
2019-12-02 16:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRun(t *testing.T) {
|
2020-11-13 19:01:06 +01:00
|
|
|
probe := newOONIProbe(t)
|
2021-04-05 15:28:13 +02:00
|
|
|
sess, err := probe.NewSession(context.Background())
|
2020-06-04 11:19:38 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-13 20:07:30 +01:00
|
|
|
network, err := database.CreateNetwork(probe.DB(), sess)
|
2019-12-02 16:57:55 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-13 20:07:30 +01:00
|
|
|
res, err := database.CreateResult(probe.DB(), probe.Home(), "middlebox", network.ID)
|
2019-12-02 16:57:55 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-12-02 17:00:12 +01:00
|
|
|
nt := HTTPInvalidRequestLine{}
|
2020-11-13 19:01:06 +01:00
|
|
|
ctl := NewController(nt, probe, res, sess)
|
2019-12-02 16:57:55 +01:00
|
|
|
nt.Run(ctl)
|
|
|
|
}
|