Skip to content

Commit

Permalink
[Core] port PR-22525: fix user plugins.xml path issue
Browse files Browse the repository at this point in the history
 - Fixed user rename plugins.xml cannot be found issue

 - CVS-131048
  • Loading branch information
riverlijunjie committed Feb 6, 2024
1 parent 4a1decc commit 11e48d5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/inference/src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@
#include "openvino/core/so_extension.hpp"
#include "openvino/runtime/device_id_parser.hpp"
#include "openvino/runtime/iremote_context.hpp"
#include "openvino/util/common_util.hpp"
#include "openvino/util/file_util.hpp"

namespace ov {

std::string find_plugins_xml(const std::string& xml_file) {
std::string xml_file_name = xml_file;
if (xml_file_name.empty()) {
// Default plugin xml file name.
// Default plugin xml file name, will search in OV folder.
xml_file_name = "plugins.xml";
} else {
// Exclude relative path
const auto relative_path_symbol = std::string("..") + util::FileTraits<char>().file_separator;
if (xml_file_name.find(relative_path_symbol) != xml_file_name.npos) {
OPENVINO_THROW("Unsupport plugin xml relative path: ", xml_file_name.c_str());
// User can set any path for plugins xml file but need guarantee security issue if apply file path out of OV
// folder.
// If the xml file exists or file path contains file separator, return file path;
// Else search it in OV folder with no restriction on file name and extension.
if (ov::util::file_exists(xml_file_name) ||
xml_file_name.find(util::FileTraits<char>().file_separator) != xml_file_name.npos) {
return xml_file_name;
}
}
const auto ov_library_path = ov::util::get_ov_lib_path();
Expand Down
81 changes: 81 additions & 0 deletions src/inference/tests/functional/ov_core_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <gtest/gtest.h>

#include <fstream>

#include "common_test_utils/common_utils.hpp"
#include "common_test_utils/file_utils.hpp"
#include "openvino/runtime/core.hpp"
#include "openvino/util/file_util.hpp"

#ifndef OPENVINO_STATIC_LIBRARY

static void create_plugin_xml(const std::string& file_name) {
std::ofstream file(file_name);

file << "<ie><plugins><plugin location=\"";
file << ov::test::utils::getExecutableDirectory();
file << ov::util::FileTraits<char>::file_separator;
file << ov::util::FileTraits<char>::library_prefix();
file << "mock_engine";
file << OV_BUILD_POSTFIX;
file << ov::util::FileTraits<char>::dot_symbol;
file << ov::util::FileTraits<char>::library_ext();
file << "\" name=\"1\"></plugin></plugins></ie>";
file.flush();
file.close();
}

static void remove_plugin_xml(const std::string& file_name) {
ov::test::utils::removeFile(file_name);
}

TEST(CoreBaseTest, LoadPluginXML) {
std::string xml_file_name = "test_plugin.xml";
std::string xml_file_path =
ov::test::utils::getOpenvinoLibDirectory() + ov::util::FileTraits<char>::file_separator + xml_file_name;
create_plugin_xml(xml_file_path);
EXPECT_NO_THROW(ov::Core core(xml_file_name));
remove_plugin_xml(xml_file_path);
}

TEST(CoreBaseTest, LoadPluginDifferentXMLExtension) {
std::string xml_file_name = "test_plugin.test";
std::string xml_file_path =
ov::test::utils::getOpenvinoLibDirectory() + ov::util::FileTraits<char>::file_separator + xml_file_name;
create_plugin_xml(xml_file_path);
EXPECT_NO_THROW(ov::Core core(xml_file_name));
remove_plugin_xml(xml_file_path);
}

TEST(CoreBaseTest, LoadAbsoluteOVPathPluginXML) {
std::string xml_file_name = "test_plugin.xml";
std::string xml_file_path =
ov::test::utils::getOpenvinoLibDirectory() + ov::util::FileTraits<char>::file_separator + xml_file_name;
create_plugin_xml(xml_file_path);
EXPECT_NO_THROW(ov::Core core(xml_file_path));
remove_plugin_xml(xml_file_path);
}

TEST(CoreBaseTest, LoadAbsoluteCWPathPluginXML) {
std::string xml_file_name = "test_plugin.xml";
std::string xml_file_path =
ov::test::utils::getCurrentWorkingDir() + ov::util::FileTraits<char>::file_separator + xml_file_name;
create_plugin_xml(xml_file_path);
EXPECT_NO_THROW(ov::Core core(xml_file_path));
remove_plugin_xml(xml_file_path);
}

TEST(CoreBaseTest, LoadRelativeCWPathPluginXML) {
std::string xml_file_name = "test_plugin.xml";
std::string xml_file_path =
ov::test::utils::getCurrentWorkingDir() + ov::util::FileTraits<char>::file_separator + xml_file_name;
create_plugin_xml(xml_file_path);
EXPECT_NO_THROW(ov::Core core(xml_file_name));
remove_plugin_xml(xml_file_path);
}

#endif

0 comments on commit 11e48d5

Please sign in to comment.