2020-10-29 16:39:11 +00:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2021-06-01 18:06:55 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2020-10-29 16:39:11 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-11 14:32:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-29 16:39:11 +00:00
|
|
|
"howett.net/plist"
|
|
|
|
)
|
|
|
|
|
2020-11-25 15:09:41 +00:00
|
|
|
func TestHandleMobileConfigDOH(t *testing.T) {
|
|
|
|
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 18:06:55 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-29 16:39:11 +00:00
|
|
|
|
2020-11-25 15:09:41 +00:00
|
|
|
w := httptest.NewRecorder()
|
2020-10-29 16:39:11 +00:00
|
|
|
|
2020-11-25 15:09:41 +00:00
|
|
|
handleMobileConfigDOH(w, r)
|
2021-03-11 14:32:58 +00:00
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
2020-10-29 16:39:11 +00:00
|
|
|
|
2020-12-07 13:04:53 +00:00
|
|
|
var mc mobileConfig
|
2020-11-25 15:09:41 +00:00
|
|
|
_, err = plist.Unmarshal(w.Body.Bytes(), &mc)
|
2021-06-01 18:06:55 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-29 16:39:11 +00:00
|
|
|
|
2021-03-11 14:32:58 +00:00
|
|
|
require.Len(t, mc.PayloadContent, 1)
|
|
|
|
assert.Equal(t, "example.org DoH", mc.PayloadContent[0].Name)
|
|
|
|
assert.Equal(t, "example.org DoH", mc.PayloadContent[0].PayloadDisplayName)
|
|
|
|
assert.Equal(t, "https://example.org/dns-query", mc.PayloadContent[0].DNSSettings.ServerURL)
|
2020-11-25 15:09:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error_no_host", func(t *testing.T) {
|
|
|
|
oldTLSConf := Context.tls
|
|
|
|
t.Cleanup(func() { Context.tls = oldTLSConf })
|
|
|
|
|
|
|
|
Context.tls = &TLSMod{conf: tlsConfigSettings{}}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "https://example.com:12345/apple/doh.mobileconfig", nil)
|
2021-06-01 18:06:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
err = json.NewEncoder(b).Encode(&jsonError{
|
|
|
|
Message: errEmptyHost.Error(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2020-11-25 15:09:41 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handleMobileConfigDOH(w, r)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
2021-06-01 18:06:55 +00:00
|
|
|
assert.JSONEq(t, w.Body.String(), b.String())
|
2020-11-25 15:09:41 +00:00
|
|
|
})
|
2021-01-27 15:32:13 +00: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 18:06:55 +00:00
|
|
|
require.NoError(t, err)
|
2021-01-27 15:32:13 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handleMobileConfigDOH(w, r)
|
2021-03-11 14:32:58 +00:00
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
2021-01-27 15:32:13 +00:00
|
|
|
|
|
|
|
var mc mobileConfig
|
|
|
|
_, err = plist.Unmarshal(w.Body.Bytes(), &mc)
|
2021-06-01 18:06:55 +00:00
|
|
|
require.NoError(t, err)
|
2021-01-27 15:32:13 +00:00
|
|
|
|
2021-03-11 14:32:58 +00:00
|
|
|
require.Len(t, mc.PayloadContent, 1)
|
|
|
|
assert.Equal(t, "example.org DoH", mc.PayloadContent[0].Name)
|
|
|
|
assert.Equal(t, "example.org DoH", mc.PayloadContent[0].PayloadDisplayName)
|
|
|
|
assert.Equal(t, "https://example.org/dns-query/cli42", mc.PayloadContent[0].DNSSettings.ServerURL)
|
2021-01-27 15:32:13 +00:00
|
|
|
})
|
2020-11-25 15:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleMobileConfigDOT(t *testing.T) {
|
|
|
|
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 18:06:55 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-25 15:09:41 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handleMobileConfigDOT(w, r)
|
2021-03-11 14:32:58 +00:00
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
2020-11-25 15:09:41 +00:00
|
|
|
|
2020-12-07 13:04:53 +00:00
|
|
|
var mc mobileConfig
|
2020-11-25 15:09:41 +00:00
|
|
|
_, err = plist.Unmarshal(w.Body.Bytes(), &mc)
|
2021-06-01 18:06:55 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-25 15:09:41 +00:00
|
|
|
|
2021-03-11 14:32:58 +00:00
|
|
|
require.Len(t, mc.PayloadContent, 1)
|
|
|
|
assert.Equal(t, "example.org DoT", mc.PayloadContent[0].Name)
|
|
|
|
assert.Equal(t, "example.org DoT", mc.PayloadContent[0].PayloadDisplayName)
|
|
|
|
assert.Equal(t, "example.org", mc.PayloadContent[0].DNSSettings.ServerName)
|
2020-11-25 15:09:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error_no_host", func(t *testing.T) {
|
|
|
|
oldTLSConf := Context.tls
|
|
|
|
t.Cleanup(func() { Context.tls = oldTLSConf })
|
|
|
|
|
|
|
|
Context.tls = &TLSMod{conf: tlsConfigSettings{}}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "https://example.com:12345/apple/dot.mobileconfig", nil)
|
2021-06-01 18:06:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
err = json.NewEncoder(b).Encode(&jsonError{
|
|
|
|
Message: errEmptyHost.Error(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2020-11-25 15:09:41 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handleMobileConfigDOT(w, r)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
2021-06-01 18:06:55 +00:00
|
|
|
|
|
|
|
assert.JSONEq(t, w.Body.String(), b.String())
|
2020-11-25 15:09:41 +00:00
|
|
|
})
|
2021-01-27 15:32:13 +00: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 18:06:55 +00:00
|
|
|
require.NoError(t, err)
|
2021-01-27 15:32:13 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handleMobileConfigDOT(w, r)
|
2021-03-11 14:32:58 +00:00
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
2021-01-27 15:32:13 +00:00
|
|
|
|
|
|
|
var mc mobileConfig
|
|
|
|
_, err = plist.Unmarshal(w.Body.Bytes(), &mc)
|
2021-06-01 18:06:55 +00:00
|
|
|
require.NoError(t, err)
|
2021-01-27 15:32:13 +00:00
|
|
|
|
2021-03-11 14:32:58 +00:00
|
|
|
require.Len(t, mc.PayloadContent, 1)
|
|
|
|
assert.Equal(t, "example.org DoT", mc.PayloadContent[0].Name)
|
|
|
|
assert.Equal(t, "example.org DoT", mc.PayloadContent[0].PayloadDisplayName)
|
|
|
|
assert.Equal(t, "cli42.example.org", mc.PayloadContent[0].DNSSettings.ServerName)
|
2021-01-27 15:32:13 +00:00
|
|
|
})
|
2020-10-29 16:39:11 +00:00
|
|
|
}
|