varnishtest "New ban-lurker test"

server s1 {
	rxreq
	expect req.url == /1
	txresp -hdr "Foo: bar1"

	rxreq
	expect req.url == /2
	txresp -hdr "Foo: bar2"

	rxreq
	expect req.url == /3
	txresp -hdr "Foo: bar3"

	rxreq
	expect req.url == /4
	txresp -hdr "Foo: bar4"

	rxreq
	expect req.url == /5
	txresp -hdr "Foo: bar5"

	rxreq
	expect req.url == /6
	txresp -hdr "Foo: bar6"

	rxreq
	expect req.url == /7
	txresp -hdr "Foo: bar7"

	rxreq
	expect req.url == /4
	txresp -hdr "Foo: bar4.1"

	rxreq
	expect req.url == /r1
	txresp
	rxreq
	expect req.url == /r2
	txresp
	rxreq
	expect req.url == /r3
	txresp
} -start

varnish v1 -vcl+backend {} -start

varnish v1 -cliok "param.set ban_lurker_age 0"
varnish v1 -cliok "param.set ban_lurker_sleep 0"
varnish v1 -cliok "param.set debug +lurker"
varnish v1 -cliok "param.set debug +syncvsl"


client c1 {
	txreq -url /1
	rxresp
	expect resp.http.foo == bar1

	txreq -url /2
	rxresp
	expect resp.http.foo == bar2
} -run

varnish v1 -cliok "ban obj.http.foo == bar1"

client c1 {
	txreq -url /3
	rxresp
	expect resp.http.foo == bar3
} -run

varnish v1 -cliok "ban obj.http.foo == bar2 && obj.http.foo != foof"

client c1 {
	txreq -url /4
	rxresp
	expect resp.http.foo == bar4
} -run

varnish v1 -cliok "ban req.http.kill == yes"

client c1 {
	txreq -url /5
	rxresp
	expect resp.http.foo == bar5
} -run

varnish v1 -cliok "ban obj.http.foo == bar5"

client c1 {
	txreq -url /6
	rxresp
	expect resp.http.foo == bar6
} -run

varnish v1 -cliok "ban obj.http.foo == bar6"

client c1 {
	txreq -url /7
	rxresp
	expect resp.http.foo == bar7
} -run

# Get the VSL out of the way
delay 1

varnish v1 -cliok "ban.list"

varnish v1 -expect bans == 6
varnish v1 -expect bans_completed == 1
varnish v1 -expect bans_req == 1
varnish v1 -expect bans_obj == 4
varnish v1 -expect bans_added == 6
varnish v1 -expect bans_deleted == 0
varnish v1 -expect bans_tested == 0
varnish v1 -expect bans_tests_tested == 0
varnish v1 -expect bans_obj_killed == 0
varnish v1 -expect bans_lurker_tested == 0
varnish v1 -expect bans_lurker_tests_tested == 0
varnish v1 -expect bans_lurker_obj_killed == 0
varnish v1 -expect bans_dups == 0


varnish v1 -cliok "param.set ban_lurker_sleep .01"

delay 1

varnish v1 -cliok "ban.list"

delay 1

varnish v1 -cliok "ban.list"

varnish v1 -expect bans == 4
varnish v1 -expect bans_completed == 3
varnish v1 -expect bans_req == 1
varnish v1 -expect bans_obj == 3
varnish v1 -expect bans_added == 6
varnish v1 -expect bans_deleted == 2
varnish v1 -expect bans_tested == 0
varnish v1 -expect bans_tests_tested == 0
varnish v1 -expect bans_obj_killed == 0
varnish v1 -expect bans_lurker_tested == 10
varnish v1 -expect bans_lurker_tests_tested == 11
varnish v1 -expect bans_lurker_obj_killed == 4
varnish v1 -expect bans_dups == 0

client c1 {
	txreq -url /3
	rxresp
	expect resp.http.foo == bar3
} -run

# Give lurker time to trim tail
delay 1

varnish v1 -cliok "ban.list"

varnish v1 -expect bans == 4
varnish v1 -expect bans_completed == 3
varnish v1 -expect bans_req == 1
varnish v1 -expect bans_obj == 3
varnish v1 -expect bans_added == 6
varnish v1 -expect bans_deleted == 2
varnish v1 -expect bans_tested == 1
varnish v1 -expect bans_tests_tested == 1
varnish v1 -expect bans_obj_killed == 0
varnish v1 -expect bans_lurker_tested == 10
varnish v1 -expect bans_lurker_tests_tested == 11
varnish v1 -expect bans_lurker_obj_killed == 4
varnish v1 -expect bans_dups == 0

client c1 {
	txreq -url /4 -hdr "kill: yes"
	rxresp
	expect resp.http.foo == bar4.1
} -run

# Give lurker time to trim tail
delay 1

varnish v1 -cliok "ban.list"

varnish v1 -expect bans == 1
varnish v1 -expect bans_completed == 1
varnish v1 -expect bans_req == 0
varnish v1 -expect bans_obj == 1
varnish v1 -expect bans_added == 6
varnish v1 -expect bans_deleted == 5
varnish v1 -expect bans_tested == 2
varnish v1 -expect bans_tests_tested == 2
varnish v1 -expect bans_obj_killed == 1
varnish v1 -expect bans_lurker_tested == 10
varnish v1 -expect bans_lurker_tests_tested == 11
varnish v1 -expect bans_lurker_obj_killed == 4
varnish v1 -expect bans_dups == 0

varnish v1 -expect n_object == 3

# adding more bans than the cutoff purges all untested objects
# (here: all objects)

varnish v1 -cliok "param.set ban_lurker_age 2"
varnish v1 -cliok "param.set ban_cutoff 4"

varnish v1 -cliok "ban.list"
varnish v1 -cliok "ban obj.http.nomatch == 1"
varnish v1 -cliok "ban obj.http.nomatch == 2"
varnish v1 -cliok "ban obj.http.nomatch == 3"
varnish v1 -cliok "ban obj.http.nomatch == 4"
varnish v1 -cliok "ban obj.http.nomatch == 5"
varnish v1 -cliok "ban.list"
varnish v1 -cliok "param.set ban_lurker_age .1"
delay 3

varnish v1 -clijson "ban.list -j"
varnish v1 -cliok "ban.list"

varnish v1 -expect bans == 1
varnish v1 -expect bans_completed == 1
varnish v1 -expect bans_req == 0
varnish v1 -expect bans_obj == 1
varnish v1 -expect bans_added == 11
varnish v1 -expect bans_deleted == 10
varnish v1 -expect bans_tested == 2
varnish v1 -expect bans_tests_tested == 2
varnish v1 -expect bans_obj_killed == 1
varnish v1 -expect bans_lurker_tested == 10
varnish v1 -expect bans_lurker_tests_tested == 11
varnish v1 -expect bans_lurker_obj_killed == 4
varnish v1 -expect bans_lurker_obj_killed_cutoff == 3
varnish v1 -expect bans_dups == 0

varnish v1 -expect n_object == 0

client c1 {
	txreq -url /r1
	rxresp
} -run

varnish v1 -cliok "ban req.http.nevermatch == 1"

client c1 {
	txreq -url /r2
	rxresp
} -run

varnish v1 -cliok "ban req.http.nevermatch == 2"

client c1 {
	txreq -url /r3
	rxresp
} -run

varnish v1 -cliok "ban req.http.nevermatch == 3"

varnish v1 -cliok "ban.list"

varnish v1 -cliok "ban obj.http.status != 0"

delay 1

varnish v1 -cliok "ban.list"

varnish v1 -expect n_object == 0
