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

Commit ca1e3438 authored by Mark Salyzyn's avatar Mark Salyzyn Committed by Gerrit Code Review
Browse files

Merge "logcat: test: add clear and blocking check"

parents ecb5dd69 9b986497
Loading
Loading
Loading
Loading
+95 −9
Original line number Diff line number Diff line
@@ -319,12 +319,16 @@ static void caught_blocking(int signum)

TEST(logcat, blocking) {
    FILE *fp;
    unsigned long long v = 0xDEADBEEFA55A0000ULL;
    unsigned long long v = 0xDEADBEEFA55F0000ULL;

    pid_t pid = getpid();

    v += pid & 0xFFFF;

    LOG_FAILURE_RETRY(__android_log_btwrite(0, EVENT_TYPE_LONG, &v, sizeof(v)));

    v &= 0xFFFFFFFFFFFAFFFFULL;

    ASSERT_EQ(0, NULL == (fp = popen(
      "( trap exit HUP QUIT INT PIPE KILL ; sleep 6; echo DONE )&"
      " logcat -b events 2>&1",
@@ -341,12 +345,12 @@ TEST(logcat, blocking) {
    while (fgets(buffer, sizeof(buffer), fp)) {
        alarm(2);

        ++count;

        if (!strncmp(buffer, "DONE", 4)) {
            break;
        }

        ++count;

        int p;
        unsigned long long l;

@@ -369,7 +373,7 @@ TEST(logcat, blocking) {

    pclose(fp);

    ASSERT_LT(10, count);
    ASSERT_LE(2, count);

    ASSERT_EQ(1, signals);
}
@@ -385,12 +389,16 @@ static void caught_blocking_tail(int signum)

TEST(logcat, blocking_tail) {
    FILE *fp;
    unsigned long long v = 0xA55ADEADBEEF0000ULL;
    unsigned long long v = 0xA55FDEADBEEF0000ULL;

    pid_t pid = getpid();

    v += pid & 0xFFFF;

    LOG_FAILURE_RETRY(__android_log_btwrite(0, EVENT_TYPE_LONG, &v, sizeof(v)));

    v &= 0xFFFAFFFFFFFFFFFFULL;

    ASSERT_EQ(0, NULL == (fp = popen(
      "( trap exit HUP QUIT INT PIPE KILL ; sleep 6; echo DONE )&"
      " logcat -b events -T 5 2>&1",
@@ -407,12 +415,12 @@ TEST(logcat, blocking_tail) {
    while (fgets(buffer, sizeof(buffer), fp)) {
        alarm(2);

        ++count;

        if (!strncmp(buffer, "DONE", 4)) {
            break;
        }

        ++count;

        int p;
        unsigned long long l;

@@ -431,13 +439,91 @@ TEST(logcat, blocking_tail) {
    alarm(0);
    signal(SIGALRM, SIG_DFL);

    /* Generate SIGPIPE */
    // Generate SIGPIPE
    fclose(fp);
    caught_blocking_tail(0);

    pclose(fp);

    ASSERT_LT(5, count);
    ASSERT_LE(2, count);

    ASSERT_EQ(1, signals);
}

static void caught_blocking_clear(int signum)
{
    unsigned long long v = 0xDEADBEEFA55C0000ULL;

    v += getpid() & 0xFFFF;

    LOG_FAILURE_RETRY(__android_log_btwrite(0, EVENT_TYPE_LONG, &v, sizeof(v)));
}

TEST(logcat, blocking_clear) {
    FILE *fp;
    unsigned long long v = 0xDEADBEEFA55C0000ULL;

    pid_t pid = getpid();

    v += pid & 0xFFFF;

    // This test is racey; an event occurs between clear and dump.
    // We accept that we will get a false positive, but never a false negative.
    ASSERT_EQ(0, 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",
      "r")));

    char buffer[5120];

    int count = 0;

    int signals = 0;

    signal(SIGALRM, caught_blocking_clear);
    alarm(2);
    while (fgets(buffer, sizeof(buffer), fp)) {
        alarm(2);

        if (!strncmp(buffer, "clearLog: ", 10)) {
            fprintf(stderr, "WARNING: Test lacks permission to run :-(\n");
            count = signals = 1;
            break;
        }

        if (!strncmp(buffer, "DONE", 4)) {
            break;
        }

        ++count;

        int p;
        unsigned long long l;

        if ((2 != sscanf(buffer, "I/[0] ( %u): %lld", &p, &l))
         || (p != pid)) {
            continue;
        }

        if (l == v) {
            if (count > 1) {
                fprintf(stderr, "WARNING: Possible false positive\n");
            }
            ++signals;
            break;
        }
    }
    alarm(0);
    signal(SIGALRM, SIG_DFL);

    // Generate SIGPIPE
    fclose(fp);
    caught_blocking_clear(0);

    pclose(fp);

    ASSERT_LE(1, count);

    ASSERT_EQ(1, signals);
}