1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 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 <hwext/gtest-ext.h> 19 #include <hwext/gtest-tag.h> 20 #include <unistd.h> 21 #include <vector> 22 23 #include "common_types.pb.h" 24 #include "logging.h" 25 #include "trace_file_writer.h" 26 27 using namespace testing::ext; 28 29 namespace { 30 class TraceFileWriterTest : public ::testing::Test { 31 protected: 32 std::string path = "trace.bin"; 33 SetUpTestCase()34 static void SetUpTestCase() {} TearDownTestCase()35 static void TearDownTestCase() {} 36 SetUp()37 void SetUp() override {} 38 TearDown()39 void TearDown() override 40 { 41 int retval = unlink(path.c_str()); 42 HILOG_DEBUG(LOG_CORE, "unlink(%s): %d", path.c_str(), retval); 43 } 44 }; 45 46 /** 47 * @tc.name: server 48 * @tc.desc: Class-strengthening. 49 * @tc.type: FUNC 50 */ 51 HWTEST_F(TraceFileWriterTest, CtorDtor, TestSize.Level1) 52 { 53 auto writer = std::make_shared<TraceFileWriter>(path); 54 EXPECT_NE(writer, nullptr); 55 } 56 57 /** 58 * @tc.name: server 59 * @tc.desc: write. 60 * @tc.type: FUNC 61 */ 62 HWTEST_F(TraceFileWriterTest, Write, TestSize.Level1) 63 { 64 path = "trace-write.bin"; 65 auto writer = std::make_shared<TraceFileWriter>(path); 66 ASSERT_NE(writer, nullptr); 67 68 std::string testData = "Hello, Wrold!"; 69 EXPECT_EQ(writer->Write(testData.data(), testData.size()), sizeof(uint32_t) + testData.size()); 70 } 71 72 /** 73 * @tc.name: server 74 * @tc.desc: flush. 75 * @tc.type: FUNC 76 */ 77 HWTEST_F(TraceFileWriterTest, Flush, TestSize.Level1) 78 { 79 std::string testData = "Hello, Wrold!"; 80 path = "trace-flush.bin"; 81 { 82 auto writer = std::make_shared<TraceFileWriter>(path); 83 ASSERT_NE(writer, nullptr); 84 EXPECT_EQ(writer->Write(testData.data(), testData.size()), sizeof(uint32_t) + testData.size()); 85 EXPECT_EQ(writer->Flush(), true); 86 } 87 88 uint32_t msgLen = 0; 89 std::ifstream fin(path, std::ios_base::in | std::ios_base::binary); 90 ASSERT_TRUE(fin.is_open()); 91 92 // check file length 93 fin.seekg(0, std::ios_base::end); 94 EXPECT_EQ(fin.tellg(), TraceFileHeader::HEADER_SIZE + sizeof(msgLen) + testData.size()); 95 96 // check msg length 97 fin.seekg(TraceFileHeader::HEADER_SIZE, std::ios_base::beg); // skip file header 98 fin.read(reinterpret_cast<char*>(&msgLen), sizeof(msgLen)); 99 EXPECT_EQ(msgLen, testData.size()); 100 101 // check msg data 102 std::vector<char> outData(testData.size()); 103 fin.read(outData.data(), outData.size()); // read into outData 104 EXPECT_EQ(memcmp(outData.data(), testData.data(), outData.size()), 0); 105 } 106 107 /** 108 * @tc.name: server 109 * @tc.desc: write message. 110 * @tc.type: FUNC 111 */ 112 HWTEST_F(TraceFileWriterTest, WriteMessage, TestSize.Level1) 113 { 114 path = "trace-write-message.bin"; 115 auto writer = std::make_shared<TraceFileWriter>(path); 116 ASSERT_NE(writer, nullptr); 117 118 ProfilerPluginData pluginData; 119 pluginData.set_name("ABC"); 120 pluginData.set_status(0); 121 pluginData.set_data("DEF"); 122 EXPECT_GT(writer->Write(pluginData), 0); 123 } 124 } // namespace 125