diff --git a/internal/experiment/openvpn/targets.go b/internal/experiment/openvpn/targets.go index e180c574d..6a85b48ca 100644 --- a/internal/experiment/openvpn/targets.go +++ b/internal/experiment/openvpn/targets.go @@ -15,15 +15,15 @@ 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 { @@ -31,7 +31,7 @@ func defaultOONITargetURL(ip string) string { } func defaultOONIOpenVPNTargetUDP() (string, error) { - ip, err := resolveTarget() + ip, err := resolveTarget(defaultOpenVPNEndpoint) if err != nil { return "", err } @@ -39,7 +39,7 @@ func defaultOONIOpenVPNTargetUDP() (string, error) { } func defaultOONIOpenVPNTargetTCP() (string, error) { - ip, err := resolveTarget() + ip, err := resolveTarget(defaultOpenVPNEndpoint) if err != nil { return "", err } diff --git a/internal/experiment/openvpn/targets_test.go b/internal/experiment/openvpn/targets_test.go new file mode 100644 index 000000000..44e008431 --- /dev/null +++ b/internal/experiment/openvpn/targets_test.go @@ -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") + } +}