From ab3a741d4f45c260746d21e78e4a5b034e36097d Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Mon, 3 Feb 2025 21:38:35 +0100 Subject: [PATCH] change tests with strdup --- tests/kds_s2000w_net_get_opt_tests_run.c | 7 ++- tests/kds_s2000w_net_tests.c | 61 +++++++++++------------- tests/kds_s2000w_net_tests_run.c | 9 ++-- 3 files changed, 34 insertions(+), 43 deletions(-) diff --git a/tests/kds_s2000w_net_get_opt_tests_run.c b/tests/kds_s2000w_net_get_opt_tests_run.c index 5ddc782..2bd71ee 100644 --- a/tests/kds_s2000w_net_get_opt_tests_run.c +++ b/tests/kds_s2000w_net_get_opt_tests_run.c @@ -6,7 +6,6 @@ int setup_default_get_option(void** state) { response* resp = kds_s2000w_client_response_init(); - resp->data = realloc(resp->data, 919); const char* responsedata = "{ \ \"Status\": { \ \"NumImagesScanned\": 0, \ @@ -41,10 +40,10 @@ int setup_default_get_option(void** state) \"MaxDocumentLength\": 140, \ \"ScanSource\": \"DocumentFeeder\" \ } \ - }\0"; - memcpy(resp->data, responsedata, 919); + }"; + resp->data = strdup(responsedata); resp->code = 200; - resp->size = 919; + resp->size = 918; *state = resp; return 0; } diff --git a/tests/kds_s2000w_net_tests.c b/tests/kds_s2000w_net_tests.c index a2cd8c6..ea110a8 100644 --- a/tests/kds_s2000w_net_tests.c +++ b/tests/kds_s2000w_net_tests.c @@ -210,7 +210,6 @@ void sane_kds_s2000w_net_start_test(void** state) will_return(mock_response, response_list[0]); will_return(__wrap_kds_s2000w_client_start_scan, 0); response* resp_status = kds_s2000w_client_response_init(); - resp_status->data = realloc(resp_status->data, 205); const char* status = "{ \ \"Status\": { \ \"NumImagesScanned\": 2, \ @@ -220,10 +219,10 @@ void sane_kds_s2000w_net_start_test(void** state) \"LastError\": \"Status: 200 Success\", \ \"PaperDetected\": \"0\", \ \"PercentAvailable\": 99 \ - }}\0"; - memcpy(resp_status->data, status, 205); + }}"; + resp_status->data = strdup(status); resp_status->code = 200; - resp_status->size = 205; + resp_status->size = 204; will_return(mock_response, resp_status); will_return(__wrap_kds_s2000w_client_status_session, 0); will_return(mock_response, response_list[2]); @@ -257,7 +256,6 @@ void sane_kds_s2000w_net_start_one_page_per_flatscan_test(void** state) will_return(mock_response, response_list[0]); will_return(__wrap_kds_s2000w_client_start_scan, 0); response* resp_status = kds_s2000w_client_response_init(); - resp_status->data = realloc(resp_status->data, 205); const char* status = "{ \ \"Status\": { \ \"NumImagesScanned\": 1, \ @@ -267,10 +265,10 @@ void sane_kds_s2000w_net_start_one_page_per_flatscan_test(void** state) \"LastError\": \"Status: 200 Success\", \ \"PaperDetected\": \"0\", \ \"PercentAvailable\": 99 \ - }}\0"; - memcpy(resp_status->data, status, 205); + }}"; + resp_status->data = strdup(status); resp_status->code = 200; - resp_status->size = 205; + resp_status->size = 204; will_return(mock_response, resp_status); will_return(__wrap_kds_s2000w_client_status_session, 0); will_return(mock_response, response_list[2]); @@ -305,7 +303,6 @@ void sane_kds_s2000w_net_start_one_page_per_feeder_test(void** state) will_return(mock_response, response_list[0]); will_return(__wrap_kds_s2000w_client_start_scan, 0); response* resp_status = kds_s2000w_client_response_init(); - resp_status->data = realloc(resp_status->data, 210); const char* status = "{ \ \"Status\": { \ \"NumImagesScanned\": 1, \ @@ -315,10 +312,10 @@ void sane_kds_s2000w_net_start_one_page_per_feeder_test(void** state) \"LastError\": \"Status: 200 Success\", \ \"PaperDetected\": \"0\", \ \"PercentAvailable\": 99 \ - }}\0"; - memcpy(resp_status->data, status, 210); + }}"; + resp_status->data = strdup(status); resp_status->code = 200; - resp_status->size = 210; + resp_status->size = 209; will_return(mock_response, resp_status); will_return(__wrap_kds_s2000w_client_status_session, 0); will_return(mock_response, response_list[2]); @@ -355,7 +352,6 @@ void sane_kds_s2000w_net_start_two_pages_per_feeder_test(void** state) will_return(mock_response, response_list[0]); will_return(__wrap_kds_s2000w_client_start_scan, 0); response* resp_status = kds_s2000w_client_response_init(); - resp_status->data = realloc(resp_status->data, 210); const char* status = "{ \ \"Status\": { \ \"NumImagesScanned\": 2, \ @@ -365,10 +361,10 @@ void sane_kds_s2000w_net_start_two_pages_per_feeder_test(void** state) \"LastError\": \"Status: 200 Success\", \ \"PaperDetected\": \"0\", \ \"PercentAvailable\": 99 \ - }}\0"; - memcpy(resp_status->data, status, 210); + }}"; + resp_status->data = strdup(status); resp_status->code = 200; - resp_status->size = 210; + resp_status->size = 209; will_return(mock_response, resp_status); will_return(__wrap_kds_s2000w_client_status_session, 0); will_return(mock_response, response_list[2]); @@ -399,9 +395,11 @@ void sane_kds_s2000w_net_start_two_pages_per_feeder_test(void** state) \"PaperDetected\": \"0\", \ \"PercentAvailable\": 99 \ }}\0"; - memcpy(resp_status->data, status2, 210); + free(resp_status->data); + resp_status->data = NULL; + resp_status->data = strdup(status2); resp_status->code = 200; - resp_status->size = 210; + resp_status->size = 209; will_return(mock_response, resp_status); will_return(__wrap_kds_s2000w_client_status_session, 0); will_return(mock_response, response_list[2]); @@ -430,7 +428,6 @@ void sane_kds_s2000w_net_start_two_pages_per_feeder_test(void** state) void sane_kds_s2000w_net_cancel_test() { response* resp_status = kds_s2000w_client_response_init(); - resp_status->data = realloc(resp_status->data, 205); const char* status = "{ \ \"Status\": { \ \"NumImagesScanned\": 2, \ @@ -440,10 +437,10 @@ void sane_kds_s2000w_net_cancel_test() \"LastError\": \"Status: 200 Success\", \ \"PaperDetected\": \"0\", \ \"PercentAvailable\": 99 \ - }}\0"; - memcpy(resp_status->data, status, 205); + }}"; + resp_status->data = strdup(status); resp_status->code = 200; - resp_status->size = 205; + resp_status->size = 204; will_return(mock_response, resp_status); will_return(__wrap_kds_s2000w_client_status_session, 0); response* resp = kds_s2000w_client_response_init(); @@ -484,9 +481,8 @@ void sane_kds_s2000w_net_cancel_start_test() void sane_kds_s2000w_net_cancel_canceled_test() { response* resp_status = kds_s2000w_client_response_init(); - resp_status->data = realloc(resp_status->data, 27); const char* status = "{\"SessionId\":\"1251877821\"}"; - memcpy(resp_status->data, status, 27); + resp_status->data = strdup(status); resp_status->code = 200; resp_status->size = 27; will_return(mock_response, resp_status); @@ -517,7 +513,6 @@ void sane_kds_s2000w_net_cancel_canceled_test() void sane_kds_s2000w_net_cancel_completed_scan_feeder_test() { response* resp_status = kds_s2000w_client_response_init(); - resp_status->data = realloc(resp_status->data, 210); const char* status = "{ \ \"Status\": { \ \"NumImagesScanned\": 2, \ @@ -527,10 +522,10 @@ void sane_kds_s2000w_net_cancel_completed_scan_feeder_test() \"LastError\": \"Status: 200 Success\", \ \"PaperDetected\": \"0\", \ \"PercentAvailable\": 99 \ - }}\0"; - memcpy(resp_status->data, status, 210); + }}"; + resp_status->data = strdup(status); resp_status->code = 200; - resp_status->size = 210; + resp_status->size = 209; will_return(mock_response, resp_status); will_return(__wrap_kds_s2000w_client_status_session, 0); expect_function_call(__wrap_kds_s2000w_client_status_session); @@ -551,7 +546,6 @@ void sane_kds_s2000w_net_cancel_completed_scan_feeder_test() void sane_kds_s2000w_net_cancel_completed_scan_flatbed_test() { response* resp_status = kds_s2000w_client_response_init(); - resp_status->data = realloc(resp_status->data, 205); const char* status = "{ \ \"Status\": { \ \"NumImagesScanned\": 2, \ @@ -561,16 +555,15 @@ void sane_kds_s2000w_net_cancel_completed_scan_flatbed_test() \"LastError\": \"Status: 200 Success\", \ \"PaperDetected\": \"0\", \ \"PercentAvailable\": 99 \ - }}\0"; - memcpy(resp_status->data, status, 205); + }}"; + resp_status->data = strdup(status); resp_status->code = 200; - resp_status->size = 205; + resp_status->size = 204; will_return(mock_response, resp_status); will_return(__wrap_kds_s2000w_client_status_session, 0); response* resp_session = kds_s2000w_client_response_init(); - resp_session->data = realloc(resp_session->data, 27); const char* responsedata = "{\"SessionId\":\"1251877821\"}"; - memcpy(resp_session->data, responsedata, 27); + resp_session->data = strdup(responsedata); resp_session->code = 200; resp_session->size = 27; will_return(mock_response, resp_session); diff --git a/tests/kds_s2000w_net_tests_run.c b/tests/kds_s2000w_net_tests_run.c index 665bcf3..e838695 100644 --- a/tests/kds_s2000w_net_tests_run.c +++ b/tests/kds_s2000w_net_tests_run.c @@ -9,7 +9,6 @@ int setup(void** state) response_list[0] = kds_s2000w_client_response_init(); response_list[0]->code = 200; response_list[1] = kds_s2000w_client_response_init(); - response_list[1]->data = realloc(response_list[1]->data, 107); const char* metadata = "{\ \"Metadata\": {\ \"BitDepth\": 8, \ @@ -17,14 +16,14 @@ int setup(void** state) \"ImageWidth\": 1920, \ \"ImageHeight\": 1080, \ }\ - }\0"; - memcpy(response_list[1]->data, metadata, 107); + }"; + response_list[1]->data = strdup(metadata); response_list[1]->code = 200; - response_list[1]->size = 107; + response_list[1]->size = 106; response_list[2] = kds_s2000w_client_response_init(); response_list[2]->code = 200; response_list[2]->size = 17; - response_list[2]->data = realloc(response_list[2]->data, 17); + response_list[2]->data = malloc(sizeof(char) * 17); char* image_data = (char*) response_list[2]->data; image_data[0] = 0x50; image_data[1] = 0x36; -- 2.39.5