From 9ed9798dfee857a10dd1966cc67a4398e34815ba Mon Sep 17 00:00:00 2001
From: Dominik Loidolt <dominik.loidolt@univie.ac.at>
Date: Thu, 16 Jan 2025 16:33:56 +0100
Subject: [PATCH] Fix: Update error handling for model file size checks

- Modified warning message in `cmp_io.c` to clarify file data status
- Updated `cmp_tool.c` to check model file data size more comprehensively
- Refactored `cmp_tool_integration_test.py` to handle non-binary test case for model file size mismatch
---
 programs/cmp_io.c                          |  2 +-
 programs/cmp_tool.c                        | 19 ++++++++++---
 test/cmp_tool/cmp_tool_integration_test.py | 33 +++++++++++++++++++++-
 3 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/programs/cmp_io.c b/programs/cmp_io.c
index 8dc453d..50ce1d0 100644
--- a/programs/cmp_io.c
+++ b/programs/cmp_io.c
@@ -1305,7 +1305,7 @@ static __inline ssize_t str2uint8_arr(const char *str, uint8_t *data, uint32_t b
 			nptr++;
 	}
 	if (*nptr != '\0') {
-		fprintf(stderr, "%s: %s: Warning: The file may contain more data than specified by the samples or cmp_size parameter.\n",
+		fprintf(stderr, "%s: %s: Warning: The file may contain more data than read from it.\n",
 			PROGRAM_NAME, file_name);
 	}
 
diff --git a/programs/cmp_tool.c b/programs/cmp_tool.c
index 3d0e7f2..99eb66d 100644
--- a/programs/cmp_tool.c
+++ b/programs/cmp_tool.c
@@ -19,6 +19,7 @@
  * @see Data Compression User Manual PLATO-UVIE-PL-UM-0001
  */
 
+#include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -500,6 +501,19 @@ int main(int argc, char **argv)
 		else
 			model_size = cmp_ent_get_original_size(decomp_entity);
 
+		size = read_file_data(model_file_name, cmp_type, NULL,
+				      model_size, io_flags);
+		if (size < 0)
+			goto fail;
+		if (size < (ssize_t)model_size) {
+			fprintf(stderr, "%s: %s: Error: The files do not contain enough data. Expected: 0x%x, has 0x%zx.\n",
+				PROGRAM_NAME, model_file_name, model_size, size);
+			goto fail;
+		}
+		if (size != (ssize_t)model_size) {
+			fprintf(stderr, "%s: %s: Error: Model file size does not match original data size.\n", PROGRAM_NAME, model_file_name);
+			goto fail;
+		}
 
 		input_model_buf = malloc(model_size);
 		if (!input_model_buf) {
@@ -511,10 +525,7 @@ int main(int argc, char **argv)
 				      model_size, io_flags);
 		if (size < 0)
 			goto fail;
-		if (size != (ssize_t)model_size) {
-			fprintf(stderr, "%s: %s: Error: Model file size does not match original data size.\n", PROGRAM_NAME, model_file_name);
-			goto fail;
-		}
+
 
 		printf("DONE\n");
 
diff --git a/test/cmp_tool/cmp_tool_integration_test.py b/test/cmp_tool/cmp_tool_integration_test.py
index 3f7e004..e0e3fbe 100755
--- a/test/cmp_tool/cmp_tool_integration_test.py
+++ b/test/cmp_tool/cmp_tool_integration_test.py
@@ -1331,7 +1331,7 @@ def test_model_fiel_erros():
         del_file(output_prefix+"_upmodel.dat")
 
 
-def test_decmp_model_fiel_original_size_miss_match():
+def test_decmp_model_fiel_original_size_miss_match_binary():
     cmp_data = b'8000000d000029000004097ce800cbd5097ce800cbfe00010108d01001000000001001001110078700'
     to_large_model = b'111111111111' # should be 4 byte large in normal case
     output_prefix = 'model_file_to_large'
@@ -1356,6 +1356,37 @@ def test_decmp_model_fiel_original_size_miss_match():
     finally:
         del_file(cmp_data_file_name)
         del_file(model_file_name)
+        del_file(output_prefix+'.dat')
+        del_file(output_prefix+'_upmodel.dat')
+
+
+def test_decmp_model_fiel_original_size_miss_match_binary():
+    cmp_data = '8000000d000029000004097ce800cbd5097ce800cbfe00010108d01001000000001001001110078700'
+    to_large_model = '111111111111' # should be 4 byte large in normal case
+    output_prefix = 'model_file_to_large'
+    cmp_data_file_name = 'cmp_data.cmp'
+    model_file_name = 'to_large_model.dat'
+
+    try:
+        with open(cmp_data_file_name, 'w', encoding='utf-8') as f:
+            f.write(cmp_data)
+
+        with open(model_file_name, 'w', encoding='utf-8') as f:
+            f.write(to_large_model)
+
+        returncode, stdout, stderr = call_cmp_tool(
+            " -d "+cmp_data_file_name + " -m " + model_file_name + " -o "+output_prefix)
+        assert(returncode == EXIT_FAILURE)
+        assert(stdout == CMP_START_STR_DECMP +
+               "Importing compressed data file %s ... DONE\n" % (cmp_data_file_name) +
+               "Importing model file %s ... FAILED\n" % (model_file_name))
+        assert(stderr == "cmp_tool: %s: Error: Model file size does not match original data size.\n" % (model_file_name))
+
+    finally:
+         del_file(cmp_data_file_name)
+         del_file(model_file_name)
+         del_file(output_prefix+'.dat')
+         del_file(output_prefix+'_upmodel.dat')
 
 
 def test_rdcu_pkt():
-- 
GitLab