MCPcopy
hub / github.com/AdguardTeam/dnsproxy / TestCache_getWithSubnet_mask

Function TestCache_getWithSubnet_mask

proxy/cache_internal_test.go:734–791  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

732}
733
734func TestCache_getWithSubnet_mask(t *testing.T) {
735 const testFQDN = "example.com."
736
737 testIP := net.IP{176, 112, 191, 0}
738 noMatchIP := net.IP{177, 112, 191, 0}
739
740 // cachedIP/cidrMask network contains the testIP.
741 const cidrMaskOnes = 20
742 cidrMask := net.CIDRMask(cidrMaskOnes, netutil.IPv4BitLen)
743 cachedIP := net.IP{176, 112, 176, 0}
744
745 ansIP := net.IP{4, 4, 4, 4}
746
747 c := newTestCache(t, &cacheConfig{
748 withECS: true,
749 optimistic: true,
750 })
751
752 req := (&dns.Msg{}).SetQuestion(testFQDN, dns.TypeA)
753 resp := (&dns.Msg{
754 Answer: []dns.RR{newRR(t, testFQDN, dns.TypeA, 300, ansIP)},
755 }).SetReply(req)
756
757 // Cache IP network that contains the testIP.
758 c.setWithSubnet(
759 req,
760 resp,
761 upstreamWithAddr,
762 &net.IPNet{IP: cachedIP, Mask: cidrMask},
763 testLogger,
764 )
765
766 t.Run("mask_matched", func(t *testing.T) {
767 ci, expired, key := c.getWithSubnet(req, &net.IPNet{
768 IP: testIP,
769 Mask: net.CIDRMask(24, netutil.IPv4BitLen),
770 })
771 assert.False(t, expired)
772 assert.Equal(t, msgToKeyWithSubnet(req, testIP.Mask(cidrMask), cidrMaskOnes), key)
773
774 require.NotNil(t, ci)
775 require.NotNil(t, ci.m)
776 require.NotEmpty(t, ci.m.Answer)
777
778 a := testutil.RequireTypeAssert[*dns.A](t, ci.m.Answer[0])
779 assert.True(t, a.A.Equal(ansIP))
780 })
781
782 t.Run("no_mask_matched", func(t *testing.T) {
783 ci, expired, key := c.getWithSubnet(req, &net.IPNet{
784 IP: noMatchIP,
785 Mask: net.CIDRMask(24, netutil.IPv4BitLen),
786 })
787 assert.False(t, expired)
788 assert.Equal(t, msgToKeyWithSubnet(req, noMatchIP, 0), key)
789 assert.Nil(t, ci)
790 })
791}

Callers

nothing calls this directly

Calls 5

newTestCacheFunction · 0.85
newRRFunction · 0.85
msgToKeyWithSubnetFunction · 0.85
setWithSubnetMethod · 0.80
getWithSubnetMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…