Add some libdlogutil negative tests 07/224307/2
authorMateusz Majewski <m.majewski2@samsung.com>
Mon, 27 Jan 2020 13:46:43 +0000 (14:46 +0100)
committerMateusz Majewski <m.majewski2@samsung.com>
Mon, 10 Feb 2020 07:22:18 +0000 (08:22 +0100)
Change-Id: I7fc0aef2e522ae1a997399fb318b448a55c0a237

tests/dlog_test.in
tests/test_libdlogutil.c

index d6ddb99..d6dd703 100644 (file)
@@ -373,6 +373,8 @@ LOG_DETAILS="testing if libdlogutil works correctly with prefix filtering (2/2)"
 test_libdlogutil prefix_wrong $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
 LOG_DETAILS="testing if libdlogutil propagates the callback return value correctly"
 test_libdlogutil return $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
+LOG_DETAILS="testing if libdlogutil errors out correctly if used improperly"
+test_libdlogutil negative $LIBDLOGUTIL_CORRECT_PID $type && ok || fail
 
 LOG_DETAILS="testing if libdlogutil works correctly in continuous mode"
 test_libdlogutil continuous $LIBDLOGUTIL_CORRECT_PID $type &
index a070789..8d47eca 100644 (file)
@@ -192,15 +192,86 @@ void traits_main(bool pipe)
        assert(available);
 }
 
-int clear_callback(const dlogutil_entry_s *ent, void *var)
+int failing_callback(const dlogutil_entry_s *ent, void *var)
 {
+       assert(false);
        return 1;
 }
 
 void clear_main()
 {
        assert(dlogutil_buffer_clear(DLOGUTIL_BUF_MAIN) == 0);
-       assert(dlogutil_get_logs_dump(DLOGUTIL_BUF_MAIN, DLOGUTIL_MAX_DUMP_SIZE, clear_callback, NULL, NULL, NULL) == 0);
+       assert(dlogutil_get_logs_dump(DLOGUTIL_BUF_MAIN, DLOGUTIL_MAX_DUMP_SIZE, failing_callback, NULL, NULL, NULL) == 0);
+}
+
+void negative_main()
+{
+       void *bad_ptr = (void *)0xFA1l;
+
+       // NULL callback
+       assert(dlogutil_get_logs_continuous(DLOGUTIL_BUF_KMSG, NULL, NULL, NULL, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_get_logs_dump(DLOGUTIL_BUF_KMSG, DLOGUTIL_MAX_DUMP_SIZE, NULL, NULL, NULL, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+
+       // Invalid buffer combination
+       assert(dlogutil_get_logs_continuous(0, failing_callback, NULL, NULL, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_get_logs_dump(0, DLOGUTIL_MAX_DUMP_SIZE, failing_callback, NULL, NULL, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_get_logs_continuous(DLOGUTIL_BUF_MAIN | 1 << 16, failing_callback, NULL, NULL, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_get_logs_dump(DLOGUTIL_BUF_MAIN | 1 << 16, DLOGUTIL_MAX_DUMP_SIZE, failing_callback, NULL, NULL, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_get_logs_continuous(DLOGUTIL_BUF_MAIN | DLOGUTIL_BUF_KMSG, failing_callback, NULL, NULL, NULL) == TIZEN_ERROR_NOT_SUPPORTED);
+       assert(dlogutil_get_logs_dump(DLOGUTIL_BUF_MAIN | DLOGUTIL_BUF_KMSG, DLOGUTIL_MAX_DUMP_SIZE, failing_callback, NULL, NULL, NULL) == TIZEN_ERROR_NOT_SUPPORTED);
+
+       // Invalid single buffer
+       assert(dlogutil_buffer_clear(0) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_get_name(0, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_get_size(0, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_get_default_ts_type(0, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_check_ts_type_available(0, DLOGUTIL_SORT_RECV_MONO, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_clear(1 << 16) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_get_name(1 << 16, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_get_size(1 << 16, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_get_default_ts_type(1 << 16, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_check_ts_type_available(1 << 16, DLOGUTIL_SORT_RECV_MONO, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_clear(DLOGUTIL_BUF_MAIN | DLOGUTIL_BUF_KMSG) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_get_name(DLOGUTIL_BUF_MAIN | DLOGUTIL_BUF_KMSG, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_get_size(DLOGUTIL_BUF_MAIN | DLOGUTIL_BUF_KMSG, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_get_default_ts_type(DLOGUTIL_BUF_MAIN | DLOGUTIL_BUF_KMSG, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_check_ts_type_available(DLOGUTIL_BUF_MAIN | DLOGUTIL_BUF_KMSG, DLOGUTIL_SORT_RECV_MONO, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+
+       // Retrieval of data from NULL
+       assert(dlogutil_entry_get_tid(NULL, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_entry_get_pid(NULL, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_entry_get_priority(NULL, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_entry_get_timestamp(NULL, DLOGUTIL_SORT_RECV_MONO, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_entry_get_tag(NULL, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_entry_get_message(NULL, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+
+       // Retrieval of data into NULL
+       assert(dlogutil_entry_get_tid(bad_ptr, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_entry_get_pid(bad_ptr, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_entry_get_priority(bad_ptr, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_entry_get_timestamp(bad_ptr, DLOGUTIL_SORT_RECV_MONO, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_entry_get_tag(bad_ptr, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_entry_get_message(bad_ptr, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_get_name(DLOGUTIL_BUF_MAIN, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_get_size(DLOGUTIL_BUF_MAIN, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_get_default_ts_type(DLOGUTIL_BUF_MAIN, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_buffer_check_ts_type_available(DLOGUTIL_BUF_MAIN, DLOGUTIL_SORT_RECV_MONO, NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+
+       // Wrong timestamp
+       assert(dlogutil_entry_get_timestamp(bad_ptr, 1337, bad_ptr) == TIZEN_ERROR_INVALID_PARAMETER);
+
+       // NULL options
+       assert(dlogutil_filter_options_set_tid(NULL, 0) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_filter_options_set_pid(NULL, 0) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_filter_options_set_filterspec(NULL, "TIZEN") == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_sorting_options_disable_sorting(NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_sorting_options_enable_sorting(NULL) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_sorting_options_enable_sorting_with_size(NULL, 7) == TIZEN_ERROR_INVALID_PARAMETER);
+       assert(dlogutil_sorting_options_set_sort_by(NULL, DLOGUTIL_SORT_RECV_MONO) == TIZEN_ERROR_INVALID_PARAMETER);
+
+       // Destroying a NULL - no error, but doesn't crash either
+       dlogutil_filter_options_destroy(NULL);
+       dlogutil_sorting_options_destroy(NULL);
 }
 
 enum test_mode {
@@ -221,6 +292,7 @@ enum test_mode {
        MODE_SORTING,
        MODE_TRAITS,
        MODE_CLEAR,
+       MODE_NEGATIVE,
        MODE_INVALID,
 };
 
@@ -242,6 +314,7 @@ const char *mode_names[] = {
        [MODE_SORTING] = "sorting",
        [MODE_TRAITS] = "traits",
        [MODE_CLEAR] = "clear",
+       [MODE_NEGATIVE] = "negative",
 };
 
 const char *mode_desc[] = {
@@ -262,6 +335,7 @@ const char *mode_desc[] = {
        [MODE_SORTING] = "test timestamp sorting -- in this case, the PID is ignored",
        [MODE_TRAITS] = "test buffer traits functionality -- in this case, the PID is ignored",
        [MODE_CLEAR] = "test clearing functionality -- in this case, the PID is ignored",
+       [MODE_NEGATIVE] = "test whether libdlogutil errors out correctly",
 };
 
 void usage(const char *name)
@@ -308,6 +382,9 @@ int main(int argc, char **argv)
        } else if (t == MODE_CLEAR) {
                clear_main();
                return EXIT_SUCCESS;
+       } else if (t == MODE_NEGATIVE) {
+               negative_main();
+               return EXIT_SUCCESS;
        }
 
        struct test_data data = { .correct_tid_pid = atoi(argv[2]), .sleep = t == MODE_TIMER };