chore(JSON): add stringify unicode tests #4707 (#4720)

* chore(JSON): add stringify unicode tests #4707

* enh(Mutex): Error code for pthread_mutex_lock failure #4712
This commit is contained in:
Aleksandar Fabijanic 2024-10-14 06:48:45 -05:00 committed by GitHub
parent 8e958f685f
commit 71a085c1dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 88 additions and 47 deletions

View File

@ -43,6 +43,9 @@ public:
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.
};

View File

@ -20,8 +20,8 @@
#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/Error.h"
#include <pthread.h>
#include <errno.h>
namespace Poco {
@ -56,8 +56,9 @@ protected:
//
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));
}
@ -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));
}

View File

@ -20,6 +20,7 @@
#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/Error.h"
#include "Poco/UnWindows.h"
@ -55,7 +56,7 @@ inline void MutexImpl::lockImpl()
}
catch (...)
{
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getLastMessage());
}
}
@ -69,7 +70,7 @@ inline bool MutexImpl::tryLockImpl()
catch (...)
{
}
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getLastMessage());
}

View File

@ -20,8 +20,8 @@
#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/Error.h"
#include <pthread.h>
#include <errno.h>
namespace Poco {
@ -48,8 +48,9 @@ private:
//
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));
}
@ -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));
}
@ -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));
}

View File

@ -102,8 +102,13 @@ namespace Poco {
return helper.message();
}
#endif
std::string Error::getLastMessage()
{
return getMessage(last());
}
} // namespace Poco

View File

@ -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;
@ -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;

View File

@ -47,7 +47,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
}
catch (...)
{
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getLastMessage());
}
Sleep(sleepMillis);
}

View File

@ -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);
}
}
@ -2375,52 +2403,52 @@ 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);
@ -2428,11 +2456,11 @@ void JSONTest::testEnum()
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);
@ -2440,11 +2468,11 @@ void JSONTest::testEnum()
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);
@ -2452,11 +2480,11 @@ void JSONTest::testEnum()
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);
@ -2464,11 +2492,11 @@ void JSONTest::testEnum()
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);
@ -2476,11 +2504,11 @@ void JSONTest::testEnum()
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);
@ -2488,11 +2516,11 @@ void JSONTest::testEnum()
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);
@ -2500,11 +2528,11 @@ void JSONTest::testEnum()
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);
@ -2512,11 +2540,11 @@ void JSONTest::testEnum()
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);
@ -2524,11 +2552,11 @@ void JSONTest::testEnum()
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);
@ -2536,7 +2564,7 @@ void JSONTest::testEnum()
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);
}