package telegram_test import ( "context" "fmt" "io" "testing" "github.com/apex/log" "github.com/ooni/probe-cli/v3/internal/atomicx" "github.com/ooni/probe-cli/v3/internal/engine/experiment/telegram" "github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter" "github.com/ooni/probe-cli/v3/internal/engine/mockable" "github.com/ooni/probe-cli/v3/internal/model" "github.com/ooni/probe-cli/v3/internal/netxlite" ) func TestNewExperimentMeasurer(t *testing.T) { measurer := telegram.NewExperimentMeasurer(telegram.Config{}) if measurer.ExperimentName() != "telegram" { t.Fatal("unexpected name") } if measurer.ExperimentVersion() != "0.2.0" { t.Fatal("unexpected version") } } func TestGood(t *testing.T) { measurer := telegram.NewExperimentMeasurer(telegram.Config{}) measurement := new(model.Measurement) err := measurer.Run( context.Background(), &mockable.Session{ MockableLogger: log.Log, }, measurement, model.NewPrinterCallbacks(log.Log), ) if err != nil { t.Fatal(err) } tk := measurement.TestKeys.(*telegram.TestKeys) if tk.Agent != "redirect" { t.Fatal("unexpected Agent") } if tk.FailedOperation != nil { t.Fatal("unexpected FailedOperation") } if tk.Failure != nil { t.Fatal("unexpected Failure") } if len(tk.NetworkEvents) <= 0 { t.Fatal("no NetworkEvents?!") } if len(tk.Queries) <= 0 { t.Fatal("no Queries?!") } if len(tk.Requests) <= 0 { t.Fatal("no Requests?!") } if len(tk.TCPConnect) <= 0 { t.Fatal("no TCPConnect?!") } if len(tk.TLSHandshakes) <= 0 { t.Fatal("no TLSHandshakes?!") } if tk.TelegramHTTPBlocking != false { t.Fatal("unexpected TelegramHTTPBlocking") } if tk.TelegramTCPBlocking != false { t.Fatal("unexpected TelegramTCPBlocking") } if tk.TelegramWebFailure != nil { t.Fatal("unexpected TelegramWebFailure") } if tk.TelegramWebStatus != "ok" { t.Fatal("unexpected TelegramWebStatus") } sk, err := measurer.GetSummaryKeys(measurement) if err != nil { t.Fatal(err) } if _, ok := sk.(telegram.SummaryKeys); !ok { t.Fatal("invalid type for summary keys") } } func TestUpdateWithNoAccessPointsBlocking(t *testing.T) { tk := telegram.NewTestKeys() tk.Update(urlgetter.MultiOutput{ Input: urlgetter.MultiInput{ Config: urlgetter.Config{Method: "POST"}, Target: "http://149.154.175.50/", }, TestKeys: urlgetter.TestKeys{ Failure: (func() *string { s := netxlite.FailureEOFError return &s })(), }, }) tk.Update(urlgetter.MultiOutput{ Input: urlgetter.MultiInput{ Config: urlgetter.Config{Method: "POST"}, Target: "http://149.154.175.50:443/", }, TestKeys: urlgetter.TestKeys{ Failure: nil, // this should be enough to declare success }, }) if tk.TelegramHTTPBlocking == true { t.Fatal("there should be no TelegramHTTPBlocking") } if tk.TelegramTCPBlocking == true { t.Fatal("there should be no TelegramTCPBlocking") } } func TestUpdateWithNilFailedOperation(t *testing.T) { tk := telegram.NewTestKeys() tk.Update(urlgetter.MultiOutput{ Input: urlgetter.MultiInput{ Config: urlgetter.Config{Method: "POST"}, Target: "http://149.154.175.50/", }, TestKeys: urlgetter.TestKeys{ Failure: (func() *string { s := netxlite.FailureEOFError return &s })(), }, }) tk.Update(urlgetter.MultiOutput{ Input: urlgetter.MultiInput{ Config: urlgetter.Config{Method: "POST"}, Target: "http://149.154.175.50:443/", }, TestKeys: urlgetter.TestKeys{ Failure: (func() *string { s := netxlite.FailureEOFError return &s })(), }, }) if tk.TelegramHTTPBlocking == false { t.Fatal("there should be TelegramHTTPBlocking") } if tk.TelegramTCPBlocking == true { t.Fatal("there should be no TelegramTCPBlocking") } } func TestUpdateWithNonConnectFailedOperation(t *testing.T) { tk := telegram.NewTestKeys() tk.Update(urlgetter.MultiOutput{ Input: urlgetter.MultiInput{ Config: urlgetter.Config{Method: "POST"}, Target: "http://149.154.175.50/", }, TestKeys: urlgetter.TestKeys{ FailedOperation: (func() *string { s := netxlite.ConnectOperation return &s })(), Failure: (func() *string { s := netxlite.FailureEOFError return &s })(), }, }) tk.Update(urlgetter.MultiOutput{ Input: urlgetter.MultiInput{ Config: urlgetter.Config{Method: "POST"}, Target: "http://149.154.175.50:443/", }, TestKeys: urlgetter.TestKeys{ FailedOperation: (func() *string { s := netxlite.HTTPRoundTripOperation return &s })(), Failure: (func() *string { s := netxlite.FailureEOFError return &s })(), }, }) if tk.TelegramHTTPBlocking == false { t.Fatal("there should be TelegramHTTPBlocking") } if tk.TelegramTCPBlocking == true { t.Fatal("there should be no TelegramTCPBlocking") } } func TestUpdateWithAllConnectsFailed(t *testing.T) { tk := telegram.NewTestKeys() tk.Update(urlgetter.MultiOutput{ Input: urlgetter.MultiInput{ Config: urlgetter.Config{Method: "POST"}, Target: "http://149.154.175.50/", }, TestKeys: urlgetter.TestKeys{ FailedOperation: (func() *string { s := netxlite.ConnectOperation return &s })(), Failure: (func() *string { s := netxlite.FailureEOFError return &s })(), }, }) tk.Update(urlgetter.MultiOutput{ Input: urlgetter.MultiInput{ Config: urlgetter.Config{Method: "POST"}, Target: "http://149.154.175.50:443/", }, TestKeys: urlgetter.TestKeys{ FailedOperation: (func() *string { s := netxlite.ConnectOperation return &s })(), Failure: (func() *string { s := netxlite.FailureEOFError return &s })(), }, }) if tk.TelegramHTTPBlocking == false { t.Fatal("there should be TelegramHTTPBlocking") } if tk.TelegramTCPBlocking == false { t.Fatal("there should be TelegramTCPBlocking") } } func TestUpdateWebWithMixedResults(t *testing.T) { tk := telegram.NewTestKeys() tk.Update(urlgetter.MultiOutput{ Input: urlgetter.MultiInput{ Config: urlgetter.Config{Method: "GET"}, Target: "http://web.telegram.org/", }, TestKeys: urlgetter.TestKeys{ FailedOperation: (func() *string { s := netxlite.HTTPRoundTripOperation return &s })(), Failure: (func() *string { s := netxlite.FailureEOFError return &s })(), }, }) tk.Update(urlgetter.MultiOutput{ Input: urlgetter.MultiInput{ Config: urlgetter.Config{Method: "GET"}, Target: "https://web.telegram.org/", }, TestKeys: urlgetter.TestKeys{ HTTPResponseBody: `