|
Packit |
534379 |
// Copyright(c) 2019-2020, Intel Corporation
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
534379 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
534379 |
// this list of conditions and the following disclaimer.
|
|
Packit |
534379 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
534379 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
534379 |
// and/or other materials provided with the distribution.
|
|
Packit |
534379 |
// * Neither the name of Intel Corporation nor the names of its contributors
|
|
Packit |
534379 |
// may be used to endorse or promote products derived from this software
|
|
Packit |
534379 |
// without specific prior written permission.
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
534379 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
534379 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
534379 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
534379 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
534379 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
534379 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
534379 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
534379 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
534379 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
534379 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#include "libbitstream/bits_utils.h"
|
|
Packit |
534379 |
|
|
Packit |
534379 |
extern "C" {
|
|
Packit |
534379 |
|
|
Packit |
534379 |
bool opae_bitstream_path_invalid_chars(const char *path,
|
|
Packit |
534379 |
size_t len);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
bool opae_bitstream_path_not_file(const char *path);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
bool opae_bitstream_path_contains_dotdot(const char *path,
|
|
Packit |
534379 |
size_t len);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
bool opae_bitstream_path_contains_symlink(const char *path,
|
|
Packit |
534379 |
size_t len);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#include <config.h>
|
|
Packit |
534379 |
#include <opae/fpga.h>
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#include "gtest/gtest.h"
|
|
Packit |
534379 |
#include "mock/test_system.h"
|
|
Packit |
534379 |
|
|
Packit |
534379 |
using namespace opae::testing;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
class bits_utils_c_p : public ::testing::TestWithParam<std::string> {
|
|
Packit |
534379 |
protected:
|
|
Packit |
534379 |
|
|
Packit |
534379 |
virtual void SetUp() override {
|
|
Packit |
534379 |
std::string platform_key = GetParam();
|
|
Packit |
534379 |
ASSERT_TRUE(test_platform::exists(platform_key));
|
|
Packit |
534379 |
platform_ = test_platform::get(platform_key);
|
|
Packit |
534379 |
system_ = test_system::instance();
|
|
Packit |
534379 |
system_->initialize();
|
|
Packit |
534379 |
system_->prepare_syfs(platform_);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
j_root_ = nullptr;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
virtual void TearDown() override {
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (j_root_)
|
|
Packit |
534379 |
json_object_put(j_root_);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
system_->finalize();
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
json_object *parse(const char *json_str)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
enum json_tokener_error j_err = json_tokener_success;
|
|
Packit |
534379 |
return j_root_ = json_tokener_parse_verbose(json_str, &j_err);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
json_object *j_root_;
|
|
Packit |
534379 |
test_platform platform_;
|
|
Packit |
534379 |
test_system *system_;
|
|
Packit |
534379 |
};
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test string_err0
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_get_json_string
|
|
Packit |
534379 |
* @details If the given name doesn't exist,
|
|
Packit |
534379 |
* the fn returns FPGA_EXCEPTION.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, string_err0) {
|
|
Packit |
534379 |
const char *mdata =
|
|
Packit |
534379 |
R"mdata({
|
|
Packit |
534379 |
"a": "foo"
|
|
Packit |
534379 |
})mdata";
|
|
Packit |
534379 |
json_object *root;
|
|
Packit |
534379 |
char *value = nullptr;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
root = parse(mdata);
|
|
Packit |
534379 |
ASSERT_NE(root, nullptr);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_EQ(opae_bitstream_get_json_string(root,
|
|
Packit |
534379 |
"b",
|
|
Packit |
534379 |
&value),
|
|
Packit |
534379 |
FPGA_EXCEPTION);
|
|
Packit |
534379 |
EXPECT_EQ(value, nullptr);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test string_err1
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_get_json_string
|
|
Packit |
534379 |
* @details If the given name exists,
|
|
Packit |
534379 |
* but isn't a string,
|
|
Packit |
534379 |
* the fn returns FPGA_EXCEPTION.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, string_err1) {
|
|
Packit |
534379 |
const char *mdata =
|
|
Packit |
534379 |
R"mdata({
|
|
Packit |
534379 |
"a": 3
|
|
Packit |
534379 |
})mdata";
|
|
Packit |
534379 |
json_object *root;
|
|
Packit |
534379 |
char *value = nullptr;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
root = parse(mdata);
|
|
Packit |
534379 |
ASSERT_NE(root, nullptr);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_EQ(opae_bitstream_get_json_string(root,
|
|
Packit |
534379 |
"a",
|
|
Packit |
534379 |
&value),
|
|
Packit |
534379 |
FPGA_EXCEPTION);
|
|
Packit |
534379 |
EXPECT_EQ(value, nullptr);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test int_err0
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_get_json_int
|
|
Packit |
534379 |
* @details If the given name doesn't exist,
|
|
Packit |
534379 |
* the fn returns FPGA_EXCEPTION.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, int_err0) {
|
|
Packit |
534379 |
const char *mdata =
|
|
Packit |
534379 |
R"mdata({
|
|
Packit |
534379 |
"a": 3
|
|
Packit |
534379 |
})mdata";
|
|
Packit |
534379 |
json_object *root;
|
|
Packit |
534379 |
int value = 0;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
root = parse(mdata);
|
|
Packit |
534379 |
ASSERT_NE(root, nullptr);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_EQ(opae_bitstream_get_json_int(root,
|
|
Packit |
534379 |
"b",
|
|
Packit |
534379 |
&value),
|
|
Packit |
534379 |
FPGA_EXCEPTION);
|
|
Packit |
534379 |
EXPECT_EQ(value, 0);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test int_err1
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_get_json_int
|
|
Packit |
534379 |
* @details If the given name exists,
|
|
Packit |
534379 |
* but isn't of type integer,
|
|
Packit |
534379 |
* the fn returns FPGA_EXCEPTION.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, int_err1) {
|
|
Packit |
534379 |
const char *mdata =
|
|
Packit |
534379 |
R"mdata({
|
|
Packit |
534379 |
"a": "str"
|
|
Packit |
534379 |
})mdata";
|
|
Packit |
534379 |
json_object *root;
|
|
Packit |
534379 |
int value = 0;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
root = parse(mdata);
|
|
Packit |
534379 |
ASSERT_NE(root, nullptr);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_EQ(opae_bitstream_get_json_int(root,
|
|
Packit |
534379 |
"a",
|
|
Packit |
534379 |
&value),
|
|
Packit |
534379 |
FPGA_EXCEPTION);
|
|
Packit |
534379 |
EXPECT_EQ(value, 0);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test double_err0
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_get_json_double
|
|
Packit |
534379 |
* @details If the given name doesn't exist,
|
|
Packit |
534379 |
* the fn returns FPGA_EXCEPTION.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, double_err0) {
|
|
Packit |
534379 |
const char *mdata =
|
|
Packit |
534379 |
R"mdata({
|
|
Packit |
534379 |
"a": 3.14
|
|
Packit |
534379 |
})mdata";
|
|
Packit |
534379 |
json_object *root;
|
|
Packit |
534379 |
double value = 0.0;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
root = parse(mdata);
|
|
Packit |
534379 |
ASSERT_NE(root, nullptr);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_EQ(opae_bitstream_get_json_double(root,
|
|
Packit |
534379 |
"b",
|
|
Packit |
534379 |
&value),
|
|
Packit |
534379 |
FPGA_EXCEPTION);
|
|
Packit |
534379 |
EXPECT_EQ(value, 0.0);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test double_err1
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_get_json_double
|
|
Packit |
534379 |
* @details If the given name exists,
|
|
Packit |
534379 |
* but isn't of type double,
|
|
Packit |
534379 |
* the fn returns FPGA_EXCEPTION.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, double_err1) {
|
|
Packit |
534379 |
const char *mdata =
|
|
Packit |
534379 |
R"mdata({
|
|
Packit |
534379 |
"a": "str"
|
|
Packit |
534379 |
})mdata";
|
|
Packit |
534379 |
json_object *root;
|
|
Packit |
534379 |
double value = 0.0;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
root = parse(mdata);
|
|
Packit |
534379 |
ASSERT_NE(root, nullptr);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_EQ(opae_bitstream_get_json_double(root,
|
|
Packit |
534379 |
"a",
|
|
Packit |
534379 |
&value),
|
|
Packit |
534379 |
FPGA_EXCEPTION);
|
|
Packit |
534379 |
EXPECT_EQ(value, 0.0);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test double_err2
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_get_json_double
|
|
Packit |
534379 |
* @details If the given name exists,
|
|
Packit |
534379 |
* but isn't of type double,
|
|
Packit |
534379 |
* the fn returns FPGA_EXCEPTION.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, double_err2) {
|
|
Packit |
534379 |
const char *mdata =
|
|
Packit |
534379 |
R"mdata({
|
|
Packit |
534379 |
"a": 42
|
|
Packit |
534379 |
})mdata";
|
|
Packit |
534379 |
json_object *root;
|
|
Packit |
534379 |
double value = 0.0;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
root = parse(mdata);
|
|
Packit |
534379 |
ASSERT_NE(root, nullptr);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_EQ(opae_bitstream_get_json_double(root,
|
|
Packit |
534379 |
"a",
|
|
Packit |
534379 |
&value),
|
|
Packit |
534379 |
FPGA_EXCEPTION);
|
|
Packit |
534379 |
EXPECT_EQ(value, 0.0);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test invalid_chars0
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_invalid_chars
|
|
Packit |
534379 |
* @details Given a path that contains non-printable chars,
|
|
Packit |
534379 |
* the fn returns true.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, invalid_chars0) {
|
|
Packit |
534379 |
const char *p;
|
|
Packit |
534379 |
p = "\x01\x05xyz.gbs";
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_invalid_chars(p, strlen(p)));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test invalid_chars1
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_invalid_chars
|
|
Packit |
534379 |
* @details Given a path that contains URL encoding,
|
|
Packit |
534379 |
* the fn returns true.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, invalid_chars1) {
|
|
Packit |
534379 |
const char *p;
|
|
Packit |
534379 |
p = "my%2E.gbs";
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_invalid_chars(p, strlen(p)));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test invalid_chars2
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_invalid_chars
|
|
Packit |
534379 |
* @details Given a path that contains no invalid chars,
|
|
Packit |
534379 |
* the fn returns false.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, invalid_chars2) {
|
|
Packit |
534379 |
const char *p;
|
|
Packit |
534379 |
p = "abc.gbs";
|
|
Packit |
534379 |
EXPECT_FALSE(opae_bitstream_path_invalid_chars(p, strlen(p)));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test not_file0
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_not_file
|
|
Packit |
534379 |
* @details Given a path to a file that doesn't exist,
|
|
Packit |
534379 |
* the fn returns true.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, not_file0) {
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_not_file("doesntexist"));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test not_file1
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_not_file
|
|
Packit |
534379 |
* @details Given a path to a directory,
|
|
Packit |
534379 |
* the fn returns true.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, not_file1) {
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_not_file("/"));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test not_file2
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_not_file
|
|
Packit |
534379 |
* @details Given a path to valid file,
|
|
Packit |
534379 |
* the fn returns false.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, not_file2) {
|
|
Packit |
534379 |
char tmpfile[20];
|
|
Packit |
534379 |
|
|
Packit |
534379 |
strcpy(tmpfile, "tmp-XXXXXX.gbs");
|
|
Packit |
534379 |
close(mkstemps(tmpfile, 4));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_FALSE(opae_bitstream_path_not_file(tmpfile));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
unlink(tmpfile);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test dotdot0
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_contains_dotdot
|
|
Packit |
534379 |
* @details Given a path that contains a reference to
|
|
Packit |
534379 |
* the special directory designator ..
|
|
Packit |
534379 |
* the fn returns true.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, dotdot0) {
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_contains_dotdot("..", 2));
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_contains_dotdot("../", 3));
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_contains_dotdot("../abc.gbs", 10));
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_contains_dotdot("my/../abc.gbs", 13));
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_contains_dotdot("my/..", 5));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test dotdot1
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_contains_dotdot
|
|
Packit |
534379 |
* @details Given a path that contains the character sequence '..',
|
|
Packit |
534379 |
* if that character sequence does not designate the parent dir,
|
|
Packit |
534379 |
* the fn returns false.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, dotdot1) {
|
|
Packit |
534379 |
EXPECT_FALSE(opae_bitstream_path_contains_dotdot("my..gbs", 7));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test symlink0
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_contains_symlink
|
|
Packit |
534379 |
* @details If the given path string is empty,
|
|
Packit |
534379 |
* then the fn returns true.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, symlink0) {
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_contains_symlink("", 0));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test symlink1
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_contains_symlink
|
|
Packit |
534379 |
* @details If the given file name doesn't exist,
|
|
Packit |
534379 |
* then the fn returns true.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, symlink1) {
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_contains_symlink("doesntexist", 11));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test symlink2
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_contains_symlink
|
|
Packit |
534379 |
* @details If the given file name exists,
|
|
Packit |
534379 |
* and it does not contain any / characters,
|
|
Packit |
534379 |
* and it is a symlink,
|
|
Packit |
534379 |
* then the fn returns true.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, symlink2) {
|
|
Packit |
534379 |
char tmpfile[20];
|
|
Packit |
534379 |
|
|
Packit |
534379 |
strcpy(tmpfile, "tmp-XXXXXX.gbs");
|
|
Packit |
534379 |
close(mkstemps(tmpfile, 4));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
ASSERT_EQ(symlink(tmpfile, "mylink"), 0);
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_contains_symlink("mylink", 6));
|
|
Packit |
534379 |
unlink("mylink");
|
|
Packit |
534379 |
unlink(tmpfile);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test symlink3
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_contains_symlink
|
|
Packit |
534379 |
* @details If the given file name exists,
|
|
Packit |
534379 |
* and it does not contain a / character in position 0,
|
|
Packit |
534379 |
* and there is a symlink in any of the path components,
|
|
Packit |
534379 |
* then the fn returns true.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, symlink3) {
|
|
Packit |
534379 |
char tmpfile[20];
|
|
Packit |
534379 |
|
|
Packit |
534379 |
strcpy(tmpfile, "tmp-XXXXXX.gbs");
|
|
Packit |
534379 |
close(mkstemps(tmpfile, 4));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
std::string s;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_EQ(std::system("rm -rf bar"), 0);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// bar/baz/foo -> tmpfile
|
|
Packit |
534379 |
ASSERT_EQ(mkdir("bar", 0755), 0);
|
|
Packit |
534379 |
ASSERT_EQ(mkdir("bar/baz", 0755), 0);
|
|
Packit |
534379 |
s = std::string("../../") + std::string(tmpfile);
|
|
Packit |
534379 |
ASSERT_EQ(symlink(s.c_str(), "bar/baz/foo"), 0);
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_contains_symlink("bar/baz/foo", 11));
|
|
Packit |
534379 |
ASSERT_EQ(unlink("bar/baz/foo"), 0);
|
|
Packit |
534379 |
ASSERT_EQ(rmdir("bar/baz"), 0);
|
|
Packit |
534379 |
ASSERT_EQ(rmdir("bar"), 0);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// bar/baz -> ../
|
|
Packit |
534379 |
ASSERT_EQ(mkdir("bar", 0755), 0);
|
|
Packit |
534379 |
ASSERT_EQ(symlink("..", "bar/baz"), 0);
|
|
Packit |
534379 |
s = std::string("bar/baz/") + std::string(tmpfile);
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_contains_symlink(s.c_str(), strlen(s.c_str())));
|
|
Packit |
534379 |
ASSERT_EQ(unlink("bar/baz"), 0);
|
|
Packit |
534379 |
ASSERT_EQ(rmdir("bar"), 0);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// bar -> blah which contains baz, which contains the config file
|
|
Packit |
534379 |
ASSERT_EQ(mkdir("blah", 0755), 0);
|
|
Packit |
534379 |
ASSERT_EQ(mkdir("blah/baz", 0755), 0);
|
|
Packit |
534379 |
s = std::string("blah/baz/") + std::string(tmpfile);
|
|
Packit |
534379 |
ASSERT_EQ(rename(tmpfile, s.c_str()), 0);
|
|
Packit |
534379 |
ASSERT_EQ(symlink("blah", "bar"), 0);
|
|
Packit |
534379 |
s = std::string("bar/baz/") + std::string(tmpfile);
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_contains_symlink(s.c_str(), strlen(s.c_str())));
|
|
Packit |
534379 |
ASSERT_EQ(rename(s.c_str(), tmpfile), 0);
|
|
Packit |
534379 |
ASSERT_EQ(rmdir("blah/baz"), 0);
|
|
Packit |
534379 |
ASSERT_EQ(rmdir("blah"), 0);
|
|
Packit |
534379 |
ASSERT_EQ(unlink("bar"), 0);
|
|
Packit |
534379 |
ASSERT_EQ(unlink(tmpfile), 0);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test symlink4
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_contains_symlink
|
|
Packit |
534379 |
* @details If the given file name exists,
|
|
Packit |
534379 |
* and it contains a / character in position 0,
|
|
Packit |
534379 |
* and there is a symlink in any of the path components,
|
|
Packit |
534379 |
* then the fn returns true.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, symlink4) {
|
|
Packit |
534379 |
char tmpfile[20];
|
|
Packit |
534379 |
|
|
Packit |
534379 |
strcpy(tmpfile, "tmp-XXXXXX.gbs");
|
|
Packit |
534379 |
close(mkstemps(tmpfile, 4));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
std::string s;
|
|
Packit |
534379 |
char *d = get_current_dir_name();
|
|
Packit |
534379 |
|
|
Packit |
534379 |
ASSERT_NE(d, nullptr);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// /current/dir/foo -> cfg file
|
|
Packit |
534379 |
ASSERT_EQ(symlink(tmpfile, "foo"), 0);
|
|
Packit |
534379 |
s = std::string(d) + std::string("/foo");
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_contains_symlink(s.c_str(), strlen(s.c_str())));
|
|
Packit |
534379 |
ASSERT_EQ(unlink("foo"), 0);
|
|
Packit |
534379 |
ASSERT_EQ(unlink(tmpfile), 0);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
free(d);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test symlink5
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_contains_symlink
|
|
Packit |
534379 |
* @details If the given file name exists and is a regular file,
|
|
Packit |
534379 |
* then the fn returns false.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, symlink5) {
|
|
Packit |
534379 |
char tmpfile[20];
|
|
Packit |
534379 |
|
|
Packit |
534379 |
strcpy(tmpfile, "tmp-XXXXXX.gbs");
|
|
Packit |
534379 |
close(mkstemps(tmpfile, 4));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_FALSE(opae_bitstream_path_contains_symlink(tmpfile, strlen(tmpfile)));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
ASSERT_EQ(unlink(tmpfile), 0);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test is_valid0
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_is_valid
|
|
Packit |
534379 |
* @details If the given path pointer is NULL or
|
|
Packit |
534379 |
* points to the empty string,
|
|
Packit |
534379 |
* then the fn returns false.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, is_valid0) {
|
|
Packit |
534379 |
EXPECT_FALSE(opae_bitstream_path_is_valid(NULL, 0));
|
|
Packit |
534379 |
EXPECT_FALSE(opae_bitstream_path_is_valid("", 0));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test is_valid1
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_is_valid
|
|
Packit |
534379 |
* @details If the given path contains non-printable characters,
|
|
Packit |
534379 |
* then the fn returns false.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, is_valid1) {
|
|
Packit |
534379 |
const char *p = "\x01ijk.gbs";
|
|
Packit |
534379 |
EXPECT_FALSE(opae_bitstream_path_is_valid(p, 0));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test is_valid2
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_is_valid
|
|
Packit |
534379 |
* @details If the given path doesn't exist,
|
|
Packit |
534379 |
* then the fn returns false.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, is_valid2) {
|
|
Packit |
534379 |
EXPECT_FALSE(opae_bitstream_path_is_valid("doesntexist", 0));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test is_valid3
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_is_valid
|
|
Packit |
534379 |
* @details If the given flags parameter does not contain
|
|
Packit |
534379 |
* OPAE_BITSTREAM_PATH_NO_PARENT,
|
|
Packit |
534379 |
* and the special parent directory indicator ..
|
|
Packit |
534379 |
* appears in the path, then the fn returns true.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, is_valid3) {
|
|
Packit |
534379 |
char tmpfile[32];
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_EQ(std::system("rm -rf bar"), 0);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
ASSERT_EQ(mkdir("bar", 0755), 0);
|
|
Packit |
534379 |
strcpy(tmpfile, "tmp-XXXXXX.gbs");
|
|
Packit |
534379 |
close(mkstemps(tmpfile, 4));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
std::string s = std::string("bar/../") + std::string(tmpfile);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_is_valid(s.c_str(), 0));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
ASSERT_EQ(unlink(tmpfile), 0);
|
|
Packit |
534379 |
ASSERT_EQ(rmdir("bar"), 0);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test is_valid4
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_is_valid
|
|
Packit |
534379 |
* @details If the given flags parameter contains
|
|
Packit |
534379 |
* OPAE_BITSTREAM_PATH_NO_PARENT,
|
|
Packit |
534379 |
* and the special parent directory indicator ..
|
|
Packit |
534379 |
* appears in the path, then the fn returns false.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, is_valid4) {
|
|
Packit |
534379 |
char tmpfile[32];
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_EQ(std::system("rm -rf bar"), 0);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
ASSERT_EQ(mkdir("bar", 0755), 0);
|
|
Packit |
534379 |
strcpy(tmpfile, "tmp-XXXXXX.gbs");
|
|
Packit |
534379 |
close(mkstemps(tmpfile, 4));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
std::string s = std::string("bar/../") + std::string(tmpfile);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_FALSE(opae_bitstream_path_is_valid(s.c_str(),
|
|
Packit |
534379 |
OPAE_BITSTREAM_PATH_NO_PARENT));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
ASSERT_EQ(unlink(tmpfile), 0);
|
|
Packit |
534379 |
ASSERT_EQ(rmdir("bar"), 0);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test is_valid5
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_is_valid
|
|
Packit |
534379 |
* @details If the given flags parameter does not contain
|
|
Packit |
534379 |
* OPAE_BITSTREAM_PATH_NO_SYMLINK,
|
|
Packit |
534379 |
* and the path contains a symlink component,
|
|
Packit |
534379 |
* then the fn returns true.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, is_valid5) {
|
|
Packit |
534379 |
char tmpfile[20];
|
|
Packit |
534379 |
|
|
Packit |
534379 |
strcpy(tmpfile, "tmp-XXXXXX.gbs");
|
|
Packit |
534379 |
close(mkstemps(tmpfile, 4));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
ASSERT_EQ(symlink(tmpfile, "foo"), 0);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_TRUE(opae_bitstream_path_is_valid("foo", 0));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
ASSERT_EQ(unlink("foo"), 0);
|
|
Packit |
534379 |
ASSERT_EQ(unlink(tmpfile), 0);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test is_valid6
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_path_is_valid
|
|
Packit |
534379 |
* @details If the given flags parameter contains
|
|
Packit |
534379 |
* OPAE_BITSTREAM_PATH_NO_SYMLINK,
|
|
Packit |
534379 |
* and the path contains a symlink component,
|
|
Packit |
534379 |
* then the fn returns false.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(bits_utils_c_p, is_valid6) {
|
|
Packit |
534379 |
char tmpfile[20];
|
|
Packit |
534379 |
|
|
Packit |
534379 |
strcpy(tmpfile, "tmp-XXXXXX.gbs");
|
|
Packit |
534379 |
close(mkstemps(tmpfile, 4));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
ASSERT_EQ(symlink(tmpfile, "foo"), 0);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
EXPECT_FALSE(opae_bitstream_path_is_valid("foo",
|
|
Packit |
534379 |
OPAE_BITSTREAM_PATH_NO_SYMLINK));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
ASSERT_EQ(unlink("foo"), 0);
|
|
Packit |
534379 |
ASSERT_EQ(unlink(tmpfile), 0);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
INSTANTIATE_TEST_CASE_P(bits_utils_c, bits_utils_c_p,
|
|
Packit |
534379 |
::testing::ValuesIn(test_platform::platforms({})));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
|
|
Packit |
534379 |
class mock_bits_utils_c_p : public bits_utils_c_p {};
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/**
|
|
Packit |
534379 |
* @test string_err2
|
|
Packit |
534379 |
* @brief Test: opae_bitstream_get_json_string
|
|
Packit |
534379 |
* @details If malloc fails,
|
|
Packit |
534379 |
* the fn returns FPGA_NO_MEMORY.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
TEST_P(mock_bits_utils_c_p, string_err2) {
|
|
Packit |
534379 |
const char *mdata =
|
|
Packit |
534379 |
R"mdata({
|
|
Packit |
534379 |
"a": "str"
|
|
Packit |
534379 |
})mdata";
|
|
Packit |
534379 |
json_object *root;
|
|
Packit |
534379 |
char *value = nullptr;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
root = parse(mdata);
|
|
Packit |
534379 |
ASSERT_NE(root, nullptr);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
system_->invalidate_malloc(0, "opae_bitstream_get_json_string");
|
|
Packit |
534379 |
EXPECT_EQ(opae_bitstream_get_json_string(root,
|
|
Packit |
534379 |
"a",
|
|
Packit |
534379 |
&value),
|
|
Packit |
534379 |
FPGA_NO_MEMORY);
|
|
Packit |
534379 |
EXPECT_EQ(value, nullptr);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
INSTANTIATE_TEST_CASE_P(bits_utils_c, mock_bits_utils_c_p,
|
|
Packit |
534379 |
::testing::ValuesIn(test_platform::mock_platforms({})));
|