2013-09-24 01:35:40 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/test/test_data_util.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2022-10-25 16:15:54 +00:00
|
|
|
#include "glog/logging.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-09-24 01:35:40 +00:00
|
|
|
namespace media {
|
|
|
|
|
2022-10-25 16:15:54 +00:00
|
|
|
// Returns a file path for a file in the media/test/data directory.
|
|
|
|
std::filesystem::path GetTestDataFilePath(const std::string& name) {
|
2023-07-18 18:59:21 +00:00
|
|
|
auto data_dir = std::filesystem::u8path(TEST_DATA_DIR);
|
2023-07-17 17:06:10 +00:00
|
|
|
return data_dir / name;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 16:15:54 +00:00
|
|
|
// Returns a file path for a file in the media/app/test/testdata directory.
|
|
|
|
std::filesystem::path GetAppTestDataFilePath(const std::string& name) {
|
2023-07-18 18:59:21 +00:00
|
|
|
auto data_dir = std::filesystem::u8path(TEST_DATA_DIR);
|
2023-09-09 00:44:17 +00:00
|
|
|
auto app_data_dir = data_dir.parent_path().parent_path().parent_path() /
|
|
|
|
"app" / "test" / "testdata";
|
2023-07-17 17:06:10 +00:00
|
|
|
return app_data_dir / name;
|
2017-05-31 18:14:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 16:15:54 +00:00
|
|
|
// Reads a test file from media/test/data directory and returns its content.
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> ReadTestDataFile(const std::string& name) {
|
2023-07-18 18:59:21 +00:00
|
|
|
auto path = GetTestDataFilePath(name);
|
2022-10-25 16:15:54 +00:00
|
|
|
|
|
|
|
FILE* f = fopen(path.string().c_str(), "rb");
|
|
|
|
if (!f) {
|
2023-07-17 20:16:22 +00:00
|
|
|
LOG(ERROR) << "Failed to read test data from " << path;
|
2022-10-25 16:15:54 +00:00
|
|
|
return std::vector<uint8_t>();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8_t> data;
|
|
|
|
data.resize(std::filesystem::file_size(path));
|
|
|
|
size_t size = fread(data.data(), 1, data.size(), f);
|
|
|
|
data.resize(size);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return data;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2023-08-05 03:45:21 +00:00
|
|
|
|
|
|
|
// Get the content of |file_path|. Returns empty string on error.
|
|
|
|
std::string GetPathContent(std::filesystem::path& file_path) {
|
|
|
|
std::string content;
|
|
|
|
|
|
|
|
FILE* f = fopen(file_path.string().c_str(), "rb");
|
|
|
|
if (!f) {
|
|
|
|
LOG(FATAL) << "Failed to read test data from " << file_path;
|
|
|
|
return std::string{};
|
|
|
|
}
|
|
|
|
|
|
|
|
content.resize(std::filesystem::file_size(file_path));
|
|
|
|
size_t size = fread(content.data(), 1, content.size(), f);
|
|
|
|
content.resize(size);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|