From e50fbfa8ef6b859f253206d947f60c07f666ff40 Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Thu, 23 Mar 2023 16:48:10 +0100 Subject: [PATCH] iox-#1942 Implement and test FilePath Signed-off-by: Christian Eltzschig --- iceoryx_hoofs/CMakeLists.txt | 1 - .../posix/vocabulary/source/file_path.cpp | 63 +++++++++++++++++++ .../posix/vocabulary/source/group_name.cpp | 0 .../test_vocabulary_semantic_string.cpp | 43 ++++++++++++- 4 files changed, 105 insertions(+), 2 deletions(-) delete mode 100644 iceoryx_hoofs/posix/vocabulary/source/group_name.cpp diff --git a/iceoryx_hoofs/CMakeLists.txt b/iceoryx_hoofs/CMakeLists.txt index 3f7b03fe7d..59f9ea5a15 100644 --- a/iceoryx_hoofs/CMakeLists.txt +++ b/iceoryx_hoofs/CMakeLists.txt @@ -116,7 +116,6 @@ iox_add_library( posix/time/source/adaptive_wait.cpp posix/time/source/deadline_timer.cpp posix/vocabulary/source/user_name.cpp - posix/vocabulary/source/group_name.cpp posix/vocabulary/source/file_name.cpp posix/vocabulary/source/file_path.cpp posix/vocabulary/source/path.cpp diff --git a/iceoryx_hoofs/posix/vocabulary/source/file_path.cpp b/iceoryx_hoofs/posix/vocabulary/source/file_path.cpp index e69de29bb2..03acb5785a 100644 --- a/iceoryx_hoofs/posix/vocabulary/source/file_path.cpp +++ b/iceoryx_hoofs/posix/vocabulary/source/file_path.cpp @@ -0,0 +1,63 @@ +// Copyright (c) 2023 by Apex.AI Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: Apache-2.0 + +#include "iox/file_path.hpp" +#include "iox/filesystem.hpp" + +namespace iox +{ +namespace details +{ +bool file_path_does_contain_invalid_characters(const string& value) noexcept +{ + const auto valueSize = value.size(); + + for (uint64_t i{0}; i < valueSize; ++i) + { + // AXIVION Next Construct AutosarC++19_03-A3.9.1: Not used as an integer but as actual character + const char c{value[i]}; + + const bool isSmallLetter{'a' <= c && c <= 'z'}; + const bool isCapitalLetter{'A' <= c && c <= 'Z'}; + const bool isNumber{'0' <= c && c <= '9'}; + const bool isSpecialCharacter{c == '-' || c == '.' || c == ':' || c == '_'}; + const bool isPathSeparator{[&] { + for (const auto separator : platform::IOX_PATH_SEPARATORS) + { + if (c == separator) + { + return true; + } + } + return false; + }()}; + + if ((!isSmallLetter && !isCapitalLetter) && (!isNumber && !isSpecialCharacter) && !isPathSeparator) + { + return true; + } + } + + return false; +} + +bool file_path_does_contain_invalid_content(const string& value) noexcept +{ + return !isValidPathToFile(value); +} +} // namespace details +} // namespace iox + diff --git a/iceoryx_hoofs/posix/vocabulary/source/group_name.cpp b/iceoryx_hoofs/posix/vocabulary/source/group_name.cpp deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/iceoryx_hoofs/test/moduletests/test_vocabulary_semantic_string.cpp b/iceoryx_hoofs/test/moduletests/test_vocabulary_semantic_string.cpp index c3c79c8d75..9ed5a2aada 100644 --- a/iceoryx_hoofs/test/moduletests/test_vocabulary_semantic_string.cpp +++ b/iceoryx_hoofs/test/moduletests/test_vocabulary_semantic_string.cpp @@ -18,6 +18,7 @@ #include "iceoryx_hoofs/testing/fatal_failure.hpp" #include "iceoryx_platform/platform_settings.hpp" #include "iox/file_name.hpp" +#include "iox/file_path.hpp" #include "iox/group_name.hpp" #include "iox/semantic_string.hpp" #include "iox/user_name.hpp" @@ -127,6 +128,46 @@ const std::string TestValues::MAX_CAPACITY_VALUE{std::string(platform: // END: FileName /////////////////// +/////////////////// +// START: FilePath +/////////////////// +template <> +const uint64_t TestValues::CAPACITY = platform::IOX_MAX_PATH_LENGTH; +template <> +const std::vector TestValues::VALID_VALUES{{"file"}, + {"another_file.bla"}, + {"123.456"}, + {".hidden_me"}, + {"/some/file/path"}, + {"./relative/path"}, + {"another/../../relative/path"}, + {"another/../...bla"}, + {"not/yet/another/path/../fuu"}}; +template <> +const std::vector TestValues::INVALID_CHARACTER_VALUES{{"some-!user"}, + {"*kasjd"}, + {"$_fuuuas"}, + {";'1'fuuuu"}, + {"so*me/path/to/."}, + {"/some/pa)th/to/."}, + {"another/relative/pa]th/at/the/end/.."}}; +template <> +const std::vector TestValues::INVALID_CONTENT_VALUES{ + {""}, {"."}, {".."}, {"stop/with/relative/.."}, "another/relative/part/at/the/end/."}; +template <> +const std::vector TestValues::TOO_LONG_CONTENT_VALUES{ + std::string(platform::IOX_MAX_PATH_LENGTH + 2, 'a')}; +template <> +const std::string TestValues::GREATER_VALID_VALUE{"9-i-am-a-file"}; +template <> +const std::string TestValues::SMALLER_VALID_VALUE{"0.me.too.be.file"}; +template <> +const std::string TestValues::MAX_CAPACITY_VALUE{std::string(platform::IOX_MAX_PATH_LENGTH, 'b')}; +/////////////////// +// END: FilePath +/////////////////// + + template class SemanticString_test : public Test { @@ -154,7 +195,7 @@ class SemanticString_test : public Test SutType smaller_value = SutType::create(smaller_value_str).expect("Failed to create test string."); }; -using Implementations = Types; +using Implementations = Types; TYPED_TEST_SUITE(SemanticString_test, Implementations, );