proxygen
LogMessageTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
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  */
17 
18 #include <folly/String.h>
19 #include <folly/logging/Logger.h>
20 #include <folly/logging/LoggerDB.h>
22 
23 using namespace folly;
24 
25 #define CHECK_MSG(expected, value, hasNewlines) \
26  { \
27  SCOPED_TRACE( \
28  "input string: \"" + folly::backslashify<std::string>(value) + "\""); \
29  LogMessage checkMsg{category, \
30  LogLevel::ERR, \
31  __FILE__, \
32  __LINE__, \
33  __func__, \
34  std::string{value}}; \
35  EXPECT_EQ(expected, checkMsg.getMessage()); \
36  EXPECT_EQ(static_cast<int>(hasNewlines), checkMsg.containsNewlines()); \
37  EXPECT_EQ(__FILE__, checkMsg.getFileName()); \
38  EXPECT_EQ(__LINE__, checkMsg.getLineNumber()); \
39  }
40 
41 TEST(LogMessage, sanitize) {
43  Logger logger{&db, "test"};
44  auto* category = logger.getCategory();
45 
46  CHECK_MSG("foo", "foo", false);
47  CHECK_MSG("foo\\\\bar", "foo\\bar", false);
48  CHECK_MSG("foo\\x01test", "foo\01test", false);
49  CHECK_MSG("test 1234 ", "test 1234 ", false);
50  CHECK_MSG("\\x07", "\a", false);
51  CHECK_MSG("\n", "\n", true);
52  CHECK_MSG("\t", "\t", false);
53  CHECK_MSG("\n\t\n", "\n\t\n", true);
54  // Test strings containing NUL bytes
55  CHECK_MSG("test\\x00.1234\\x00", std::string("test\0.1234\0", 11), false);
56  CHECK_MSG("test\\x00\n1234\\x00", std::string("test\0\n1234\0", 11), true);
57  // Test all ASCII characters except NUL
58  CHECK_MSG(
59  ("\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08"
60  "\t\n\\x0b\\x0c\\x0d\\x0e\\x0f"
61  "\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17"
62  "\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f"
63  " !\"#$%&'()*+,-./0123456789:;<=>?"
64  "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_"
65  "`abcdefghijklmnopqrstuvwxyz{|}~\\x7f"),
66  ("\001\002\003\004\005\006\007"
67  "\010\011\012\013\014\015\016\017"
68  "\020\021\022\023\024\025\026\027"
69  "\030\031\032\033\034\035\036\037"
70  " !\"#$%&'()*+,-./0123456789:;<=>?"
71  "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
72  "`abcdefghijklmnopqrstuvwxyz{|}~\177"),
73  true);
74 
75  // Test some high-bit characters
76  CHECK_MSG("\x82\x83", "\x82\x83", false);
77  CHECK_MSG("\x82\n\x83\n", "\x82\n\x83\n", true);
78  CHECK_MSG("\x82\n\\x0c\x83\n", "\x82\n\f\x83\n", true);
79 }
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
#define CHECK_MSG(expected, value, hasNewlines)
LogCategory * getCategory() const
Definition: Logger.h:183
const char * string
Definition: Conv.cpp:212
TEST(SequencedExecutor, CPUThreadPoolExecutor)