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 #include <fstream> 16 #include <hwext/gtest-ext.h> 17 #include <hwext/gtest-tag.h> 18 #include <random> 19 #include <string> 20 21 #include "ftrace_data_reader.h" 22 23 using FTRACE_NS::FtraceDataReader; 24 using testing::ext::TestSize; 25 26 namespace { 27 constexpr char RANDOM_CHAR_TABLE[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 28 constexpr long RANDOM_CHAR_NUMBER = std::size(RANDOM_CHAR_TABLE) - 1; 29 constexpr int RAW_DATA_SIZE = 1024 * 1024; 30 #ifndef PAGE_SIZE 31 constexpr uint32_t PAGE_SIZE = 4096; 32 #endif 33 34 class FtraceDataReaderTest : public ::testing::Test { 35 protected: 36 std::mt19937 gen_; 37 std::string path_ = "raw_data.bin"; 38 long dataSize_ = RAW_DATA_SIZE; 39 SetUp()40 void SetUp() override 41 { 42 std::ofstream fout(path_); 43 std::string data = RandomString(dataSize_); 44 fout.write(data.data(), data.size()); 45 } 46 TearDown()47 void TearDown() override 48 { 49 unlink(path_.c_str()); 50 } 51 RandomInt(int a,int b)52 int RandomInt(int a, int b) 53 { 54 std::uniform_int_distribution<int> distrib(a, b); 55 return distrib(gen_); 56 } 57 RandomChar()58 char RandomChar() 59 { 60 return RANDOM_CHAR_TABLE[RandomInt(0, RANDOM_CHAR_NUMBER - 1)]; 61 } 62 RandomString(int len)63 std::string RandomString(int len) 64 { 65 std::string str; 66 str.reserve(len); 67 for (int i = 0; i < len; i++) { 68 str.push_back(RandomChar()); 69 } 70 return str; 71 } 72 }; 73 74 /* 75 * @tc.name: ReadNormal 76 * @tc.desc: test FtraceDataReader::Read with normal case. 77 * @tc.type: FUNC 78 */ 79 HWTEST_F(FtraceDataReaderTest, ReadNormal, TestSize.Level1) 80 { 81 FtraceDataReader reader(path_); 82 std::vector<uint8_t> zeros(PAGE_SIZE, 0); 83 std::vector<uint8_t> buffer(PAGE_SIZE, 0); 84 85 for (long i = 0; i < dataSize_; i += buffer.size()) { 86 long bufferSize = static_cast<long>(buffer.size()); 87 EXPECT_EQ(reader.Read(buffer.data(), buffer.size()), bufferSize); 88 EXPECT_NE(buffer, zeros); 89 } 90 } 91 } // namespace