* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
* \file test_file.cpp
* \brief
*/
#include <gtest/gtest.h>
#include <string>
#include <utime.h>
#include "utils/file_utils.h"
using namespace npu::tile_fwk;
#define PATH_MAX 4096
class FileTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
void SetUp() override {}
void TearDown() override {}
};
TEST(FileTest, NullptrTest)
{
uint8_t a = 255;
std::vector<uint8_t> data{a};
SaveFile("", data);
bool ret = SaveFile("", &a, 1);
EXPECT_EQ(ret, false);
EXPECT_FALSE(CopyFile("", ""));
}
TEST(FileTest, LoadFileTest)
{
uint8_t a = 255;
std::vector<uint8_t> data{a};
data = ReadFile("");
EXPECT_EQ(data.size(), 0);
}
TEST(FileTest, ReadFileReturnsFullContents)
{
const std::string path = "/tmp/test_read_file_" + std::to_string(::getpid());
const std::vector<uint8_t> payload{0xde, 0xad, 0xbe, 0xef};
ASSERT_TRUE(SaveFile(path, payload));
const auto data = ReadFile(path);
EXPECT_EQ(data.size(), payload.size());
EXPECT_EQ(data, payload);
DeleteFile(path);
}
TEST(FileTest, RemoveOldDirectoriesKeepsNewestAndOnlyMatchingPrefix)
{
const std::string base = "/tmp/test_remove_old_dirs_" + std::to_string(::getpid());
const std::string prefix = "run_";
DeleteDir(base, true);
ASSERT_TRUE(CreateDir(base, true));
for (int i = 0; i < 3; ++i) {
const std::string d = base + "/" + prefix + std::to_string(i);
ASSERT_TRUE(CreateDir(d, true));
ASSERT_TRUE(SaveFile(d + "/f.bin", std::vector<uint8_t>{0x01}));
const struct utimbuf times {
100 + i * 100, 100 + i * 100
};
ASSERT_EQ(utime(d.c_str(), ×), 0);
}
const std::string other = base + "/unrelated";
ASSERT_TRUE(CreateDir(other, true));
RemoveOldDirectories(base, prefix, 1);
EXPECT_TRUE(IsPathExist(other));
EXPECT_FALSE(IsPathExist(base + "/run_0"));
EXPECT_FALSE(IsPathExist(base + "/run_1"));
EXPECT_TRUE(IsPathExist(base + "/run_2/f.bin"));
DeleteDir(base, true);
}
TEST(FileTest, RemoveOldDirectoriesHandlesRetentionBounds)
{
const std::string base = "/tmp/test_remove_old_dirs_bounds_" + std::to_string(::getpid());
const std::string prefix = "run_";
DeleteDir(base, true);
ASSERT_TRUE(CreateDir(base, true));
for (int i = 0; i < 3; ++i) {
ASSERT_TRUE(CreateDir(base + "/" + prefix + std::to_string(i), true));
}
const std::string other = base + "/unrelated";
ASSERT_TRUE(CreateDir(other, true));
const std::string file = base + "/run_file";
ASSERT_TRUE(SaveFile(file, std::vector<uint8_t>{0x01}));
for (size_t kept : {4, 3}) {
RemoveOldDirectories(base, prefix, kept);
for (int i = 0; i < 3; ++i) {
EXPECT_TRUE(IsPathExist(base + "/" + prefix + std::to_string(i)));
}
EXPECT_TRUE(IsPathExist(other));
EXPECT_TRUE(IsPathExist(file));
}
RemoveOldDirectories(base, prefix, 0);
for (int i = 0; i < 3; ++i) {
EXPECT_FALSE(IsPathExist(base + "/" + prefix + std::to_string(i)));
}
EXPECT_TRUE(IsPathExist(other));
EXPECT_TRUE(IsPathExist(file));
DeleteDir(base, true);
}
TEST(FileTest, RealPathTest)
{
EXPECT_TRUE(RealPath("").empty());
EXPECT_TRUE(RealPath("/nonexitent/file").empty());
EXPECT_FALSE(RealPath("/tmp").empty());
EXPECT_TRUE(RealPath(std::string(PATH_MAX, 'a')).empty());
EXPECT_FALSE(CreateDir(std::string(PATH_MAX, 'a')));
}
TEST(FileTest, CreateDirFailedWhenParentNotExist)
{
std::string path = "/tmp/no_such_parent_dir" + std::to_string(::getpid()) + "/child";
EXPECT_FALSE(CreateDir(path));
}
TEST(FileTest, CreateDirHandlesConsecutiveSlashes)
{
const std::string base = "/tmp/create_dir_slashes_" + std::to_string(::getpid());
const std::string path = base + "///a///b//c";
DeleteDir(base);
ASSERT_TRUE(CreateDir(path, true));
EXPECT_TRUE(IsPathExist(path));
EXPECT_TRUE(CreateDir(path, true));
EXPECT_TRUE(CreateDir(path + "/", true));
DeleteDir(base, true);
EXPECT_TRUE(CreateDir("/tmp", true));
}
TEST(FileTest, CreateMultiLevelDirFailedWhenParentIsFile)
{
const std::string parent = "/tmp/parent_is_file_" + std::to_string(::getpid());
const std::string target = parent + "/child";
DeleteDir(target);
DeleteFile(parent);
SaveFile(parent, std::vector<uint8_t>{0x01});
EXPECT_FALSE(CreateDir(target, true));
DeleteDir(target, true);
DeleteFile(parent);
}