require "vnd.dovecot.testsuite";
require "comparator-i;ascii-numeric";
require "relational";
require "variables";

require "editheader";

test "Invalid field-name" {
	if test_script_compile "errors/field-name.sieve" {
		test_fail "compile should have failed";
	}

	if not test_error :count "eq" :comparator "i;ascii-numeric" "5" {
		test_fail "wrong number of errors reported";
	}

	if not test_error :index 1 :matches "*field name*X-field:*invalid*" {
		test_fail "wrong error reported (1)";
	}

	if not test_error :index 2 :matches "*field name*X field*invalid*" {
		test_fail "wrong error reported (2)";
	}

	if not test_error :index 3 :matches "*field name*X-field:*invalid*" {
		test_fail "wrong error reported (3)";
	}

	if not test_error :index 4 :matches "*field name*X field*invalid*" {
		test_fail "wrong error reported (4)";
	}
}

test "Invalid field-name at runtime " {
	if not test_script_compile "errors/field-name-runtime.sieve" {
		test_fail "compile failed";
	}

	if test_script_run {
		test_fail "run should have failed";
	}

	if not test_error :count "eq" :comparator "i;ascii-numeric" "1" {
		test_fail "wrong number of errors reported";
	}

	if not test_error :matches "*field name*X-field:*invalid*" {
		test_fail "wrong error reported";
	}
}

test "Invalid field value" {
	if test_script_compile "errors/field-value.sieve" {
		test_fail "compile should have failed";
	}

	if not test_error :count "eq" :comparator "i;ascii-numeric" "2" {
		test_fail "wrong number of errors reported";
	}

	if not test_error :index 1 :matches "*value*Woah*invalid*" {
		test_fail "wrong error reported (1): ${0}";
	}
}

test "Command syntax (FIXME: count only)" {
	if test_script_compile "errors/command-syntax.sieve" {
		test_fail "compile should have failed";
	}

	if not test_error :count "eq" :comparator "i;ascii-numeric" "10" {
		test_fail "wrong number of errors reported";
	}
}

/*
 * TEST - Size limit
 */

test "Size limit" {
	if not test_script_compile "errors/size-limit.sieve" {
		test_fail "compile should have succeeded";
	}

	test_config_set "sieve_editheader_max_header_size" "1024";
	test_config_reload :extension "editheader";

	if test_script_compile "errors/size-limit.sieve" {
		test_fail "compile should have failed";
	}

	if not test_error :count "eq" :comparator "i;ascii-numeric" "2" {
		test_fail "wrong number of errors reported";
	}
}


/*
 * TEST - Size limit at runtime
 */

test_config_set "sieve_editheader_max_header_size" "";
test_config_reload :extension "editheader";

test "Size limit at runtime" {
	if not test_script_compile "errors/size-limit-runtime.sieve" {
		test_fail "compile should have succeeded";
	}

	if not test_script_run {
		test_fail "run failed";
	}

	test_config_set "sieve_editheader_max_header_size" "1024";
	test_config_reload :extension "editheader";

	if not test_script_compile "errors/size-limit-runtime.sieve" {
		test_fail "compile should have succeeded";
	}

	if test_script_run {
		test_fail "run should have failed";
	}

	if not test_error :count "eq" :comparator "i;ascii-numeric" "1" {
		test_fail "wrong number of errors reported";
	}
}

/*
 * TEST - Implicit keep at runtime error
 */

test_set "message" text:
From: stephan@example.com
To: tss@example.com
Subject: Frop

Frop!
.
;

test "Implicit keep at runtime error" {
	if not test_script_compile "errors/runtime-error.sieve" {
		test_fail "compile failed";
	}

	if not test_script_run {
		test_fail "run failed";
	}

	if test_result_execute {
		test_fail "result execution should have failed";
	}

	if not test_message :folder "INBOX" 0 {
		test_fail "message not stored (no implicit keep)";
	}

	if exists "X-Frop" {
		test_fail "implicit keep message has editheader changes";
	}
}

