Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 95cfc7b8 authored by Mark Salyzyn's avatar Mark Salyzyn
Browse files

logcat: test White Black list

Change-Id: Ia9509ad09dd86e4d9169ed7d0b33911f1ca9eec6
parent a1c60cf8
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -28,7 +28,11 @@ test_c_flags := \
    -g \
    -Wall -Wextra \
    -Werror \
    -fno-builtin \
    -fno-builtin

ifneq ($(filter userdebug eng,$(TARGET_BUILD_VARIANT)),)
test_c_flags += -DUSERDEBUG_BUILD=1
endif

test_src_files := \
    logcat_test.cpp \
+97 −0
Original line number Diff line number Diff line
@@ -527,3 +527,100 @@ TEST(logcat, blocking_clear) {

    ASSERT_EQ(1, signals);
}

#ifdef USERDEBUG_BUILD
static bool get_white_black(char **list) {
    FILE *fp;

    fp = popen("logcat -p 2>/dev/null", "r");
    if (fp == NULL) {
        fprintf(stderr, "ERROR: logcat -p 2>/dev/null\n");
        return false;
    }

    char buffer[5120];

    while (fgets(buffer, sizeof(buffer), fp)) {
        char *hold = *list;
        char *buf = buffer;
	while (isspace(*buf)) {
            ++buf;
        }
        char *end = buf + strlen(buf);
        while (isspace(*--end) && (end >= buf)) {
            *end = '\0';
        }
        if (end < buf) {
            continue;
        }
        if (hold) {
            asprintf(list, "%s %s", hold, buf);
            free(hold);
        } else {
            asprintf(list, "%s", buf);
        }
    }
    pclose(fp);
    return *list != NULL;
}

static bool set_white_black(const char *list) {
    FILE *fp;

    char buffer[5120];

    snprintf(buffer, sizeof(buffer), "logcat -P '%s' 2>&1", list);
    fp = popen(buffer, "r");
    if (fp == NULL) {
        fprintf(stderr, "ERROR: %s\n", buffer);
        return false;
    }

    while (fgets(buffer, sizeof(buffer), fp)) {
        char *buf = buffer;
	while (isspace(*buf)) {
            ++buf;
        }
        char *end = buf + strlen(buf);
        while (isspace(*--end) && (end >= buf)) {
            *end = '\0';
        }
        if (end < buf) {
            continue;
        }
        fprintf(stderr, "%s\n", buf);
        pclose(fp);
        return false;
    }
    return pclose(fp) == 0;
}

TEST(logcat, white_black_adjust) {
    char *list = NULL;
    char *adjust = NULL;

    ASSERT_EQ(true, get_white_black(&list));

    static const char adjustment[] = "~! ~1000";
    ASSERT_EQ(true, set_white_black(adjustment));
    ASSERT_EQ(true, get_white_black(&adjust));
    if (strcmp(adjustment, adjust)) {
        fprintf(stderr, "ERROR: '%s' != '%s'\n", adjustment, adjust);
    }
    ASSERT_STREQ(adjustment, adjust);
    free(adjust);
    adjust = NULL;

    ASSERT_EQ(true, set_white_black(list));
    ASSERT_EQ(true, get_white_black(&adjust));
    if (strcmp(list, adjust)) {
        fprintf(stderr, "ERROR: '%s' != '%s'\n", list, adjust);
    }
    ASSERT_STREQ(list, adjust);
    free(adjust);
    adjust = NULL;

    free(list);
    list = NULL;
}
#endif // USERDEBUG_BUILD