1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
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 <folly/experimental/symbolizer/SymbolizedFrame.h>
18 
19 #include <glog/logging.h>
20 
21 #include <folly/portability/GTest.h>
22 
23 using namespace folly::symbolizer;
24 
checkPath(std::string expectedPath,std::string expectedBaseDir,std::string expectedSubDir,std::string expectedFile,std::string rawBaseDir,std::string rawSubDir,std::string rawFile)25 void checkPath(
26     std::string expectedPath,
27     std::string expectedBaseDir,
28     std::string expectedSubDir,
29     std::string expectedFile,
30     std::string rawBaseDir,
31     std::string rawSubDir,
32     std::string rawFile) {
33   Path path(rawBaseDir, rawSubDir, rawFile);
34 
35   CHECK_EQ(expectedBaseDir, path.baseDir())
36       << "Path(" << rawBaseDir << ", " << rawSubDir << ", " << rawFile << ")";
37   CHECK_EQ(expectedSubDir, path.subDir())
38       << "Path(" << rawBaseDir << ", " << rawSubDir << ", " << rawFile << ")";
39   CHECK_EQ(expectedFile, path.file())
40       << "Path(" << rawBaseDir << ", " << rawSubDir << ", " << rawFile << ")";
41 
42   CHECK_EQ(expectedPath, path.toString());
43 
44   // Check the `toBuffer` function.
45   char buf[1024];
46   size_t len;
47   len = path.toBuffer(buf, 1024);
48   CHECK_EQ(expectedPath, std::string(buf, len));
49 }
50 
TEST(SymbolizedFrame,Path)51 TEST(SymbolizedFrame, Path) {
52   checkPath("hello.cpp", "", "", "hello.cpp", "", "", "hello.cpp");
53   checkPath("foo/hello.cpp", "foo", "", "hello.cpp", "foo", "", "hello.cpp");
54   checkPath("foo/hello.cpp", "foo", "", "hello.cpp", "", "foo", "hello.cpp");
55   checkPath("hello.cpp", "", "", "hello.cpp", "./////", "./////", "hello.cpp");
56   checkPath("/hello.cpp", "/", "", "hello.cpp", "/////", "./////", "hello.cpp");
57   checkPath(
58       "/hello.cpp", "/", "", "hello.cpp", "/./././././././", "", "hello.cpp");
59 }
60