Skip to content

Commit

Permalink
chore(JSON): add stringify unicode tests #4707 (#4720)
Browse files Browse the repository at this point in the history
* chore(JSON): add stringify unicode tests #4707

* enh(Mutex): Error code for pthread_mutex_lock failure #4712
  • Loading branch information
aleks-f authored Oct 14, 2024
1 parent 8e958f6 commit 71a085c
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 47 deletions.
3 changes: 3 additions & 0 deletions Foundation/include/Poco/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class Foundation_API Error
static std::string getMessage(int errorCode);
/// Utility function translating numeric error code to string.
#endif

static std::string getLastMessage();
/// Utility function returning the last error message.
};


Expand Down
14 changes: 8 additions & 6 deletions Foundation/include/Poco/Mutex_POSIX.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/Error.h"
#include <pthread.h>
#include <errno.h>


namespace Poco {
Expand Down Expand Up @@ -56,8 +56,9 @@ class Foundation_API FastMutexImpl: public MutexImpl
//
inline void MutexImpl::lockImpl()
{
if (pthread_mutex_lock(&_mutex))
throw SystemException("cannot lock mutex");
int rc;
if ((rc = pthread_mutex_lock(&_mutex)))
throw SystemException("cannot lock mutex", Error::getMessage(rc));
}


Expand All @@ -69,14 +70,15 @@ inline bool MutexImpl::tryLockImpl()
else if (rc == EBUSY)
return false;
else
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getMessage(rc));
}


inline void MutexImpl::unlockImpl()
{
if (pthread_mutex_unlock(&_mutex))
throw SystemException("cannot unlock mutex");
int rc;
if ((rc = pthread_mutex_unlock(&_mutex)))
throw SystemException("cannot unlock mutex", Error::getMessage(rc));
}


Expand Down
5 changes: 3 additions & 2 deletions Foundation/include/Poco/Mutex_WIN32.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/Error.h"
#include "Poco/UnWindows.h"


Expand Down Expand Up @@ -55,7 +56,7 @@ inline void MutexImpl::lockImpl()
}
catch (...)
{
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getLastMessage());
}
}

Expand All @@ -69,7 +70,7 @@ inline bool MutexImpl::tryLockImpl()
catch (...)
{
}
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getLastMessage());
}


Expand Down
14 changes: 8 additions & 6 deletions Foundation/include/Poco/RWLock_VX.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/Error.h"
#include <pthread.h>
#include <errno.h>


namespace Poco {
Expand All @@ -48,8 +48,9 @@ class Foundation_API RWLockImpl
//
inline void RWLockImpl::readLockImpl()
{
if (pthread_mutex_lock(&_mutex))
throw SystemException("cannot lock mutex");
int rc = 0;
if ((rc = pthread_mutex_lock(&_mutex)))
throw SystemException("cannot lock mutex", Error::getMessage(rc));
}


Expand All @@ -61,7 +62,7 @@ inline bool RWLockImpl::tryReadLockImpl()
else if (rc == EBUSY)
return false;
else
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getMessage(rc));

}

Expand All @@ -81,8 +82,9 @@ inline bool RWLockImpl::tryWriteLockImpl()

inline void RWLockImpl::unlockImpl()
{
if (pthread_mutex_unlock(&_mutex))
throw SystemException("cannot unlock mutex");
int rc = 0;
if ((rc = pthread_mutex_unlock(&_mutex)))
throw SystemException("cannot unlock mutex", Error::getMessage(rc));
}


Expand Down
7 changes: 6 additions & 1 deletion Foundation/src/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ namespace Poco {
return helper.message();
}


#endif


std::string Error::getLastMessage()
{
return getMessage(last());
}


} // namespace Poco
4 changes: 2 additions & 2 deletions Foundation/src/Mutex_POSIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
else if (rc == ETIMEDOUT)
return false;
else
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getMessage(rc));
#else
const int sleepMillis = 5;
Timestamp now;
Expand All @@ -142,7 +142,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
if (rc == 0)
return true;
else if (rc != EBUSY)
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getMessage(rc));
#if defined(POCO_VXWORKS)
struct timespec ts;
ts.tv_sec = 0;
Expand Down
2 changes: 1 addition & 1 deletion Foundation/src/Mutex_WIN32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
}
catch (...)
{
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getLastMessage());
}
Sleep(sleepMillis);
}
Expand Down
86 changes: 57 additions & 29 deletions JSON/testsuite/src/JSONTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,34 @@ void JSONTest::testStringify()
" }\n"
"}";
assertTrue (ostr.str() == str);

{
std::string jsonStr = R"json({"default":"\u0007\u0007"})json";
auto jsonStrUnescape = Poco::UTF8::unescape(jsonStr);
Poco::JSON::Parser parser;
Poco::Dynamic::Var result = parser.parse(jsonStr);
const auto & obj = result.extract<Poco::JSON::Object::Ptr>();
auto default_val = obj->get("default");
Poco::JSON::Object::Ptr json = new Poco::JSON::Object();
json->set("default", default_val);
std::stringstream ss;
json->stringify(ss);
assertEqual(ss.str(), jsonStr);
}

{
std::string jsonStr = R"json({"default":"\u0050\u0050"})json";
auto jsonStrUnescape = Poco::UTF8::unescape(jsonStr);
Poco::JSON::Parser parser;
Poco::Dynamic::Var result = parser.parse(jsonStr);
const auto & obj = result.extract<Poco::JSON::Object::Ptr>();
auto default_val = obj->get("default");
Poco::JSON::Object::Ptr json = new Poco::JSON::Object();
json->set("default", default_val);
std::stringstream ss;
json->stringify(ss);
assertEqual(ss.str(), jsonStrUnescape);
}
}


Expand Down Expand Up @@ -2375,168 +2403,168 @@ void JSONTest::testEnum()
{
SE_VALUE = 42
};

enum class SAMPLE_ENUM_CLASS
{
VALUE = 42
};

enum class SAMPLE_ENUM_CLASS_I8: Poco::Int8
{
VALUE = 42
};

enum class SAMPLE_ENUM_CLASS_I16: Poco::Int16
{
VALUE = 42
};

enum class SAMPLE_ENUM_CLASS_I32: Poco::Int32
{
VALUE = 42
};

enum class SAMPLE_ENUM_CLASS_I64: Poco::Int64
{
VALUE = 42
};

enum class SAMPLE_ENUM_CLASS_UI8: Poco::UInt8
{
VALUE = 42
};

enum class SAMPLE_ENUM_CLASS_UI16: Poco::UInt16
{
VALUE = 42
};

enum class SAMPLE_ENUM_CLASS_UI32: Poco::UInt32
{
VALUE = 42
};

enum class SAMPLE_ENUM_CLASS_UI64: Poco::UInt64
{
VALUE = 42
};

{
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
obj->set("simple_enum", SE_VALUE);
Poco::Dynamic::Var var(obj);
std::string expected = "{\"simple_enum\":42}";
std::string result = var.convert<std::string>();
assertEquals(expected, result);

SAMPLE_ENUM se = obj->get("simple_enum").extract<SAMPLE_ENUM>();
assertTrue(se == SE_VALUE);
}

{
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
obj->set("enum_class", SAMPLE_ENUM_CLASS::VALUE);
Poco::Dynamic::Var var(obj);
std::string expected = "{\"enum_class\":42}";
std::string result = var.convert<std::string>();
assertEquals(expected, result);

SAMPLE_ENUM_CLASS se = obj->get("enum_class").extract<SAMPLE_ENUM_CLASS>();
assertTrue(se == SAMPLE_ENUM_CLASS::VALUE);
}

{
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
obj->set("enum_class_i8", SAMPLE_ENUM_CLASS_I8::VALUE);
Poco::Dynamic::Var var(obj);
std::string expected = "{\"enum_class_i8\":42}";
std::string result = var.convert<std::string>();
assertEquals(expected, result);

SAMPLE_ENUM_CLASS_I8 se = obj->get("enum_class_i8").extract<SAMPLE_ENUM_CLASS_I8>();
assertTrue(se == SAMPLE_ENUM_CLASS_I8::VALUE);
}

{
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
obj->set("enum_class_i16", SAMPLE_ENUM_CLASS_I16::VALUE);
Poco::Dynamic::Var var(obj);
std::string expected = "{\"enum_class_i16\":42}";
std::string result = var.convert<std::string>();
assertEquals(expected, result);

SAMPLE_ENUM_CLASS_I16 se = obj->get("enum_class_i16").extract<SAMPLE_ENUM_CLASS_I16>();
assertTrue(se == SAMPLE_ENUM_CLASS_I16::VALUE);
}

{
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
obj->set("enum_class_i32", SAMPLE_ENUM_CLASS_I32::VALUE);
Poco::Dynamic::Var var(obj);
std::string expected = "{\"enum_class_i32\":42}";
std::string result = var.convert<std::string>();
assertEquals(expected, result);

SAMPLE_ENUM_CLASS_I32 se = obj->get("enum_class_i32").extract<SAMPLE_ENUM_CLASS_I32>();
assertTrue(se == SAMPLE_ENUM_CLASS_I32::VALUE);
}

{
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
obj->set("enum_class_i64", SAMPLE_ENUM_CLASS_I64::VALUE);
Poco::Dynamic::Var var(obj);
std::string expected = "{\"enum_class_i64\":42}";
std::string result = var.convert<std::string>();
assertEquals(expected, result);

SAMPLE_ENUM_CLASS_I64 se = obj->get("enum_class_i64").extract<SAMPLE_ENUM_CLASS_I64>();
assertTrue(se == SAMPLE_ENUM_CLASS_I64::VALUE);
}

{
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
obj->set("enum_class_ui8", SAMPLE_ENUM_CLASS_UI8::VALUE);
Poco::Dynamic::Var var(obj);
std::string expected = "{\"enum_class_ui8\":42}";
std::string result = var.convert<std::string>();
assertEquals(expected, result);

SAMPLE_ENUM_CLASS_UI8 se = obj->get("enum_class_ui8").extract<SAMPLE_ENUM_CLASS_UI8>();
assertTrue(se == SAMPLE_ENUM_CLASS_UI8::VALUE);
}

{
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
obj->set("enum_class_ui16", SAMPLE_ENUM_CLASS_UI16::VALUE);
Poco::Dynamic::Var var(obj);
std::string expected = "{\"enum_class_ui16\":42}";
std::string result = var.convert<std::string>();
assertEquals(expected, result);

SAMPLE_ENUM_CLASS_UI16 se = obj->get("enum_class_ui16").extract<SAMPLE_ENUM_CLASS_UI16>();
assertTrue(se == SAMPLE_ENUM_CLASS_UI16::VALUE);
}

{
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
obj->set("enum_class_ui32", SAMPLE_ENUM_CLASS_UI32::VALUE);
Poco::Dynamic::Var var(obj);
std::string expected = "{\"enum_class_ui32\":42}";
std::string result = var.convert<std::string>();
assertEquals(expected, result);

SAMPLE_ENUM_CLASS_UI32 se = obj->get("enum_class_ui32").extract<SAMPLE_ENUM_CLASS_UI32>();
assertTrue(se == SAMPLE_ENUM_CLASS_UI32::VALUE);
}

{
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
obj->set("enum_class_ui64", SAMPLE_ENUM_CLASS_UI64::VALUE);
Poco::Dynamic::Var var(obj);
std::string expected = "{\"enum_class_ui64\":42}";
std::string result = var.convert<std::string>();
assertEquals(expected, result);

SAMPLE_ENUM_CLASS_UI64 se = obj->get("enum_class_ui64").extract<SAMPLE_ENUM_CLASS_UI64>();
assertTrue(se == SAMPLE_ENUM_CLASS_UI64::VALUE);
}
Expand Down

0 comments on commit 71a085c

Please sign in to comment.