From 8c028e953ee6e35d7c4d73ba184661a6bdce7397 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sun, 9 Feb 2025 19:08:50 +0100 Subject: [PATCH] add type check tiff test with bool --- src/kds_s2000w_image_type_check.c | 9 +++-- src/kds_s2000w_image_type_check.h | 7 ++-- tests/CMakeLists.txt | 10 +++++ tests/kds_s2000w_image_type_check_tests.c | 37 +++++++++++++++++++ tests/kds_s2000w_image_type_check_tests.h | 12 ++++++ tests/kds_s2000w_image_type_check_tests_run.c | 11 ++++++ 6 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 tests/kds_s2000w_image_type_check_tests.c create mode 100644 tests/kds_s2000w_image_type_check_tests.h create mode 100644 tests/kds_s2000w_image_type_check_tests_run.c diff --git a/src/kds_s2000w_image_type_check.c b/src/kds_s2000w_image_type_check.c index f405ca6..95773e1 100644 --- a/src/kds_s2000w_image_type_check.c +++ b/src/kds_s2000w_image_type_check.c @@ -1,8 +1,9 @@ #include + #include "kds_s2000w_debug.h" #include "kds_s2000w_image_type_check.h" -uint8_t kds_s2000w_image_type_check_is_tiff(blobdata* image) +bool kds_s2000w_image_type_check_is_tiff(blobdata* image) { kds_s2000w_debug_printf(ALL, "kds_s2000w_image_type_check_is_tiff"); @@ -11,7 +12,7 @@ uint8_t kds_s2000w_image_type_check_is_tiff(blobdata* image) char* image_data = (char*) image->data; - uint8_t result = image_data[0] == (char)0x49; + bool result = image_data[0] == (char)0x49; result = result && image_data[1] == (char)0x49; result = result && image_data[2] == (char)0x2A; result = result && image_data[3] == (char)0x00; @@ -25,7 +26,7 @@ uint8_t kds_s2000w_image_type_check_is_tiff(blobdata* image) return result && image_data[3] == (char)0x2A; } -uint8_t kds_s2000w_image_type_check_is_pnm(blobdata* image) +bool kds_s2000w_image_type_check_is_pnm(blobdata* image) { kds_s2000w_debug_printf(ALL, "kds_s2000w_image_type_check_is_pnm"); @@ -39,7 +40,7 @@ uint8_t kds_s2000w_image_type_check_is_pnm(blobdata* image) return strncmp(image_data, "P4", 2) == 0; } -uint8_t kds_s2000w_image_type_check_is_jpeg(blobdata* image) +bool kds_s2000w_image_type_check_is_jpeg(blobdata* image) { kds_s2000w_debug_printf(ALL, "kds_s2000w_image_type_check_is_jpeg"); diff --git a/src/kds_s2000w_image_type_check.h b/src/kds_s2000w_image_type_check.h index d82665f..5dfa852 100644 --- a/src/kds_s2000w_image_type_check.h +++ b/src/kds_s2000w_image_type_check.h @@ -1,10 +1,11 @@ #ifndef KDS_S2000W_IMAGE_TYPE_CHECK_H #define KDS_S2000W_IMAGE_TYPE_CHECK_H #include +#include #include "kds_s2000w_image_converter.h" -uint8_t kds_s2000w_image_type_check_is_tiff(blobdata* image); -uint8_t kds_s2000w_image_type_check_is_pnm(blobdata* image); -uint8_t kds_s2000w_image_type_check_is_jpeg(blobdata* image); +bool kds_s2000w_image_type_check_is_tiff(blobdata* image); +bool kds_s2000w_image_type_check_is_pnm(blobdata* image); +bool kds_s2000w_image_type_check_is_jpeg(blobdata* image); #endif \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 12d7748..8812431 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -58,6 +58,14 @@ TARGET_LINK_LIBRARIES("kds_s2000w_option_descriptor_tests_run" ${CMOCKA_LIBRARY} sane-kds_s2000w_net-static) +ADD_EXECUTABLE("kds_s2000w_image_type_check_tests_run" + "kds_s2000w_image_type_check_tests_run.c" + "kds_s2000w_image_type_check_tests.c") +ADD_DEPENDENCIES("kds_s2000w_image_type_check_tests_run" sane-kds_s2000w_net-static) +TARGET_LINK_LIBRARIES("kds_s2000w_image_type_check_tests_run" + ${CMOCKA_LIBRARY} + sane-kds_s2000w_net-static) + INCLUDE(CTest) ENABLE_TESTING() @@ -73,6 +81,8 @@ ADD_TEST(NAME "kds_s2000w_converter_tests_run" COMMAND "kds_s2000w_image_converter_tests_run") ADD_TEST(NAME "kds_s2000w_option_descriptor_tests_run" COMMAND "kds_s2000w_option_descriptor_tests_run") +ADD_TEST(NAME "kds_s2000w_image_type_check_tests_run" + COMMAND "kds_s2000w_image_type_check_tests_run") ADD_CUSTOM_TARGET("runningtests" ALL "ctest" "--verbose" diff --git a/tests/kds_s2000w_image_type_check_tests.c b/tests/kds_s2000w_image_type_check_tests.c new file mode 100644 index 0000000..4b5e4fc --- /dev/null +++ b/tests/kds_s2000w_image_type_check_tests.c @@ -0,0 +1,37 @@ +#include +#include "kds_s2000w_image_type_check_tests.h" +#include "../src/kds_s2000w_image_type_check.h" + +void kds_s2000w_image_type_check_is_tiff_true() +{ + blobdata* image = malloc(sizeof(blobdata)); + image->data = malloc(sizeof(char) * 4); + char* imagedata = (char*) image->data; + imagedata[0] = 0x49; + imagedata[1] = 0x49; + imagedata[2] = 0x2A; + imagedata[3] = 0x00; + image->size = 4; + + uint8_t result = kds_s2000w_image_type_check_is_tiff(image); + + assert_true(result); + + free(image->data); + image->data = NULL; + free(image); + image = NULL; +} + +void kds_s2000w_image_type_check_is_tiff_false() +{ + blobdata* image = malloc(sizeof(blobdata)); + image->size = 0; + + uint8_t result = kds_s2000w_image_type_check_is_tiff(image); + + assert_false(result); + + free(image); + image = NULL; +} \ No newline at end of file diff --git a/tests/kds_s2000w_image_type_check_tests.h b/tests/kds_s2000w_image_type_check_tests.h new file mode 100644 index 0000000..9cf4b9d --- /dev/null +++ b/tests/kds_s2000w_image_type_check_tests.h @@ -0,0 +1,12 @@ +#ifndef KDS_S2000W_IMAGE_TYPE_CHECK_TESTS +#define KDS_S2000W_IMAGE_TYPE_CHECK_TESTS +#include +#include +#include +#include +#include + +void kds_s2000w_image_type_check_is_tiff_true(); +void kds_s2000w_image_type_check_is_tiff_false(); + +#endif \ No newline at end of file diff --git a/tests/kds_s2000w_image_type_check_tests_run.c b/tests/kds_s2000w_image_type_check_tests_run.c new file mode 100644 index 0000000..e96cb5f --- /dev/null +++ b/tests/kds_s2000w_image_type_check_tests_run.c @@ -0,0 +1,11 @@ +#include "kds_s2000w_image_type_check_tests.h" + +int main() +{ + const struct CMUnitTest type_check_tests[] = { + cmocka_unit_test(kds_s2000w_image_type_check_is_tiff_true), + cmocka_unit_test(kds_s2000w_image_type_check_is_tiff_false) + }; + + return cmocka_run_group_tests(type_check_tests, NULL, NULL); +} \ No newline at end of file -- 2.39.5