|
5 | 5 | "net/http" |
6 | 6 | "reflect" |
7 | 7 | "testing" |
| 8 | + "time" |
8 | 9 |
|
9 | 10 | "github.com/jarcoal/httpmock" |
10 | 11 | "github.com/miekg/dns" |
@@ -113,3 +114,44 @@ func TestDNSProxy_getResponse(t *testing.T) { |
113 | 114 | }) |
114 | 115 | } |
115 | 116 | } |
| 117 | + |
| 118 | +func TestDNSProxy_auditCacheTTL(t *testing.T) { |
| 119 | + apiclient := &ApiClient{Client: &http.Client{}, APIURL: agentApiBaseUrl} |
| 120 | + |
| 121 | + httpmock.ActivateNonDefault(apiclient.Client) |
| 122 | + |
| 123 | + httpmock.RegisterResponder("GET", "https://dns.google/resolve?name=domain12345.com.&type=a", |
| 124 | + httpmock.ResponderFromMultipleResponses( |
| 125 | + []*http.Response{ |
| 126 | + httpmock.NewStringResponse(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain12345.com.","type":1}],"Answer":[{"name":"domain12345.com.","type":1,"TTL":30,"data":"68.68.68.68"}]}`), |
| 127 | + httpmock.NewStringResponse(200, `{"Status":0,"TC":false,"RD":true,"RA":true,"AD":false,"CD":false,"Question":[{"name":"domain12345.com.","type":1}],"Answer":[{"name":"domain12345.com.","type":1,"TTL":30,"data":"68.68.68.68"}]}`), |
| 128 | + }, |
| 129 | + t.Log)) |
| 130 | + |
| 131 | + cache := InitCache(EgressPolicyAudit) |
| 132 | + |
| 133 | + proxy := &DNSProxy{ |
| 134 | + Cache: &cache, |
| 135 | + ApiClient: apiclient, |
| 136 | + EgressPolicy: EgressPolicyAudit, |
| 137 | + } |
| 138 | + |
| 139 | + // should call httpmock |
| 140 | + proxy.getResponse(&dns.Msg{Question: []dns.Question{{Name: "domain12345.com.", Qtype: dns.TypeA}}}) |
| 141 | + |
| 142 | + time.Sleep(2 * time.Second) |
| 143 | + |
| 144 | + // should get from cache |
| 145 | + proxy.getResponse(&dns.Msg{Question: []dns.Question{{Name: "domain12345.com.", Qtype: dns.TypeA}}}) |
| 146 | + |
| 147 | + time.Sleep(30 * time.Second) |
| 148 | + |
| 149 | + // should call httpmock |
| 150 | + proxy.getResponse(&dns.Msg{Question: []dns.Question{{Name: "domain12345.com.", Qtype: dns.TypeA}}}) |
| 151 | + |
| 152 | + info := httpmock.GetCallCountInfo() |
| 153 | + count := info["GET https://dns.google/resolve?name=domain12345.com.&type=a"] |
| 154 | + if count != 2 { |
| 155 | + t.Errorf("incorrect call count %d, expected 2, %v", count, info) |
| 156 | + } |
| 157 | +} |
0 commit comments