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