From 412835d7fac699a7224dcdbb5aba971ece8de2fc Mon Sep 17 00:00:00 2001 From: KimLS Date: Sun, 24 Aug 2014 16:26:51 -0700 Subject: [PATCH 1/2] Basic string tests, plus fix for StringFormat returning a std::string that was just very subtley malformed. --- common/string_util.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- common/string_util.h | 1 + tests/CMakeLists.txt | 5 +++-- tests/main.cpp | 2 ++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/common/string_util.cpp b/common/string_util.cpp index 0ccd0e4ef..2fcb59e05 100644 --- a/common/string_util.cpp +++ b/common/string_util.cpp @@ -53,10 +53,11 @@ const std::string vStringFormat(const char* format, va_list args) return ""; } else if ((unsigned int)characters_used > output.capacity()) { - output.resize(characters_used+1); + output.resize(characters_used + 1); va_copy(tmpargs,args); characters_used = vsnprintf(&output[0], output.capacity(), format, tmpargs); va_end(tmpargs); + output.resize(characters_used); if (characters_used < 0) { // We shouldn't have a format error by this point, but I can't imagine what error we @@ -72,6 +73,8 @@ const std::string vStringFormat(const char* format, va_list args) characters_used = vsnprintf(&output[0], output.capacity(), format, tmpargs); va_end(tmpargs); + output.resize(characters_used); + if (characters_used < 0) { // We shouldn't have a format error by this point, but I can't imagine what error we // could have by this point. Still, return empty string; @@ -380,6 +383,42 @@ std::string EscapeString(const std::string &s) { return ret; } +std::string EscapeString(const char *src, size_t sz) { + std::string ret; + + for(size_t i = 0; i < sz; ++i) { + char c = src[i]; + switch(c) { + case '\x00': + ret += "\\x00"; + break; + case '\n': + ret += "\\n"; + break; + case '\r': + ret += "\\r"; + break; + case '\\': + ret += "\\\\"; + break; + case '\'': + ret += "\\'"; + break; + case '\"': + ret += "\\\""; + break; + case '\x1a': + ret += "\\x1a"; + break; + default: + ret.push_back(c); + break; + } + } + + return ret; +} + bool isAlphaNumeric(const char *text) { for (unsigned int charIndex=0; charIndex Date: Sun, 24 Aug 2014 16:30:02 -0700 Subject: [PATCH 2/2] Idiot kls missed a file --- tests/string_util_test.h | 85 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 tests/string_util_test.h diff --git a/tests/string_util_test.h b/tests/string_util_test.h new file mode 100644 index 000000000..374878065 --- /dev/null +++ b/tests/string_util_test.h @@ -0,0 +1,85 @@ +/* EQEMu: Everquest Server Emulator + Copyright (C) 2001-2013 EQEMu Development Team (http://eqemulator.net) + + 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; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY except by those people which sell it, which + are required to give you total support for your newly bought product; + 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, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __EQEMU_TESTS_STRING_UTIL_H +#define __EQEMU_TESTS_STRING_UTIL_H + +#include "cppunit/cpptest.h" +#include "../common/string_util.h" + +class StringUtilTest : public Test::Suite { + typedef void(IPCMutexTest::*TestFunction)(void); +public: + StringUtilTest() { + TEST_ADD(StringUtilTest::StringFormatTest); + TEST_ADD(StringUtilTest::EscapeStringTest); + TEST_ADD(StringUtilTest::EscapeStringMemoryTest); + } + + ~StringUtilTest() { + } + + private: + void StringFormatTest() { + const char* fmt = "Test: %c %d %4.2f"; + char c = 'a'; + int i = 2014; + float f = 3.1416; + + auto s = StringFormat(fmt, c, i, f); + TEST_ASSERT(s.length() == 17); + TEST_ASSERT(s.compare("Test: a 2014 3.14") == 0); + } + + void EscapeStringTest() { + std::string t; + t.resize(10); + t[0] = 'a'; + t[1] = 'b'; + t[2] = 'c'; + t[3] = '\x00'; + t[4] = '\n'; + t[5] = '\r'; + t[6] = '\\'; + t[7] = '\''; + t[8] = '\"'; + t[9] = '\x1a'; + + auto s = EscapeString(t); + TEST_ASSERT(s.compare("abc\\x00\\n\\r\\\\\\'\\\"\\x1a") == 0); + } + + void EscapeStringMemoryTest() { + char t[10] = { 0 }; + t[0] = 'a'; + t[1] = 'b'; + t[2] = 'c'; + t[3] = '\x00'; + t[4] = '\n'; + t[5] = '\r'; + t[6] = '\\'; + t[7] = '\''; + t[8] = '\"'; + t[9] = '\x1a'; + + auto s = EscapeString(t, 10); + TEST_ASSERT(s.compare("abc\\x00\\n\\r\\\\\\'\\\"\\x1a") == 0); + } +}; + +#endif