"Fossies" - the Fresh Open Source Software Archive

Member "AdGuardHome-0.104.3/internal/dnsforward/dnsforward_http_test.go" (19 Nov 2020, 9832 Bytes) of package /linux/misc/dns/AdGuardHome-0.104.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package dnsforward
    2 
    3 import (
    4     "io/ioutil"
    5     "net/http"
    6     "net/http/httptest"
    7     "strings"
    8     "testing"
    9 
   10     "github.com/stretchr/testify/assert"
   11 )
   12 
   13 func TestDNSForwardHTTTP_handleGetConfig(t *testing.T) {
   14     s := createTestServer(t)
   15     err := s.Start()
   16     assert.Nil(t, err)
   17     defer assert.Nil(t, s.Stop())
   18 
   19     defaultConf := s.conf
   20 
   21     w := httptest.NewRecorder()
   22 
   23     testCases := []struct {
   24         name string
   25         conf func() ServerConfig
   26         want string
   27     }{{
   28         name: "all_right",
   29         conf: func() ServerConfig {
   30             return defaultConf
   31         },
   32         want: "{\"upstream_dns\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\",\"149.112.112.10\",\"2620:fe::10\",\"2620:fe::fe:10\"],\"protection_enabled\":true,\"ratelimit\":0,\"blocking_mode\":\"\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":false,\"dnssec_enabled\":false,\"disable_ipv6\":false,\"upstream_mode\":\"\",\"cache_size\":0,\"cache_ttl_min\":0,\"cache_ttl_max\":0}",
   33     }, {
   34         name: "fastest_addr",
   35         conf: func() ServerConfig {
   36             conf := defaultConf
   37             conf.FastestAddr = true
   38             return conf
   39         },
   40         want: "{\"upstream_dns\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\",\"149.112.112.10\",\"2620:fe::10\",\"2620:fe::fe:10\"],\"protection_enabled\":true,\"ratelimit\":0,\"blocking_mode\":\"\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":false,\"dnssec_enabled\":false,\"disable_ipv6\":false,\"upstream_mode\":\"fastest_addr\",\"cache_size\":0,\"cache_ttl_min\":0,\"cache_ttl_max\":0}",
   41     }, {
   42         name: "parallel",
   43         conf: func() ServerConfig {
   44             conf := defaultConf
   45             conf.AllServers = true
   46             return conf
   47         },
   48         want: "{\"upstream_dns\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\",\"149.112.112.10\",\"2620:fe::10\",\"2620:fe::fe:10\"],\"protection_enabled\":true,\"ratelimit\":0,\"blocking_mode\":\"\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":false,\"dnssec_enabled\":false,\"disable_ipv6\":false,\"upstream_mode\":\"parallel\",\"cache_size\":0,\"cache_ttl_min\":0,\"cache_ttl_max\":0}",
   49     }}
   50 
   51     for _, tc := range testCases {
   52         t.Run(tc.name, func(t *testing.T) {
   53             s.conf = tc.conf()
   54             s.handleGetConfig(w, nil)
   55             assert.Equal(t, tc.want, w.Body.String())
   56 
   57             assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
   58         })
   59         w.Body.Reset()
   60     }
   61 }
   62 
   63 func TestDNSForwardHTTTP_handleSetConfig(t *testing.T) {
   64     s := createTestServer(t)
   65 
   66     defaultConf := s.conf
   67 
   68     err := s.Start()
   69     assert.Nil(t, err)
   70     defer func() {
   71         assert.Nil(t, s.Stop())
   72     }()
   73 
   74     w := httptest.NewRecorder()
   75 
   76     const defaultConfJSON = "{\"upstream_dns\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\",\"149.112.112.10\",\"2620:fe::10\",\"2620:fe::fe:10\"],\"protection_enabled\":true,\"ratelimit\":0,\"blocking_mode\":\"\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":false,\"dnssec_enabled\":false,\"disable_ipv6\":false,\"upstream_mode\":\"\",\"cache_size\":0,\"cache_ttl_min\":0,\"cache_ttl_max\":0}"
   77     testCases := []struct {
   78         name    string
   79         req     string
   80         wantSet string
   81         wantGet string
   82     }{{
   83         name:    "upstream_dns",
   84         req:     "{\"upstream_dns\":[\"8.8.8.8:77\",\"8.8.4.4:77\"]}",
   85         wantSet: "",
   86         wantGet: "{\"upstream_dns\":[\"8.8.8.8:77\",\"8.8.4.4:77\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\",\"149.112.112.10\",\"2620:fe::10\",\"2620:fe::fe:10\"],\"protection_enabled\":true,\"ratelimit\":0,\"blocking_mode\":\"\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":false,\"dnssec_enabled\":false,\"disable_ipv6\":false,\"upstream_mode\":\"\",\"cache_size\":0,\"cache_ttl_min\":0,\"cache_ttl_max\":0}",
   87     }, {
   88         name:    "bootstraps",
   89         req:     "{\"bootstrap_dns\":[\"9.9.9.10\"]}",
   90         wantSet: "",
   91         wantGet: "{\"upstream_dns\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\"],\"protection_enabled\":true,\"ratelimit\":0,\"blocking_mode\":\"\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":false,\"dnssec_enabled\":false,\"disable_ipv6\":false,\"upstream_mode\":\"\",\"cache_size\":0,\"cache_ttl_min\":0,\"cache_ttl_max\":0}",
   92     }, {
   93         name:    "blocking_mode_good",
   94         req:     "{\"blocking_mode\":\"refused\"}",
   95         wantSet: "",
   96         wantGet: "{\"upstream_dns\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\",\"149.112.112.10\",\"2620:fe::10\",\"2620:fe::fe:10\"],\"protection_enabled\":true,\"ratelimit\":0,\"blocking_mode\":\"refused\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":false,\"dnssec_enabled\":false,\"disable_ipv6\":false,\"upstream_mode\":\"\",\"cache_size\":0,\"cache_ttl_min\":0,\"cache_ttl_max\":0}",
   97     }, {
   98         name:    "blocking_mode_bad",
   99         req:     "{\"blocking_mode\":\"custom_ip\"}",
  100         wantSet: "blocking_mode: incorrect value\n",
  101         wantGet: "{\"upstream_dns\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\",\"149.112.112.10\",\"2620:fe::10\",\"2620:fe::fe:10\"],\"protection_enabled\":true,\"ratelimit\":0,\"blocking_mode\":\"\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":false,\"dnssec_enabled\":false,\"disable_ipv6\":false,\"upstream_mode\":\"\",\"cache_size\":0,\"cache_ttl_min\":0,\"cache_ttl_max\":0}",
  102     }, {
  103         name:    "ratelimit",
  104         req:     "{\"ratelimit\":6}",
  105         wantSet: "",
  106         wantGet: "{\"upstream_dns\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\",\"149.112.112.10\",\"2620:fe::10\",\"2620:fe::fe:10\"],\"protection_enabled\":true,\"ratelimit\":6,\"blocking_mode\":\"\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":false,\"dnssec_enabled\":false,\"disable_ipv6\":false,\"upstream_mode\":\"\",\"cache_size\":0,\"cache_ttl_min\":0,\"cache_ttl_max\":0}",
  107     }, {
  108         name:    "edns_cs_enabled",
  109         req:     "{\"edns_cs_enabled\":true}",
  110         wantSet: "",
  111         wantGet: "{\"upstream_dns\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\",\"149.112.112.10\",\"2620:fe::10\",\"2620:fe::fe:10\"],\"protection_enabled\":true,\"ratelimit\":0,\"blocking_mode\":\"\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":true,\"dnssec_enabled\":false,\"disable_ipv6\":false,\"upstream_mode\":\"\",\"cache_size\":0,\"cache_ttl_min\":0,\"cache_ttl_max\":0}",
  112     }, {
  113         name:    "dnssec_enabled",
  114         req:     "{\"dnssec_enabled\":true}",
  115         wantSet: "",
  116         wantGet: "{\"upstream_dns\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\",\"149.112.112.10\",\"2620:fe::10\",\"2620:fe::fe:10\"],\"protection_enabled\":true,\"ratelimit\":0,\"blocking_mode\":\"\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":false,\"dnssec_enabled\":true,\"disable_ipv6\":false,\"upstream_mode\":\"\",\"cache_size\":0,\"cache_ttl_min\":0,\"cache_ttl_max\":0}",
  117     }, {
  118         name:    "cache_size",
  119         req:     "{\"cache_size\":1024}",
  120         wantSet: "",
  121         wantGet: "{\"upstream_dns\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\",\"149.112.112.10\",\"2620:fe::10\",\"2620:fe::fe:10\"],\"protection_enabled\":true,\"ratelimit\":0,\"blocking_mode\":\"\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":false,\"dnssec_enabled\":false,\"disable_ipv6\":false,\"upstream_mode\":\"\",\"cache_size\":1024,\"cache_ttl_min\":0,\"cache_ttl_max\":0}",
  122     }, {
  123         name:    "upstream_mode_parallel",
  124         req:     "{\"upstream_mode\":\"parallel\"}",
  125         wantSet: "",
  126         wantGet: "{\"upstream_dns\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\",\"149.112.112.10\",\"2620:fe::10\",\"2620:fe::fe:10\"],\"protection_enabled\":true,\"ratelimit\":0,\"blocking_mode\":\"\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":false,\"dnssec_enabled\":false,\"disable_ipv6\":false,\"upstream_mode\":\"parallel\",\"cache_size\":0,\"cache_ttl_min\":0,\"cache_ttl_max\":0}",
  127     }, {
  128         name:    "upstream_mode_fastest_addr",
  129         req:     "{\"upstream_mode\":\"fastest_addr\"}",
  130         wantSet: "",
  131         wantGet: "{\"upstream_dns\":[\"8.8.8.8:53\",\"8.8.4.4:53\"],\"upstream_dns_file\":\"\",\"bootstrap_dns\":[\"9.9.9.10\",\"149.112.112.10\",\"2620:fe::10\",\"2620:fe::fe:10\"],\"protection_enabled\":true,\"ratelimit\":0,\"blocking_mode\":\"\",\"blocking_ipv4\":\"\",\"blocking_ipv6\":\"\",\"edns_cs_enabled\":false,\"dnssec_enabled\":false,\"disable_ipv6\":false,\"upstream_mode\":\"fastest_addr\",\"cache_size\":0,\"cache_ttl_min\":0,\"cache_ttl_max\":0}",
  132     }, {
  133         name:    "upstream_dns_bad",
  134         req:     "{\"upstream_dns\":[\"\"]}",
  135         wantSet: "wrong upstreams specification: missing port in address\n",
  136         wantGet: defaultConfJSON,
  137     }, {
  138         name:    "bootstraps_bad",
  139         req:     "{\"bootstrap_dns\":[\"a\"]}",
  140         wantSet: "a can not be used as bootstrap dns cause: invalid bootstrap server address: Resolver a is not eligible to be a bootstrap DNS server\n",
  141         wantGet: defaultConfJSON,
  142     }, {
  143         name:    "cache_bad_ttl",
  144         req:     "{\"cache_ttl_min\":1024,\"cache_ttl_max\":512}",
  145         wantSet: "cache_ttl_min must be less or equal than cache_ttl_max\n",
  146         wantGet: defaultConfJSON,
  147     }, {
  148         name:    "upstream_mode_bad",
  149         req:     "{\"upstream_mode\":\"somethingelse\"}",
  150         wantSet: "upstream_mode: incorrect value\n",
  151         wantGet: defaultConfJSON,
  152     }}
  153 
  154     for _, tc := range testCases {
  155         t.Run(tc.name, func(t *testing.T) {
  156             rBody := ioutil.NopCloser(strings.NewReader(tc.req))
  157             r, err := http.NewRequest(http.MethodPost, "http://example.com", rBody)
  158             assert.Nil(t, err)
  159 
  160             s.handleSetConfig(w, r)
  161             assert.Equal(t, tc.wantSet, w.Body.String())
  162             w.Body.Reset()
  163 
  164             s.handleGetConfig(w, nil)
  165             assert.Equal(t, tc.wantGet, w.Body.String())
  166             w.Body.Reset()
  167         })
  168         s.conf = defaultConf
  169     }
  170 }