varnishtest "VMOD blob IDENTITY encode and decode"

varnish v1 -vcl {
	import blob;
	backend b { .host = "${bad_ip}"; }

	sub vcl_recv {
	  return(synth(200));
	}

	sub vcl_synth {
	    set resp.http.id =
	      blob.encode(IDENTITY, blob=blob.decode(IDENTITY,
			encoded="The quick brown fox jumps over the lazy dog"));

	    set resp.http.hobbes =
	      blob.encode(IDENTITY, blob=blob.decode(IDENTITY, encoded=
{"Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."}));

	    set resp.http.list =
	      blob.encode(IDENTITY, blob=blob.decode(IDENTITY, encoded=
			      "" + req.http.unset + req.url +
			      "The quick brown fox jumps over " +
			      req.http.unset + "" + req.http.unset + "" +
			      "the lazy dog" + req.url + req.http.unset + ""));

	    set resp.http.empty =
	      blob.encode(IDENTITY, blob=blob.decode(IDENTITY, encoded=""));

	    set resp.http.undef =
	      blob.encode(IDENTITY, blob=blob.decode(IDENTITY,
						     encoded=req.http.unset));

	    set resp.http.emptylist =
	      blob.encode(IDENTITY, blob=blob.decode(IDENTITY,
			  encoded=req.http.unset + "" + req.http.unset + ""));

	    set resp.http.param =
	      blob.encode(blob=
			      blob.decode(encoded=
				 "The quick brown fox jumps over the lazy dog",
					    decoding=IDENTITY),
			      encoding=IDENTITY);

	    set resp.http.paramlist =
	      blob.encode(IDENTITY, blob=blob.decode(encoded=
			      "" + req.http.unset + req.url +
			      "The quick brown fox jumps over " +
			      req.http.unset + "" + req.http.unset + "" +
			      "the lazy dog" + req.url + req.http.unset + "",
			      decoding=IDENTITY));

	    set resp.http.truncated =
	      blob.encode(IDENTITY,
			  blob=blob.decode(HEX, encoded="666f6f00626172"));

	    set resp.http.pos =
	      blob.encode(IDENTITY, DEFAULT, blob.decode(IDENTITY,
							 encoded="foobar"));
	}
} -start

client c1 {
	txreq -url "/"
	rxresp
	expect resp.http.id == "The quick brown fox jumps over the lazy dog"
	expect resp.http.hobbes == "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."
	expect resp.http.list == "/The quick brown fox jumps over the lazy dog/"
	expect resp.http.empty == ""
	expect resp.http.undef == ""
	expect resp.http.emptylist == ""
	expect resp.http.param == "The quick brown fox jumps over the lazy dog"
	expect resp.http.paramlist == "/The quick brown fox jumps over the lazy dog/"
	expect resp.http.truncated == "foo"
	expect resp.http.pos == "foobar"
} -run

# Require case=DEFAULT

server s1 -repeat 2 {
	rxreq
	txresp
} -start

varnish v1 -vcl+backend {
	import blob;

	sub vcl_deliver {
	  if (req.url == "/lc") {
	    set resp.http.lc =
	      blob.encode(IDENTITY, LOWER, blob.decode(IDENTITY,
						       encoded="foobar"));
	  }
	  elsif (req.url == "/uc") {
	    set resp.http.uc =
	      blob.encode(IDENTITY, UPPER, blob.decode(IDENTITY,
						       encoded="foobar"));
	  }
	}
}

client c1 {
	txreq -url "/lc"
	rxresp
	expect resp.status == 503
	expect resp.reason == "VCL failed"
	expect resp.http.lc == <undef>
} -run

client c1 {
	txreq -url "/uc"
	rxresp
	expect resp.status == 503
	expect resp.reason == "VCL failed"
	expect resp.http.uc == <undef>
} -run

logexpect l1 -v v1 -d 1 -g vxid -q "VCL_Error" {
	expect * * VCL_Error "^vmod blob error: case LOWER is illegal with encoding IDENTITY$"
	expect * * VCL_Error "^vmod blob error: case UPPER is illegal with encoding IDENTITY$"
} -start

logexpect l1 -wait
