| 
									
										
										
										
											2018-01-08 11:52:25 +01:00
										 |  |  | package template
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 09:04:47 +08:00
										 |  |  | 	"github.com/caddyserver/caddy"
 | 
					
						
							| 
									
										
										
										
											2018-01-08 11:52:25 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestSetup(t *testing.T) {
 | 
					
						
							|  |  |  | 	c := caddy.NewTestController("dns", `template ANY ANY {
 | 
					
						
							|  |  |  | 		rcode
 | 
					
						
							|  |  |  | 	}`)
 | 
					
						
							|  |  |  | 	err := setupTemplate(c)
 | 
					
						
							|  |  |  | 	if err == nil {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected setupTemplate to fail on broken template, got no error")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	c = caddy.NewTestController("dns", `template ANY ANY {
 | 
					
						
							|  |  |  | 		rcode NXDOMAIN
 | 
					
						
							|  |  |  | 	}`)
 | 
					
						
							|  |  |  | 	err = setupTemplate(c)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected no errors, got: %v", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestSetupParse(t *testing.T) {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	serverBlockKeys := []string{"domain.com.:8053", "dynamic.domain.com.:8053"}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct {
 | 
					
						
							|  |  |  | 		inputFileRules string
 | 
					
						
							|  |  |  | 		shouldErr      bool
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		// parse errors
 | 
					
						
							|  |  |  | 		{`template`, true},
 | 
					
						
							|  |  |  | 		{`template X`, true},
 | 
					
						
							|  |  |  | 		{`template ANY`, true},
 | 
					
						
							|  |  |  | 		{`template ANY X`, true},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`template ANY ANY .* {
 | 
					
						
							|  |  |  | 				notavailable
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			true,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`template ANY ANY {
 | 
					
						
							|  |  |  | 				answer
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			true,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`template ANY ANY {
 | 
					
						
							|  |  |  | 				additional
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			true,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`template ANY ANY {
 | 
					
						
							|  |  |  | 				rcode
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			true,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`template ANY ANY {
 | 
					
						
							|  |  |  | 				rcode UNDEFINED
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			true,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`template ANY ANY {
 | 
					
						
							|  |  |  | 				answer	"{{"
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			true,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`template ANY ANY {
 | 
					
						
							|  |  |  | 				additional "{{"
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			true,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`template ANY ANY {
 | 
					
						
							|  |  |  | 				authority "{{"
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			true,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		// examples
 | 
					
						
							| 
									
										
										
										
											2019-06-01 03:40:12 -04:00
										 |  |  | 		{`template ANY ANY (?P<x>`, false},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`template ANY ANY {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			false,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2018-01-08 11:52:25 +01:00
										 |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2018-01-09 22:30:58 +01:00
										 |  |  | 			`template ANY A example.com {
 | 
					
						
							|  |  |  | 				match ip-(?P<a>[0-9]*)-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]com
 | 
					
						
							| 
									
										
										
										
											2018-01-08 11:52:25 +01:00
										 |  |  | 				answer "{{ .Name }} A {{ .Group.a }}.{{ .Group.b }}.{{ .Group.c }}.{{ .Grup.d }}."
 | 
					
						
							| 
									
										
										
										
											2018-01-09 22:30:58 +01:00
										 |  |  | 				fallthrough
 | 
					
						
							| 
									
										
										
										
											2018-01-08 11:52:25 +01:00
										 |  |  | 			}`,
 | 
					
						
							|  |  |  | 			false,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2018-05-21 08:45:45 +02:00
										 |  |  | 		{
 | 
					
						
							|  |  |  | 			`template ANY AAAA example.com {
 | 
					
						
							|  |  |  | 				match ip-(?P<a>[0-9]*)-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]com
 | 
					
						
							|  |  |  | 				authority "example.com 60 IN SOA ns.example.com hostmaster.example.com (1 60 60 60 60)"
 | 
					
						
							|  |  |  | 				fallthrough
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			false,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2018-01-08 11:52:25 +01:00
										 |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2018-01-09 22:30:58 +01:00
										 |  |  | 			`template IN ANY example.com {
 | 
					
						
							|  |  |  | 				match "[.](example[.]com[.]dc1[.]example[.]com[.])$"
 | 
					
						
							| 
									
										
										
										
											2018-01-08 11:52:25 +01:00
										 |  |  | 				rcode NXDOMAIN
 | 
					
						
							| 
									
										
										
										
											2018-05-21 08:45:45 +02:00
										 |  |  | 				authority "{{ index .Match 1 }} 60 IN SOA ns.{{ index .Match 1 }} hostmaster.example.com (1 60 60 60 60)"
 | 
					
						
							| 
									
										
										
										
											2018-01-09 22:30:58 +01:00
										 |  |  | 				fallthrough example.com
 | 
					
						
							| 
									
										
										
										
											2018-01-08 11:52:25 +01:00
										 |  |  | 			}`,
 | 
					
						
							|  |  |  | 			false,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2018-01-09 22:30:58 +01:00
										 |  |  | 			`template IN A example {
 | 
					
						
							|  |  |  | 				match ^ip-10-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]$
 | 
					
						
							| 
									
										
										
										
											2018-01-08 11:52:25 +01:00
										 |  |  | 				answer "{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-01-09 22:30:58 +01:00
										 |  |  | 			template IN MX example. {
 | 
					
						
							|  |  |  | 				match ^ip-10-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]$
 | 
					
						
							| 
									
										
										
										
											2018-01-08 11:52:25 +01:00
										 |  |  | 				answer "{{ .Name }} 60 IN MX 10 {{ .Name }}"
 | 
					
						
							|  |  |  | 				additional "{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			false,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2018-01-09 22:30:58 +01:00
										 |  |  | 			`template IN MX example {
 | 
					
						
							|  |  |  | 					match ^ip-10-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]$
 | 
					
						
							| 
									
										
										
										
											2018-01-08 11:52:25 +01:00
										 |  |  | 					answer "{{ .Name }} 60 IN MX 10 {{ .Name }}"
 | 
					
						
							|  |  |  | 					additional "{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"
 | 
					
						
							|  |  |  | 					authority  "example. 60 IN NS ns0.example."
 | 
					
						
							|  |  |  | 					authority  "example. 60 IN NS ns1.example."
 | 
					
						
							|  |  |  | 					additional "ns0.example. 60 IN A 203.0.113.8"
 | 
					
						
							|  |  |  | 					additional "ns1.example. 60 IN A 198.51.100.8"
 | 
					
						
							|  |  |  | 				}`,
 | 
					
						
							|  |  |  | 			false,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for i, test := range tests {
 | 
					
						
							|  |  |  | 		c := caddy.NewTestController("dns", test.inputFileRules)
 | 
					
						
							|  |  |  | 		c.ServerBlockKeys = serverBlockKeys
 | 
					
						
							|  |  |  | 		templates, err := templateParse(c)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err == nil && test.shouldErr {
 | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected errors, but got no error\n---\n%s\n---\n%v", i, test.inputFileRules, templates)
 | 
					
						
							|  |  |  | 		} else if err != nil && !test.shouldErr {
 | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |