"Fossies" - the Fresh Open Source Software Archive

Member "AdGuardHome-0.104.3/internal/home/home_test.go" (19 Nov 2020, 4624 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. See also the latest Fossies "Diffs" side-by-side code changes report for "home_test.go": 0.104.1_vs_0.104.3.

    1 // +build !race
    2 
    3 // TODO(e.burkov): remove this weird buildtag.
    4 
    5 package home
    6 
    7 import (
    8     "context"
    9     "encoding/base64"
   10     "io/ioutil"
   11     "net/http"
   12     "os"
   13     "path/filepath"
   14     "testing"
   15     "time"
   16 
   17     "github.com/AdguardTeam/dnsproxy/proxyutil"
   18     "github.com/AdguardTeam/dnsproxy/upstream"
   19     "github.com/miekg/dns"
   20     "github.com/stretchr/testify/assert"
   21 )
   22 
   23 const yamlConf = `bind_host: 127.0.0.1
   24 bind_port: 3000
   25 users: []
   26 language: en
   27 rlimit_nofile: 0
   28 web_session_ttl: 720
   29 dns:
   30   bind_host: 127.0.0.1
   31   port: 5354
   32   statistics_interval: 90
   33   querylog_enabled: true
   34   querylog_interval: 90
   35   querylog_size_memory: 0
   36   protection_enabled: true
   37   blocking_mode: null_ip
   38   blocked_response_ttl: 0
   39   ratelimit: 100
   40   ratelimit_whitelist: []
   41   refuse_any: false
   42   bootstrap_dns:
   43   - 1.1.1.1:53
   44   all_servers: false
   45   allowed_clients: []
   46   disallowed_clients: []
   47   blocked_hosts: []
   48   parental_block_host: family-block.dns.adguard.com
   49   safebrowsing_block_host: standard-block.dns.adguard.com
   50   cache_size: 0
   51   upstream_dns:
   52   - https://1.1.1.1/dns-query
   53   filtering_enabled: true
   54   filters_update_interval: 168
   55   parental_sensitivity: 13
   56   parental_enabled: true
   57   safesearch_enabled: false
   58   safebrowsing_enabled: false
   59   safebrowsing_cache_size: 1048576
   60   safesearch_cache_size: 1048576
   61   parental_cache_size: 1048576
   62   cache_time: 30
   63   rewrites: []
   64   blocked_services: []
   65 tls:
   66   enabled: false
   67   server_name: www.example.com
   68   force_https: false
   69   port_https: 443
   70   port_dns_over_tls: 853
   71   allow_unencrypted_doh: true
   72   certificate_chain: ""
   73   private_key: ""
   74   certificate_path: ""
   75   private_key_path: ""
   76 filters:
   77 - enabled: true
   78   url: https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt
   79   name: AdGuard Simplified Domain Names filter
   80   id: 1
   81 - enabled: false
   82   url: https://hosts-file.net/ad_servers.txt
   83   name: hpHosts - Ad and Tracking servers only
   84   id: 2
   85 - enabled: false
   86   url: https://adaway.org/hosts.txt
   87   name: adaway
   88   id: 3
   89 user_rules:
   90 - ""
   91 dhcp:
   92   enabled: false
   93   interface_name: ""
   94   gateway_ip: ""
   95   subnet_mask: ""
   96   range_start: ""
   97   range_end: ""
   98   lease_duration: 86400
   99   icmp_timeout_msec: 1000
  100 clients: []
  101 log_file: ""
  102 verbose: false
  103 schema_version: 5
  104 `
  105 
  106 // . Create a configuration file
  107 // . Start AGH instance
  108 // . Check Web server
  109 // . Check DNS server
  110 // . Check DNS server with DOH
  111 // . Wait until the filters are downloaded
  112 // . Stop and cleanup
  113 func TestHome(t *testing.T) {
  114     // Init new context
  115     Context = homeContext{}
  116 
  117     dir := prepareTestDir()
  118     defer func() { _ = os.RemoveAll(dir) }()
  119     fn := filepath.Join(dir, "AdGuardHome.yaml")
  120 
  121     // Prepare the test config
  122     assert.True(t, ioutil.WriteFile(fn, []byte(yamlConf), 0644) == nil)
  123     fn, _ = filepath.Abs(fn)
  124 
  125     config = configuration{} // the global variable is dirty because of the previous tests run
  126     args := options{}
  127     args.configFilename = fn
  128     args.workDir = dir
  129     go run(args)
  130 
  131     var err error
  132     var resp *http.Response
  133     h := http.Client{}
  134     for i := 0; i != 50; i++ {
  135         resp, err = h.Get("http://127.0.0.1:3000/")
  136         if err == nil && resp.StatusCode != 404 {
  137             break
  138         }
  139         time.Sleep(100 * time.Millisecond)
  140     }
  141     assert.Truef(t, err == nil, "%s", err)
  142     assert.Equal(t, http.StatusOK, resp.StatusCode)
  143 
  144     resp, err = h.Get("http://127.0.0.1:3000/control/status")
  145     assert.Truef(t, err == nil, "%s", err)
  146     assert.Equal(t, http.StatusOK, resp.StatusCode)
  147 
  148     // test DNS over UDP
  149     r, err := upstream.NewResolver("127.0.0.1:5354", 3*time.Second)
  150     assert.Nil(t, err)
  151     addrs, err := r.LookupIPAddr(context.TODO(), "static.adguard.com")
  152     assert.Nil(t, err)
  153     haveIP := len(addrs) != 0
  154     assert.True(t, haveIP)
  155 
  156     // test DNS over HTTP without encryption
  157     req := dns.Msg{}
  158     req.Id = dns.Id()
  159     req.RecursionDesired = true
  160     req.Question = []dns.Question{{Name: "static.adguard.com.", Qtype: dns.TypeA, Qclass: dns.ClassINET}}
  161     buf, err := req.Pack()
  162     assert.True(t, err == nil, "%s", err)
  163     requestURL := "http://127.0.0.1:3000/dns-query?dns=" + base64.RawURLEncoding.EncodeToString(buf)
  164     resp, err = http.DefaultClient.Get(requestURL)
  165     assert.True(t, err == nil, "%s", err)
  166     body, err := ioutil.ReadAll(resp.Body)
  167     assert.True(t, err == nil, "%s", err)
  168     assert.True(t, resp.StatusCode == http.StatusOK)
  169     response := dns.Msg{}
  170     err = response.Unpack(body)
  171     assert.True(t, err == nil, "%s", err)
  172     addrs = nil
  173     proxyutil.AppendIPAddrs(&addrs, response.Answer)
  174     haveIP = len(addrs) != 0
  175     assert.True(t, haveIP)
  176 
  177     for i := 1; ; i++ {
  178         st, err := os.Stat(filepath.Join(dir, "data", "filters", "1.txt"))
  179         if err == nil && st.Size() != 0 {
  180             break
  181         }
  182         if i == 5 {
  183             assert.True(t, false)
  184             break
  185         }
  186         time.Sleep(1 * time.Second)
  187     }
  188 
  189     cleanup()
  190     cleanupAlways()
  191 }