; config options
server:
	module-config: "validator iterator"
	qname-minimisation: "no"
	minimal-responses: no
	iter-scrub-promiscuous: no
	serve-expired: yes
	serve-expired-client-timeout: 1
	serve-expired-reply-ttl: 123
	ede: yes
	ede-serve-expired: yes

stub-zone:
	name: "example.com"
	stub-addr: 1.2.3.4
CONFIG_END

SCENARIO_BEGIN Test serve-expired with client-timeout and a SERVFAIL upstream reply
; Scenario overview:
; - query for example.com. IN A
; - check that we get an answer for example.com. IN A with the correct TTL
; - query again right after the TTL expired
; - answer from upstream is servfail
; - (expired cached answer will not be replaced, instead marked as unresolvable for NORR_TTL(5))
; - check that we get the expired cached answer
; - query again (the answer is available on the upstream server now)
; - check that we get the immediate expired answer back instead
; - query again (the answer is available on the upstream server now)
; - check that we *still* get the immediate expired answer back instead, recursion is blocked for NORR_TTL(5)
; - wait for NORR_TTL(5) to expire
; - query again
; - check that we get the freshly cached answer

; ns.example.com.
RANGE_BEGIN 0 20
	ADDRESS 1.2.3.4
	ENTRY_BEGIN
		MATCH opcode qtype qname
		ADJUST copy_id
		REPLY QR NOERROR
		SECTION QUESTION
			example.com. IN NS
		SECTION ANSWER
			example.com. 10 IN NS ns.example.com.
		SECTION ADDITIONAL
			ns.example.com. 10 IN A 1.2.3.4
	ENTRY_END

	ENTRY_BEGIN
		MATCH opcode qtype qname
		ADJUST copy_id
		REPLY QR NOERROR
		SECTION QUESTION
			example.com. IN A
		SECTION ANSWER
			example.com. 10 IN A 5.6.7.8
		SECTION AUTHORITY
			example.com. 10 IN NS ns.example.com.
		SECTION ADDITIONAL
			ns.example.com. 10 IN A 1.2.3.4
	ENTRY_END
RANGE_END

; ns.example.com.
RANGE_BEGIN 30 40
	ADDRESS 1.2.3.4
	; response to A query
	ENTRY_BEGIN
		MATCH opcode qtype qname
		ADJUST copy_id
		REPLY QR AA SERVFAIL
		SECTION QUESTION
			example.com. IN A
	ENTRY_END
RANGE_END

; ns.example.com.
RANGE_BEGIN 50 100
	ADDRESS 1.2.3.4
	; response to A query
	ENTRY_BEGIN
		MATCH opcode qtype qname
		ADJUST copy_id
		REPLY QR NOERROR
		SECTION QUESTION
			example.com. IN A
		SECTION ANSWER
			example.com. 10 IN A 5.6.7.8
		SECTION AUTHORITY
			example.com. 10 IN NS ns.example.com.
		SECTION ADDITIONAL
			ns.example.com. 10 IN A 1.2.3.4
	ENTRY_END
RANGE_END

; Query with RD flag
STEP 1 QUERY
ENTRY_BEGIN
	REPLY RD
	SECTION QUESTION
		example.com. IN A
ENTRY_END

; Check that we got the correct answer (should be cached)
STEP 10 CHECK_ANSWER
ENTRY_BEGIN
	MATCH all ttl
	REPLY QR RD RA NOERROR
	SECTION QUESTION
		example.com. IN A
	SECTION ANSWER
		example.com. 10 IN A 5.6.7.8
	SECTION AUTHORITY
		example.com. 10 IN NS ns.example.com.
	SECTION ADDITIONAL
		ns.example.com. 10 IN A 1.2.3.4
ENTRY_END

; Wait for the TTL to expire
STEP 11 TIME_PASSES ELAPSE 11

; Query again
STEP 30 QUERY
ENTRY_BEGIN
	REPLY RD DO
	SECTION QUESTION
		example.com. IN A
ENTRY_END

; Check that we got a stale answer because of the upstream SERVFAIL
STEP 40 CHECK_ANSWER
ENTRY_BEGIN
	MATCH all ttl ede=3
	REPLY QR RD RA DO NOERROR
	SECTION QUESTION
		example.com. IN A
	SECTION ANSWER
		example.com.  123 IN A 5.6.7.8
	SECTION AUTHORITY
		example.com. 123 IN NS ns.example.com.
	SECTION ADDITIONAL
		ns.example.com. 123 IN A 1.2.3.4
ENTRY_END

; Query again
STEP 50 QUERY
ENTRY_BEGIN
	REPLY RD DO
	SECTION QUESTION
		example.com. IN A
ENTRY_END

; Check that we got an immediate stale answer because of the previous failure,
; regardless if upstream has the answer already in this range.
STEP 60 CHECK_ANSWER
ENTRY_BEGIN
	MATCH all ttl ede=3
	REPLY QR RD RA DO NOERROR
	SECTION QUESTION
		example.com. IN A
	SECTION ANSWER
		example.com.  123 IN A 5.6.7.8
	SECTION AUTHORITY
		example.com. 123 IN NS ns.example.com.
	SECTION ADDITIONAL
		ns.example.com. 123 IN A 1.2.3.4
ENTRY_END

; Query again
STEP 70 QUERY
ENTRY_BEGIN
	REPLY RD DO
	SECTION QUESTION
		example.com. IN A
ENTRY_END

; Check that we still get the immediate stale answer because of the previous failure,
; regardless if upstream has the answer already in this range. NORR_TTL(5) blocks us from
; recursion.
STEP 80 CHECK_ANSWER
ENTRY_BEGIN
	MATCH all ttl ede=3
	REPLY QR RD RA DO NOERROR
	SECTION QUESTION
		example.com. IN A
	SECTION ANSWER
		example.com.  123 IN A 5.6.7.8
	SECTION AUTHORITY
		example.com. 123 IN NS ns.example.com.
	SECTION ADDITIONAL
		ns.example.com. 123 IN A 1.2.3.4
ENTRY_END

; Let NORR_TTL(5) expire
STEP 81 TIME_PASSES ELAPSE 5

; Query again
STEP 90 QUERY
ENTRY_BEGIN
	REPLY RD
	SECTION QUESTION
		example.com. IN A
ENTRY_END

; Check fresh reply
STEP 100 CHECK_ANSWER
ENTRY_BEGIN
	MATCH all ttl
	REPLY QR RD RA NOERROR
	SECTION QUESTION
		example.com. IN A
	SECTION ANSWER
		example.com. 10 IN A 5.6.7.8
	SECTION AUTHORITY
		example.com. 10 IN NS ns.example.com.
	SECTION ADDITIONAL
		ns.example.com. 10 IN A 1.2.3.4
ENTRY_END

SCENARIO_END
