diff options
Diffstat (limited to 'logcat/tests/logcat_test.cpp')
-rw-r--r-- | logcat/tests/logcat_test.cpp | 272 |
1 files changed, 159 insertions, 113 deletions
diff --git a/logcat/tests/logcat_test.cpp b/logcat/tests/logcat_test.cpp index b07cc8b..b1412ff 100644 --- a/logcat/tests/logcat_test.cpp +++ b/logcat/tests/logcat_test.cpp @@ -17,6 +17,7 @@ #include <ctype.h> #include <signal.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <gtest/gtest.h> @@ -41,99 +42,6 @@ static const char begin[] = "--------- beginning of "; -TEST(logcat, sorted_order) { - FILE *fp; - - ASSERT_TRUE(NULL != (fp = popen( - "logcat -v time -b radio -b events -b system -b main -d 2>/dev/null", - "r"))); - - class timestamp { - private: - int month; - int day; - int hour; - int minute; - int second; - int millisecond; - bool ok; - - public: - void init(const char *buffer) - { - ok = false; - if (buffer != NULL) { - ok = sscanf(buffer, "%d-%d %d:%d:%d.%d ", - &month, &day, &hour, &minute, &second, &millisecond) == 6; - } - } - - timestamp(const char *buffer) - { - init(buffer); - } - - bool operator< (timestamp &T) - { - return !ok || !T.ok - || (month < T.month) - || ((month == T.month) - && ((day < T.day) - || ((day == T.day) - && ((hour < T.hour) - || ((hour == T.hour) - && ((minute < T.minute) - || ((minute == T.minute) - && ((second < T.second) - || ((second == T.second) - && (millisecond < T.millisecond)))))))))); - } - - bool valid(void) - { - return ok; - } - } last(NULL); - - char *last_buffer = NULL; - char buffer[5120]; - - int count = 0; - int next_lt_last = 0; - - while (fgets(buffer, sizeof(buffer), fp)) { - if (!strncmp(begin, buffer, sizeof(begin) - 1)) { - continue; - } - if (!last.valid()) { - free(last_buffer); - last_buffer = strdup(buffer); - last.init(buffer); - } - timestamp next(buffer); - if (next < last) { - if (last_buffer) { - fprintf(stderr, "<%s", last_buffer); - } - fprintf(stderr, ">%s", buffer); - ++next_lt_last; - } - if (next.valid()) { - free(last_buffer); - last_buffer = strdup(buffer); - last.init(buffer); - } - ++count; - } - free(last_buffer); - - pclose(fp); - - EXPECT_EQ(0, next_lt_last); - - EXPECT_LT(100, count); -} - TEST(logcat, buckets) { FILE *fp; @@ -335,7 +243,7 @@ TEST(logcat, End_to_End) { FILE *fp; ASSERT_TRUE(NULL != (fp = popen( - "logcat -b events -t 100 2>/dev/null", + "logcat -v brief -b events -t 100 2>/dev/null", "r"))); char buffer[5120]; @@ -362,11 +270,12 @@ TEST(logcat, End_to_End) { ASSERT_EQ(1, count); } -TEST(logcat, get_) { +TEST(logcat, get_size) { FILE *fp; + // NB: crash log only available in user space ASSERT_TRUE(NULL != (fp = popen( - "logcat -b radio -b events -b system -b main -g 2>/dev/null", + "logcat -v brief -b radio -b events -b system -b main -g 2>/dev/null", "r"))); char buffer[5120]; @@ -375,13 +284,53 @@ TEST(logcat, get_) { while (fgets(buffer, sizeof(buffer), fp)) { int size, consumed, max, payload; + char size_mult[2], consumed_mult[2]; + long full_size, full_consumed; size = consumed = max = payload = 0; - if ((4 == sscanf(buffer, "%*s ring buffer is %dKb (%dKb consumed)," - " max entry is %db, max payload is %db", - &size, &consumed, &max, &payload)) - && ((size * 3) >= consumed) - && ((size * 1024) > max) + // NB: crash log can be very small, not hit a Kb of consumed space + // doubly lucky we are not including it. + if (6 != sscanf(buffer, "%*s ring buffer is %d%2s (%d%2s consumed)," + " max entry is %db, max payload is %db", + &size, size_mult, &consumed, consumed_mult, + &max, &payload)) { + fprintf(stderr, "WARNING: Parse error: %s", buffer); + continue; + } + full_size = size; + switch(size_mult[0]) { + case 'G': + full_size *= 1024; + /* FALLTHRU */ + case 'M': + full_size *= 1024; + /* FALLTHRU */ + case 'K': + full_size *= 1024; + /* FALLTHRU */ + case 'b': + break; + } + full_consumed = consumed; + switch(consumed_mult[0]) { + case 'G': + full_consumed *= 1024; + /* FALLTHRU */ + case 'M': + full_consumed *= 1024; + /* FALLTHRU */ + case 'K': + full_consumed *= 1024; + /* FALLTHRU */ + case 'b': + break; + } + EXPECT_GT((full_size * 9) / 4, full_consumed); + EXPECT_GT(full_size, max); + EXPECT_GT(max, payload); + + if ((((full_size * 9) / 4) >= full_consumed) + && (full_size > max) && (max > payload)) { ++count; } @@ -392,7 +341,7 @@ TEST(logcat, get_) { ASSERT_EQ(4, count); } -static void caught_blocking(int signum) +static void caught_blocking(int /*signum*/) { unsigned long long v = 0xDEADBEEFA55A0000ULL; @@ -415,7 +364,7 @@ TEST(logcat, blocking) { ASSERT_TRUE(NULL != (fp = popen( "( trap exit HUP QUIT INT PIPE KILL ; sleep 6; echo DONE )&" - " logcat -b events 2>&1", + " logcat -v brief -b events 2>&1", "r"))); char buffer[5120]; @@ -461,7 +410,7 @@ TEST(logcat, blocking) { EXPECT_EQ(1, signals); } -static void caught_blocking_tail(int signum) +static void caught_blocking_tail(int /*signum*/) { unsigned long long v = 0xA55ADEADBEEF0000ULL; @@ -484,7 +433,7 @@ TEST(logcat, blocking_tail) { ASSERT_TRUE(NULL != (fp = popen( "( trap exit HUP QUIT INT PIPE KILL ; sleep 6; echo DONE )&" - " logcat -b events -T 5 2>&1", + " logcat -v brief -b events -T 5 2>&1", "r"))); char buffer[5120]; @@ -532,7 +481,104 @@ TEST(logcat, blocking_tail) { EXPECT_EQ(1, signals); } -static void caught_blocking_clear(int signum) +TEST(logcat, logrotate) { + static const char form[] = "/data/local/tmp/logcat.logrotate.XXXXXX"; + char buf[sizeof(form)]; + ASSERT_TRUE(NULL != mkdtemp(strcpy(buf, form))); + + static const char comm[] = "logcat -b radio -b events -b system -b main" + " -d -f %s/log.txt -n 7 -r 1"; + char command[sizeof(buf) + sizeof(comm)]; + sprintf(command, comm, buf); + + int ret; + EXPECT_FALSE((ret = system(command))); + if (!ret) { + sprintf(command, "ls -s %s 2>/dev/null", buf); + + FILE *fp; + EXPECT_TRUE(NULL != (fp = popen(command, "r"))); + if (fp) { + char buffer[5120]; + int count = 0; + + while (fgets(buffer, sizeof(buffer), fp)) { + static const char match_1[] = "4 log.txt"; + static const char match_2[] = "8 log.txt"; + static const char match_3[] = "16 log.txt"; + static const char total[] = "total "; + + if (!strncmp(buffer, match_1, sizeof(match_1) - 1) + || !strncmp(buffer, match_2, sizeof(match_2) - 1) + || !strncmp(buffer, match_3, sizeof(match_3) - 1)) { + ++count; + } else if (strncmp(buffer, total, sizeof(total) - 1)) { + fprintf(stderr, "WARNING: Parse error: %s", buffer); + } + } + pclose(fp); + EXPECT_TRUE(count == 7 || count == 8); + } + } + sprintf(command, "rm -rf %s", buf); + EXPECT_FALSE(system(command)); +} + +TEST(logcat, logrotate_suffix) { + static const char tmp_out_dir_form[] = "/data/local/tmp/logcat.logrotate.XXXXXX"; + char tmp_out_dir[sizeof(tmp_out_dir_form)]; + ASSERT_TRUE(NULL != mkdtemp(strcpy(tmp_out_dir, tmp_out_dir_form))); + + static const char logcat_cmd[] = "logcat -b radio -b events -b system -b main" + " -d -f %s/log.txt -n 10 -r 1"; + char command[sizeof(tmp_out_dir) + sizeof(logcat_cmd)]; + sprintf(command, logcat_cmd, tmp_out_dir); + + int ret; + EXPECT_FALSE((ret = system(command))); + if (!ret) { + sprintf(command, "ls %s 2>/dev/null", tmp_out_dir); + + FILE *fp; + EXPECT_TRUE(NULL != (fp = popen(command, "r"))); + char buffer[5120]; + int log_file_count = 0; + + while (fgets(buffer, sizeof(buffer), fp)) { + static const char rotated_log_filename_prefix[] = "log.txt."; + static const size_t rotated_log_filename_prefix_len = + strlen(rotated_log_filename_prefix); + static const char total[] = "total "; + static const char log_filename[] = "log.txt"; + + if (!strncmp(buffer, rotated_log_filename_prefix, rotated_log_filename_prefix_len)) { + // Rotated file should have form log.txt.## + char* rotated_log_filename_suffix = buffer + rotated_log_filename_prefix_len; + char* endptr; + const long int suffix_value = strtol(rotated_log_filename_suffix, &endptr, 10); + EXPECT_EQ(rotated_log_filename_suffix + 2, endptr); + EXPECT_LE(suffix_value, 10); + EXPECT_GT(suffix_value, 0); + ++log_file_count; + continue; + } + + if (!strncmp(buffer, log_filename, strlen(log_filename))) { + ++log_file_count; + continue; + } + + fprintf(stderr, "ERROR: Unexpected file: %s", buffer); + ADD_FAILURE(); + } + pclose(fp); + EXPECT_EQ(11, log_file_count); + } + sprintf(command, "rm -rf %s", tmp_out_dir); + EXPECT_FALSE(system(command)); +} + +static void caught_blocking_clear(int /*signum*/) { unsigned long long v = 0xDEADBEEFA55C0000ULL; @@ -554,7 +600,7 @@ TEST(logcat, blocking_clear) { ASSERT_TRUE(NULL != (fp = popen( "( trap exit HUP QUIT INT PIPE KILL ; sleep 6; echo DONE )&" " logcat -b events -c 2>&1 ;" - " logcat -b events 2>&1", + " logcat -v brief -b events 2>&1", "r"))); char buffer[5120]; @@ -649,7 +695,7 @@ static bool set_white_black(const char *list) { char buffer[5120]; - snprintf(buffer, sizeof(buffer), "logcat -P '%s' 2>&1", list); + snprintf(buffer, sizeof(buffer), "logcat -P '%s' 2>&1", list ? list : ""); fp = popen(buffer, "r"); if (fp == NULL) { fprintf(stderr, "ERROR: %s\n", buffer); @@ -662,10 +708,10 @@ static bool set_white_black(const char *list) { ++buf; } char *end = buf + strlen(buf); - while (isspace(*--end) && (end >= buf)) { + while ((end > buf) && isspace(*--end)) { *end = '\0'; } - if (end < buf) { + if (end <= buf) { continue; } fprintf(stderr, "%s\n", buf); @@ -679,7 +725,7 @@ TEST(logcat, white_black_adjust) { char *list = NULL; char *adjust = NULL; - ASSERT_EQ(true, get_white_black(&list)); + get_white_black(&list); static const char adjustment[] = "~! 300/20 300/25 2000 ~1000/5 ~1000/30"; ASSERT_EQ(true, set_white_black(adjustment)); @@ -696,8 +742,8 @@ TEST(logcat, white_black_adjust) { adjust = NULL; ASSERT_EQ(true, set_white_black(list)); - ASSERT_EQ(true, get_white_black(&adjust)); - EXPECT_STREQ(list, adjust); + get_white_black(&adjust); + EXPECT_STREQ(list ? list : "", adjust ? adjust : ""); free(adjust); adjust = NULL; |