• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 using nanoapp_testing::sendFatalFailureToHost;
27 using nanoapp_testing::sendSuccessToHost;
28 
29 namespace general_test {
30 
LoggingSanityTest()31 LoggingSanityTest::LoggingSanityTest()
32     : Test(CHRE_API_VERSION_1_0) {
33 }
34 
setUp(uint32_t messageSize,const void *)35 void LoggingSanityTest::setUp(uint32_t messageSize,
36                               const void * /* message */) {
37   if (messageSize != 0) {
38     sendFatalFailureToHost(
39         "LoggingSanity 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, "%" kPrefix "d %" kPrefix "u 0%" kPrefix "o 0x%" \
81             kPrefix "x 0x%" kPrefix "X", value, value, value, value, \
82             value); \
83   }
84 
85   INT_TYPES("hh", char);
86   INT_TYPES("h", short);
87   INT_TYPES("l", long);
88   INT_TYPES("ll", long long);
89   INT_TYPES("z", size_t);
90   INT_TYPES("t", ptrdiff_t);
91 
92   float f = 12.34f;
93   // Other required formats, including escaping the '%'.
94   chreLog(kInfo, "%% %f %c %s %p", f, '?', "str", &f);
95 
96 
97   // OPTIONAL specifiers.  See chreLog() API documentation for extensive
98   // discussion of what OPTIONAL means.
99   // <width> and '-'
100   chreLog(kInfo, "(%5s) (%-5s) (%5d) (%-5d)", "str", "str", 10, 10);
101   // '+'
102   chreLog(kInfo, "(%+d) (%+d) (%+f) (%+f)", -5, 5, -5.f, 5.f);
103   // ' '
104   chreLog(kInfo, "(% d) (% d) (% f) (% f)", -5, 5, -5.f, 5.f);
105   // '#'
106   chreLog(kInfo, "%#o %#x %#X %#f", 8, 15, 15, 1.f);
107   // '0' padding
108   chreLog(kInfo, "%08d 0x%04x", 123, 0xF);
109   // '.'<precision>
110   chreLog(kInfo, "%.3d %.3d %.3f %.3f %.3s", 12, 1234, 1.5, 1.0625, "abcdef");
111 
112   // TODO: In some future Android release, when chreLog() is required to
113   //     output to logcat, we'll just send a Continue to the Host and have
114   //     the Host verify this output.  But for Android N, we leave it to
115   //     the test runner to manually verify.
116   sendSuccessToHost();
117 }
118 
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)119 void LoggingSanityTest::handleEvent(uint32_t senderInstanceId,
120                                     uint16_t eventType, const void* eventData) {
121   unexpectedEvent(eventType);
122 }
123 
124 }  // namespace general_test
125