xref: /aosp_15_r20/system/logging/liblog/tests/libc_test.cpp (revision 598139dc91b21518d67c408eaea2644226490971)
1  /*
2   * Copyright (C) 2014 The Android Open Source Project
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *      http://www.apache.org/licenses/LICENSE-2.0
9   *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  #include <gtest/gtest.h>
18  
19  #include <errno.h>
20  #include <stdio.h>
21  
TEST(libc,__pstore_append)22  TEST(libc, __pstore_append) {
23  #ifdef __ANDROID__
24  #ifndef NO_PSTORE
25    if (access("/dev/pmsg0", W_OK) != 0) {
26      GTEST_SKIP() << "pmsg0 not found, skipping test";
27    }
28  
29    FILE* fp;
30    ASSERT_TRUE(NULL != (fp = fopen("/dev/pmsg0", "ae")));
31    static const char message[] = "libc.__pstore_append\n";
32    ASSERT_EQ((size_t)1, fwrite(message, sizeof(message), 1, fp));
33    int fflushReturn = fflush(fp);
34    int fflushErrno = fflushReturn ? errno : 0;
35    ASSERT_EQ(0, fflushReturn);
36    ASSERT_EQ(0, fflushErrno);
37    int fcloseReturn = fclose(fp);
38    int fcloseErrno = fcloseReturn ? errno : 0;
39    ASSERT_EQ(0, fcloseReturn);
40    ASSERT_EQ(0, fcloseErrno);
41    if ((fcloseErrno == ENOMEM) || (fflushErrno == ENOMEM)) {
42      fprintf(stderr,
43              "Kernel does not have space allocated to pmsg pstore driver "
44              "configured\n");
45    }
46    if (!fcloseReturn && !fcloseErrno && !fflushReturn && !fflushErrno) {
47      fprintf(stderr,
48              "Reboot, ensure string libc.__pstore_append is in "
49              "/sys/fs/pstore/pmsg-ramoops-0\n");
50    }
51  #else  /* NO_PSTORE */
52    GTEST_LOG_(INFO) << "This test does nothing because of NO_PSTORE.\n";
53  #endif /* NO_PSTORE */
54  #else
55    GTEST_LOG_(INFO) << "This test does nothing.\n";
56  #endif
57  }
58