Skip to content

Commit

Permalink
feat: test for the mixed policies case
Browse files Browse the repository at this point in the history
Previously, we were only testing with DNS returning error, while
now we should also have a test case for when it's working given that
we're mixing tactics together now.
  • Loading branch information
bassosimone committed Apr 15, 2024
1 parent 0d2b0f2 commit aee17cf
Showing 1 changed file with 71 additions and 3 deletions.
74 changes: 71 additions & 3 deletions internal/enginenetx/bridgespolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ func TestBridgesPolicy(t *testing.T) {
}
})

// TODO(bassosimone): we need to write better test cases for what
// happens when we have a mixture of tactics here.

t.Run("for the api.ooni.io domain with DNS failure", func(t *testing.T) {
expected := errors.New("mocked error")
p := &bridgesPolicy{
Expand All @@ -95,6 +92,7 @@ func TestBridgesPolicy(t *testing.T) {
ctx := context.Background()
tactics := p.LookupTactics(ctx, "api.ooni.io", "443")

// since the DNS fails, we should only see tactics generated by bridges
var count int
for tactic := range tactics {
count++
Expand All @@ -120,6 +118,76 @@ func TestBridgesPolicy(t *testing.T) {
}
})

t.Run("for the api.ooni.io domain with DNS success", func(t *testing.T) {
p := &bridgesPolicy{
Fallback: &dnsPolicy{
Logger: model.DiscardLogger,
Resolver: &mocks.Resolver{
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
return []string{"130.192.91.211"}, nil
},
},
},
}

ctx := context.Background()
tactics := p.LookupTactics(ctx, "api.ooni.io", "443")

// since the DNS succeeds we should see bridge tactics mixed with DNS tactics
var (
bridgesCount int
dnsCount int
overallCount int
)
for tactic := range tactics {
overallCount++

t.Log(overallCount, tactic)

if tactic.Port != "443" {
t.Fatal("the port should always be 443")
}

switch {
case overallCount == 5:
if tactic.Address != "130.192.91.211" {
t.Fatal("the host should be 130.192.91.211 for count == 5")
}

if tactic.SNI != "api.ooni.io" {
t.Fatal("we should see the `api.ooni.io` SNI on the wire for count == 5")
}

dnsCount++

default:
if tactic.Address != "162.55.247.208" {
t.Fatal("the host should be 162.55.247.208 for count != 5")
}

if tactic.SNI == "api.ooni.io" {
t.Fatal("we should not see the `api.ooni.io` SNI on the wire for count != 5")
}

bridgesCount++
}

if tactic.VerifyHostname != "api.ooni.io" {
t.Fatal("the VerifyHostname field should always be like `api.ooni.io`")
}
}

if overallCount <= 0 {
t.Fatal("expected to see at least one tactic")
}
if dnsCount != 1 {
t.Fatal("expected to see exactly one DNS based tactic")
}
if bridgesCount <= 0 {
t.Fatal("expected to see at least one bridge tactic")
}
})

t.Run("for test helper domains", func(t *testing.T) {
for _, domain := range bridgesPolicyTestHelpersDomains {
t.Run(domain, func(t *testing.T) {
Expand Down

0 comments on commit aee17cf

Please sign in to comment.