| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | // Package dnstest allows for easy testing of DNS client against a test server.
 | 
					
						
							|  |  |  | package dnstest
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Recorder is a type of ResponseWriter that captures
 | 
					
						
							|  |  |  | // the rcode code written to it and also the size of the message
 | 
					
						
							|  |  |  | // written in the response. A rcode code does not have
 | 
					
						
							|  |  |  | // to be written, however, in which case 0 must be assumed.
 | 
					
						
							|  |  |  | // It is best to have the constructor initialize this type
 | 
					
						
							|  |  |  | // with that default status code.
 | 
					
						
							|  |  |  | type Recorder struct {
 | 
					
						
							|  |  |  | 	dns.ResponseWriter
 | 
					
						
							|  |  |  | 	Rcode int
 | 
					
						
							| 
									
										
										
										
											2016-11-29 11:02:43 +00:00
										 |  |  | 	Len   int
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	Msg   *dns.Msg
 | 
					
						
							|  |  |  | 	Start time.Time
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 16:13:04 -07:00
										 |  |  | // NewRecorder makes and returns a new Recorder,
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | // which captures the DNS rcode from the ResponseWriter
 | 
					
						
							|  |  |  | // and also the length of the response message written through it.
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | func NewRecorder(w dns.ResponseWriter) *Recorder {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	return &Recorder{
 | 
					
						
							|  |  |  | 		ResponseWriter: w,
 | 
					
						
							|  |  |  | 		Rcode:          0,
 | 
					
						
							|  |  |  | 		Msg:            nil,
 | 
					
						
							|  |  |  | 		Start:          time.Now(),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteMsg records the status code and calls the
 | 
					
						
							|  |  |  | // underlying ResponseWriter's WriteMsg method.
 | 
					
						
							|  |  |  | func (r *Recorder) WriteMsg(res *dns.Msg) error {
 | 
					
						
							|  |  |  | 	r.Rcode = res.Rcode
 | 
					
						
							|  |  |  | 	// We may get called multiple times (axfr for instance).
 | 
					
						
							|  |  |  | 	// Save the last message, but add the sizes.
 | 
					
						
							| 
									
										
										
										
											2016-11-29 11:02:43 +00:00
										 |  |  | 	r.Len += res.Len()
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	r.Msg = res
 | 
					
						
							|  |  |  | 	return r.ResponseWriter.WriteMsg(res)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-29 11:02:43 +00:00
										 |  |  | // Write is a wrapper that records the length of the message that gets written.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (r *Recorder) Write(buf []byte) (int, error) {
 | 
					
						
							|  |  |  | 	n, err := r.ResponseWriter.Write(buf)
 | 
					
						
							|  |  |  | 	if err == nil {
 | 
					
						
							| 
									
										
										
										
											2016-11-29 11:02:43 +00:00
										 |  |  | 		r.Len += n
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	return n, err
 | 
					
						
							|  |  |  | }
 |