mirror of
				https://github.com/Telecominfraproject/wlan-cloud-lib-poco.git
				synced 2025-11-03 20:18:01 +00:00 
			
		
		
		
	unit tests and changes according to comments
This commit is contained in:
		@@ -97,11 +97,11 @@ public:
 | 
				
			|||||||
		///
 | 
							///
 | 
				
			||||||
		/// Throws a NotFoundException if the value does not exist.
 | 
							/// Throws a NotFoundException if the value does not exist.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void setBinary(const std::string& name, const std::string& value); 
 | 
						void setBinary(const std::string& name, const std::vector<char>& value); 
 | 
				
			||||||
		/// Sets the string value (REG_BINARY) with the given name.
 | 
							/// Sets the string value (REG_BINARY) with the given name.
 | 
				
			||||||
		/// An empty name denotes the default value.
 | 
							/// An empty name denotes the default value.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	std::string getBinary(const std::string& name);
 | 
						std::vector<char> getBinary(const std::string& name);
 | 
				
			||||||
		/// Returns the string value (REG_BINARY) with the given name.
 | 
							/// Returns the string value (REG_BINARY) with the given name.
 | 
				
			||||||
		/// An empty name denotes the default value.
 | 
							/// An empty name denotes the default value.
 | 
				
			||||||
		///
 | 
							///
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -63,7 +63,10 @@ bool WinRegistryConfiguration::getRaw(const std::string& key, std::string& value
 | 
				
			|||||||
			value = aKey.getStringExpand(keyName);
 | 
								value = aKey.getStringExpand(keyName);
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		case WinRegistryKey::REGT_BINARY:
 | 
							case WinRegistryKey::REGT_BINARY:
 | 
				
			||||||
			value = aKey.getBinary(keyName);
 | 
								{
 | 
				
			||||||
 | 
									std::vector<char> tmp = aKey.getBinary(keyName);
 | 
				
			||||||
 | 
									value.assign(tmp.begin(), tmp.end());
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		case WinRegistryKey::REGT_DWORD:
 | 
							case WinRegistryKey::REGT_DWORD:
 | 
				
			||||||
			value = Poco::NumberFormatter::format(aKey.getInt(keyName));
 | 
								value = Poco::NumberFormatter::format(aKey.getInt(keyName));
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,6 +19,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include "Poco/Util/WinRegistryKey.h"
 | 
					#include "Poco/Util/WinRegistryKey.h"
 | 
				
			||||||
#include "Poco/Exception.h"
 | 
					#include "Poco/Exception.h"
 | 
				
			||||||
 | 
					#include "Poco/Buffer.h"
 | 
				
			||||||
#if defined(POCO_WIN32_UTF8)
 | 
					#if defined(POCO_WIN32_UTF8)
 | 
				
			||||||
#include "Poco/UnicodeConverter.h"
 | 
					#include "Poco/UnicodeConverter.h"
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
@@ -121,11 +122,10 @@ std::string WinRegistryKey::getString(const std::string& name)
 | 
				
			|||||||
	if (size > 0)
 | 
						if (size > 0)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		DWORD len = size/2;
 | 
							DWORD len = size/2;
 | 
				
			||||||
		wchar_t* buffer = new wchar_t[len + 1];
 | 
							Poco::Buffer<wchar_t> buffer(len + 1);
 | 
				
			||||||
		RegQueryValueExW(_hKey, uname.c_str(), NULL, NULL, (BYTE*) buffer, &size);
 | 
							RegQueryValueExW(_hKey, uname.c_str(), NULL, NULL, (BYTE*) buffer.begin(), &size);
 | 
				
			||||||
		buffer[len] = 0;
 | 
							buffer[len] = 0;
 | 
				
			||||||
		std::wstring uresult(buffer);
 | 
							std::wstring uresult(buffer.begin());
 | 
				
			||||||
		delete [] buffer;
 | 
					 | 
				
			||||||
		std::string result;
 | 
							std::string result;
 | 
				
			||||||
		Poco::UnicodeConverter::toUTF8(uresult, result);
 | 
							Poco::UnicodeConverter::toUTF8(uresult, result);
 | 
				
			||||||
		return result;
 | 
							return result;
 | 
				
			||||||
@@ -135,11 +135,10 @@ std::string WinRegistryKey::getString(const std::string& name)
 | 
				
			|||||||
		throw NotFoundException(key(name));
 | 
							throw NotFoundException(key(name));
 | 
				
			||||||
	if (size > 0)
 | 
						if (size > 0)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		char* buffer = new char[size + 1];
 | 
							Poco::Buffer<char> buffer(new char[size + 1]);
 | 
				
			||||||
		RegQueryValueExA(_hKey, name.c_str(), NULL, NULL, (BYTE*) buffer, &size);
 | 
							RegQueryValueExA(_hKey, name.c_str(), NULL, NULL, (BYTE*) buffer.begin(), &size);
 | 
				
			||||||
		buffer[size] = 0;
 | 
							buffer[size] = 0;
 | 
				
			||||||
		std::string result(buffer);
 | 
							std::string result(buffer);
 | 
				
			||||||
		delete [] buffer;
 | 
					 | 
				
			||||||
		return result;
 | 
							return result;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
@@ -213,26 +212,28 @@ std::string WinRegistryKey::getStringExpand(const std::string& name)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void WinRegistryKey::setBinary( const std::string& name, const std::string& value )
 | 
					void WinRegistryKey::setBinary( const std::string& name, const std::vector<char>& value )
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	open();
 | 
						open();
 | 
				
			||||||
#if defined(POCO_WIN32_UTF8)
 | 
					#if defined(POCO_WIN32_UTF8)
 | 
				
			||||||
	std::wstring uname;
 | 
						std::wstring uname;
 | 
				
			||||||
	Poco::UnicodeConverter::toUTF16(name, uname);
 | 
						Poco::UnicodeConverter::toUTF16(name, uname);
 | 
				
			||||||
	if (RegSetValueExW(_hKey, uname.c_str(), 0, REG_BINARY, (CONST BYTE*) value.c_str(), (DWORD) value.size()) != ERROR_SUCCESS)
 | 
						if (RegSetValueExW(_hKey, uname.c_str(), 0, REG_BINARY, (CONST BYTE*) value.data(), (DWORD) value.size()) != ERROR_SUCCESS)
 | 
				
			||||||
		handleSetError(name); 
 | 
							handleSetError(name); 
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
	if (RegSetValueExA(_hKey,  name.c_str(), 0, REG_BINARY, (CONST BYTE*) value.c_str(), (DWORD) value.size()) != ERROR_SUCCESS)
 | 
						if (RegSetValueExA(_hKey,  name.c_str(), 0, REG_BINARY, (CONST BYTE*) value.data(), (DWORD) value.size()) != ERROR_SUCCESS)
 | 
				
			||||||
		handleSetError(name); 
 | 
							handleSetError(name); 
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
std::string WinRegistryKey::getBinary( const std::string& name )
 | 
					std::vector<char> WinRegistryKey::getBinary( const std::string& name )
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	open();
 | 
						open();
 | 
				
			||||||
	DWORD type;
 | 
						DWORD type;
 | 
				
			||||||
	DWORD size;
 | 
						DWORD size;
 | 
				
			||||||
 | 
						std::vector<char> result;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if defined(POCO_WIN32_UTF8)
 | 
					#if defined(POCO_WIN32_UTF8)
 | 
				
			||||||
	std::wstring uname;
 | 
						std::wstring uname;
 | 
				
			||||||
	Poco::UnicodeConverter::toUTF16(name, uname);
 | 
						Poco::UnicodeConverter::toUTF16(name, uname);
 | 
				
			||||||
@@ -240,25 +241,19 @@ std::string WinRegistryKey::getBinary( const std::string& name )
 | 
				
			|||||||
		throw NotFoundException(key(name));
 | 
							throw NotFoundException(key(name));
 | 
				
			||||||
	if (size > 0)
 | 
						if (size > 0)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		char* buffer = new char[size];
 | 
							result.resize(size);
 | 
				
			||||||
		RegQueryValueExW(_hKey, uname.c_str(), NULL, NULL, (BYTE*) buffer, &size);
 | 
							RegQueryValueExW(_hKey, uname.c_str(), NULL, NULL, (BYTE*) result.data(), &size);
 | 
				
			||||||
		std::string result(buffer, size);
 | 
					 | 
				
			||||||
		delete [] buffer;
 | 
					 | 
				
			||||||
		return result;
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
	if (RegQueryValueExA(_hKey, name.c_str(), NULL, &type, NULL, &size) != ERROR_SUCCESS || type != REG_BINARY)
 | 
						if (RegQueryValueExA(_hKey, name.c_str(), NULL, &type, NULL, &size) != ERROR_SUCCESS || type != REG_BINARY)
 | 
				
			||||||
		throw NotFoundException(key(name));
 | 
							throw NotFoundException(key(name));
 | 
				
			||||||
	if (size > 0)
 | 
						if (size > 0)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		char* buffer = new char[size];
 | 
							result.resize(size);
 | 
				
			||||||
		RegQueryValueExA(_hKey, name.c_str(), NULL, NULL, (BYTE*) buffer, &size);
 | 
							RegQueryValueExA(_hKey, name.c_str(), NULL, NULL, (BYTE*) result.data(), &size);
 | 
				
			||||||
		std::string result(buffer, size);
 | 
					 | 
				
			||||||
		delete [] buffer;
 | 
					 | 
				
			||||||
		return result;
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
	return std::string();
 | 
						return result;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -62,7 +62,22 @@ void WinConfigurationTest::testConfiguration()
 | 
				
			|||||||
	assert (pReg->getUInt64("name2") == std::numeric_limits<UInt64>::max());
 | 
						assert (pReg->getUInt64("name2") == std::numeric_limits<UInt64>::max());
 | 
				
			||||||
	pReg->setInt64("name2", std::numeric_limits<Int64>::min()); 
 | 
						pReg->setInt64("name2", std::numeric_limits<Int64>::min()); 
 | 
				
			||||||
	assert (pReg->getInt64("name2") == std::numeric_limits<Int64>::min());
 | 
						assert (pReg->getInt64("name2") == std::numeric_limits<Int64>::min());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/// write real int64 value type
 | 
				
			||||||
 | 
						regKey.setInt64("name3", std::numeric_limits<Int64>::max());
 | 
				
			||||||
 | 
						assert (pReg->getInt64("name3") == std::numeric_limits<Int64>::max());
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/// create fake binary data
 | 
				
			||||||
 | 
						const int dataSize = 127;
 | 
				
			||||||
 | 
						std::vector<char> data(dataSize);
 | 
				
			||||||
 | 
						for (int i = 0; i < dataSize; ++i)
 | 
				
			||||||
 | 
							data[i] = rand() % 256;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						regKey.setBinary("name4", data);
 | 
				
			||||||
 | 
						assert (pReg->getString("name4") == std::string(data.begin(), data.end()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	assert (pReg->hasProperty("name1"));
 | 
						assert (pReg->hasProperty("name1"));
 | 
				
			||||||
	assert (pReg->hasProperty("name2"));
 | 
						assert (pReg->hasProperty("name2"));
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
@@ -82,9 +97,11 @@ void WinConfigurationTest::testConfiguration()
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	Poco::Util::AbstractConfiguration::Keys keys;
 | 
						Poco::Util::AbstractConfiguration::Keys keys;
 | 
				
			||||||
	pReg->keys(keys);
 | 
						pReg->keys(keys);
 | 
				
			||||||
	assert (keys.size() == 3);
 | 
						assert (keys.size() == 5);
 | 
				
			||||||
	assert (std::find(keys.begin(), keys.end(), "name1") != keys.end());
 | 
						assert (std::find(keys.begin(), keys.end(), "name1") != keys.end());
 | 
				
			||||||
	assert (std::find(keys.begin(), keys.end(), "name2") != keys.end());
 | 
						assert (std::find(keys.begin(), keys.end(), "name2") != keys.end());
 | 
				
			||||||
 | 
						assert (std::find(keys.begin(), keys.end(), "name3") != keys.end());
 | 
				
			||||||
 | 
						assert (std::find(keys.begin(), keys.end(), "name4") != keys.end());
 | 
				
			||||||
	assert (std::find(keys.begin(), keys.end(), "config") != keys.end());
 | 
						assert (std::find(keys.begin(), keys.end(), "config") != keys.end());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pReg->keys("config", keys);
 | 
						pReg->keys("config", keys);
 | 
				
			||||||
@@ -105,9 +122,11 @@ void WinConfigurationTest::testConfiguration()
 | 
				
			|||||||
	assert (std::find(keys.begin(), keys.end(), "HKEY_USERS") != keys.end());
 | 
						assert (std::find(keys.begin(), keys.end(), "HKEY_USERS") != keys.end());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pRootReg->keys("HKEY_CURRENT_USER.Software.Applied Informatics.Test", keys);
 | 
						pRootReg->keys("HKEY_CURRENT_USER.Software.Applied Informatics.Test", keys);
 | 
				
			||||||
	assert (keys.size() == 3);
 | 
						assert (keys.size() == 5);
 | 
				
			||||||
	assert (std::find(keys.begin(), keys.end(), "name1") != keys.end());
 | 
						assert (std::find(keys.begin(), keys.end(), "name1") != keys.end());
 | 
				
			||||||
	assert (std::find(keys.begin(), keys.end(), "name2") != keys.end());
 | 
						assert (std::find(keys.begin(), keys.end(), "name2") != keys.end());
 | 
				
			||||||
 | 
						assert (std::find(keys.begin(), keys.end(), "name3") != keys.end());
 | 
				
			||||||
 | 
						assert (std::find(keys.begin(), keys.end(), "name4") != keys.end());
 | 
				
			||||||
	assert (std::find(keys.begin(), keys.end(), "config") != keys.end());
 | 
						assert (std::find(keys.begin(), keys.end(), "config") != keys.end());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,12 +19,19 @@
 | 
				
			|||||||
#include "Poco/Util/WinRegistryKey.h"
 | 
					#include "Poco/Util/WinRegistryKey.h"
 | 
				
			||||||
#include "Poco/Environment.h"
 | 
					#include "Poco/Environment.h"
 | 
				
			||||||
#include "Poco/Exception.h"
 | 
					#include "Poco/Exception.h"
 | 
				
			||||||
 | 
					#undef min
 | 
				
			||||||
 | 
					#undef max
 | 
				
			||||||
 | 
					#include <limits>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if defined(POCO_HAVE_INT64)
 | 
				
			||||||
 | 
					using Poco::Int64;
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
using Poco::Util::WinRegistryKey;
 | 
					using Poco::Util::WinRegistryKey;
 | 
				
			||||||
using Poco::Environment;
 | 
					using Poco::Environment;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
WinRegistryTest::WinRegistryTest(const std::string& name): CppUnit::TestCase(name)
 | 
					WinRegistryTest::WinRegistryTest(const std::string& name): CppUnit::TestCase(name)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -81,6 +88,26 @@ void WinRegistryTest::testRegistry()
 | 
				
			|||||||
	regKey.deleteValue("name4");
 | 
						regKey.deleteValue("name4");
 | 
				
			||||||
	assert (!regKey.exists("name4"));
 | 
						assert (!regKey.exists("name4"));
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
					#if defined(POCO_HAVE_INT64)
 | 
				
			||||||
 | 
						regKey.setInt64("name5", std::numeric_limits<Int64>::max());
 | 
				
			||||||
 | 
						assert (regKey.getInt64("name5") == std::numeric_limits<Int64>::max());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						assert (regKey.exists("name5"));
 | 
				
			||||||
 | 
						regKey.deleteValue("name5");
 | 
				
			||||||
 | 
						assert (!regKey.exists("name5"));
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const int dataSize = 127;
 | 
				
			||||||
 | 
						std::vector<char> data(dataSize);
 | 
				
			||||||
 | 
						for (int i = 0; i < dataSize; ++i)
 | 
				
			||||||
 | 
							data[i] = rand() % 256;
 | 
				
			||||||
 | 
						regKey.setBinary("binary", data);
 | 
				
			||||||
 | 
						assert (regKey.getBinary("binary") == data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						assert (regKey.exists("binary"));
 | 
				
			||||||
 | 
						regKey.deleteValue("binary");
 | 
				
			||||||
 | 
						assert (!regKey.exists("binary"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	regKey.deleteKey();
 | 
						regKey.deleteKey();
 | 
				
			||||||
	assert (!regKey.exists());
 | 
						assert (!regKey.exists());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user