2020-10-29 19:39:11 +03:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2021-06-01 21:06:55 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2020-10-29 19:39:11 +03:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-11-02 16:18:02 +03:00
|
|
|
"net/netip"
|
2020-10-29 19:39:11 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-11 17:32:58 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-29 19:39:11 +03:00
|
|
|
"howett.net/plist"
|
|
|
|
)
|
|
|
|
|
2021-09-17 18:31:07 +03:00
|
|
|
// setupDNSIPs is a helper that sets up the server IP address configuration for
|
|
|
|
// tests and also tears it down in a cleanup function.
|
|
|
|
func setupDNSIPs(t testing.TB) {
|
2021-09-13 16:00:36 +03:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
prevConfig := config
|
2021-09-17 18:31:07 +03:00
|
|
|
prevTLS := Context.tls
|
2021-09-13 16:00:36 +03:00
|
|
|
t.Cleanup(func() {
|
|
|
|
config = prevConfig
|
2021-09-17 18:31:07 +03:00
|
|
|
Context.tls = prevTLS
|
2021-09-13 16:00:36 +03:00
|
|
|
})
|
2021-09-17 18:31:07 +03:00
|
|
|
|
2021-09-13 16:00:36 +03:00
|
|
|
config = &configuration{
|
|
|
|
DNS: dnsConfig{
|
2022-11-02 16:18:02 +03:00
|
|
|
BindHosts: []netip.Addr{netip.IPv4Unspecified()},
|
2021-09-17 18:31:07 +03:00
|
|
|
Port: defaultPortDNS,
|
2021-09-13 16:00:36 +03:00
|
|
|
},
|
|
|
|
}
|
2021-09-17 18:31:07 +03:00
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
Context.tls = &tlsManager{}
|
2021-09-13 16:00:36 +03:00
|
|
|
}
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
func TestHandleMobileConfigDoH(t *testing.T) {
|
2021-09-17 18:31:07 +03:00
|
|
|
setupDNSIPs(t)
|
2021-09-13 16:00:36 +03:00
|
|
|
|
2020-11-25 18:09:41 +03:00
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "https://example.com:12345/apple/doh.mobileconfig?host=example.org", nil)
|
2021-06-01 21:06:55 +03:00
|
|
|
require.NoError(t, err)
|
2020-10-29 19:39:11 +03:00
|
|
|
|
2020-11-25 18:09:41 +03:00
|
|
|
w := httptest.NewRecorder()
|
2020-10-29 19:39:11 +03:00
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
handleMobileConfigDoH(w, r)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
2020-10-29 19:39:11 +03:00
|
|
|
|
2020-12-07 16:04:53 +03:00
|
|
|
var mc mobileConfig
|
2020-11-25 18:09:41 +03:00
|
|
|
_, err = plist.Unmarshal(w.Body.Bytes(), &mc)
|
2021-06-01 21:06:55 +03:00
|
|
|
require.NoError(t, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Len(t, mc.PayloadContent, 1)
|
2021-09-13 16:00:36 +03:00
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.Equal(t, "example.org DoH", mc.PayloadContent[0].PayloadDisplayName)
|
2021-09-13 16:00:36 +03:00
|
|
|
|
|
|
|
s := mc.PayloadContent[0].DNSSettings
|
|
|
|
require.NotNil(t, s)
|
|
|
|
|
|
|
|
assert.Empty(t, s.ServerName)
|
|
|
|
assert.Equal(t, "https://example.org/dns-query", s.ServerURL)
|
2020-11-25 18:09:41 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error_no_host", func(t *testing.T) {
|
|
|
|
oldTLSConf := Context.tls
|
|
|
|
t.Cleanup(func() { Context.tls = oldTLSConf })
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
Context.tls = &tlsManager{conf: tlsConfigSettings{}}
|
2020-11-25 18:09:41 +03:00
|
|
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "https://example.com:12345/apple/doh.mobileconfig", nil)
|
2021-06-01 21:06:55 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
err = json.NewEncoder(b).Encode(&jsonError{
|
|
|
|
Message: errEmptyHost.Error(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2020-11-25 18:09:41 +03:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
handleMobileConfigDoH(w, r)
|
2020-11-25 18:09:41 +03:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
2021-06-01 21:06:55 +03:00
|
|
|
assert.JSONEq(t, w.Body.String(), b.String())
|
2020-11-25 18:09:41 +03:00
|
|
|
})
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
t.Run("client_id", func(t *testing.T) {
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "https://example.com:12345/apple/doh.mobileconfig?host=example.org&client_id=cli42", nil)
|
2021-06-01 21:06:55 +03:00
|
|
|
require.NoError(t, err)
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
handleMobileConfigDoH(w, r)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
var mc mobileConfig
|
|
|
|
_, err = plist.Unmarshal(w.Body.Bytes(), &mc)
|
2021-06-01 21:06:55 +03:00
|
|
|
require.NoError(t, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Len(t, mc.PayloadContent, 1)
|
2021-09-13 16:00:36 +03:00
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.Equal(t, "example.org DoH", mc.PayloadContent[0].PayloadDisplayName)
|
2021-09-13 16:00:36 +03:00
|
|
|
|
|
|
|
s := mc.PayloadContent[0].DNSSettings
|
|
|
|
require.NotNil(t, s)
|
|
|
|
|
|
|
|
assert.Empty(t, s.ServerName)
|
|
|
|
assert.Equal(t, "https://example.org/dns-query/cli42", s.ServerURL)
|
2021-01-27 18:32:13 +03:00
|
|
|
})
|
2020-11-25 18:09:41 +03:00
|
|
|
}
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
func TestHandleMobileConfigDoT(t *testing.T) {
|
2021-09-17 18:31:07 +03:00
|
|
|
setupDNSIPs(t)
|
2021-09-13 16:00:36 +03:00
|
|
|
|
2020-11-25 18:09:41 +03:00
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "https://example.com:12345/apple/dot.mobileconfig?host=example.org", nil)
|
2021-06-01 21:06:55 +03:00
|
|
|
require.NoError(t, err)
|
2020-11-25 18:09:41 +03:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
handleMobileConfigDoT(w, r)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
2020-11-25 18:09:41 +03:00
|
|
|
|
2020-12-07 16:04:53 +03:00
|
|
|
var mc mobileConfig
|
2020-11-25 18:09:41 +03:00
|
|
|
_, err = plist.Unmarshal(w.Body.Bytes(), &mc)
|
2021-06-01 21:06:55 +03:00
|
|
|
require.NoError(t, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Len(t, mc.PayloadContent, 1)
|
2021-09-13 16:00:36 +03:00
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.Equal(t, "example.org DoT", mc.PayloadContent[0].PayloadDisplayName)
|
2021-09-13 16:00:36 +03:00
|
|
|
|
|
|
|
s := mc.PayloadContent[0].DNSSettings
|
|
|
|
require.NotNil(t, s)
|
|
|
|
|
|
|
|
assert.Equal(t, "example.org", s.ServerName)
|
|
|
|
assert.Empty(t, s.ServerURL)
|
2020-11-25 18:09:41 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error_no_host", func(t *testing.T) {
|
|
|
|
oldTLSConf := Context.tls
|
|
|
|
t.Cleanup(func() { Context.tls = oldTLSConf })
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
Context.tls = &tlsManager{conf: tlsConfigSettings{}}
|
2020-11-25 18:09:41 +03:00
|
|
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "https://example.com:12345/apple/dot.mobileconfig", nil)
|
2021-06-01 21:06:55 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
err = json.NewEncoder(b).Encode(&jsonError{
|
|
|
|
Message: errEmptyHost.Error(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2020-11-25 18:09:41 +03:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
handleMobileConfigDoT(w, r)
|
2020-11-25 18:09:41 +03:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
2021-06-01 21:06:55 +03:00
|
|
|
assert.JSONEq(t, w.Body.String(), b.String())
|
2020-11-25 18:09:41 +03:00
|
|
|
})
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
t.Run("client_id", func(t *testing.T) {
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "https://example.com:12345/apple/dot.mobileconfig?host=example.org&client_id=cli42", nil)
|
2021-06-01 21:06:55 +03:00
|
|
|
require.NoError(t, err)
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
handleMobileConfigDoT(w, r)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
var mc mobileConfig
|
|
|
|
_, err = plist.Unmarshal(w.Body.Bytes(), &mc)
|
2021-06-01 21:06:55 +03:00
|
|
|
require.NoError(t, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Len(t, mc.PayloadContent, 1)
|
2021-09-13 16:00:36 +03:00
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.Equal(t, "example.org DoT", mc.PayloadContent[0].PayloadDisplayName)
|
2021-09-13 16:00:36 +03:00
|
|
|
|
|
|
|
s := mc.PayloadContent[0].DNSSettings
|
|
|
|
require.NotNil(t, s)
|
|
|
|
|
|
|
|
assert.Equal(t, "cli42.example.org", s.ServerName)
|
|
|
|
assert.Empty(t, s.ServerURL)
|
2021-01-27 18:32:13 +03:00
|
|
|
})
|
2020-10-29 19:39:11 +03:00
|
|
|
}
|