2020-10-29 09:39:11 -07:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"howett.net/plist"
|
|
|
|
)
|
|
|
|
|
2020-11-25 08:09:41 -07: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)
|
|
|
|
assert.Nil(t, err)
|
2020-10-29 09:39:11 -07:00
|
|
|
|
2020-11-25 08:09:41 -07:00
|
|
|
w := httptest.NewRecorder()
|
2020-10-29 09:39:11 -07:00
|
|
|
|
2020-11-25 08:09:41 -07:00
|
|
|
handleMobileConfigDOH(w, r)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2020-10-29 09:39:11 -07:00
|
|
|
|
2020-12-07 06:04:53 -07:00
|
|
|
var mc mobileConfig
|
2020-11-25 08:09:41 -07:00
|
|
|
_, err = plist.Unmarshal(w.Body.Bytes(), &mc)
|
|
|
|
assert.Nil(t, err)
|
2020-10-29 09:39:11 -07:00
|
|
|
|
2021-01-13 06:56:05 -07:00
|
|
|
if assert.Len(t, mc.PayloadContent, 1) {
|
2020-11-25 08:09:41 -07:00
|
|
|
assert.Equal(t, "example.org DoH", mc.PayloadContent[0].Name)
|
|
|
|
assert.Equal(t, "example.org DoH", mc.PayloadContent[0].PayloadDisplayName)
|
|
|
|
assert.Equal(t, "example.org", mc.PayloadContent[0].DNSSettings.ServerName)
|
|
|
|
assert.Equal(t, "https://example.org/dns-query", mc.PayloadContent[0].DNSSettings.ServerURL)
|
|
|
|
}
|
|
|
|
})
|
2020-10-29 09:39:11 -07:00
|
|
|
|
2020-11-25 08:09:41 -07:00
|
|
|
t.Run("success_no_host", func(t *testing.T) {
|
|
|
|
oldTLSConf := Context.tls
|
|
|
|
t.Cleanup(func() { Context.tls = oldTLSConf })
|
|
|
|
|
|
|
|
Context.tls = &TLSMod{
|
|
|
|
conf: tlsConfigSettings{ServerName: "example.org"},
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "https://example.com:12345/apple/doh.mobileconfig", nil)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handleMobileConfigDOH(w, r)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
|
2020-12-07 06:04:53 -07:00
|
|
|
var mc mobileConfig
|
2020-11-25 08:09:41 -07:00
|
|
|
_, err = plist.Unmarshal(w.Body.Bytes(), &mc)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-01-13 06:56:05 -07:00
|
|
|
if assert.Len(t, mc.PayloadContent, 1) {
|
2020-11-25 08:09:41 -07:00
|
|
|
assert.Equal(t, "example.org DoH", mc.PayloadContent[0].Name)
|
|
|
|
assert.Equal(t, "example.org DoH", mc.PayloadContent[0].PayloadDisplayName)
|
|
|
|
assert.Equal(t, "example.org", mc.PayloadContent[0].DNSSettings.ServerName)
|
|
|
|
assert.Equal(t, "https://example.org/dns-query", mc.PayloadContent[0].DNSSettings.ServerURL)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handleMobileConfigDOH(w, r)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handleMobileConfigDOT(w, r)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
|
2020-12-07 06:04:53 -07:00
|
|
|
var mc mobileConfig
|
2020-11-25 08:09:41 -07:00
|
|
|
_, err = plist.Unmarshal(w.Body.Bytes(), &mc)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-01-13 06:56:05 -07:00
|
|
|
if assert.Len(t, mc.PayloadContent, 1) {
|
2020-11-25 08:09:41 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success_no_host", func(t *testing.T) {
|
|
|
|
oldTLSConf := Context.tls
|
|
|
|
t.Cleanup(func() { Context.tls = oldTLSConf })
|
|
|
|
|
|
|
|
Context.tls = &TLSMod{
|
|
|
|
conf: tlsConfigSettings{ServerName: "example.org"},
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "https://example.com:12345/apple/dot.mobileconfig", nil)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handleMobileConfigDOT(w, r)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
|
2020-12-07 06:04:53 -07:00
|
|
|
var mc mobileConfig
|
2020-11-25 08:09:41 -07:00
|
|
|
_, err = plist.Unmarshal(w.Body.Bytes(), &mc)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-01-13 06:56:05 -07:00
|
|
|
if assert.Len(t, mc.PayloadContent, 1) {
|
2020-11-25 08:09:41 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handleMobileConfigDOT(w, r)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
|
|
|
})
|
2020-10-29 09:39:11 -07:00
|
|
|
}
|