-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fix FileReader Base64 padding (#12522) * Add test BaseFileReaderResourceUnitTest::Base64EncodesCorrectly * Add BaseFileReaderResourceUnitTest.cpp * Use "string" reader type * Add '=' padding as needed * Fix typo * Change files * Update vnext/Shared/Shared.vcxitems.filters Co-authored-by: Jon Thysell <thysell@gmail.com> --------- Co-authored-by: Jon Thysell <thysell@gmail.com> * Change files --------- Co-authored-by: Jon Thysell <thysell@gmail.com>
- Loading branch information
1 parent
216a9d0
commit 4896ca5
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/react-native-windows-b0f6c82f-5705-494d-b815-65d0e32b8682.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "Fix FileReader Base64 padding (#12522)", | ||
"packageName": "react-native-windows", | ||
"email": "julio.rocha@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
81 changes: 81 additions & 0 deletions
81
vnext/Desktop.UnitTests/BaseFileReaderResourceUnitTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#include <CppUnitTest.h> | ||
|
||
#include <BaseFileReaderResource.h> | ||
|
||
// Windows Libraries | ||
#include <winrt/Windows.Security.Cryptography.h> | ||
|
||
using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||
|
||
using std::shared_ptr; | ||
using std::string; | ||
using std::vector; | ||
using winrt::array_view; | ||
|
||
namespace Microsoft::React::Test { | ||
|
||
TEST_CLASS (BaseFileReaderResourceUnitTest) { | ||
TEST_METHOD(Base64EncodesCorrectly) { | ||
class DummyBlobPersistor final : public IBlobPersistor { | ||
std::unordered_map<string, vector<uint8_t>> m_blobs; | ||
|
||
public: | ||
#pragma region IBlobPersistor | ||
|
||
array_view<uint8_t const> ResolveMessage(string &&blobId, int64_t offset, int64_t size) override { | ||
auto dataItr = m_blobs.find(std::move(blobId)); | ||
// Not found. | ||
if (dataItr == m_blobs.cend()) | ||
throw std::invalid_argument("Blob object not found"); | ||
|
||
auto &bytes = (*dataItr).second; | ||
auto endBound = static_cast<size_t>(offset + size); | ||
// Out of bounds. | ||
if (endBound > bytes.size() || offset >= static_cast<int64_t>(bytes.size()) || offset < 0) | ||
throw std::out_of_range("Offset or size out of range"); | ||
|
||
return array_view<uint8_t const>(bytes.data() + offset, bytes.data() + endBound); | ||
} | ||
|
||
void RemoveMessage(string && /*blobId*/) noexcept override { | ||
// Not implemented | ||
} | ||
|
||
void StoreMessage(vector<uint8_t> &&message, string &&blobId) noexcept override { | ||
m_blobs.insert_or_assign(std::move(blobId), std::move(message)); | ||
} | ||
|
||
string StoreMessage(vector<uint8_t> && /*message*/) noexcept override { | ||
return "Not implemented"; | ||
} | ||
|
||
#pragma endregion IBlobPersistor | ||
}; | ||
|
||
string messageStr = "abcde"; | ||
// Computed using [System.Convert]::ToBase64String('abcd'.ToCharArray()) | ||
constexpr char expected[] = "data:string;base64,YWJjZGU="; | ||
constexpr char guid[] = "93252b5d-a419-4d98-a928-c3ef386f2445"; | ||
|
||
vector<uint8_t> message{}; | ||
message.reserve(messageStr.size()); | ||
message.insert(message.end(), messageStr.begin(), messageStr.end()); | ||
|
||
shared_ptr<IBlobPersistor> persistor = std::make_shared<DummyBlobPersistor>(); | ||
shared_ptr<IFileReaderResource> reader = std::make_shared<BaseFileReaderResource>(persistor); | ||
string result; | ||
auto resolver = [&result](string &&value) { result = std::move(value); }; | ||
auto rejecter = [&result](string &&) { result = "ERROR"; }; | ||
|
||
persistor->StoreMessage(std::move(message), string{guid}); | ||
|
||
reader->ReadAsDataUrl(guid, 0 /*offset*/, messageStr.size(), "string", std::move(resolver), std::move(rejecter)); | ||
|
||
Assert::AreEqual(expected, result.c_str()); | ||
} | ||
}; | ||
|
||
} // namespace Microsoft::React::Test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters