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_consistency_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/macros.h"
27 #include "chre/util/toolchain.h"
28
29 using nanoapp_testing::sendFatalFailureToHost;
30 using nanoapp_testing::sendSuccessToHost;
31
32 namespace general_test {
33
LoggingConsistencyTest()34 LoggingConsistencyTest::LoggingConsistencyTest() : Test(CHRE_API_VERSION_1_0) {}
35
setUp(uint32_t messageSize,const void *)36 void LoggingConsistencyTest::setUp(uint32_t messageSize,
37 const void * /* message */) {
38 if (messageSize != 0) {
39 sendFatalFailureToHost(
40 "LoggingConsistency message expects 0 additional bytes, got ",
41 &messageSize);
42 }
43
44 // Test each warning level.
45 chreLog(CHRE_LOG_ERROR, "Level: Error");
46 chreLog(CHRE_LOG_WARN, "Level: Warn");
47 chreLog(CHRE_LOG_INFO, "Level: Info");
48 chreLog(CHRE_LOG_DEBUG, "Level: Debug");
49
50 // Now we'll just test everything with INFO.
51 constexpr chreLogLevel kInfo = CHRE_LOG_INFO;
52
53 // Empty string
54 char emptyString[1] = {'\0'};
55 chreLog(kInfo, "%s", emptyString);
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, \
83 "%" kPrefix "d %" kPrefix "u 0%" kPrefix "o 0x%" kPrefix \
84 "x 0x%" kPrefix "X", \
85 value, value, value, value, value); \
86 }
87
88 INT_TYPES("hh", char);
89 INT_TYPES("h", short);
90 INT_TYPES("l", long);
91 INT_TYPES("ll", long long);
92 INT_TYPES("z", size_t);
93 INT_TYPES("t", ptrdiff_t);
94
95 // Disables logging-related double promotion warnings
96 CHRE_LOG_PREAMBLE
97
98 float f = 12.34f;
99 // Other required formats, including escaping the '%'.
100 chreLog(kInfo, "%% %f %c %s %p", f, '?', "str", &f);
101
102 // OPTIONAL specifiers. See chreLog() API documentation for extensive
103 // discussion of what OPTIONAL means.
104 // <width> and '-'
105 chreLog(kInfo, "(%5s) (%-5s) (%5d) (%-5d)", "str", "str", 10, 10);
106 // '+'
107 chreLog(kInfo, "(%+d) (%+d) (%+f) (%+f)", -5, 5, -5.f, 5.f);
108 // ' '
109 chreLog(kInfo, "(% d) (% d) (% f) (% f)", -5, 5, -5.f, 5.f);
110 // '#'
111 chreLog(kInfo, "%#o %#x %#X %#f", 8, 15, 15, 1.f);
112 // '0' padding
113 chreLog(kInfo, "%08d 0x%04x", 123, 0xF);
114 // '.'<precision>
115 chreLog(kInfo, "%.3d %.3d %.3f %.3f %.3s", 12, 1234, 1.5, 1.0625, "abcdef");
116
117 // Re-enable logging-related double warnings
118 CHRE_LOG_EPILOGUE
119
120 // TODO: In some future Android release, when chreLog() is required to
121 // output to logcat, we'll just send a Continue to the Host and have
122 // the Host verify this output. But for Android N, we leave it to
123 // the test runner to manually verify.
124 sendSuccessToHost();
125 }
126
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)127 void LoggingConsistencyTest::handleEvent(uint32_t senderInstanceId,
128 uint16_t eventType,
129 const void *eventData) {
130 UNUSED_VAR(senderInstanceId);
131 UNUSED_VAR(eventData);
132
133 unexpectedEvent(eventType);
134 }
135
136 } // namespace general_test
137