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