tests: logutil.c divided into positive and negative 65/232465/2
authorAgnieszka Baumann <a.baumann@samsung.com>
Thu, 30 Apr 2020 16:32:32 +0000 (18:32 +0200)
committerMichal Bloch <m.bloch@partner.samsung.com>
Thu, 21 May 2020 13:25:03 +0000 (13:25 +0000)
Change-Id: I2f0c1078939005c8f95ec1cf36b2426d70347bba

Makefile.am
src/tests/logutil_neg.c [new file with mode: 0644]
src/tests/logutil_pos.c [new file with mode: 0644]
src/tests/logutil_wrap.c [new file with mode: 0644]

index 95e7636..98d93f7 100644 (file)
@@ -294,7 +294,8 @@ check_PROGRAMS = \
        src/tests/logctl \
        src/tests/logprint \
        src/tests/logger \
-       src/tests/logutil \
+       src/tests/logutil_pos \
+       src/tests/logutil_neg \
        src/tests/critical_log \
        src/tests/salvage_pipe_entry \
        src/tests/filters
@@ -441,7 +442,7 @@ src_tests_logger_SOURCES = src/tests/logger.c $(dlog_logger_SOURCES)
 src_tests_logger_CFLAGS = $(check_CFLAGS)
 src_tests_logger_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=getgrnam_r,--wrap=getpwnam_r,--wrap=getegid,--wrap=geteuid,--wrap=setgid,--wrap=setuid,--wrap=socket,--wrap=unlink,--wrap=bind,--wrap=close,--wrap=chmod,--wrap=listen,--wrap=sysconf,--wrap=sd_listen_fds,--wrap=sd_is_socket_unix,--wrap=symlink,--wrap=calloc,--wrap=open,--wrap=open64,--wrap=fcntl,--wrap=fcntl64,--wrap=log_storage_reader_get_ready_bytes,--wrap=free,--wrap=logfile_free,--wrap=log_storage_add_new_entry,--wrap=log_storage_release_reader,--wrap=epoll_ctl,--wrap=log_storage_new_reader
 
-src_tests_logutil_SOURCES = src/tests/logutil.c \
+src_tests_logutil_pos_SOURCES = src/tests/logutil_pos.c \
        src/libdlogutil/privilege.c \
        src/libdlogutil/sort_vector.c \
        src/libdlogutil/fd_info.c \
@@ -459,8 +460,29 @@ src_tests_logutil_SOURCES = src/tests/logutil.c \
        src/shared/ptrs_list.c \
        src/shared/logconfig.c \
        src/libdlogutil/logretrieve.c
-src_tests_logutil_CFLAGS = $(check_CFLAGS)
-src_tests_logutil_LDFLAGS = $(AM_LDFLAGS) -lcap -Wl,--wrap=clock_gettime,--wrap=log_should_print_line,--wrap=fdi_push_log
+src_tests_logutil_pos_CFLAGS = $(check_CFLAGS)
+src_tests_logutil_pos_LDFLAGS = $(AM_LDFLAGS) -lcap -Wl,--wrap=clock_gettime,--wrap=log_should_print_line,--wrap=fdi_push_log
+
+src_tests_logutil_neg_SOURCES = src/tests/logutil_neg.c \
+       src/libdlogutil/privilege.c \
+       src/libdlogutil/sort_vector.c \
+       src/libdlogutil/fd_info.c \
+       src/libdlogutil/fdi_pipe.c \
+       src/libdlogutil/fdi_logger.c \
+       src/libdlogutil/lib.c \
+       src/shared/backend_androidlogger.c \
+       src/shared/logcommon.c \
+       src/shared/logprint.c \
+       src/shared/parsers.c \
+       src/shared/queued_entry.c \
+       src/shared/queued_entry_timestamp.c \
+       src/shared/translate_syslog.c \
+       src/shared/log_file.c \
+       src/shared/ptrs_list.c \
+       src/shared/logconfig.c \
+       src/libdlogutil/logretrieve.c
+src_tests_logutil_neg_CFLAGS = $(check_CFLAGS)
+src_tests_logutil_neg_LDFLAGS = $(AM_LDFLAGS) -lcap -Wl,--wrap=clock_gettime,--wrap=log_should_print_line,--wrap=fdi_push_log
 
 src_tests_kmsg_parser_pos_SOURCES = src/tests/kmsg_parser_pos.c \
        src/shared/logcommon.c \
diff --git a/src/tests/logutil_neg.c b/src/tests/logutil_neg.c
new file mode 100644 (file)
index 0000000..f5e3261
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2019-2020, Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "logutil_wrap.c"
+
+int main()
+{
+       struct sort_vector sv;
+       sort_vector_init(&sv);
+       sv.sort_by = DLOGUTIL_SORT_SENT_MONO;
+       sv.timeout = 60;
+       sv.dump = DLOGUTIL_MODE_CONTINUOUS;
+       sv.size = DEFAULT_SORT_BUFFER_SIZE;
+       sort_vector_finalize(&sv);
+       assert(sort_vector_time_span(&sv) == 0);
+
+       struct fd_ops test_ops = {
+               .has_log = test_has_log,
+               .peek_entry = test_peek_entry,
+       };
+
+       struct fd_info fd_stor[10] = {};
+       struct fdi_internal ii_stor[10] = {};
+       dlogutil_entry_s ent_stor[10 - 3] = {};
+       for (int i = 0; i < 10; ++i) {
+               if (i < 3)
+                       ii_stor[i].has_log = false;
+               else {
+                       ent_stor[i - 3].sec_sent_mono = i % 2;
+                       ent_stor[i - 3].nsec_sent_mono = i;
+
+                       ii_stor[i].has_log = true;
+                       ii_stor[i].ent = &ent_stor[i - 3];
+               }
+
+               int t = i * 3 % 10;
+               fd_stor[i].ops = &test_ops;
+               fd_stor[i].priv_data = &ii_stor[t];
+       }
+
+       struct fd_info *fds[10] = {};
+       for (int i = 0; i < 10; ++i) {
+               fds[i] = &fd_stor[i];
+       }
+
+       dlogutil_entry_s *vector_overflown = NULL;
+       bool need_epoll = false, all_drained = false;
+       fdi_push_log_ret = -EIO;
+       assert(put_logs_into_vector(fds, 10, &sv, (dlogutil_filter_options_s *)0xEEE, &need_epoll, &all_drained, &vector_overflown) == -EIO);
+       assert(!vector_overflown);
+
+       sort_vector_free(&sv);
+}
diff --git a/src/tests/logutil_pos.c b/src/tests/logutil_pos.c
new file mode 100644 (file)
index 0000000..6a39ee4
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2019-2020, Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "logutil_wrap.c"
+
+int main()
+{
+       dlogutil_entry_s ent = {
+               .sec_sent_mono = 0,
+               .nsec_sent_mono = 900000000,
+               .sec_sent_real = 0,
+               .nsec_sent_real = 0,
+               .sec_recv_mono = 1,
+               .nsec_recv_mono = 0,
+               .sec_recv_real = 0,
+               .nsec_recv_real = 999999999,
+       };
+
+       struct timespec ts = {
+               .tv_sec = 1,
+               .tv_nsec = 100000,
+       };
+
+       struct sort_vector sv;
+       sort_vector_init(&sv);
+       sv.sort_by = DLOGUTIL_SORT_SENT_MONO;
+       sv.timeout = 60;
+       sv.dump = DLOGUTIL_MODE_CONTINUOUS;
+       sv.size = DEFAULT_SORT_BUFFER_SIZE;
+       sort_vector_finalize(&sv);
+       assert(sort_vector_time_span(&sv) == 0);
+
+       for (int i = 0; i < 10; ++i) {
+               dlogutil_entry_s *ent2 = calloc(1, sizeof(dlogutil_entry_s));
+               assert(ent2);
+               ent2->sec_sent_mono = 1;
+               ent2->nsec_sent_mono = (79 + i) * 10000000;
+               sort_vector_push(&sv, ent2);
+       }
+
+       assert(sort_vector_time_span(&sv) == 110);
+
+       dlogutil_entry_s *ent3 = calloc(1, sizeof(dlogutil_entry_s));
+       assert(ent3);
+       ent3->sec_sent_mono = 0;
+       ent3->nsec_sent_mono = 999999999;
+       sort_vector_push(&sv, ent3);
+       assert(sort_vector_time_span(&sv) == 900);
+
+       struct fd_ops test_ops = {
+               .has_log = test_has_log,
+               .peek_entry = test_peek_entry,
+       };
+
+       struct fd_info fd_stor[10] = {};
+       struct fdi_internal ii_stor[10] = {};
+       dlogutil_entry_s ent_stor[10 - 3] = {};
+       for (int i = 0; i < 10; ++i) {
+               if (i < 3)
+                       ii_stor[i].has_log = false;
+               else {
+                       ent_stor[i - 3].sec_sent_mono = i % 2;
+                       ent_stor[i - 3].nsec_sent_mono = i;
+
+                       ii_stor[i].has_log = true;
+                       ii_stor[i].ent = &ent_stor[i - 3];
+               }
+
+               int t = i * 3 % 10;
+               fd_stor[i].ops = &test_ops;
+               fd_stor[i].priv_data = &ii_stor[t];
+       }
+
+       struct fd_info *fds[10] = {};
+       for (int i = 0; i < 10; ++i) {
+               fds[i] = &fd_stor[i];
+       }
+       assert(!find_earliest_log(fds, 0, DLOGUTIL_SORT_SENT_MONO));
+       assert(find_earliest_log(fds, 10, DLOGUTIL_SORT_SENT_MONO) == fds[8]);
+
+       while (!sort_vector_empty(&sv))
+               sort_vector_pop(&sv);
+
+       dlogutil_entry_s *vector_overflown = NULL;
+       bool need_epoll = false, all_drained = false;
+       assert(put_logs_into_vector(fds, 0, &sv, (dlogutil_filter_options_s *)0xEEE, &need_epoll, &all_drained, &vector_overflown) == 0);
+       assert(!vector_overflown);
+       assert(all_drained);
+       assert(need_epoll);
+
+       fdi_push_log_ret = 0;
+       all_drained = false;
+       need_epoll = false;
+       assert(put_logs_into_vector(fds, 10, &sv, (dlogutil_filter_options_s *)0xEEE, &need_epoll, &all_drained, &vector_overflown) == 0);
+       assert(!vector_overflown);
+       assert(sort_vector_used_size(&sv) == 1);
+       assert(!all_drained);
+       assert(need_epoll);
+
+       sort_vector_free(&sv);
+}
diff --git a/src/tests/logutil_wrap.c b/src/tests/logutil_wrap.c
new file mode 100644 (file)
index 0000000..06918f7
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2019-2020, Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdbool.h>
+
+#include <queued_entry.h>
+#include <log_file.h>
+#include <dlog-internal.h>
+#include <sort_vector.h>
+#include <fd_info.h>
+
+bool process_log(const dlogutil_entry_s *e, struct timespec reference_ts, dlogutil_sorting_order_e stamp_type, long timeout, int *callback_ret);
+struct fd_info *find_earliest_log(struct fd_info **data_fds, int fd_count, dlogutil_sorting_order_e sort_by);
+int put_logs_into_vector(struct fd_info **data_fds, int fd_count, struct sort_vector *logs, dlogutil_filter_options_s *filter, bool *need_epoll, bool *any_drained, dlogutil_entry_s **entry_out);
+
+int __wrap_clock_gettime(clockid_t clk_id, struct timespec *tp)
+{
+       assert(clk_id == CLOCK_MONOTONIC);
+
+       tp->tv_sec  = 1;
+       tp->tv_nsec = 900000000;
+       return 0;
+}
+
+bool __wrap_log_should_print_line(dlogutil_filter_options_s *p_filter, const dlogutil_entry_s *entry)
+{
+       return true;
+}
+
+struct fdi_internal
+{
+       bool has_log;
+       const dlogutil_entry_s *ent;
+};
+
+bool test_has_log(struct fd_info *fdi)
+{
+       struct fdi_internal *ii = fdi->priv_data;
+       return ii->has_log;
+}
+
+const dlogutil_entry_s *test_peek_entry(const struct fd_info *fdi)
+{
+       struct fdi_internal *ii = fdi->priv_data;
+       return ii->ent;
+}
+
+int fdi_push_log_ret = 0;
+int __wrap_fdi_push_log(struct fd_info *fdi, struct sort_vector *logs, dlogutil_entry_s **entry_out, dlogutil_filter_options_s *filter)
+{
+       assert(filter == (dlogutil_filter_options_s *)0xEEE);
+       if (fdi_push_log_ret != 0)
+               return fdi_push_log_ret;
+       dlogutil_entry_s *ent = calloc(1, sizeof(dlogutil_entry_s));
+       assert(ent);
+       memcpy(ent, fdi->ops->peek_entry(fdi), sizeof(dlogutil_entry_s));
+       struct fdi_internal *ii = fdi->priv_data;
+       ii->has_log = false;
+
+       *entry_out = NULL;
+       if (sort_vector_full(logs)) {
+               if (logs->dump == DLOGUTIL_MODE_CONTINUOUS || logs->dump == DLOGUTIL_MAX_DUMP_SIZE)
+                       *entry_out = sort_vector_pop_ret(logs);
+               else
+                       sort_vector_pop(logs);
+       }
+       sort_vector_push(logs, ent);
+       return 0;
+}