Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ainghazal committed Jul 31, 2024
1 parent 4e79fde commit 8844976
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
14 changes: 7 additions & 7 deletions internal/experiment/openvpn/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ const defaultOpenVPNEndpoint = "openvpn-server1.ooni.io"
// and perhaps also transform DNS failure into a specific failure of the experiment, not
// a skip.
// TODO(ain): update the openvpn spec to reflect the CURRENT state of delivering the targets.
func resolveTarget() (string, error) {
ips, err := net.LookupIP(defaultOpenVPNEndpoint)
func resolveTarget(domain string) (string, error) {
ips, err := net.LookupIP(domain)
if err != nil {
return "", err
}
if len(ips) == 0 {
return "", fmt.Errorf("cannot resolve %v", defaultOpenVPNEndpoint)
if len(ips) > 0 {
return ips[0].String(), nil
}
return ips[0].String(), nil
return "", fmt.Errorf("cannot resolve %v", defaultOpenVPNEndpoint)
}

func defaultOONITargetURL(ip string) string {
return "openvpn://oonivpn.corp/?address=" + ip + ":1194"
}

func defaultOONIOpenVPNTargetUDP() (string, error) {
ip, err := resolveTarget()
ip, err := resolveTarget(defaultOpenVPNEndpoint)
if err != nil {
return "", err
}
return defaultOONITargetURL(ip) + "&transport=udp", nil
}

func defaultOONIOpenVPNTargetTCP() (string, error) {
ip, err := resolveTarget()
ip, err := resolveTarget(defaultOpenVPNEndpoint)
if err != nil {
return "", err
}
Expand Down
57 changes: 57 additions & 0 deletions internal/experiment/openvpn/targets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package openvpn

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func Test_resolveTarget(t *testing.T) {
_, err := resolveTarget("google.com")
if err != nil {
t.Fatal("should be able to resolve the target")
}

_, err = resolveTarget("nothing.corp")
if err == nil {
t.Fatal("should be able to resolve the target")
}

_, err = resolveTarget("asfasfasfasfasfafs.ooni.io")
if err == nil {
t.Fatal("should be able to resolve the target")
}
}

func Test_defaultOONIOpenVPNTargetUDP(t *testing.T) {
url, err := defaultOONIOpenVPNTargetUDP()
if err != nil {
t.Fatal("unexpected error")
}
expected := "openvpn://oonivpn.corp/?address=37.218.243.98:1194&transport=udp"
if diff := cmp.Diff(url, expected); diff != "" {
t.Fatal(diff)
}
}

func Test_defaultOONIOpenVPNTargetTCP(t *testing.T) {
url, err := defaultOONIOpenVPNTargetTCP()
if err != nil {
t.Fatal("unexpected error")
}
expected := "openvpn://oonivpn.corp/?address=37.218.243.98:1194&transport=tcp"
if diff := cmp.Diff(url, expected); diff != "" {
t.Fatal(diff)
}
}

func Test_pickFromDefaultOONIOpenVPNConfig(t *testing.T) {
pick := pickFromDefaultOONIOpenVPNConfig()

if pick.Cipher != "AES-256-GCM" {
t.Fatal("cipher unexpected")
}
if pick.SafeCA != defaultCA {
t.Fatal("ca unexpected")
}
}

0 comments on commit 8844976

Please sign in to comment.