• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #define LOG_TAG "TraceFileWriterTest"
17 #include <fstream>
18 #include <gtest/gtest.h>
19 #include <unistd.h>
20 #include <vector>
21 
22 #include "common_types.pb.h"
23 #include "logging.h"
24 #include "trace_file_writer.h"
25 
26 using namespace testing::ext;
27 
28 namespace {
29 class TraceFileWriterTest : public ::testing::Test {
30 protected:
31     std::string path = "trace.bin";
32 
SetUpTestCase()33     static void SetUpTestCase() {}
TearDownTestCase()34     static void TearDownTestCase() {}
35 
SetUp()36     void SetUp() override {}
37 
TearDown()38     void TearDown() override
39     {
40         int retval = unlink(path.c_str());
41         PROFILER_LOG_DEBUG(LOG_CORE, "unlink(%s): %d", path.c_str(), retval);
42     }
43 };
44 
45 /**
46  * @tc.name: server
47  * @tc.desc: Class-strengthening.
48  * @tc.type: FUNC
49  */
50 HWTEST_F(TraceFileWriterTest, CtorDtor, TestSize.Level1)
51 {
52     auto writer = std::make_shared<TraceFileWriter>(path);
53     EXPECT_NE(writer, nullptr);
54 }
55 
56 /**
57  * @tc.name: server
58  * @tc.desc: write.
59  * @tc.type: FUNC
60  */
61 HWTEST_F(TraceFileWriterTest, Write, TestSize.Level1)
62 {
63     path = "trace-write.bin";
64     auto writer = std::make_shared<TraceFileWriter>(path);
65     ASSERT_NE(writer, nullptr);
66 
67     std::string testData = "Hello, Wrold!";
68     EXPECT_EQ(writer->Write(testData.data(), testData.size()), sizeof(uint32_t) + testData.size());
69 }
70 
71 /**
72  * @tc.name: server
73  * @tc.desc: flush.
74  * @tc.type: FUNC
75  */
76 HWTEST_F(TraceFileWriterTest, Flush, TestSize.Level1)
77 {
78     std::string testData = "Hello, Wrold!";
79     path = "trace-flush.bin";
80     {
81         auto writer = std::make_shared<TraceFileWriter>(path);
82         ASSERT_NE(writer, nullptr);
83         EXPECT_EQ(writer->Write(testData.data(), testData.size()), sizeof(uint32_t) + testData.size());
84         EXPECT_EQ(writer->Flush(), true);
85     }
86 
87     uint32_t msgLen = 0;
88     std::ifstream fin(path, std::ios_base::in | std::ios_base::binary);
89     ASSERT_TRUE(fin.is_open());
90 
91     // check file length
92     fin.seekg(0, std::ios_base::end);
93     EXPECT_EQ(fin.tellg(), TraceFileHeader::HEADER_SIZE + sizeof(msgLen) + testData.size());
94 
95     // check msg length
96     fin.seekg(TraceFileHeader::HEADER_SIZE, std::ios_base::beg); // skip file header
97     fin.read(reinterpret_cast<char*>(&msgLen), sizeof(msgLen));
98     EXPECT_EQ(msgLen, testData.size());
99 
100     // check msg data
101     std::vector<char> outData(testData.size());
102     fin.read(outData.data(), outData.size()); // read into outData
103     EXPECT_EQ(memcmp(outData.data(), testData.data(), outData.size()), 0);
104 }
105 
106 /**
107  * @tc.name: server
108  * @tc.desc: write message.
109  * @tc.type: FUNC
110  */
111 HWTEST_F(TraceFileWriterTest, WriteMessage, TestSize.Level1)
112 {
113     path = "trace-write-message.bin";
114     auto writer = std::make_shared<TraceFileWriter>(path);
115     ASSERT_NE(writer, nullptr);
116 
117     ProfilerPluginData pluginData;
118     pluginData.set_name("ABC");
119     pluginData.set_status(0);
120     pluginData.set_data("DEF");
121     EXPECT_GT(writer->Write(pluginData), 0);
122 }
123 
124 /**
125  * @tc.name: server
126  * @tc.desc: Split file.
127  * @tc.type: FUNC
128  */
129 HWTEST_F(TraceFileWriterTest, SplitFileWriter, TestSize.Level1)
130 {
131     path = "trace-write-test.bin";
132     auto writer = std::make_shared<TraceFileWriter>(path, true, 0, 0);
133     EXPECT_NE(writer, nullptr);
134     writer->Path();
135     writer->SetStopSplitFile(false);
136     std::string testData = "this is a test case!";
137     EXPECT_EQ(writer->Write(testData.data(), testData.size()), sizeof(uint32_t) + testData.size());
138     std::string testStr = "test case";
139     writer->SetPluginConfig(testStr.data(), testStr.size());
140 
141     std::string testPath = "trace-write-bin";
142     auto writerTestPath = std::make_shared<TraceFileWriter>(testPath, true, 0, 1);
143     EXPECT_NE(writerTestPath, nullptr);
144     writerTestPath->splitFilePaths_.push("trace-write-path-1");
145     writerTestPath->splitFilePaths_.push("trace-write-path-2");
146     writerTestPath->DeleteOldSplitFile();
147     EXPECT_TRUE(writerTestPath->IsSplitFile(300 * 1024 * 1024));
148 
149     std::string testPathTemp = "/data/local/tmp/trace-write-path";
150     auto writerTemp = std::make_shared<TraceFileWriter>(testPathTemp, true, 0, 1);
151     EXPECT_NE(writerTemp, nullptr);
152 }
153 } // namespace
154