From 97eed877c29315d2abccfa3a8262e36c4312a466 Mon Sep 17 00:00:00 2001 From: Maxime Gervais Date: Tue, 3 Nov 2026 12:55:59 +0100 Subject: [PATCH] Return error on unknown/invalid policy Signed-off-by: Maxime Gervais --- Project/GNU/CLI/Makefile.am | 3 +- Project/GNU/CLI/test/test_invalid_policy.sh | 62 +++++++++++++++++++++ Source/CLI/CLI.cpp | 8 ++- Source/Common/Policies.cpp | 14 +---- 4 files changed, 73 insertions(+), 14 deletions(-) create mode 100755 Project/GNU/CLI/test/test_invalid_policy.sh diff --git a/Project/GNU/CLI/Makefile.am b/Project/GNU/CLI/Makefile.am index 538690ed..e6e85341 100644 --- a/Project/GNU/CLI/Makefile.am +++ b/Project/GNU/CLI/Makefile.am @@ -61,7 +61,8 @@ TESTS = \ test/filename.sh \ test/test_mk.sh \ test/test_ffv1.sh \ - test/test_policy.sh + test/test_policy.sh \ + test/test_invalid_policy.sh SAMPLES_DIR = test/SampleFiles diff --git a/Project/GNU/CLI/test/test_invalid_policy.sh b/Project/GNU/CLI/test/test_invalid_policy.sh new file mode 100755 index 00000000..1c0d1138 --- /dev/null +++ b/Project/GNU/CLI/test/test_invalid_policy.sh @@ -0,0 +1,62 @@ +#!/bin/sh + +PATH_SCRIPT=$(dirname "$0") +FILES_DIRECTORY="$PATH_SCRIPT/SampleFiles/ImplementationTestFiles/Matroska" +MC="${PWD}/mediaconch" +MEDIA_FILE="$FILES_DIRECTORY/tiny.mkv" +SCHEMA_FILE="$PATH_SCRIPT/../../../../Source/Resource/schema/MatroskaSchema.xml" + +TMP_DIR=$(mktemp -d "${TMPDIR:-/tmp}/mediaconch-policy.XXXXXX") || exit 1 +trap 'rm -rf "$TMP_DIR"' 0 + +RCODE=0 + +check_invalid_policy() +{ + POLICY_FILE="$1" + EXPECTED_ERROR="$2" + shift 2 + + "${MC}" "--ImplementationSchema=$SCHEMA_FILE" -p "$POLICY_FILE" "$@" "$MEDIA_FILE" >"$TMP_DIR/stdout" 2>"$TMP_DIR/stderr" + STATUS=$? + + if [ "$STATUS" -eq 0 ]; then + echo "NOK: $POLICY_FILE, exited with status 0" >&9 + RCODE=1 + elif ! grep -F -q "$EXPECTED_ERROR" "$TMP_DIR/stderr"; then + echo "NOK: $POLICY_FILE, expected error not found" >&9 + RCODE=1 + elif [ -s "$TMP_DIR/stdout" ]; then + echo "NOK: $POLICY_FILE, unexpected standard output" >&9 + RCODE=1 + else + echo "OK: $POLICY_FILE" >&9 + fi +} + +printf '%s\n' \ + '' \ + '' \ + '' \ + ' MXF' \ + '' >"$TMP_DIR/bad_comment.xml" + +printf '%s\n' \ + '' \ + '' >"$TMP_DIR/not_policy.xml" + +: >"$TMP_DIR/empty.xml" + +check_invalid_policy "$TMP_DIR/bad_comment.xml" "The schema given cannot be parsed" +check_invalid_policy "$TMP_DIR/bad_comment.xml" "The schema given cannot be parsed" -fx +check_invalid_policy "$TMP_DIR/not_policy.xml" "The XML policy tag accepted are 'policy' or 'rule'" +check_invalid_policy "$TMP_DIR/not_policy.xml" "The XML policy tag accepted are 'policy' or 'rule'" -fj +check_invalid_policy "$TMP_DIR/empty.xml" "The schema does not exist" + +check_invalid_policy "$TMP_DIR/bad_comment.xml" "The schema given cannot be parsed" -ox "$TMP_DIR/report.xml" +if [ -e "$TMP_DIR/report.xml" ]; then + echo "NOK: report file created for an invalid policy" >&9 + RCODE=1 +fi + +exit "$RCODE" diff --git a/Source/CLI/CLI.cpp b/Source/CLI/CLI.cpp index 9ed451c4..a25b71ce 100644 --- a/Source/CLI/CLI.cpp +++ b/Source/CLI/CLI.cpp @@ -434,7 +434,9 @@ void __stdcall Event_CallBackFunction(unsigned char* Data_Content, size_t Data_S cr.display_content = &display_content; - MCL.checker_get_report(cr, &result, error); + if (MCL.checker_get_report(cr, &result, err) < 0) + return CLI_RETURN_ERROR; + MediaInfoLib::String report_mi = ZenLib::Ztring().From_UTF8(result.report); STRINGOUT(report_mi); @@ -460,7 +462,9 @@ void __stdcall Event_CallBackFunction(unsigned char* Data_Content, size_t Data_S cr.format = format; - MCL.checker_get_report(cr, &result, error); + if (MCL.checker_get_report(cr, &result, err) < 0) + return CLI_RETURN_ERROR; + MediaInfoLib::String report_mi = ZenLib::Ztring().From_UTF8(result.report); ZenLib::File F; diff --git a/Source/Common/Policies.cpp b/Source/Common/Policies.cpp index 4ee1afa6..8e99d960 100644 --- a/Source/Common/Policies.cpp +++ b/Source/Common/Policies.cpp @@ -1004,17 +1004,9 @@ int Policies::policy_get_policy_content(const std::string& policy, const std::ma ret = p->import_schema_from_memory(policy.c_str(), policy.length(), ""); if (ret < 0) { - if (p) - delete p; - - p = new UnknownPolicy(this, !core->accepts_https()); - ret = p->import_schema_from_memory(policy.c_str(), policy.length(), ""); - if (ret < 0) - { - err = p->get_error(); - delete p; - return -1; - } + err = p->get_error(); + delete p; + return -1; } ret = policy_get_policy_id(p, opts, xml_policies, xslt_policies, err);