summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorHuynh Tran <nthuynh75@gmail.com>2005-06-19 07:14:11 +0000
committerHuynh Tran <nthuynh75@gmail.com>2005-06-19 07:14:11 +0000
commit8b9ecdf0b86890d34bab10e79a1ba46dcccd894e (patch)
tree85ce59d03e876c3e5041ce68295d6dc77ae22f32 /src/utils
parentee3b57e273e4b0437aaaa23ba6addb68e4c4fcca (diff)
downloadmanaserv-8b9ecdf0b86890d34bab10e79a1ba46dcccd894e.tar.gz
manaserv-8b9ecdf0b86890d34bab10e79a1ba46dcccd894e.tar.bz2
manaserv-8b9ecdf0b86890d34bab10e79a1ba46dcccd894e.tar.xz
manaserv-8b9ecdf0b86890d34bab10e79a1ba46dcccd894e.zip
Moved unit tests main from dal to src, rewrote Logger and added Cipher (requires libcrypto from OpenSSL) + unit tests.
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/cipher.cpp100
-rw-r--r--src/utils/cipher.h115
-rw-r--r--src/utils/logger.cpp215
-rw-r--r--src/utils/logger.h301
-rw-r--r--src/utils/singleton.h96
-rw-r--r--src/utils/testcipher.cpp154
-rw-r--r--src/utils/testcipher.h115
7 files changed, 1096 insertions, 0 deletions
diff --git a/src/utils/cipher.cpp b/src/utils/cipher.cpp
new file mode 100644
index 00000000..1b232e7e
--- /dev/null
+++ b/src/utils/cipher.cpp
@@ -0,0 +1,100 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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 any later version.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+
+#include <iomanip>
+#include <sstream>
+
+#include <openssl/evp.h>
+#include <openssl/md5.h>
+
+#include "cipher.h"
+
+
+namespace tmwserv
+{
+namespace utils
+{
+
+
+/**
+ * Default constructor.
+ */
+Cipher::Cipher(void)
+ throw()
+{
+ // NOOP
+}
+
+
+/**
+ * Destructor.
+ */
+Cipher::~Cipher(void)
+ throw()
+{
+ // NOOP
+}
+
+
+/**
+ * Encode using the MD5 digest algorithm.
+ */
+std::string
+Cipher::md5(const std::string& str)
+{
+ unsigned char md[MD5_DIGEST_LENGTH];
+
+ EVP_Digest(
+ (unsigned char*) str.c_str(),
+ (unsigned long) str.length(),
+ md,
+ NULL,
+ EVP_md5(),
+ NULL
+ );
+
+ return toHex(md, MD5_DIGEST_LENGTH);
+}
+
+
+/**
+ * Convert a string into hexadecimal.
+ */
+std::string
+Cipher::toHex(const unsigned char* str,
+ const unsigned short length)
+{
+ using namespace std;
+
+ ostringstream os;
+
+ for (int i = 0; i < length; ++i) {
+ os << setfill('0') << setw(2) << hex << (int) str[i];
+ }
+
+ return os.str();
+}
+
+
+} // namespace utils
+} // namespace tmwserv
diff --git a/src/utils/cipher.h b/src/utils/cipher.h
new file mode 100644
index 00000000..a6e6c552
--- /dev/null
+++ b/src/utils/cipher.h
@@ -0,0 +1,115 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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 any later version.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+
+#ifndef _TMWSERV_CIPHER_H_
+#define _TMWSERV_CIPHER_H_
+
+
+#include <string>
+
+#include "singleton.h"
+
+
+namespace tmwserv
+{
+namespace utils
+{
+
+
+/**
+ * A helper class for the encoding of strings using different algorithms.
+ *
+ * Notes:
+ * - this class implements the Meyer's singleton design pattern.
+ * - this class uses the OpenSSL's crypto library.
+ */
+class Cipher: public Singleton<Cipher>
+{
+ // friend so that Singleton can call the constructor.
+ friend class Singleton<Cipher>;
+
+
+ public:
+ /**
+ * Encode using the MD5 digest algorithm.
+ *
+ * @param str the string to encode.
+ *
+ * @return the MD5 digest hash string.
+ */
+ std::string
+ md5(const std::string& str);
+
+
+ /**
+ * Add encryption algorithms here.
+ */
+
+
+ private:
+ /**
+ * Default constructor.
+ */
+ Cipher(void)
+ throw();
+
+
+ /**
+ * Destructor.
+ */
+ ~Cipher(void)
+ throw();
+
+
+ /**
+ * Copy constructor.
+ */
+ Cipher(const Cipher& rhs);
+
+
+ /**
+ * Assignment operator.
+ */
+ Cipher&
+ operator=(const Cipher& rhs);
+
+
+ /**
+ * Convert a string into hexadecimal.
+ *
+ * @param str the string to convert.
+ * @param length the string length.
+ *
+ * @return the hexadecimal string.
+ */
+ std::string
+ toHex(const unsigned char* str,
+ const unsigned short length);
+};
+
+
+} // namespace utils
+} // namespace tmwserv
+
+
+#endif // _TMWSERV_CIPHER_H_
diff --git a/src/utils/logger.cpp b/src/utils/logger.cpp
new file mode 100644
index 00000000..5010ed66
--- /dev/null
+++ b/src/utils/logger.cpp
@@ -0,0 +1,215 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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 any later version.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+
+#include <ctime>
+#include <iomanip>
+#include <iostream>
+
+#ifdef WIN32
+#include <windows.h>
+#endif
+
+#include "logger.h"
+
+
+namespace tmwserv
+{
+namespace utils
+{
+
+
+/**
+ * Default constructor.
+ */
+Logger::Logger(void)
+ throw()
+{
+ // NOOP
+}
+
+
+/**
+ * Destructor.
+ */
+Logger::~Logger(void)
+ throw()
+{
+ // the destructor of std::ofstream takes care of closing the file
+ // if it is still open :)
+}
+
+
+/**
+ * Set the log file.
+ */
+void
+Logger::setLogFile(const std::string& logFile)
+{
+ // close the current log file.
+ if (mLogFile.is_open()) {
+ mLogFile.close();
+ }
+
+ // open the file for output and remove the former file contents.
+ mLogFile.open(logFile.c_str(), std::ios::trunc);
+
+ if (!mLogFile.is_open()) {
+ std::string msg("unable to open ");
+ msg += logFile;
+ msg += " for writing.";
+
+ throw std::ios::failure(msg);
+ }
+ else {
+ // by default the streams do not throw any exception
+ // let std::ios::failbit and std::ios::badbit throw exceptions.
+ mLogFile.exceptions(std::ios::failbit | std::ios::badbit);
+ }
+}
+
+
+/**
+ * Add/remove the timestamp.
+ */
+void
+Logger::setTimestamp(bool flag)
+ throw()
+{
+ mHasTimestamp = flag;
+}
+
+
+/**
+ * Log a generic message.
+ */
+void
+Logger::log(const std::string& msg)
+{
+ log((mLogFile.is_open() ? mLogFile : std::cout), msg);
+}
+
+
+/**
+ * Log a debug message.
+ */
+void
+Logger::debug(const std::string& msg)
+{
+ log((mLogFile.is_open() ? mLogFile : std::cout), msg, "[DBG]");
+}
+
+
+/**
+ * Log an info message.
+ */
+void
+Logger::info(const std::string& msg)
+{
+ log((mLogFile.is_open() ? mLogFile : std::cout), msg, "[INF]");
+}
+
+
+/**
+ * Log a warn message.
+ */
+void
+Logger::warn(const std::string& msg)
+{
+ log((mLogFile.is_open() ? mLogFile : std::cerr), msg, "[WRN]");
+}
+
+
+/**
+ * Log an error message.
+ */
+void
+Logger::error(const std::string& msg)
+{
+ log((mLogFile.is_open() ? mLogFile : std::cerr), msg, "[ERR]");
+}
+
+
+/**
+ * Log a fatal error message.
+ */
+void
+Logger::fatal(const std::string& msg)
+{
+ log((mLogFile.is_open() ? mLogFile : std::cerr), msg, "[FTL]");
+
+#ifdef WIN32
+ MessageBox(NULL, msg.c_str(), "Fatal error", MB_ICONERROR | MB_OK);
+#endif
+}
+
+
+/**
+ * Log a generic message.
+ */
+void
+Logger::log(std::ostream& os,
+ const std::string& msg,
+ const std::string& prefix)
+{
+ if (mHasTimestamp) {
+ os << getCurrentTime() << " ";
+ }
+
+ if (prefix != "") {
+ os << prefix << " ";
+ }
+
+ os << msg << std::endl;
+}
+
+
+/**
+ * Get the current time.
+ */
+std::string
+Logger::getCurrentTime(void)
+{
+ time_t now;
+ tm local;
+
+ // get current time_t value
+ time(&now);
+
+ // convert time_t to tm struct to break the time into individual
+ // constituents
+ local = *(localtime(&now));
+
+ // stringify the time, the format is: [hh:mm:ss]
+ using namespace std;
+ ostringstream os;
+ os << "[" << setw(2) << setfill('0') << local.tm_hour
+ << ":" << setw(2) << setfill('0') << local.tm_min
+ << ":" << setw(2) << setfill('0') << local.tm_sec
+ << "]";
+
+ return os.str();
+}
+
+
+} // namespace utils
+} // namespace tmwserv
diff --git a/src/utils/logger.h b/src/utils/logger.h
new file mode 100644
index 00000000..be8d7d3e
--- /dev/null
+++ b/src/utils/logger.h
@@ -0,0 +1,301 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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 any later version.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+
+#ifndef _TMWSERV_LOGGER_H_
+#define _TMWSERV_LOGGER_H_
+
+
+#include <fstream>
+#include <sstream>
+#include <string>
+
+#include "singleton.h"
+
+
+namespace tmwserv
+{
+namespace utils
+{
+
+
+/**
+ * A very simple logger that writes messages to a log file.
+ * If the log file is not set, the messages are routed to the standard output
+ * or the standard error depending on the level of the message.
+ * By default, the messages will be timestamped but the logger can be
+ * configured to not prefix the messages with a timestamp.
+ *
+ * Limitations:
+ * - not thread-safe.
+ *
+ * Notes:
+ * - this class implements the Meyer's singleton design pattern.
+ *
+ * Example of use:
+ *
+ * <code>
+ * #include "logger.h"
+ *
+ * int main(void)
+ * {
+ * using namespace tmwserv::utils;
+ *
+ * Logger& logger = Logger::instance();
+ * logger.setLogFile("/path/to/logfile");
+ *
+ * // log messages using helper macros.
+ * LOG("hello world")
+ * LOG_DEBUG("level: " << 3)
+ * LOG_INFO("init sound")
+ * LOG_WARN("not implemented")
+ * LOG_ERROR("resource not found")
+ * LOG_FATAL("unable to init graphics")
+ *
+ * // log messages using APIs.
+ * logger.log("hello world");
+ *
+ * std::ostringstream os;
+ * os << "level: " << 3;
+ * logger.debug(os.str());
+ *
+ * logger.info("init sound");
+ * logger.warn("not implemented");
+ * logger.error("resource not found");
+ * logger.fatal("unable to init graphics");
+ *
+ * return 0;
+ * }
+ * </code>
+ */
+class Logger: public Singleton<Logger>
+{
+ // friend so that Singleton can call the constructor.
+ friend class Singleton<Logger>;
+
+
+ public:
+ /**
+ * Set the log file.
+ *
+ * This method will open the log file for writing, the former file
+ * contents are removed.
+ *
+ * @param logFile the log file name (may include path).
+ *
+ * @exception std::ios::failure if the log file could not be opened.
+ */
+ void
+ setLogFile(const std::string& logFile);
+
+
+ /**
+ * Add/remove the timestamp.
+ *
+ * @param flag if true, a log message will always be timestamped
+ * (default = true).
+ */
+ void
+ setTimestamp(bool flag = true)
+ throw();
+
+
+ /**
+ * Log a generic message.
+ *
+ * @param msg the message to log.
+ *
+ * @exception std::ios::failure.
+ */
+ void
+ log(const std::string& msg);
+
+
+ /**
+ * Log a debug message.
+ *
+ * @param msg the message to log.
+ *
+ * @exception std::ios::failure.
+ */
+ void
+ debug(const std::string& msg);
+
+
+ /**
+ * Log an info message.
+ *
+ * @param msg the message to log.
+ *
+ * @exception std::ios::failure.
+ */
+ void
+ info(const std::string& msg);
+
+
+ /**
+ * Log a warn message.
+ *
+ * @param msg the message to log.
+ *
+ * @exception std::ios::failure.
+ */
+ void
+ warn(const std::string& msg);
+
+
+ /**
+ * Log an error message.
+ *
+ * @param msg the message to log.
+ *
+ * @exception std::ios::failure.
+ */
+ void
+ error(const std::string& msg);
+
+
+ /**
+ * Log a fatal error message.
+ *
+ * @param msg the message to log.
+ *
+ * @exception std::ios::failure.
+ */
+ void
+ fatal(const std::string& msg);
+
+
+ private:
+ /**
+ * Default constructor.
+ */
+ Logger(void)
+ throw();
+
+
+ /**
+ * Destructor.
+ */
+ ~Logger(void)
+ throw();
+
+
+ /**
+ * Copy constructor.
+ */
+ Logger(const Logger& rhs);
+
+
+ /**
+ * Assignment operator.
+ */
+ Logger&
+ operator=(const Logger& rhs);
+
+
+ /**
+ * Log a generic message.
+ *
+ * @param os the output stream.
+ * @param msg the message to log.
+ * @param prefix the message prefix (default = "").
+ *
+ * @exception std::ios::failure.
+ */
+ void
+ log(std::ostream& os,
+ const std::string& msg,
+ const std::string& prefix = "");
+
+
+ /**
+ * Get the current time.
+ *
+ * @return the current time as string.
+ */
+ std::string
+ getCurrentTime(void);
+
+
+ private:
+ std::ofstream mLogFile; /**< the log file */
+ bool mHasTimestamp; /**< the timestamp flag */
+};
+
+
+} // namespace utils
+} // namespace tmwserv
+
+
+// HELPER MACROS
+
+
+#define LOG(msg) \
+ { \
+ std::ostringstream os; \
+ os << msg; \
+ ::tmwserv::utils::Logger::instance().log(os.str()); \
+ }
+
+
+#define LOG_DEBUG(msg) \
+ { \
+ std::ostringstream os; \
+ os << msg; \
+ ::tmwserv::utils::Logger::instance().debug(os.str()); \
+ }
+
+
+#define LOG_INFO(msg) \
+ { \
+ std::ostringstream os; \
+ os << msg; \
+ ::tmwserv::utils::Logger::instance().info(os.str()); \
+ }
+
+
+#define LOG_WARN(msg) \
+ { \
+ std::ostringstream os; \
+ os << msg; \
+ ::tmwserv::utils::Logger::instance().warn(os.str()); \
+ }
+
+
+#define LOG_ERROR(msg) \
+ { \
+ std::ostringstream os; \
+ os << msg; \
+ ::tmwserv::utils::Logger::instance().error(os.str()); \
+ }
+
+
+#define LOG_FATAL(msg) \
+ { \
+ std::ostringstream os; \
+ os << msg; \
+ ::tmwserv::utils::Logger::instance().fatal(os.str()); \
+ }
+
+
+#endif // _TMWSERV_LOGGER_H_
diff --git a/src/utils/singleton.h b/src/utils/singleton.h
new file mode 100644
index 00000000..4b3589f3
--- /dev/null
+++ b/src/utils/singleton.h
@@ -0,0 +1,96 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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 any later version.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+
+#ifndef _TMWSERV_SINGLETON_H_
+#define _TMWSERV_SINGLETON_H_
+
+
+namespace tmwserv
+{
+namespace utils
+{
+
+
+/**
+ * An abstract Meyer's singleton class.
+ */
+template <typename T>
+class Singleton
+{
+ public:
+ /**
+ * Create an instance of Singleton.
+ *
+ * @return the unique instance of Singleton.
+ */
+ static T&
+ instance(void)
+ {
+ static T theInstance;
+
+ return theInstance;
+ }
+
+
+ protected:
+ /**
+ * Default constructor.
+ */
+ Singleton(void)
+ throw()
+ {
+ // NOOP
+ }
+
+
+ /**
+ * Destructor.
+ */
+ virtual
+ ~Singleton(void)
+ throw()
+ {
+ // NOOP
+ }
+
+
+ private:
+ /**
+ * Copy constructor.
+ */
+ Singleton(const Singleton& rhs);
+
+
+ /**
+ * Assignment operator.
+ */
+ Singleton&
+ operator=(const Singleton& rhs);
+};
+
+
+} // namespace utils
+} // namespace tmwserv
+
+
+#endif // _TMWSERV_SINGLETON_H_
diff --git a/src/utils/testcipher.cpp b/src/utils/testcipher.cpp
new file mode 100644
index 00000000..6d2c03d1
--- /dev/null
+++ b/src/utils/testcipher.cpp
@@ -0,0 +1,154 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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 any later version.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+
+#include <string>
+
+#include "cipher.h"
+#include "testcipher.h"
+
+
+// register the fixture into the 'registry'
+CPPUNIT_TEST_SUITE_REGISTRATION(CipherTest);
+
+
+using namespace tmwserv::utils;
+
+
+/**
+ * Set up fixtures.
+ */
+void
+CipherTest::setUp(void)
+{
+ // NOOP
+}
+
+
+/**
+ * Tear down fixtures.
+ */
+void
+CipherTest::tearDown(void)
+{
+ // NOOP
+}
+
+
+/**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+void
+CipherTest::testMd5_1(void)
+{
+ const std::string expected("d41d8cd98f00b204e9800998ecf8427e");
+ std::string actual(Cipher::instance().md5(""));
+
+ CPPUNIT_ASSERT_EQUAL(actual, expected);
+}
+
+
+/**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+void
+CipherTest::testMd5_2(void)
+{
+ const std::string expected("0cc175b9c0f1b6a831c399e269772661");
+ std::string actual(Cipher::instance().md5("a"));
+
+ CPPUNIT_ASSERT_EQUAL(actual, expected);
+}
+
+
+/**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+void
+CipherTest::testMd5_3(void)
+{
+ const std::string expected("900150983cd24fb0d6963f7d28e17f72");
+ std::string actual(Cipher::instance().md5("abc"));
+
+ CPPUNIT_ASSERT_EQUAL(actual, expected);
+}
+
+
+/**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+void
+CipherTest::testMd5_4(void)
+{
+ const std::string expected("f96b697d7cb7938d525a2f31aaf161d0");
+ std::string actual(Cipher::instance().md5("message digest"));
+
+ CPPUNIT_ASSERT_EQUAL(actual, expected);
+}
+
+
+/**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+void
+CipherTest::testMd5_5(void)
+{
+ const std::string expected("c3fcd3d76192e4007dfb496cca67e13b");
+ std::string actual(Cipher::instance().md5("abcdefghijklmnopqrstuvwxyz"));
+
+ CPPUNIT_ASSERT_EQUAL(actual, expected);
+}
+
+
+/**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+void
+CipherTest::testMd5_6(void)
+{
+ const std::string expected("d174ab98d277d9f5a5611c2c9f419d9f");
+
+ std::string s("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
+ s += "abcdefghijklmnopqrstuvwxyz";
+ s += "0123456789";
+ std::string actual(Cipher::instance().md5(s));
+
+ CPPUNIT_ASSERT_EQUAL(actual, expected);
+}
+
+
+/**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+void
+CipherTest::testMd5_7(void)
+{
+ const std::string expected("57edf4a22be3c955ac49da2e2107b67a");
+
+ std::string s;
+ for (int i = 0; i < 8; ++i) {
+ s += "1234567890";
+ }
+ std::string actual(Cipher::instance().md5(s));
+
+ CPPUNIT_ASSERT_EQUAL(actual, expected);
+}
diff --git a/src/utils/testcipher.h b/src/utils/testcipher.h
new file mode 100644
index 00000000..7db81803
--- /dev/null
+++ b/src/utils/testcipher.h
@@ -0,0 +1,115 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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 any later version.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+
+#ifndef _TMWSERV_TEST_CIPHER_H_
+#define _TMWSERV_TEST_CIPHER_H_
+
+
+#include <cppunit/extensions/HelperMacros.h>
+
+
+/**
+ * Unit test for the Cipher class.
+ */
+class CipherTest: public CppUnit::TestFixture
+{
+ CPPUNIT_TEST_SUITE(CipherTest);
+
+ // add tests to the test suite.
+ CPPUNIT_TEST(testMd5_1);
+ CPPUNIT_TEST(testMd5_2);
+ CPPUNIT_TEST(testMd5_3);
+ CPPUNIT_TEST(testMd5_4);
+ CPPUNIT_TEST(testMd5_5);
+ CPPUNIT_TEST(testMd5_6);
+ CPPUNIT_TEST(testMd5_7);
+
+ CPPUNIT_TEST_SUITE_END();
+
+
+ public:
+ /**
+ * Set up fixtures.
+ */
+ void
+ setUp(void);
+
+
+ /**
+ * Tear down fixtures.
+ */
+ void
+ tearDown(void);
+
+
+ /**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+ void
+ testMd5_1(void);
+
+
+ /**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+ void
+ testMd5_2(void);
+
+
+ /**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+ void
+ testMd5_3(void);
+
+
+ /**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+ void
+ testMd5_4(void);
+
+
+ /**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+ void
+ testMd5_5(void);
+
+
+ /**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+ void
+ testMd5_6(void);
+
+
+ /**
+ * Test encoding a string with the MD5 digest algorithm.
+ */
+ void
+ testMd5_7(void);
+};
+
+
+#endif // _TMWSERV_TEST_CIPHER_H_