1 /*
2  * Copyright (C) 2016 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 <general_test/logging_sanity_test.h>
18 
19 #include <cstddef>
20 #include <limits>
21 
22 #include <shared/send_message.h>
23 
24 #include <chre.h>
25 
26 #include "chre/util/toolchain.h"
27 
28 using nanoapp_testing::sendFatalFailureToHost;
29 using nanoapp_testing::sendSuccessToHost;
30 
31 namespace general_test {
32 
LoggingSanityTest()33 LoggingSanityTest::LoggingSanityTest()
34     : Test(CHRE_API_VERSION_1_0) {
35 }
36 
setUp(uint32_t messageSize,const void *)37 void LoggingSanityTest::setUp(uint32_t messageSize,
38                               const void * /* message */) {
39   if (messageSize != 0) {
40     sendFatalFailureToHost(
41         "LoggingSanity message expects 0 additional bytes, got ",
42         &messageSize);
43   }
44 
45   // Test each warning level.
46   chreLog(CHRE_LOG_ERROR, "Level: Error");
47   chreLog(CHRE_LOG_WARN, "Level: Warn");
48   chreLog(CHRE_LOG_INFO, "Level: Info");
49   chreLog(CHRE_LOG_DEBUG, "Level: Debug");
50 
51   // Now we'll just test everything with INFO.
52   constexpr chreLogLevel kInfo = CHRE_LOG_INFO;
53 
54   // Empty string
55   chreLog(kInfo, "");
56 
57   // Try up through 10 arguments
58   chreLog(kInfo, "%d", 1);
59   chreLog(kInfo, "%d %d", 1, 2);
60   chreLog(kInfo, "%d %d %d", 1, 2, 3);
61   chreLog(kInfo, "%d %d %d %d", 1, 2, 3, 4);
62   chreLog(kInfo, "%d %d %d %d %d", 1, 2, 3, 4, 5);
63   chreLog(kInfo, "%d %d %d %d %d %d", 1, 2, 3, 4, 5, 6);
64   chreLog(kInfo, "%d %d %d %d %d %d %d", 1, 2, 3, 4, 5, 6, 7);
65   chreLog(kInfo, "%d %d %d %d %d %d %d %d", 1, 2, 3, 4, 5, 6, 7, 8);
66   chreLog(kInfo, "%d %d %d %d %d %d %d %d %d", 1, 2, 3, 4, 5, 6, 7, 8, 9);
67   chreLog(kInfo, "%d %d %d %d %d %d %d %d %d %d", 1, 2, 3, 4, 5, 6, 7, 8, 9,
68           10);
69 
70   // Various 'int' specifiers.  The value of the "%u" output depends on the
71   // size of 'int' on this machine.
72   chreLog(kInfo, "%d %u 0%o 0x%x 0x%X", -1, -1, 01234, 0xF4E, 0xF4E);
73 
74   // Generic testing of all specific types.  The format string is the same
75   // as the chreLog() above us, just using the appropriate prefix for each.
76   // We also use the min() value for all these signed types, assuring that
77   // we'll get different %d vs %u output, and we'll get letters within our
78   // %x and %X output.
79 #define INT_TYPES(kPrefix, type) \
80   { \
81     type value = std::numeric_limits<type>::min(); \
82     chreLog(kInfo, "%" kPrefix "d %" kPrefix "u 0%" kPrefix "o 0x%" \
83             kPrefix "x 0x%" kPrefix "X", value, value, value, value, \
84             value); \
85   }
86 
87   INT_TYPES("hh", char);
88   INT_TYPES("h", short);
89   INT_TYPES("l", long);
90   INT_TYPES("ll", long long);
91   INT_TYPES("z", size_t);
92   INT_TYPES("t", ptrdiff_t);
93 
94   // Disables logging-related double promotion warnings
95   CHRE_LOG_PREAMBLE
96 
97   float f = 12.34f;
98   // Other required formats, including escaping the '%'.
99   chreLog(kInfo, "%% %f %c %s %p", f, '?', "str", &f);
100 
101   // OPTIONAL specifiers.  See chreLog() API documentation for extensive
102   // discussion of what OPTIONAL means.
103   // <width> and '-'
104   chreLog(kInfo, "(%5s) (%-5s) (%5d) (%-5d)", "str", "str", 10, 10);
105   // '+'
106   chreLog(kInfo, "(%+d) (%+d) (%+f) (%+f)", -5, 5, -5.f, 5.f);
107   // ' '
108   chreLog(kInfo, "(% d) (% d) (% f) (% f)", -5, 5, -5.f, 5.f);
109   // '#'
110   chreLog(kInfo, "%#o %#x %#X %#f", 8, 15, 15, 1.f);
111   // '0' padding
112   chreLog(kInfo, "%08d 0x%04x", 123, 0xF);
113   // '.'<precision>
114   chreLog(kInfo, "%.3d %.3d %.3f %.3f %.3s", 12, 1234, 1.5, 1.0625, "abcdef");
115 
116   // Re-enable logging-related double warnings
117   CHRE_LOG_EPILOGUE
118 
119   // TODO: In some future Android release, when chreLog() is required to
120   //     output to logcat, we'll just send a Continue to the Host and have
121   //     the Host verify this output.  But for Android N, we leave it to
122   //     the test runner to manually verify.
123   sendSuccessToHost();
124 }
125 
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)126 void LoggingSanityTest::handleEvent(uint32_t senderInstanceId,
127                                     uint16_t eventType, const void* eventData) {
128   unexpectedEvent(eventType);
129 }
130 
131 }  // namespace general_test
132