From f1289b6cc0d091dce29a7beada5a532c25a979ef Mon Sep 17 00:00:00 2001 From: Marcin Siodelski Date: Thu, 31 Mar 2016 10:44:42 +0200 Subject: [PATCH] [4300] Added utility function to convert string in quotes to vector. --- src/lib/util/strutil.cc | 20 ++++++++++++- src/lib/util/strutil.h | 21 +++++++++++++- src/lib/util/tests/strutil_unittest.cc | 40 +++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/src/lib/util/strutil.cc b/src/lib/util/strutil.cc index 846661c55d..0bcdea1540 100644 --- a/src/lib/util/strutil.cc +++ b/src/lib/util/strutil.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -133,6 +133,24 @@ getToken(std::istringstream& iss) { return (token); } +std::vector +quotedStringToBinary(const std::string& quoted_string) { + std::vector binary; + // Remove whitespace before and after the quotes. + std::string trimmed_string = trim(quoted_string); + + // We require two quote characters, so the length of the string must be + // equal to 2 at minimum, and it must start and end with quotes. + if ((trimmed_string.length() > 1) && ((trimmed_string[0] == '\'') && + (trimmed_string[trimmed_string.length()-1] == '\''))) { + // Remove quotes and trim the text inside the quotes. + trimmed_string = trim(trimmed_string.substr(1, trimmed_string.length() - 2)); + // Copy string contents into the vector. + binary.assign(trimmed_string.begin(), trimmed_string.end()); + } + // Return resulting vector or empty vector. + return (binary); +} } // namespace str } // namespace util diff --git a/src/lib/util/strutil.h b/src/lib/util/strutil.h index 1294f22102..73cd129176 100644 --- a/src/lib/util/strutil.h +++ b/src/lib/util/strutil.h @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -194,6 +194,25 @@ tokenToNum(const std::string& num_token) { return (num); } +/// \brief Converts a string in quotes into vector. +/// +/// A converted string is first trimmed. If a trimmed string is in +/// quotes, the quotes are removed and the resulting string is copied +/// into a vector. If the string is not in quotes, an empty vector is +/// returned. +/// +/// The resulting string is copied to a vector and returned. +/// +/// This function is intended to be used by the server configuration +/// parsers to convert string values surrounded with quotes into +/// binary form. +/// +/// \param quoted_string String to be converted. +/// \return Vector containing converted string or empty string if +/// input string didn't contain expected quote characters. +std::vector +quotedStringToBinary(const std::string& quoted_string); + } // namespace str } // namespace util } // namespace isc diff --git a/src/lib/util/tests/strutil_unittest.cc b/src/lib/util/tests/strutil_unittest.cc index 1153b83d85..b99d2a120b 100644 --- a/src/lib/util/tests/strutil_unittest.cc +++ b/src/lib/util/tests/strutil_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -259,3 +259,41 @@ TEST(StringUtilTest, tokenToNum) { isc::util::str::StringTokenError); } + +/// @brief Convenience function which calls quotedStringToBinary +/// and converts returned vector back to string. +/// +/// @param s Input string. +/// @return String holding a copy of a vector returned by the +/// quotedStringToBinary. +std::string testQuoted(const std::string& s) { + std::vector vec = str::quotedStringToBinary(s); + std::string s2(vec.begin(), vec.end()); + return (s2); +} + +TEST(StringUtilTest, quotedStringToBinary) { + // No opening or closing quote should result in empty string. + EXPECT_TRUE(str::quotedStringToBinary("'").empty()); + EXPECT_TRUE(str::quotedStringToBinary("").empty()); + EXPECT_TRUE(str::quotedStringToBinary(" ").empty()); + EXPECT_TRUE(str::quotedStringToBinary("'circuit id").empty()); + EXPECT_TRUE(str::quotedStringToBinary("circuit id'").empty()); + + // If there is only opening and closing quote, an empty + // vector should be returned. + EXPECT_TRUE(str::quotedStringToBinary("''").empty()); + + // Both opening and ending quote is present. + EXPECT_EQ("circuit id", testQuoted("'circuit id'")); + EXPECT_EQ("remote id", testQuoted(" ' remote id'")); + EXPECT_EQ("duid", testQuoted(" ' duid'")); + EXPECT_EQ("duid", testQuoted("'duid ' ")); + EXPECT_EQ("remote'id", testQuoted(" ' remote'id '")); + EXPECT_EQ("remote id'", testQuoted("'remote id''")); + EXPECT_EQ("'remote id", testQuoted("''remote id'")); + + // Multiple quotes. + EXPECT_EQ("'", testQuoted("'''")); + EXPECT_EQ("''", testQuoted("''''")); +} -- 2.47.3