Skip to content

Commit a617976

Browse files
authored
Merge pull request #521 from xqp/fix/win32_encryption_throws_by_empty_string
Fix/win32 encryption throws by empty string
2 parents f20f378 + d947fb5 commit a617976

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

Release/src/utilities/web_utilities.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ plaintext_string winrt_encryption::decrypt() const
9292
win32_encryption::win32_encryption(const std::wstring &data) :
9393
m_numCharacters(data.size())
9494
{
95+
// Early return because CryptProtectMemory crashs with empty string
96+
if (m_numCharacters == 0)
97+
{
98+
return;
99+
}
100+
95101
const auto dataNumBytes = data.size() * sizeof(std::wstring::value_type);
96102
m_buffer.resize(dataNumBytes);
97103
memcpy_s(m_buffer.data(), m_buffer.size(), data.c_str(), dataNumBytes);

Release/tests/functional/utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(SOURCES
44
strings.cpp
55
macro_test.cpp
66
nonce_generator_tests.cpp
7+
win32_encryption_tests.cpp
78
stdafx.cpp
89
)
910

Release/tests/functional/utils/stdafx.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "cpprest/uri.h"
1818
#include "cpprest/asyncrt_utils.h"
19+
#include "cpprest/details/web_utilities.h"
1920

2021
#include "unittestpp.h"
21-
#include "utils_tests.h"
22+
#include "utils_tests.h"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/***
2+
* Copyright (C) Microsoft. All rights reserved.
3+
* Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4+
*
5+
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
6+
*
7+
* win32_encryption_tests.cpp
8+
*
9+
* Tests for win32_encryption class.
10+
*
11+
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
12+
****/
13+
14+
#include "stdafx.h"
15+
16+
using namespace utility;
17+
18+
namespace tests { namespace functional { namespace utils_tests {
19+
20+
#if defined(_WIN32) && !defined(CPPREST_TARGET_XP) && !defined(__cplusplus_winrt)
21+
SUITE(win32_encryption)
22+
{
23+
24+
TEST(win32_encryption_random_string)
25+
{
26+
utility::string_t rndStr = utility::conversions::to_string_t("random string");
27+
web::details::win32_encryption enc(rndStr);
28+
29+
VERIFY_ARE_EQUAL(*enc.decrypt(), rndStr);
30+
}
31+
32+
TEST(win32_encryption_empty_string)
33+
{
34+
utility::string_t emptyStr = utility::conversions::to_string_t("");
35+
web::details::win32_encryption enc(emptyStr);
36+
37+
VERIFY_ARE_EQUAL(*enc.decrypt(), emptyStr);
38+
}
39+
40+
} // SUITE(win32_encryption)
41+
42+
#endif
43+
44+
}}}

0 commit comments

Comments
 (0)