2021-02-02 12:05:47 +01:00
|
|
|
package oonimkall_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/geolocate"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
refactor: start building an Android package (#205)
* refactor: start building an Android package
Part of https://github.com/ooni/probe/issues/1335.
This seems also a good moment to move some packages out of the
engine, e.g., oonimkall. This package, for example, is a consumer
of the engine, so it makes sense it's not _inside_ it.
* fix: committed some stuff I didn't need to commit
* fix: oonimkall needs to be public to build
The side effect is that we will probably need to bump the major
version number every time we change one of these APIs.
(We can also of course choose to violate the basic guidelines of Go
software, but I believe this is bad form.)
I have no problem in bumping the major quite frequently and in
any case this monorepo solution is convinving me more than continuing
to keep a split between engine and cli. The need to embed assets to
make the probe more reliable trumps the negative effects of having to
~frequently bump major because we expose a public API.
* fix: let's not forget about libooniffi
Honestly, I don't know what to do with this library. I added it
to provide a drop in replacement for MK but I have no idea whether
it's used and useful. I would not feel comfortable exposing it,
unlike oonimkall, since we're not using it.
It may be that the right thing to do here is just to delete the
package and reduce the amount of code we're maintaining?
* woops, we're still missing the publish android script
* fix(publish-android.bash): add proper API key
* ouch fix another place where the name changed
2021-02-03 10:51:14 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/pkg/oonimkall"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2021-03-18 08:44:58 +01:00
|
|
|
func NewSessionForTestingWithAssetsDir(assetsDir string) (*oonimkall.Session, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return oonimkall.NewSession(&oonimkall.SessionConfig{
|
|
|
|
AssetsDir: assetsDir,
|
|
|
|
ProbeServicesURL: "https://ams-pg-test.ooni.org/",
|
|
|
|
SoftwareName: "oonimkall-test",
|
|
|
|
SoftwareVersion: "0.1.0",
|
|
|
|
StateDir: "../testdata/oonimkall/state",
|
|
|
|
TempDir: "../testdata/",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-18 08:44:58 +01:00
|
|
|
func NewSessionForTesting() (*oonimkall.Session, error) {
|
|
|
|
return NewSessionForTestingWithAssetsDir("../testdata/oonimkall/assets")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewSessionWithInvalidStateDir(t *testing.T) {
|
|
|
|
sess, err := oonimkall.NewSession(&oonimkall.SessionConfig{
|
|
|
|
StateDir: "",
|
|
|
|
})
|
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "no such file or directory") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatal("expected a nil Session here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReduceErrorForGeolocate(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return errors.New("we expected an error here")
|
|
|
|
}
|
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
return nil // when we have not downloaded the resources yet
|
|
|
|
}
|
|
|
|
if !errors.Is(err, geolocate.ErrAllIPLookuppersFailed) {
|
|
|
|
return nil // otherwise
|
|
|
|
}
|
|
|
|
return fmt.Errorf("not the error we expected: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGeolocateWithCancelledContext(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
2021-03-18 08:44:58 +01:00
|
|
|
sess, err := NewSessionForTesting()
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := sess.NewContext()
|
|
|
|
ctx.Cancel() // cause immediate failure
|
|
|
|
location, err := sess.Geolocate(ctx)
|
|
|
|
if err := ReduceErrorForGeolocate(err); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if location != nil {
|
|
|
|
t.Fatal("expected nil location here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGeolocateGood(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
2021-03-18 08:44:58 +01:00
|
|
|
sess, err := NewSessionForTesting()
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := sess.NewContext()
|
|
|
|
location, err := sess.Geolocate(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if location.ASN == "" {
|
|
|
|
t.Fatal("location.ASN is empty")
|
|
|
|
}
|
|
|
|
if location.Country == "" {
|
|
|
|
t.Fatal("location.Country is empty")
|
|
|
|
}
|
|
|
|
if location.IP == "" {
|
|
|
|
t.Fatal("location.IP is empty")
|
|
|
|
}
|
|
|
|
if location.Org == "" {
|
|
|
|
t.Fatal("location.Org is empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReduceErrorForSubmitter(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return errors.New("we expected an error here")
|
|
|
|
}
|
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
return nil // when we have not downloaded the resources yet
|
|
|
|
}
|
|
|
|
if err.Error() == "all available probe services failed" {
|
|
|
|
return nil // otherwise
|
|
|
|
}
|
|
|
|
return fmt.Errorf("not the error we expected: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubmitWithCancelledContext(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
2021-03-18 08:44:58 +01:00
|
|
|
sess, err := NewSessionForTesting()
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := sess.NewContext()
|
|
|
|
ctx.Cancel() // cause immediate failure
|
|
|
|
result, err := sess.Submit(ctx, "{}")
|
|
|
|
if err := ReduceErrorForSubmitter(err); err != nil {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if result != nil {
|
|
|
|
t.Fatal("expected nil result here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubmitWithInvalidJSON(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
2021-03-18 08:44:58 +01:00
|
|
|
sess, err := NewSessionForTesting()
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := sess.NewContext()
|
|
|
|
result, err := sess.Submit(ctx, "{")
|
|
|
|
if err == nil || err.Error() != "unexpected end of JSON input" {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if result != nil {
|
|
|
|
t.Fatal("expected nil result here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DoSubmission(ctx *oonimkall.Context, sess *oonimkall.Session) error {
|
|
|
|
inputm := model.Measurement{
|
|
|
|
DataFormatVersion: "0.2.0",
|
|
|
|
MeasurementStartTime: "2019-10-28 12:51:07",
|
|
|
|
MeasurementRuntime: 1.71,
|
|
|
|
ProbeASN: "AS30722",
|
|
|
|
ProbeCC: "IT",
|
|
|
|
ProbeIP: "127.0.0.1",
|
|
|
|
ReportID: "",
|
|
|
|
ResolverIP: "172.217.33.129",
|
|
|
|
SoftwareName: "miniooni",
|
|
|
|
SoftwareVersion: "0.1.0-dev",
|
|
|
|
TestKeys: map[string]bool{"success": true},
|
|
|
|
TestName: "example",
|
|
|
|
TestVersion: "0.1.0",
|
|
|
|
}
|
|
|
|
inputd, err := json.Marshal(inputm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
result, err := sess.Submit(ctx, string(inputd))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("session_test.go: submit failed: %w", err)
|
|
|
|
}
|
|
|
|
if result.UpdatedMeasurement == "" {
|
|
|
|
return errors.New("expected non empty measurement")
|
|
|
|
}
|
|
|
|
if result.UpdatedReportID == "" {
|
|
|
|
return errors.New("expected non empty report ID")
|
|
|
|
}
|
|
|
|
var outputm model.Measurement
|
|
|
|
return json.Unmarshal([]byte(result.UpdatedMeasurement), &outputm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubmitMeasurementGood(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
2021-03-18 08:44:58 +01:00
|
|
|
sess, err := NewSessionForTesting()
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := sess.NewContext()
|
|
|
|
if err := DoSubmission(ctx, sess); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubmitCancelContextAfterFirstSubmission(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
2021-03-18 08:44:58 +01:00
|
|
|
sess, err := NewSessionForTesting()
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := sess.NewContext()
|
|
|
|
if err := DoSubmission(ctx, sess); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx.Cancel() // fail second submission
|
|
|
|
err = DoSubmission(ctx, sess)
|
|
|
|
if err == nil || !strings.HasPrefix(err.Error(), "session_test.go: submit failed") {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckInSuccess(t *testing.T) {
|
2022-05-24 21:01:15 +02:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
2021-03-18 08:44:58 +01:00
|
|
|
sess, err := NewSessionForTesting()
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := sess.NewContext()
|
|
|
|
config := oonimkall.CheckInConfig{
|
|
|
|
Charging: true,
|
|
|
|
OnWiFi: true,
|
|
|
|
Platform: "android",
|
2022-05-23 18:41:34 +02:00
|
|
|
RunType: string(model.RunTypeTimed),
|
2021-02-02 12:05:47 +01:00
|
|
|
SoftwareName: "ooniprobe-android",
|
|
|
|
SoftwareVersion: "2.7.1",
|
|
|
|
WebConnectivity: &oonimkall.CheckInConfigWebConnectivity{},
|
|
|
|
}
|
|
|
|
config.WebConnectivity.Add("NEWS")
|
|
|
|
config.WebConnectivity.Add("CULTR")
|
|
|
|
result, err := sess.CheckIn(ctx, &config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %+v", err)
|
|
|
|
}
|
|
|
|
if result == nil || result.WebConnectivity == nil {
|
|
|
|
t.Fatal("got nil result or WebConnectivity")
|
|
|
|
}
|
|
|
|
if len(result.WebConnectivity.URLs) < 1 {
|
|
|
|
t.Fatal("unexpected number of URLs")
|
|
|
|
}
|
|
|
|
if result.WebConnectivity.ReportID == "" {
|
|
|
|
t.Fatal("got empty report ID")
|
|
|
|
}
|
|
|
|
siz := result.WebConnectivity.Size()
|
|
|
|
if siz <= 0 {
|
|
|
|
t.Fatal("unexpected number of URLs")
|
|
|
|
}
|
|
|
|
for idx := int64(0); idx < siz; idx++ {
|
|
|
|
entry := result.WebConnectivity.At(idx)
|
|
|
|
if entry.CategoryCode != "NEWS" && entry.CategoryCode != "CULTR" {
|
|
|
|
t.Fatalf("unexpected category code: %+v", entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if result.WebConnectivity.At(-1) != nil {
|
|
|
|
t.Fatal("expected nil here")
|
|
|
|
}
|
|
|
|
if result.WebConnectivity.At(siz) != nil {
|
|
|
|
t.Fatal("expected nil here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckInLookupLocationFailure(t *testing.T) {
|
2022-05-24 21:01:15 +02:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
2021-03-18 08:44:58 +01:00
|
|
|
sess, err := NewSessionForTesting()
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := sess.NewContext()
|
|
|
|
config := oonimkall.CheckInConfig{
|
|
|
|
Charging: true,
|
|
|
|
OnWiFi: true,
|
|
|
|
Platform: "android",
|
2022-05-23 18:41:34 +02:00
|
|
|
RunType: string(model.RunTypeTimed),
|
2021-02-02 12:05:47 +01:00
|
|
|
SoftwareName: "ooniprobe-android",
|
|
|
|
SoftwareVersion: "2.7.1",
|
|
|
|
WebConnectivity: &oonimkall.CheckInConfigWebConnectivity{},
|
|
|
|
}
|
|
|
|
config.WebConnectivity.Add("NEWS")
|
|
|
|
config.WebConnectivity.Add("CULTR")
|
|
|
|
ctx.Cancel() // immediate failure
|
|
|
|
result, err := sess.CheckIn(ctx, &config)
|
|
|
|
if !errors.Is(err, geolocate.ErrAllIPLookuppersFailed) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if result != nil {
|
|
|
|
t.Fatal("expected nil result here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckInNewProbeServicesFailure(t *testing.T) {
|
2022-05-24 21:01:15 +02:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
2021-03-18 08:44:58 +01:00
|
|
|
sess, err := NewSessionForTesting()
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sess.TestingCheckInBeforeNewProbeServicesClient = func(ctx *oonimkall.Context) {
|
|
|
|
ctx.Cancel() // cancel execution
|
|
|
|
}
|
|
|
|
ctx := sess.NewContext()
|
|
|
|
config := oonimkall.CheckInConfig{
|
|
|
|
Charging: true,
|
|
|
|
OnWiFi: true,
|
|
|
|
Platform: "android",
|
2022-05-23 18:41:34 +02:00
|
|
|
RunType: string(model.RunTypeTimed),
|
2021-02-02 12:05:47 +01:00
|
|
|
SoftwareName: "ooniprobe-android",
|
|
|
|
SoftwareVersion: "2.7.1",
|
|
|
|
WebConnectivity: &oonimkall.CheckInConfigWebConnectivity{},
|
|
|
|
}
|
|
|
|
config.WebConnectivity.Add("NEWS")
|
|
|
|
config.WebConnectivity.Add("CULTR")
|
|
|
|
result, err := sess.CheckIn(ctx, &config)
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
if !errors.Is(err, context.Canceled) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if result != nil {
|
|
|
|
t.Fatal("expected nil result here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckInCheckInFailure(t *testing.T) {
|
2022-05-24 21:01:15 +02:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
2021-03-18 08:44:58 +01:00
|
|
|
sess, err := NewSessionForTesting()
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sess.TestingCheckInBeforeCheckIn = func(ctx *oonimkall.Context) {
|
|
|
|
ctx.Cancel() // cancel execution
|
|
|
|
}
|
|
|
|
ctx := sess.NewContext()
|
|
|
|
config := oonimkall.CheckInConfig{
|
|
|
|
Charging: true,
|
|
|
|
OnWiFi: true,
|
|
|
|
Platform: "android",
|
2022-05-23 18:41:34 +02:00
|
|
|
RunType: string(model.RunTypeTimed),
|
2021-02-02 12:05:47 +01:00
|
|
|
SoftwareName: "ooniprobe-android",
|
|
|
|
SoftwareVersion: "2.7.1",
|
|
|
|
WebConnectivity: &oonimkall.CheckInConfigWebConnectivity{},
|
|
|
|
}
|
|
|
|
config.WebConnectivity.Add("NEWS")
|
|
|
|
config.WebConnectivity.Add("CULTR")
|
|
|
|
result, err := sess.CheckIn(ctx, &config)
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if result != nil {
|
|
|
|
t.Fatal("expected nil result here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckInNoParams(t *testing.T) {
|
2022-05-24 21:01:15 +02:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
2021-03-18 08:44:58 +01:00
|
|
|
sess, err := NewSessionForTesting()
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := sess.NewContext()
|
|
|
|
config := oonimkall.CheckInConfig{
|
|
|
|
Charging: true,
|
|
|
|
OnWiFi: true,
|
|
|
|
Platform: "android",
|
2022-05-23 18:41:34 +02:00
|
|
|
RunType: string(model.RunTypeTimed),
|
2021-02-02 12:05:47 +01:00
|
|
|
SoftwareName: "ooniprobe-android",
|
|
|
|
SoftwareVersion: "2.7.1",
|
|
|
|
}
|
|
|
|
result, err := sess.CheckIn(ctx, &config)
|
|
|
|
if err == nil || err.Error() != "oonimkall: missing webconnectivity config" {
|
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
if result != nil {
|
|
|
|
t.Fatal("unexpected not nil result here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 10:00:25 +01:00
|
|
|
func TestFetchURLListSuccess(t *testing.T) {
|
2021-09-05 11:58:02 +02:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
2021-03-18 08:44:58 +01:00
|
|
|
sess, err := NewSessionForTesting()
|
2021-02-26 10:00:25 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := sess.NewContext()
|
|
|
|
config := oonimkall.URLListConfig{
|
|
|
|
Limit: 10,
|
|
|
|
}
|
|
|
|
config.AddCategory("NEWS")
|
|
|
|
config.AddCategory("CULTR")
|
|
|
|
result, err := sess.FetchURLList(ctx, &config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %+v", err)
|
|
|
|
}
|
|
|
|
if result == nil || result.Results == nil {
|
|
|
|
t.Fatal("got nil result")
|
|
|
|
}
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
for idx := int64(0); idx < result.Size(); idx++ {
|
|
|
|
entry := result.At(idx)
|
2021-02-26 10:00:25 +01:00
|
|
|
if entry.CategoryCode != "NEWS" && entry.CategoryCode != "CULTR" {
|
|
|
|
t.Fatalf("unexpected category code: %+v", entry)
|
|
|
|
}
|
|
|
|
}
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
if result.At(-1) != nil {
|
|
|
|
t.Fatal("expected nil here")
|
|
|
|
}
|
|
|
|
if result.At(result.Size()) != nil {
|
|
|
|
t.Fatal("expected nil here")
|
|
|
|
}
|
2021-02-26 10:00:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchURLListWithCC(t *testing.T) {
|
2022-05-24 21:01:15 +02:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
2021-03-18 08:44:58 +01:00
|
|
|
sess, err := NewSessionForTesting()
|
2021-02-26 10:00:25 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx := sess.NewContext()
|
|
|
|
config := oonimkall.URLListConfig{
|
|
|
|
CountryCode: "IT",
|
|
|
|
}
|
|
|
|
config.AddCategory("NEWS")
|
|
|
|
config.AddCategory("CULTR")
|
|
|
|
result, err := sess.FetchURLList(ctx, &config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %+v", err)
|
|
|
|
}
|
|
|
|
if result == nil || result.Results == nil {
|
|
|
|
t.Fatal("got nil result")
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, entry := range result.Results {
|
|
|
|
if entry.CountryCode == "IT" {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("not found url for country code: IT")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
// Here we're basically testing whether eventually the finalizers
|
|
|
|
// will run and the number of active sessions and cancels will become
|
|
|
|
// balanced. Especially for the number of active cancels, this is an
|
|
|
|
// indication that we've correctly cleaned them up in the session.
|
|
|
|
if exitcode := m.Run(); exitcode != 0 {
|
|
|
|
os.Exit(exitcode)
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
runtime.GC()
|
|
|
|
m, n := oonimkall.ActiveContexts.Load(), oonimkall.ActiveSessions.Load()
|
|
|
|
fmt.Printf("./oonimkall: ActiveContexts: %d; ActiveSessions: %d\n", m, n)
|
|
|
|
if m == 0 && n == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|