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/util/nanoapp/log.h>
25 
26 #include "chre/util/macros.h"
27 #include "chre/util/toolchain.h"
28 #include "chre_api/chre.h"
29 
30 #define LOG_TAG "[LoggingConsistencyTest]"
31 
32 using nanoapp_testing::sendFatalFailureToHost;
33 using nanoapp_testing::sendSuccessToHost;
34 
35 namespace general_test {
36 
LoggingConsistencyTest()37 LoggingConsistencyTest::LoggingConsistencyTest() : Test(CHRE_API_VERSION_1_0) {}
38 
setUp(uint32_t messageSize,const void *)39 void LoggingConsistencyTest::setUp(uint32_t messageSize,
40                                    const void * /* message */) {
41   if (messageSize != 0) {
42     sendFatalFailureToHost(
43         "LoggingConsistency message expects 0 additional bytes, got ",
44         &messageSize);
45   }
46 
47   // Test each warning level.
48   LOGE("Level: Error");
49   LOGW("Level: Warn");
50   LOGI("Level: Info");
51   LOGD("Level: Debug");
52 
53   // Empty string
54   char emptyString[1] = {'\0'};
55   LOGI("%s", emptyString);
56 
57   // Try up through 10 arguments
58   LOGI("%d", 1);
59   LOGI("%d %d", 1, 2);
60   LOGI("%d %d %d", 1, 2, 3);
61   LOGI("%d %d %d %d", 1, 2, 3, 4);
62   LOGI("%d %d %d %d %d", 1, 2, 3, 4, 5);
63   LOGI("%d %d %d %d %d %d", 1, 2, 3, 4, 5, 6);
64   LOGI("%d %d %d %d %d %d %d", 1, 2, 3, 4, 5, 6, 7);
65   LOGI("%d %d %d %d %d %d %d %d", 1, 2, 3, 4, 5, 6, 7, 8);
66   LOGI("%d %d %d %d %d %d %d %d %d", 1, 2, 3, 4, 5, 6, 7, 8, 9);
67   LOGI("%d %d %d %d %d %d %d %d %d %d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
68 
69   // Various 'int' specifiers.  The value of the "%u" output depends on the
70   // size of 'int' on this machine.
71   LOGI("%d %u 0%o 0x%x 0x%X", -1, -1, 01234, 0xF4E, 0xF4E);
72 
73   // Generic testing of all specific types.  The format string is the same
74   // as the LOGI() above us, just using the appropriate prefix for each.
75   // We also use the min() value for all these signed types, assuring that
76   // we'll get different %d vs %u output, and we'll get letters within our
77   // %x and %X output.
78 #define INT_TYPES(kPrefix, type)                                  \
79   {                                                               \
80     type value = std::numeric_limits<type>::min();                \
81     LOGI("%" 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   LOGI("%% %f %c %s %p", f, '?', "str", &f);
99 
100   // OPTIONAL specifiers.  See LOGI() API documentation for extensive
101   // discussion of what OPTIONAL means.
102   // <width> and '-'
103   LOGI("(%5s) (%-5s) (%5d) (%-5d)", "str", "str", 10, 10);
104   // '+'
105   LOGI("(%+d) (%+d) (%+f) (%+f)", -5, 5, -5.f, 5.f);
106   // ' '
107   LOGI("(% d) (% d) (% f) (% f)", -5, 5, -5.f, 5.f);
108   // '#'
109   LOGI("%#o %#x %#X %#f", 8, 15, 15, 1.f);
110   // '0' padding
111   LOGI("%08d 0x%04x", 123, 0xF);
112   // '.'<precision>
113   LOGI("%.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 LOGI() 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   UNUSED_VAR(senderInstanceId);
129   UNUSED_VAR(eventData);
130 
131   unexpectedEvent(eventType);
132 }
133 
134 }  // namespace general_test
135