1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/*
* Copyright (C) 2004-2025 ZNC, see the NOTICE file for details.
*
* 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.
*/
#include <gtest/gtest.h>
#include <znc/FileUtils.h>
#include <znc/Utils.h>
TEST(IRC32, GetMessageTags) {
EXPECT_EQ(CUtils::GetMessageTags(""), MCString());
EXPECT_EQ(CUtils::GetMessageTags(
":nick!ident@host PRIVMSG #chan :hello world"), MCString());
MCString exp = {{"a", "b"}};
EXPECT_EQ(CUtils::GetMessageTags("@a=b"), exp);
EXPECT_EQ(CUtils::GetMessageTags(
"@a=b :nick!ident@host PRIVMSG #chan :hello world"), exp);
EXPECT_EQ(CUtils::GetMessageTags("@a=b :rest"), exp);
exp.clear();
exp = {{"ab", "cdef"}, {"znc.in/gh-ij", "klmn,op"}};
EXPECT_EQ(CUtils::GetMessageTags("@ab=cdef;znc.in/gh-ij=klmn,op :rest"),
exp);
exp = {{"a", "==b=="}};
EXPECT_EQ(CUtils::GetMessageTags("@a===b== :rest"), exp);
exp.clear();
exp = {{"a", ""}, {"b", "c"}, {"d", ""}};
EXPECT_EQ(CUtils::GetMessageTags("@a;b=c;d :rest"), exp);
exp = {{"semi-colon", ";"}, {"space", " "}, {"NUL", {'\0'}},
{"backslash", "\\"}, {"CR", {'\r'}}, {"LF", {'\n'}}};
EXPECT_EQ(
CUtils::GetMessageTags(
R"(@semi-colon=\:;space=\s;NUL=\0;backslash=\\;CR=\r;LF=\n :rest)"),
exp);
exp.clear();
exp = {{"a", "; \\\r\n"}};
EXPECT_EQ(CUtils::GetMessageTags(R"(@a=\:\s\\\r\n :rest)"), exp);
exp.clear();
}
TEST(IRC32, SetMessageTags) {
CString sLine;
sLine = ":rest";
CUtils::SetMessageTags(sLine, MCString());
EXPECT_EQ(sLine, ":rest");
MCString tags = {{"a", "b"}};
CUtils::SetMessageTags(sLine, tags);
EXPECT_EQ(sLine, "@a=b :rest");
tags = {{"a", "b"}, {"c", "d"}};
CUtils::SetMessageTags(sLine, tags);
EXPECT_EQ(sLine, "@a=b;c=d :rest");
tags = {{"a", "b"}, {"c", "d"}, {"e", ""}};
CUtils::SetMessageTags(sLine, tags);
EXPECT_EQ(sLine, "@a=b;c=d;e :rest");
tags = {{"semi-colon", ";"}, {"space", " "}, {"NUL", {'\0'}},
{"backslash", "\\"}, {"CR", {'\r'}}, {"LF", {'\n'}}};
CUtils::SetMessageTags(sLine, tags);
EXPECT_EQ(
sLine,
R"(@CR=\r;LF=\n;NUL=\0;backslash=\\;semi-colon=\:;space=\s :rest)");
tags = {{"a", "; \\\r\n"}};
CUtils::SetMessageTags(sLine, tags);
EXPECT_EQ(sLine, R"(@a=\:\s\\\r\n :rest)");
}
TEST(UtilsTest, Timezone) {
char* oldTZ = getenv("TZ");
if (oldTZ) oldTZ = strdup(oldTZ);
setenv("TZ", "Europe/Berlin", 1);
tzset();
EXPECT_EQ(CUtils::FormatTime(1673122230, "%Y-%m-%d %H:%M:%S", "UTC"), "2023-01-07 20:10:30");
EXPECT_EQ(CUtils::FormatTime(1673122230, "%Y-%m-%d %H:%M:%S", "GMT"), "2023-01-07 20:10:30");
EXPECT_EQ(CUtils::FormatTime(1673122230, "%Y-%m-%d %H:%M:%S", "GMT+7"), "2023-01-08 03:10:30");
EXPECT_EQ(CUtils::FormatTime(1673122230, "%Y-%m-%d %H:%M:%S", "GMT-7"), "2023-01-07 13:10:30");
EXPECT_EQ(CUtils::FormatTime(1673122230, "%Y-%m-%d %H:%M:%S", "Asia/Vladivostok"), "2023-01-08 06:10:30");
// Local TZ, set to Berlin in this test
EXPECT_EQ(CUtils::FormatTime(1673122230, "%Y-%m-%d %H:%M:%S", ""), "2023-01-07 21:10:30");
if (oldTZ) {
setenv("TZ", oldTZ, 1);
free(oldTZ);
} else {
unsetenv("TZ");
}
tzset();
}
TEST(UtilsTest, ServerTime) {
char* oldTZ = getenv("TZ");
if (oldTZ) oldTZ = strdup(oldTZ);
setenv("TZ", "UTC", 1);
tzset();
timeval tv1 = CUtils::ParseServerTime("2011-10-19T16:40:51.620Z");
CString str1 = CUtils::FormatServerTime(tv1);
EXPECT_EQ(str1, "2011-10-19T16:40:51.620Z");
timeval now = CUtils::GetTime();
// Strip microseconds, server time is ms only
now.tv_usec = (now.tv_usec / 1000) * 1000;
CString str2 = CUtils::FormatServerTime(now);
timeval tv2 = CUtils::ParseServerTime(str2);
EXPECT_EQ(tv2.tv_sec, now.tv_sec);
EXPECT_EQ(tv2.tv_usec, now.tv_usec);
timeval tv3 = CUtils::ParseServerTime("invalid");
CString str3 = CUtils::FormatServerTime(tv3);
EXPECT_EQ(str3, "1970-01-01T00:00:00.000Z");
if (oldTZ) {
setenv("TZ", oldTZ, 1);
free(oldTZ);
} else {
unsetenv("TZ");
}
tzset();
}
TEST(UtilsTest, ParseServerTime) {
char* oldTZ = getenv("TZ");
if (oldTZ) oldTZ = strdup(oldTZ);
setenv("TZ", "America/Montreal", 1);
tzset();
timeval tv4 = CUtils::ParseServerTime("2011-10-19T16:40:51.620Z");
CString str4 = CUtils::FormatServerTime(tv4);
EXPECT_EQ(str4, "2011-10-19T16:40:51.620Z");
if (oldTZ) {
setenv("TZ", oldTZ, 1);
free(oldTZ);
} else {
unsetenv("TZ");
}
tzset();
}
class TimeTest : public testing::TestWithParam<
std::tuple<timeval, CString, CString, CString>> {};
TEST_P(TimeTest, FormatTime) {
timeval tv = std::get<0>(GetParam());
EXPECT_EQ(std::get<1>(GetParam()), CUtils::FormatTime(tv, "%s.%f", "UTC"));
EXPECT_EQ(std::get<2>(GetParam()), CUtils::FormatTime(tv, "%s.%6f", "UTC"));
EXPECT_EQ(std::get<3>(GetParam()), CUtils::FormatTime(tv, "%s.%9f", "UTC"));
}
INSTANTIATE_TEST_CASE_P(
TimeTest, TimeTest,
testing::Values(
// leading zeroes
std::make_tuple(timeval{42, 12345}, "42.012", "42.012345", "42.012345000"),
// (no) rounding
std::make_tuple(timeval{42, 999999}, "42.999", "42.999999", "42.999999000"),
// no tv_usec part
std::make_tuple(timeval{42, 0}, "42.000", "42.000000", "42.000000000")));
TEST(UtilsTest, FormatTime) {
// Test passthrough
timeval tv1;
tv1.tv_sec = 42;
tv1.tv_usec = 123456;
CString str1 = CUtils::FormatTime(tv1, "%s", "UTC");
EXPECT_EQ(str1, "42");
// Test escapes
timeval tv2;
tv2.tv_sec = 42;
tv2.tv_usec = 123456;
CString str2 = CUtils::FormatTime(tv2, "%%f", "UTC");
EXPECT_EQ(str2, "%f");
// Test suffix
CString str3 = CUtils::FormatTime(tv2, "a%fb", "UTC");
EXPECT_EQ(str3, "a123b");
}
|