Added deep_copy_dlogutil_entry_s() function and tests for it. 69/262369/11
authorHubert Kowalski <h.kowalski@samsung.com>
Mon, 9 Aug 2021 06:54:59 +0000 (08:54 +0200)
committerHubert Kowalski <h.kowalski@samsung.com>
Thu, 12 Aug 2021 07:55:53 +0000 (09:55 +0200)
The deep_copy_dlogutil_entry_s function was made in effort
to create abstraction layer. Thus make code more readable.

Change-Id: If48246c3aefda46d77cbfbfb66337b7b18787da6

include/queued_entry.h
src/shared/queued_entry.c
src/tests/queued_entry_pos.c

index a130be5..e1a33f0 100644 (file)
@@ -53,7 +53,7 @@ typedef struct dlogutil_entry {
        int32_t     sec_recv_real;  /* time received, seconds, realtime */
        int32_t     nsec_recv_real; /* time received, nanoseconds, realtime*/
        int32_t     tag_len;
-       char        msg[];  /* the entry's payload */
+       char        msg[];  /* the entry's payload. Contains tag and message separated by \0 ie: "tag\0message\0". Has variable length, which is contained in 'len' field of this struct*/
 } dlogutil_entry_s;
 
 struct dlogutil_entry_with_msg {
@@ -129,6 +129,7 @@ int parse_kmsg_message(char *buffer, struct dlogutil_entry_with_msg *lem, int bu
 int parse_syslog_datagram(const char * buffer, int buffer_size, dlogutil_entry_s * le);
 void parse_androidlogger_message(struct android_logger_entry *ale, dlogutil_entry_s *le, size_t dgram_size);
 void parse_pipe_message(struct pipe_logger_entry *ple, dlogutil_entry_s *le, size_t msg_size);
+void deep_copy_dlogutil_entry_s(dlogutil_entry_s *dest, const dlogutil_entry_s *source);
 
 #ifdef __cplusplus
 }
index a8c3da7..3aaa9ba 100644 (file)
@@ -423,5 +423,17 @@ int parse_syslog_datagram(const char *buffer, int buffer_size, dlogutil_entry_s
 }
 
 /**
+ * @brief This function makes a deep copy of dlogutil_entry_s.
+ * @details In order to work properly dest, has to have sufficient allocated memory and source has to have a correct len field.
+ * @param[out] dest Struct to which data is copied
+ * @param[in] source Struct from which data is copied
+ */
+void deep_copy_dlogutil_entry_s(dlogutil_entry_s *dest, const dlogutil_entry_s *source)
+{
+       assert(dest);
+       assert(source);
+       memcpy(dest, source, sizeof(dlogutil_entry_s)+source->len);
+}
+/**
  * @}
  */
index 6df3ac5..83b29fc 100644 (file)
 #include <queued_entry.h>
 #include <queued_entry_timestamp.h>
 
+
+/**
+ * @brief This function tests deep_copy_dlogutil_entry_s function
+ */
+void check_deep_copy(void)
+{
+       char test_data[] = "tag\0important message";
+       dlogutil_entry_s *source = calloc(1, sizeof(*source) + sizeof test_data);
+       assert(source);
+
+       //setting up struct
+       source->len = sizeof test_data;
+       source->priority = DLOG_DEBUG;
+       source->padding = 16;
+       source->pid = 17;
+       source->tid = 18;
+       source->sec_sent_mono = 12;
+       source->nsec_sent_mono = 1212;
+       source->sec_sent_real = 13;
+       source->nsec_sent_real = 1313;
+       source->sec_recv_mono = 14;
+       source->nsec_recv_mono = 1414;
+       source->sec_recv_real = 15;
+       source->nsec_recv_real = 1515;
+       source->tag_len = strlen(test_data);
+       memcpy(source->msg, test_data, source->len);
+
+       //creating new struct
+       dlogutil_entry_s *dest = calloc(1, sizeof(*dest) + sizeof test_data);
+
+       //copying
+       deep_copy_dlogutil_entry_s(dest, source);
+
+       //check if pointer isn't just copied
+       assert(source != dest);
+
+       //check if internal fields match
+       assert(dest->len == source->len);
+       assert(dest->priority == source->priority);
+       assert(dest->padding == source->padding);
+       assert(dest->pid == source->pid);
+       assert(dest->tid == source->tid);
+       assert(dest->sec_sent_mono == source->sec_sent_mono);
+       assert(dest->nsec_sent_mono == source->nsec_sent_mono);
+       assert(dest->sec_sent_real == source->sec_sent_real);
+       assert(dest->nsec_sent_real == source->nsec_sent_real);
+       assert(dest->sec_recv_mono == source->sec_recv_mono);
+       assert(dest->nsec_recv_mono == source->nsec_recv_mono);
+       assert(dest->sec_recv_real == source->sec_recv_real);
+       assert(dest->nsec_recv_real == source->nsec_recv_real);
+       assert(dest->tag_len == source->tag_len);
+
+       //check if payload pinter isn't just copied
+       assert(dest->msg != source->msg);
+       assert(!memcmp(dest->msg,source->msg,source->len));
+
+       free(source);
+       free(dest);
+}
+
 int main(void)
 {
+       check_deep_copy();
        assert(get_proper_clock(DLOGUTIL_SORT_SENT_MONO) == CLOCK_MONOTONIC);
        assert(get_proper_clock(DLOGUTIL_SORT_SENT_REAL) == CLOCK_REALTIME);
        assert(get_proper_clock(DLOGUTIL_SORT_RECV_MONO) == CLOCK_MONOTONIC);