1 /*
2  * Copyright (C) 2013-2017 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 <inttypes.h>
18 #include <pthread.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 
22 #include <gtest/gtest.h>
23 // Test the APIs in this standalone include file
24 #include <log/log_id.h>
25 
26 // We do not want to include <android/log.h> to acquire ANDROID_LOG_INFO for
27 // include file API purity.  We do however want to allow the _option_ that
28 // log/log_id.h could include this file, or related content, in the future.
29 #ifndef __android_LogPriority_defined
30 #define ANDROID_LOG_INFO 4
31 #endif
32 
TEST(liblog,log_id)33 TEST(liblog, log_id) {
34   int count = 0;
35 
36   for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
37     log_id_t id = static_cast<log_id_t>(i);
38     const char* name = android_log_id_to_name(id);
39     if (id != android_name_to_log_id(name)) {
40       continue;
41     }
42     ++count;
43     fprintf(stderr, "log buffer %s\r", name);
44   }
45   ASSERT_EQ(LOG_ID_MAX, count);
46 }
47 
TEST(liblog,__android_log_buf_print)48 TEST(liblog, __android_log_buf_print) {
49   EXPECT_LT(0, __android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO,
50                                        "TEST__android_log_buf_print", "radio"));
51   usleep(1000);
52   EXPECT_LT(0,
53             __android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO,
54                                     "TEST__android_log_buf_print", "system"));
55   usleep(1000);
56   EXPECT_LT(0, __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO,
57                                        "TEST__android_log_buf_print", "main"));
58   usleep(1000);
59 }
60 
TEST(liblog,__android_log_buf_write)61 TEST(liblog, __android_log_buf_write) {
62   EXPECT_LT(0, __android_log_buf_write(LOG_ID_RADIO, ANDROID_LOG_INFO,
63                                        "TEST__android_log_buf_write", "radio"));
64   usleep(1000);
65   EXPECT_LT(0,
66             __android_log_buf_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO,
67                                     "TEST__android_log_buf_write", "system"));
68   usleep(1000);
69   EXPECT_LT(0, __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_INFO,
70                                        "TEST__android_log_buf_write", "main"));
71   usleep(1000);
72 }
73 
ConcurrentPrintFn(void * arg)74 static void* ConcurrentPrintFn(void* arg) {
75   int ret = __android_log_buf_print(
76       LOG_ID_MAIN, ANDROID_LOG_INFO, "TEST__android_log_print",
77       "Concurrent %" PRIuPTR, reinterpret_cast<uintptr_t>(arg));
78   return reinterpret_cast<void*>(ret);
79 }
80 
81 #define NUM_CONCURRENT 64
82 #define _concurrent_name(a, n) a##__concurrent##n
83 #define concurrent_name(a, n) _concurrent_name(a, n)
84 
TEST(liblog,concurrent_name (__android_log_buf_print,NUM_CONCURRENT))85 TEST(liblog, concurrent_name(__android_log_buf_print, NUM_CONCURRENT)) {
86   pthread_t t[NUM_CONCURRENT];
87   int i;
88   for (i = 0; i < NUM_CONCURRENT; i++) {
89     ASSERT_EQ(0, pthread_create(&t[i], NULL, ConcurrentPrintFn,
90                                 reinterpret_cast<void*>(i)));
91   }
92   int ret = 0;
93   for (i = 0; i < NUM_CONCURRENT; i++) {
94     void* result;
95     ASSERT_EQ(0, pthread_join(t[i], &result));
96     int this_result = reinterpret_cast<uintptr_t>(result);
97     if ((0 == ret) && (0 != this_result)) {
98       ret = this_result;
99     }
100   }
101   ASSERT_LT(0, ret);
102 }
103