2021-03-23 06:17:08 +08:00
|
|
|
/*
|
|
|
|
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-05-03 05:04:26 +08:00
|
|
|
#include "zm_catch2.h"
|
2021-03-23 06:17:08 +08:00
|
|
|
|
|
|
|
#include "zm_utils.h"
|
2021-04-04 04:29:31 +08:00
|
|
|
#include <sstream>
|
2021-03-23 06:17:08 +08:00
|
|
|
|
2021-04-04 05:30:59 +08:00
|
|
|
TEST_CASE("Trim") {
|
|
|
|
REQUIRE(Trim("", "") == "");
|
|
|
|
REQUIRE(Trim("test", "") == "test");
|
|
|
|
REQUIRE(Trim(" ", "") == " ");
|
2021-03-23 06:17:08 +08:00
|
|
|
|
2021-04-04 05:30:59 +08:00
|
|
|
REQUIRE(Trim("\"test", "\"") == "test");
|
|
|
|
REQUIRE(Trim("test\"", "\"") == "test");
|
|
|
|
REQUIRE(Trim("\"test\"", "\"") == "test");
|
2021-03-23 06:17:08 +08:00
|
|
|
|
2021-04-04 05:30:59 +08:00
|
|
|
REQUIRE(Trim("te\"st", "\"") == "te\"st");
|
|
|
|
REQUIRE(Trim("\"te\"st\"", "\"") == "te\"st");
|
2021-03-23 06:17:08 +08:00
|
|
|
}
|
2021-03-23 06:21:27 +08:00
|
|
|
|
2021-04-04 05:30:59 +08:00
|
|
|
TEST_CASE("TrimSpaces") {
|
|
|
|
REQUIRE(TrimSpaces(" ") == "");
|
|
|
|
|
|
|
|
REQUIRE(TrimSpaces("test") == "test");
|
|
|
|
REQUIRE(TrimSpaces(" test ") == "test");
|
|
|
|
REQUIRE(TrimSpaces(" test ") == "test");
|
|
|
|
REQUIRE(TrimSpaces(" test ") == "test");
|
|
|
|
REQUIRE(TrimSpaces(" test") == "test");
|
|
|
|
REQUIRE(TrimSpaces("\ttest") == "test");
|
|
|
|
REQUIRE(TrimSpaces("test\t") == "test");
|
|
|
|
REQUIRE(TrimSpaces("\ttest\t") == "test");
|
|
|
|
REQUIRE(TrimSpaces(" test\t") == "test");
|
|
|
|
REQUIRE(TrimSpaces("\ttest ") == "test");
|
|
|
|
REQUIRE(TrimSpaces("\t test \t") == "test");
|
|
|
|
|
|
|
|
REQUIRE(TrimSpaces("\t te st \t") == "te st");
|
2021-03-23 06:21:27 +08:00
|
|
|
}
|
2021-03-23 06:30:45 +08:00
|
|
|
|
2021-04-04 05:43:28 +08:00
|
|
|
TEST_CASE("ReplaceAll") {
|
|
|
|
REQUIRE(ReplaceAll("", "", "") == "");
|
2021-03-23 06:30:45 +08:00
|
|
|
|
2021-04-04 05:43:28 +08:00
|
|
|
REQUIRE(ReplaceAll("a", "", "b") == "a");
|
|
|
|
REQUIRE(ReplaceAll("a", "a", "b") == "b");
|
|
|
|
REQUIRE(ReplaceAll("a", "b", "c") == "a");
|
2021-03-23 06:30:45 +08:00
|
|
|
|
2021-04-04 05:43:28 +08:00
|
|
|
REQUIRE(ReplaceAll("aa", "a", "b") == "bb");
|
|
|
|
REQUIRE(ReplaceAll("aba", "a", "c") == "cbc");
|
2021-03-23 06:30:45 +08:00
|
|
|
|
2021-04-04 05:43:28 +08:00
|
|
|
REQUIRE(ReplaceAll("aTOKENa", "TOKEN", "VAL") == "aVALa");
|
|
|
|
REQUIRE(ReplaceAll("aTOKENaTOKEN", "TOKEN", "VAL") == "aVALaVAL");
|
2021-03-23 06:30:45 +08:00
|
|
|
}
|
2021-03-23 06:36:17 +08:00
|
|
|
|
2021-04-04 05:51:12 +08:00
|
|
|
TEST_CASE("StartsWith") {
|
|
|
|
REQUIRE(StartsWith("", "") == true);
|
2021-03-23 06:36:17 +08:00
|
|
|
|
2021-04-04 05:51:12 +08:00
|
|
|
REQUIRE(StartsWith("test", "test") == true);
|
|
|
|
REQUIRE(StartsWith("test=abc", "test") == true);
|
|
|
|
REQUIRE(StartsWith(" test=abc", "test") == false);
|
2021-03-23 06:36:17 +08:00
|
|
|
}
|
2021-03-24 16:16:39 +08:00
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
TEST_CASE("Split (char delimiter)") {
|
|
|
|
std::vector<std::string> items = Split("", ' ');
|
|
|
|
REQUIRE(items == std::vector<std::string>{""});
|
2021-03-24 16:16:39 +08:00
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split("abc def ghi", ' ');
|
2021-03-24 16:16:39 +08:00
|
|
|
REQUIRE(items == std::vector<std::string>{"abc", "def", "ghi"});
|
2021-04-04 06:30:18 +08:00
|
|
|
|
|
|
|
items = Split("abc,def,,ghi", ',');
|
|
|
|
REQUIRE(items == std::vector<std::string>{"abc", "def", "", "ghi"});
|
2021-03-24 16:16:39 +08:00
|
|
|
}
|
2021-03-25 03:46:58 +08:00
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
TEST_CASE("Split (string delimiter)") {
|
2021-03-25 03:46:58 +08:00
|
|
|
std::vector<std::string> items;
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split("", "");
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items == std::vector<std::string>{""});
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split("", " ");
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items == std::vector<std::string>{""});
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split("", " \t");
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items == std::vector<std::string>{""});
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split("", " \t");
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items == std::vector<std::string>{""});
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split(" ", " ");
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items.size() == 0);
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split(" ", " ");
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items.size() == 0);
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split(" ", " \t");
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items.size() == 0);
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split("a b", "");
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items == std::vector<std::string>{"a b"});
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split("a b", " ");
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items == std::vector<std::string>{"a", "b"});
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split("a \tb", " \t");
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items == std::vector<std::string>{"a", "b"});
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split(" a \tb ", " \t");
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items == std::vector<std::string>{"a", "b"});
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split(" a=b ", "=");
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items == std::vector<std::string>{" a", "b "});
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split(" a=b ", " =");
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items == std::vector<std::string>{"a", "b"});
|
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
items = Split("a b c", " ", 2);
|
2021-03-25 03:46:58 +08:00
|
|
|
REQUIRE(items == std::vector<std::string>{"a", "b c"});
|
|
|
|
}
|
2021-04-04 03:11:38 +08:00
|
|
|
|
2021-04-04 06:30:18 +08:00
|
|
|
TEST_CASE("Join") {
|
|
|
|
REQUIRE(Join({}, "") == "");
|
|
|
|
REQUIRE(Join({}, " ") == "");
|
|
|
|
REQUIRE(Join({""}, "") == "");
|
|
|
|
REQUIRE(Join({"a"}, "") == "a");
|
|
|
|
REQUIRE(Join({"a"}, ",") == "a");
|
|
|
|
REQUIRE(Join({"a", "b"}, ",") == "a,b");
|
|
|
|
REQUIRE(Join({"a", "b"}, "") == "ab");
|
2021-04-04 03:11:38 +08:00
|
|
|
}
|
2021-04-04 03:23:54 +08:00
|
|
|
|
2021-04-04 06:39:40 +08:00
|
|
|
TEST_CASE("Base64Encode") {
|
|
|
|
REQUIRE(Base64Encode("") == "");
|
|
|
|
REQUIRE(Base64Encode("f") == "Zg==");
|
|
|
|
REQUIRE(Base64Encode("fo") == "Zm8=");
|
|
|
|
REQUIRE(Base64Encode("foo") == "Zm9v");
|
|
|
|
REQUIRE(Base64Encode("foob") == "Zm9vYg==");
|
|
|
|
REQUIRE(Base64Encode("fooba") == "Zm9vYmE=");
|
|
|
|
REQUIRE(Base64Encode("foobar") == "Zm9vYmFy");
|
2021-04-04 03:23:54 +08:00
|
|
|
}
|
2021-04-04 04:03:50 +08:00
|
|
|
|
2021-04-20 04:33:51 +08:00
|
|
|
TEST_CASE("ZM::clamp") {
|
|
|
|
REQUIRE(ZM::clamp(1, 0, 2) == 1);
|
|
|
|
REQUIRE(ZM::clamp(3, 0, 2) == 2);
|
|
|
|
REQUIRE(ZM::clamp(-1, 0, 2) == 0);
|
|
|
|
}
|
|
|
|
|
2021-04-04 04:03:50 +08:00
|
|
|
TEST_CASE("UriDecode") {
|
|
|
|
REQUIRE(UriDecode("abcABC123-_.~%21%28%29%26%3d%20") == "abcABC123-_.~!()&= ");
|
|
|
|
REQUIRE(UriDecode("abcABC123-_.~%21%28%29%26%3d+") == "abcABC123-_.~!()&= ");
|
|
|
|
}
|
2021-04-04 04:29:31 +08:00
|
|
|
|
|
|
|
TEST_CASE("QueryString") {
|
|
|
|
SECTION("no value") {
|
|
|
|
std::stringstream str("name1=");
|
|
|
|
QueryString qs(str);
|
|
|
|
|
|
|
|
REQUIRE(qs.size() == 1);
|
|
|
|
REQUIRE(qs.has("name1") == true);
|
|
|
|
|
|
|
|
const QueryParameter *p = qs.get("name1");
|
|
|
|
REQUIRE(p != nullptr);
|
|
|
|
REQUIRE(p->name() == "name1");
|
|
|
|
REQUIRE(p->size() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("no value and ampersand") {
|
|
|
|
std::stringstream str("name1=&");
|
|
|
|
QueryString qs(str);
|
|
|
|
|
|
|
|
REQUIRE(qs.size() == 1);
|
|
|
|
REQUIRE(qs.has("name1") == true);
|
|
|
|
|
|
|
|
const QueryParameter *p = qs.get("name1");
|
|
|
|
REQUIRE(p != nullptr);
|
|
|
|
REQUIRE(p->name() == "name1");
|
|
|
|
REQUIRE(p->size() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("one parameter, one value") {
|
|
|
|
std::stringstream str("name1=value1");
|
|
|
|
QueryString qs(str);
|
|
|
|
|
|
|
|
REQUIRE(qs.size() == 1);
|
|
|
|
REQUIRE(qs.has("name1") == true);
|
|
|
|
|
|
|
|
const QueryParameter *p = qs.get("name1");
|
|
|
|
REQUIRE(p != nullptr);
|
|
|
|
REQUIRE(p->name() == "name1");
|
|
|
|
REQUIRE(p->size() == 1);
|
|
|
|
REQUIRE(p->values()[0] == "value1");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("one parameter, multiple values") {
|
|
|
|
std::stringstream str("name1=value1&name1=value2");
|
|
|
|
QueryString qs(str);
|
|
|
|
|
|
|
|
REQUIRE(qs.size() == 1);
|
|
|
|
REQUIRE(qs.has("name1") == true);
|
|
|
|
|
|
|
|
const QueryParameter *p = qs.get("name1");
|
|
|
|
REQUIRE(p != nullptr);
|
|
|
|
REQUIRE(p->name() == "name1");
|
|
|
|
REQUIRE(p->size() == 2);
|
|
|
|
REQUIRE(p->values()[0] == "value1");
|
|
|
|
REQUIRE(p->values()[1] == "value2");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("multiple parameters, multiple values") {
|
|
|
|
std::stringstream str("name1=value1&name2=value2");
|
|
|
|
QueryString qs(str);
|
|
|
|
|
|
|
|
REQUIRE(qs.size() == 2);
|
|
|
|
REQUIRE(qs.has("name1") == true);
|
|
|
|
REQUIRE(qs.has("name2") == true);
|
|
|
|
|
|
|
|
const QueryParameter *p1 = qs.get("name1");
|
|
|
|
REQUIRE(p1 != nullptr);
|
|
|
|
REQUIRE(p1->name() == "name1");
|
|
|
|
REQUIRE(p1->size() == 1);
|
|
|
|
REQUIRE(p1->values()[0] == "value1");
|
|
|
|
|
|
|
|
const QueryParameter *p2 = qs.get("name2");
|
|
|
|
REQUIRE(p2 != nullptr);
|
|
|
|
REQUIRE(p2->name() == "name2");
|
|
|
|
REQUIRE(p2->size() == 1);
|
|
|
|
REQUIRE(p2->values()[0] == "value2");
|
|
|
|
}
|
|
|
|
}
|