| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | package dnssec
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/cache"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestCacheSet(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2016-10-02 15:58:01 +01:00
										 |  |  | 	fPriv, rmPriv, _ := test.TempFile(".", privKey)
 | 
					
						
							|  |  |  | 	fPub, rmPub, _ := test.TempFile(".", pubKey)
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	defer rmPriv()
 | 
					
						
							|  |  |  | 	defer rmPub()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dnskey, err := ParseKeyFile(fPub, fPriv)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("failed to parse key: %v\n", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 	c := cache.New(defaultCap)
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	m := testMsg()
 | 
					
						
							| 
									
										
										
										
											2018-01-03 11:11:56 +00:00
										 |  |  | 	state := request.Request{Req: m, Zone: "miek.nl."}
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 	k := hash(m.Answer) // calculate *before* we add the sig
 | 
					
						
							|  |  |  | 	d := New([]string{"miek.nl."}, []*DNSKEY{dnskey}, nil, c)
 | 
					
						
							| 
									
										
										
										
											2018-01-03 11:11:56 +00:00
										 |  |  | 	d.Sign(state, time.Now().UTC())
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_, ok := d.get(k)
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		t.Errorf("signature was not added to the cache")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:39:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestCacheNotValidExpired(t *testing.T) {
 | 
					
						
							|  |  |  | 	fPriv, rmPriv, _ := test.TempFile(".", privKey)
 | 
					
						
							|  |  |  | 	fPub, rmPub, _ := test.TempFile(".", pubKey)
 | 
					
						
							|  |  |  | 	defer rmPriv()
 | 
					
						
							|  |  |  | 	defer rmPub()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dnskey, err := ParseKeyFile(fPub, fPriv)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("failed to parse key: %v\n", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c := cache.New(defaultCap)
 | 
					
						
							|  |  |  | 	m := testMsg()
 | 
					
						
							|  |  |  | 	state := request.Request{Req: m, Zone: "miek.nl."}
 | 
					
						
							|  |  |  | 	k := hash(m.Answer) // calculate *before* we add the sig
 | 
					
						
							|  |  |  | 	d := New([]string{"miek.nl."}, []*DNSKEY{dnskey}, nil, c)
 | 
					
						
							|  |  |  | 	d.Sign(state, time.Now().UTC().AddDate(0, 0, -9))
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_, ok := d.get(k)
 | 
					
						
							|  |  |  | 	if ok {
 | 
					
						
							|  |  |  | 		t.Errorf("signature was added to the cache even though not valid")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestCacheNotValidYet(t *testing.T) {
 | 
					
						
							|  |  |  | 	fPriv, rmPriv, _ := test.TempFile(".", privKey)
 | 
					
						
							|  |  |  | 	fPub, rmPub, _ := test.TempFile(".", pubKey)
 | 
					
						
							|  |  |  | 	defer rmPriv()
 | 
					
						
							|  |  |  | 	defer rmPub()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dnskey, err := ParseKeyFile(fPub, fPriv)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("failed to parse key: %v\n", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c := cache.New(defaultCap)
 | 
					
						
							|  |  |  | 	m := testMsg()
 | 
					
						
							|  |  |  | 	state := request.Request{Req: m, Zone: "miek.nl."}
 | 
					
						
							|  |  |  | 	k := hash(m.Answer) // calculate *before* we add the sig
 | 
					
						
							|  |  |  | 	d := New([]string{"miek.nl."}, []*DNSKEY{dnskey}, nil, c)
 | 
					
						
							|  |  |  | 	d.Sign(state, time.Now().UTC().AddDate(0, 0, +9))
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_, ok := d.get(k)
 | 
					
						
							|  |  |  | 	if ok {
 | 
					
						
							|  |  |  | 		t.Errorf("signature was added to the cache even though not valid yet")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |