diff options
-rw-r--r-- | Swiften/Base/ByteArray.h | 15 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/ByteArrayTest.cpp | 21 | ||||
-rw-r--r-- | Swiften/SConscript | 1 |
3 files changed, 29 insertions, 8 deletions
diff --git a/Swiften/Base/ByteArray.h b/Swiften/Base/ByteArray.h index bcc3756..ea96d09 100644 --- a/Swiften/Base/ByteArray.h +++ b/Swiften/Base/ByteArray.h @@ -1,5 +1,4 @@ -#ifndef SWIFTEN_BYTEARRAY_H -#define SWIFTEN_BYTEARRAY_H +#pragma once #include <cstring> #include <vector> @@ -25,16 +24,18 @@ namespace Swift { } ByteArray(const char* c, size_t n) { - data_.resize(n); - memcpy(&data_[0], c, n); + if (n > 0) { + data_.resize(n); + memcpy(&data_[0], c, n); + } } const char* getData() const { - return &data_[0]; + return data_.empty() ? NULL : &data_[0]; } char* getData() { - return &data_[0]; + return data_.empty() ? NULL : &data_[0]; } size_t getSize() const { @@ -93,5 +94,3 @@ namespace Swift { } std::ostream& operator<<(std::ostream& os, const Swift::ByteArray& s); - -#endif diff --git a/Swiften/Base/UnitTest/ByteArrayTest.cpp b/Swiften/Base/UnitTest/ByteArrayTest.cpp new file mode 100644 index 0000000..bf893bd --- /dev/null +++ b/Swiften/Base/UnitTest/ByteArrayTest.cpp @@ -0,0 +1,21 @@ +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include "Swiften/Base/ByteArray.h" + +using namespace Swift; + +class ByteArrayTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(ByteArrayTest); + CPPUNIT_TEST(testGetData_NoData); + CPPUNIT_TEST_SUITE_END(); + + public: + void testGetData_NoData() { + ByteArray testling; + + CPPUNIT_ASSERT_EQUAL(static_cast<const char*>(NULL), static_cast<const char*>(testling.getData())); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(ByteArrayTest); diff --git a/Swiften/SConscript b/Swiften/SConscript index 12fbe15..28af98d 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -101,6 +101,7 @@ env.Append(UNITTEST_SOURCES = [ File("Application/UnitTest/ApplicationTest.cpp"), File("Base/UnitTest/IDGeneratorTest.cpp"), File("Base/UnitTest/StringTest.cpp"), + File("Base/UnitTest/ByteArrayTest.cpp"), File("Client/UnitTest/ClientSessionTest.cpp"), File("Compress/UnitTest/ZLibCompressorTest.cpp"), File("Compress/UnitTest/ZLibDecompressorTest.cpp"), |