Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ainghazal committed Mar 28, 2024
1 parent 5b599af commit 06a15c8
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 12 deletions.
4 changes: 2 additions & 2 deletions internal/engine/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,11 @@ func (s *Session) FetchOpenVPNConfig(
}
clnt, err := s.NewOrchestraClient(ctx)
if err != nil {
return &model.OOAPIVPNProviderConfig{}, err
return nil, err
}
config, err := clnt.FetchOpenVPNConfig(ctx, provider, cc)
if err != nil {
return &model.OOAPIVPNProviderConfig{}, err
return nil, err
}
s.vpnConfig[provider] = config
return &config, nil
Expand Down
11 changes: 10 additions & 1 deletion internal/engine/session_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,16 @@ func TestSessionMaybeLookupLocationContextLookupLocationContextFailure(t *testin
}

func TestSessionFetchOpenVPNConfigWithCancelledContext(t *testing.T) {
panic("TODO")
sess := &Session{}
ctx, cancel := context.WithCancel(context.Background())
cancel() // cause failure
resp, err := sess.FetchOpenVPNConfig(ctx, "riseup", "XX")
if !errors.Is(err, context.Canceled) {
t.Fatal("not the error we expected", err)
}
if resp != nil {
t.Fatal("expected nil response here")
}
}

func TestSessionFetchTorTargetsWithCancelledContext(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions internal/experiment/openvpn/openvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type TestKeys struct {
// NewTestKeys creates new openvpn TestKeys.
func NewTestKeys() *TestKeys {
return &TestKeys{
Success: true,
Success: false,
NetworkEvents: []*vpntracex.Event{},
TCPConnect: []*model.ArchivalTCPConnectResult{},
OpenVPNHandshake: []*ArchivalOpenVPNHandshakeResult{},
Expand All @@ -72,8 +72,8 @@ func (tk *TestKeys) AddConnectionTestKeys(result *SingleConnection) {
tk.NetworkEvents = append(tk.NetworkEvents, result.NetworkEvents...)
}

// allConnectionsSuccessful returns true if all the registered handshakes have Status.Success equal to true.
func (tk *TestKeys) allConnectionsSuccessful() bool {
// AllConnectionsSuccessful returns true if all the registered handshakes have Status.Success equal to true.
func (tk *TestKeys) AllConnectionsSuccessful() bool {
for _, c := range tk.OpenVPNHandshake {
if !c.Status.Success {
return false
Expand Down Expand Up @@ -168,7 +168,7 @@ func (m Measurer) Run(ctx context.Context, args *model.ExperimentArgs) error {
if connResult != nil {
tk.AddConnectionTestKeys(connResult)
}
tk.Success = tk.allConnectionsSuccessful()
tk.Success = tk.AllConnectionsSuccessful()

callbacks.OnProgress(1.0, "All endpoints probed")
measurement.TestKeys = tk
Expand Down
113 changes: 108 additions & 5 deletions internal/experiment/openvpn/openvpn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,126 @@ import (
"time"

"github.com/apex/log"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/experiment/example"
"github.com/ooni/probe-cli/v3/internal/experiment/openvpn"
"github.com/ooni/probe-cli/v3/internal/legacy/mockable"
"github.com/ooni/probe-cli/v3/internal/mocks"
"github.com/ooni/probe-cli/v3/internal/model"

vpntracex "github.com/ooni/minivpn/pkg/tracex"
)

func TestNewTestKeys(t *testing.T) {
tk := openvpn.NewTestKeys()
if tk.Success != false {
t.Fatal("default success should be false")
}
if tk.NetworkEvents == nil {
t.Fatal("NetworkEvents not initialized")
}
if tk.TCPConnect == nil {
t.Fatal("TCPConnect not initialized")
}
if tk.OpenVPNHandshake == nil {
t.Fatal("OpenVPNHandshake not initialized")
}
}

func TestAddConnectionTestKeys(t *testing.T) {
t.Run("append connection result to empty keys", func(t *testing.T) {
tk := openvpn.NewTestKeys()
sc := &openvpn.SingleConnection{
TCPConnect: &model.ArchivalTCPConnectResult{
IP: "1.1.1.1",
Port: 1194,
Status: model.ArchivalTCPConnectStatus{
Blocked: new(bool),
Failure: new(string),
Success: false,
},
T0: 0.1,
T: 0.9,
Tags: []string{},
TransactionID: 1,
},
OpenVPNHandshake: &openvpn.ArchivalOpenVPNHandshakeResult{
BootstrapTime: 1,
Endpoint: "aa",
IP: "1.1.1.1",
Port: 1194,
Transport: "tcp",
Provider: "unknown",
OpenVPNOptions: openvpn.OpenVPNOptions{},
Status: openvpn.ArchivalOpenVPNConnectStatus{},
StartTime: time.Now(),
T0: 0,
T: 0,
Tags: []string{},
TransactionID: 1,
},
NetworkEvents: []*vpntracex.Event{},
}
tk.AddConnectionTestKeys(sc)
if diff := cmp.Diff(tk.TCPConnect[0], sc.TCPConnect); diff != "" {
t.Fatal(diff)
}
if diff := cmp.Diff(tk.OpenVPNHandshake[0], sc.OpenVPNHandshake); diff != "" {
t.Fatal(diff)
}
if diff := cmp.Diff(tk.NetworkEvents, sc.NetworkEvents); diff != "" {
t.Fatal(diff)
}
})
}

func TestAllConnectionsSuccessful(t *testing.T) {
t.Run("all success", func(t *testing.T) {
tk := openvpn.NewTestKeys()
tk.OpenVPNHandshake = []*openvpn.ArchivalOpenVPNHandshakeResult{
{Status: openvpn.ArchivalOpenVPNConnectStatus{Success: true}},
{Status: openvpn.ArchivalOpenVPNConnectStatus{Success: true}},
{Status: openvpn.ArchivalOpenVPNConnectStatus{Success: true}},
}
if tk.AllConnectionsSuccessful() != true {
t.Fatal("expected all connections successful")
}
})
t.Run("one failure", func(t *testing.T) {
tk := openvpn.NewTestKeys()
tk.OpenVPNHandshake = []*openvpn.ArchivalOpenVPNHandshakeResult{
{Status: openvpn.ArchivalOpenVPNConnectStatus{Success: false}},
{Status: openvpn.ArchivalOpenVPNConnectStatus{Success: true}},
{Status: openvpn.ArchivalOpenVPNConnectStatus{Success: true}},
}
if tk.AllConnectionsSuccessful() != false {
t.Fatal("expected false")
}
})
t.Run("all failures", func(t *testing.T) {
tk := openvpn.NewTestKeys()
tk.OpenVPNHandshake = []*openvpn.ArchivalOpenVPNHandshakeResult{
{Status: openvpn.ArchivalOpenVPNConnectStatus{Success: false}},
{Status: openvpn.ArchivalOpenVPNConnectStatus{Success: false}},
{Status: openvpn.ArchivalOpenVPNConnectStatus{Success: false}},
}
if tk.AllConnectionsSuccessful() != false {
t.Fatal("expected false")
}
})

}

func TestSuccess(t *testing.T) {
m := example.NewExperimentMeasurer(example.Config{
SleepTime: int64(2 * time.Millisecond),
}, "example")
if m.ExperimentName() != "example" {
m := openvpn.NewExperimentMeasurer(openvpn.Config{}, "openvpn")
if m.ExperimentName() != "openvpn" {
t.Fatal("invalid ExperimentName")
}
if m.ExperimentVersion() != "0.1.0" {
t.Fatal("invalid ExperimentVersion")
}
ctx := context.Background()
sess := &mockable.Session{MockableLogger: log.Log}
sess := &mocks.Session{}
callbacks := model.NewPrinterCallbacks(sess.Logger())
measurement := new(model.Measurement)
args := &model.ExperimentArgs{
Expand Down

0 comments on commit 06a15c8

Please sign in to comment.