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

Function TestCache_getWithSubnet

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

Source from the content-addressed store, hash-verified

651}
652
653func TestCache_getWithSubnet(t *testing.T) {
654 const testFQDN = "example.com."
655
656 ip1234, ip2234, ip3234 := net.IP{1, 2, 3, 4}, net.IP{2, 2, 3, 4}, net.IP{3, 2, 3, 4}
657 req := (&dns.Msg{}).SetQuestion(testFQDN, dns.TypeA)
658 mask16 := net.CIDRMask(16, netutil.IPv4BitLen)
659 mask24 := net.CIDRMask(24, netutil.IPv4BitLen)
660
661 c := newTestCache(t, &cacheConfig{withECS: true})
662
663 t.Run("empty", func(t *testing.T) {
664 ci, expired, _ := c.getWithSubnet(req, &net.IPNet{IP: ip1234, Mask: mask24})
665 assert.Nil(t, ci)
666 assert.False(t, expired)
667 })
668
669 // Add a response with subnet.
670 resp := (&dns.Msg{
671 Answer: []dns.RR{newRR(t, testFQDN, dns.TypeA, 1, net.IP{1, 1, 1, 1})},
672 }).SetReply(req)
673 c.setWithSubnet(req, resp, upstreamWithAddr, &net.IPNet{IP: ip1234, Mask: mask16}, testLogger)
674
675 t.Run("different_ip", func(t *testing.T) {
676 ci, expired, key := c.getWithSubnet(req, &net.IPNet{IP: ip2234, Mask: mask24})
677 assert.False(t, expired)
678 assert.Equal(t, msgToKeyWithSubnet(req, ip2234, 0), key)
679 assert.Nil(t, ci)
680 })
681
682 // Add a response entry with subnet #2.
683 resp = (&dns.Msg{
684 Answer: []dns.RR{newRR(t, testFQDN, dns.TypeA, 1, net.IP{2, 2, 2, 2})},
685 }).SetReply(req)
686 c.setWithSubnet(req, resp, upstreamWithAddr, &net.IPNet{IP: ip2234, Mask: mask16}, testLogger)
687
688 // Add a response entry without subnet.
689 resp = (&dns.Msg{
690 Answer: []dns.RR{newRR(t, testFQDN, dns.TypeA, 1, net.IP{3, 3, 3, 3})},
691 }).SetReply(req)
692 c.setWithSubnet(req, resp, upstreamWithAddr, &net.IPNet{IP: nil, Mask: nil}, testLogger)
693
694 t.Run("with_subnet_1", func(t *testing.T) {
695 ci, expired, key := c.getWithSubnet(req, &net.IPNet{IP: ip1234, Mask: mask24})
696 assert.False(t, expired)
697 assert.Equal(t, msgToKeyWithSubnet(req, ip1234.Mask(mask16), 16), key)
698
699 require.NotNil(t, ci)
700 require.NotNil(t, ci.m)
701 require.NotEmpty(t, ci.m.Answer)
702
703 a := testutil.RequireTypeAssert[*dns.A](t, ci.m.Answer[0])
704 assert.True(t, a.A.Equal(net.IP{1, 1, 1, 1}))
705 })
706
707 t.Run("with_subnet_2", func(t *testing.T) {
708 ci, expired, key := c.getWithSubnet(req, &net.IPNet{IP: ip2234, Mask: mask24})
709 assert.False(t, expired)
710 assert.Equal(t, msgToKeyWithSubnet(req, ip2234.Mask(mask16), 16), key)

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…