| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Jan Schär | 75ea9f4 | 2024-07-29 17:01:41 +0200 | [diff] [blame] | 4 | package proxy |
| 5 | |
| 6 | // Taken and modified from CoreDNS, under Apache 2.0. |
| 7 | |
| 8 | import ( |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/miekg/dns" |
| 13 | |
| 14 | "source.monogon.dev/osbase/net/dns/test" |
| 15 | ) |
| 16 | |
| 17 | func TestCached(t *testing.T) { |
| 18 | s := test.NewServer(func(w dns.ResponseWriter, r *dns.Msg) { |
| 19 | ret := new(dns.Msg) |
| 20 | ret.SetReply(r) |
| 21 | w.WriteMsg(ret) |
| 22 | }) |
| 23 | defer s.Close() |
| 24 | |
| 25 | tr := newTransport(s.Addr) |
| 26 | tr.Start() |
| 27 | defer tr.Stop() |
| 28 | |
| 29 | c1, cache1, _ := tr.Dial("udp") |
| 30 | c2, cache2, _ := tr.Dial("udp") |
| 31 | |
| 32 | if cache1 || cache2 { |
| 33 | t.Errorf("Expected non-cached connection") |
| 34 | } |
| 35 | |
| 36 | tr.Yield(c1) |
| 37 | tr.Yield(c2) |
| 38 | c3, cached3, _ := tr.Dial("udp") |
| 39 | if !cached3 { |
| 40 | t.Error("Expected cached connection (c3)") |
| 41 | } |
| 42 | if c2 != c3 { |
| 43 | t.Error("Expected c2 == c3") |
| 44 | } |
| 45 | |
| 46 | tr.Yield(c3) |
| 47 | |
| 48 | // dial another protocol |
| 49 | c4, cached4, _ := tr.Dial("tcp") |
| 50 | if cached4 { |
| 51 | t.Errorf("Expected non-cached connection (c4)") |
| 52 | } |
| 53 | tr.Yield(c4) |
| 54 | } |
| 55 | |
| 56 | func TestCleanupByTimer(t *testing.T) { |
| 57 | s := test.NewServer(func(w dns.ResponseWriter, r *dns.Msg) { |
| 58 | ret := new(dns.Msg) |
| 59 | ret.SetReply(r) |
| 60 | w.WriteMsg(ret) |
| 61 | }) |
| 62 | defer s.Close() |
| 63 | |
| 64 | tr := newTransport(s.Addr) |
| 65 | tr.SetExpire(10 * time.Millisecond) |
| 66 | tr.Start() |
| 67 | defer tr.Stop() |
| 68 | |
| 69 | c1, _, _ := tr.Dial("udp") |
| 70 | c2, _, _ := tr.Dial("udp") |
| 71 | tr.Yield(c1) |
| 72 | time.Sleep(2 * time.Millisecond) |
| 73 | tr.Yield(c2) |
| 74 | |
| 75 | time.Sleep(15 * time.Millisecond) |
| 76 | c3, cached, _ := tr.Dial("udp") |
| 77 | if cached { |
| 78 | t.Error("Expected non-cached connection (c3)") |
| 79 | } |
| 80 | tr.Yield(c3) |
| 81 | |
| 82 | time.Sleep(15 * time.Millisecond) |
| 83 | c4, cached, _ := tr.Dial("udp") |
| 84 | if cached { |
| 85 | t.Error("Expected non-cached connection (c4)") |
| 86 | } |
| 87 | tr.Yield(c4) |
| 88 | } |
| 89 | |
| 90 | func TestCleanupAll(t *testing.T) { |
| 91 | s := test.NewServer(func(w dns.ResponseWriter, r *dns.Msg) { |
| 92 | ret := new(dns.Msg) |
| 93 | ret.SetReply(r) |
| 94 | w.WriteMsg(ret) |
| 95 | }) |
| 96 | defer s.Close() |
| 97 | |
| 98 | tr := newTransport(s.Addr) |
| 99 | |
| 100 | c1, _ := dns.DialTimeout("udp", tr.addr, maxDialTimeout) |
| 101 | c2, _ := dns.DialTimeout("udp", tr.addr, maxDialTimeout) |
| 102 | c3, _ := dns.DialTimeout("udp", tr.addr, maxDialTimeout) |
| 103 | |
| 104 | tr.conns[typeUDP] = []*persistConn{{c1, time.Now()}, {c2, time.Now()}, {c3, time.Now()}} |
| 105 | |
| 106 | if len(tr.conns[typeUDP]) != 3 { |
| 107 | t.Error("Expected 3 connections") |
| 108 | } |
| 109 | tr.cleanup(true) |
| 110 | |
| 111 | if len(tr.conns[typeUDP]) > 0 { |
| 112 | t.Error("Expected no cached connections") |
| 113 | } |
| 114 | } |