|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/foxcpp/go-mockdns" |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | + "github.com/wallarm/api-firewall/internal/config" |
| 12 | + "github.com/wallarm/api-firewall/internal/platform/proxy" |
| 13 | +) |
| 14 | + |
| 15 | +func TestWithoutRCCDNSCacheBasic(t *testing.T) { |
| 16 | + |
| 17 | + logger := logrus.New() |
| 18 | + logger.SetLevel(logrus.ErrorLevel) |
| 19 | + |
| 20 | + var cfg = config.ProxyMode{ |
| 21 | + RequestValidation: "BLOCK", |
| 22 | + ResponseValidation: "BLOCK", |
| 23 | + CustomBlockStatusCode: 403, |
| 24 | + AddValidationStatusHeader: false, |
| 25 | + ShadowAPI: config.ShadowAPI{ |
| 26 | + ExcludeList: []int{404, 401}, |
| 27 | + }, |
| 28 | + DNS: config.DNS{ |
| 29 | + Cache: true, |
| 30 | + FetchTimeout: 1000 * time.Millisecond, |
| 31 | + LookupTimeout: 400 * time.Millisecond, |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + srv, _ := mockdns.NewServer(map[string]mockdns.Zone{ |
| 36 | + "example.org.": { |
| 37 | + A: []string{"1.2.3.4", "5.6.7.8"}, |
| 38 | + }, |
| 39 | + }, false) |
| 40 | + defer srv.Close() |
| 41 | + |
| 42 | + srUpdatedOrder, _ := mockdns.NewServer(map[string]mockdns.Zone{ |
| 43 | + "example.org.": { |
| 44 | + A: []string{"5.6.7.8", "1.2.3.4"}, |
| 45 | + }, |
| 46 | + }, false) |
| 47 | + defer srUpdatedOrder.Close() |
| 48 | + |
| 49 | + r := &net.Resolver{} |
| 50 | + srv.PatchNet(r) |
| 51 | + |
| 52 | + dnsCache, err := proxy.NewDNSResolver(cfg.DNS.FetchTimeout, cfg.DNS.LookupTimeout, r, logger) |
| 53 | + if err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } |
| 56 | + defer dnsCache.Stop() |
| 57 | + |
| 58 | + addr, err := dnsCache.Fetch(context.Background(), "example.org") |
| 59 | + if err != nil { |
| 60 | + t.Error(err) |
| 61 | + } |
| 62 | + |
| 63 | + if addr[0].String() != "1.2.3.4" { |
| 64 | + t.Errorf("Incorrect response from local DNS server. Expected: 1.2.3.4 and got %s", |
| 65 | + addr[0].String()) |
| 66 | + } |
| 67 | + |
| 68 | + srUpdatedOrder.PatchNet(r) |
| 69 | + |
| 70 | + time.Sleep(600 * time.Millisecond) |
| 71 | + |
| 72 | + addr, err = dnsCache.Fetch(context.Background(), "example.org") |
| 73 | + if err != nil { |
| 74 | + t.Error(err) |
| 75 | + } |
| 76 | + |
| 77 | + if addr[0].String() != "1.2.3.4" { |
| 78 | + t.Errorf("Incorrect response from local DNS server. Expected: 1.2.3.4 and got %s", |
| 79 | + addr[0].String()) |
| 80 | + } |
| 81 | + |
| 82 | + time.Sleep(800 * time.Millisecond) |
| 83 | + |
| 84 | + addr, err = dnsCache.Fetch(context.Background(), "example.org") |
| 85 | + if err != nil { |
| 86 | + t.Error(err) |
| 87 | + } |
| 88 | + |
| 89 | + if addr[0].String() != "5.6.7.8" { |
| 90 | + t.Errorf("Incorrect response from local DNS server. Expected: 5.6.7.8 and got %s", |
| 91 | + addr[0].String()) |
| 92 | + } |
| 93 | +} |
0 commit comments