varnishtest "Test vmod_path param"

feature topbuild

server s1 {
	rxreq
	txresp
} -start

shell {
	echo "vcl 4.1; import std; backend dummy None;" >${tmpdir}/test.vcl
	varnishd -pvmod_path=${topbuild}/vmod/.libs \
		 -C -f ${tmpdir}/test.vcl 2>/dev/null
}

varnish v1 -arg "-pvmod_path=${topbuild}/vmod/.libs/" -vcl+backend {
	import std;
} -start

varnish v1 -cliok "param.set vmod_path /nonexistent"

varnish v1 -errvcl {Could not find VMOD std} { import std; }

varnish v1 -cliok "param.set vmod_path ${topbuild}/vmod/.libs/"

varnish v1 -vcl+backend { import std; }

varnish v1 -cliok "param.set vmod_path ${tmpdir}"

shell {
	cp ${topbuild}/vmod/.libs/libvmod_debug.so \
	   ${tmpdir}/libvmod_wrong.so
}

varnish v1 -errvcl {Wrong file for VMOD wrong} { import wrong; }

filewrite ${tmpdir}/libvmod_wrong.so ""
varnish v1 -errvcl {Error: No VMOD JSON found} { import wrong; }

shell "chmod 000 ${tmpdir}/libvmod_wrong.so"
varnish v1 -errvcl {Could not open VMOD wrong} { import wrong; }
shell "rm -f ${tmpdir}/libvmod_wrong.so"

filewrite ${tmpdir}/libvmod_wrong.so "BLA" "VMOD_JSON_SPEC\x02" "BLA"
varnish v1 -errvcl {Truncated VMOD JSON} { import wrong; }

filewrite ${tmpdir}/libvmod_wrong.so "VMOD_JSON_SPEC\x02" "BLA" "\x03"
varnish v1 -errvcl {VMOD wrong: bad metadata} { import wrong; }

filewrite ${tmpdir}/libvmod_wrong.so "VMOD_JSON_SPEC\x02" "0" "\x03"
varnish v1 -errvcl {Not array[0]} { import wrong; }

filewrite ${tmpdir}/libvmod_wrong.so "VMOD_JSON_SPEC\x02" "[0]" "\x03"
varnish v1 -errvcl {Not array[1]} { import wrong; }

filewrite ${tmpdir}/libvmod_wrong.so "VMOD_JSON_SPEC\x02" "[[0]]" "\x03"
varnish v1 -errvcl {Not string[2]} { import wrong; }

filewrite ${tmpdir}/libvmod_wrong.so "VMOD_JSON_SPEC\x02" "[[\"$VBLA\"]]" "\x03"
varnish v1 -errvcl {Not $VMOD[3]} { import wrong; }

filewrite ${tmpdir}/libvmod_wrong.so "VMOD_JSON_SPEC\x02"
filewrite -a ${tmpdir}/libvmod_wrong.so {
    [
	[
	    "$VMOD",
	    "1.0",
	    "wrong",
	    "Vmod_vmod_wrong_Func",
	    "0000000000000000000000000000000000000000000000000000000000000000",
	    "0000000000000000000000000000000000000000000000000000000000000000",
	    "0",
	    "0U"
	]
    ]
}
filewrite -a ${tmpdir}/libvmod_wrong.so "\x03"
varnish v1 -errvcl {Incompatible VMOD wrong} { import wrong; }

filewrite ${tmpdir}/libvmod_wrong.so "VMOD_JSON_SPEC\x02"
filewrite -a ${tmpdir}/libvmod_wrong.so {
    [
	[
	    "$VMOD",
	    "1.0",
	    "wrong",
	    "Vmod_vmod_wrong_Func",
	    "0000000000000000000000000000000000000000000000000000000000000000",
	    "0000000000000000000000000000000000000000000000000000000000000000",
	    "1U",
	    "0"
	]
    ]
}
filewrite -a ${tmpdir}/libvmod_wrong.so "\x03"
varnish v1 -errvcl {VMOD wants ABI version 1.0} { import wrong; }

#############################################################
# NB: in the tests below "18" should track VRT_MAJOR_VERSION

filewrite ${tmpdir}/libvmod_wrong.so "VMOD_JSON_SPEC\x02"
filewrite -a ${tmpdir}/libvmod_wrong.so {
    [
	[
	    "$VMOD",
	    "1.0",
	    "wrong",
	    "Vmod_vmod_wrong_Func",
	    "0000000000000000000000000000000000000000000000000000000000000000",
	    "0000000000000000000000000000000000000000000000000000000000000000",
	    "18",
	    "0"
	], [
	    "$FOOBAR"
	]
    ]
}
filewrite -a ${tmpdir}/libvmod_wrong.so "\x03"
varnish v1 -errvcl {Unknown metadata stanza.} { import wrong; }

filewrite ${tmpdir}/libvmod_wrong.so "VMOD_JSON_SPEC\x02"
filewrite -a ${tmpdir}/libvmod_wrong.so {
    [
	[
	    "$VMOD",
	    "1.0",
	    "wrong",
	    "Vmod_vmod_wrong_Func",
	    "0000000000000000000000000000000000000000000000000000000000000000",
	    "0000000000000000000000000000000000000000000000000000000000000000",
	    "18",
	    "0"
	]
    ]
}
filewrite -a ${tmpdir}/libvmod_wrong.so "\x03"
varnish v1 -errvcl {Bad cproto stanza} { import wrong; }

filewrite ${tmpdir}/libvmod_wrong.so "VMOD_JSON_SPEC\x02"
filewrite -a ${tmpdir}/libvmod_wrong.so {
    [
	[
	    "$VMOD",
	    "1.0",
	    "wrong",
	    "Vmod_vmod_wrong_Func",
	    "0000000000000000000000000000000000000000000000000000000000000000",
	    "0000000000000000000000000000000000000000000000000000000000000000",
	    "18",
	    "0"
	], [
	    "$CPROTO"
	], [
	    "$VMOD"
	]
    ]
}
filewrite -a ${tmpdir}/libvmod_wrong.so "\x03"
varnish v1 -errvcl {Bad vmod stanza} { import wrong; }

filewrite ${tmpdir}/libvmod_wrong.so "VMOD_JSON_SPEC\x02"
filewrite -a ${tmpdir}/libvmod_wrong.so {
    [
	[
	    "$VMOD",
	    "1.0",
	    "std",
	    "Vmod_vmod_std_Func",
	    "0000000000000000000000000000000000000000000000000000000000000000",
	    "0000000000000000000000000000000000000000000000000000000000000000",
	    "18",
	    "0"
	], [
	    "$CPROTO", "/* blabla */"
	]
    ]
}
filewrite -a ${tmpdir}/libvmod_wrong.so "\x03"

varnish v1 -cliok "param.set vmod_path ${topbuild}/vmod/.libs/"
varnish v1 -cliok "stop"

filewrite  ${tmpdir}/wrong.vcl {
	vcl 4.1;
	import std;
	import std as foo from "${tmpdir}/libvmod_wrong.so";
	backend default none;
}

varnish v1 -cliexpect {Different version of VMOD std already loaded} {
	vcl.load dupvmod ${tmpdir}/wrong.vcl
}

shell "rm -f ${tmpdir}/libvmod_wrong.so ${tmpdir}wrong.vcl"
