Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple unit tests for SettingsHandler
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright 2024 Dolphin Emulator Project | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "Common/SettingsHandler.h" | ||
|
||
namespace | ||
{ | ||
constexpr size_t BUFFER_SIZE = Common::SettingsHandler::SETTINGS_SIZE; | ||
|
||
// The encrypted bytes corresponding to the following settings, in order: | ||
// "key" = "val" | ||
std::array<u8, 9> BUFFER_CONTENTS_A = {(u8)'\x91', (u8)'\x91', (u8)'\x90', (u8)'\xEE', (u8)'\xD1', | ||
(u8)'/', (u8)'\xF0', (u8)'4', (u8)'y'}; | ||
|
||
// The encrypted bytes corresponding to the following settings, in order: | ||
// "key1" = "val1" | ||
// "key2" = "val2" | ||
// "foo" = "bar" | ||
std::array<u8, 31> BUFFER_CONTENTS_B = { | ||
(u8)'\x91', (u8)'\x91', (u8)'\x90', (u8)'\xE2', (u8)'\x9A', (u8)'8', (u8)'\xFD', (u8)'U', | ||
(u8)'B', (u8)'\xEA', (u8)'\xC4', (u8)'\xF6', (u8)'^', (u8)'\xF', (u8)'\xDF', (u8)'\xE7', | ||
(u8)'\xC3', (u8)'\n', (u8)'\xBB', (u8)'\x9C', (u8)'P', (u8)'\xB1', (u8)'\x10', (u8)'\x82', | ||
(u8)'\xB4', (u8)'\x8A', (u8)'\r', (u8)'\xBE', (u8)'\xCD', (u8)'r', (u8)'\xF4'}; | ||
} // namespace | ||
|
||
TEST(SettingsHandlerTest, EncryptSingleSetting) | ||
{ | ||
Common::SettingsHandler handler; | ||
handler.AddSetting("key", "val"); | ||
Common::SettingsHandler::Buffer buffer = handler.GetBytes(); | ||
|
||
ASSERT_EQ(buffer.size(), BUFFER_SIZE); | ||
EXPECT_TRUE(std::equal(BUFFER_CONTENTS_A.begin(), BUFFER_CONTENTS_A.end(), buffer.begin())); | ||
} | ||
|
||
TEST(SettingsHandlerTest, DecryptSingleSetting) | ||
{ | ||
Common::SettingsHandler::Buffer buffer; | ||
std::copy(BUFFER_CONTENTS_A.begin(), BUFFER_CONTENTS_A.end(), buffer.begin()); | ||
Common::SettingsHandler handler(std::move(buffer)); | ||
EXPECT_EQ(handler.GetValue("key"), "val"); | ||
} | ||
|
||
TEST(SettingsHandlerTest, EncryptMultipleSettings) | ||
{ | ||
Common::SettingsHandler handler; | ||
handler.AddSetting("key1", "val1"); | ||
handler.AddSetting("key2", "val2"); | ||
handler.AddSetting("foo", "bar"); | ||
Common::SettingsHandler::Buffer buffer = handler.GetBytes(); | ||
|
||
ASSERT_EQ(buffer.size(), BUFFER_SIZE); | ||
EXPECT_TRUE(std::equal(BUFFER_CONTENTS_B.begin(), BUFFER_CONTENTS_B.end(), buffer.begin())); | ||
} | ||
|
||
TEST(SettingsHandlerTest, DecryptMultipleSettings) | ||
{ | ||
Common::SettingsHandler::Buffer buffer; | ||
std::copy(BUFFER_CONTENTS_B.begin(), BUFFER_CONTENTS_B.end(), buffer.begin()); | ||
Common::SettingsHandler handler(std::move(buffer)); | ||
EXPECT_EQ(handler.GetValue("key1"), "val1"); | ||
EXPECT_EQ(handler.GetValue("key2"), "val2"); | ||
EXPECT_EQ(handler.GetValue("foo"), "bar"); | ||
} | ||
|
||
TEST(SettingsHandlerTest, SetBytesOverwritesExistingBuffer) | ||
{ | ||
Common::SettingsHandler::Buffer buffer; | ||
std::copy(BUFFER_CONTENTS_A.begin(), BUFFER_CONTENTS_A.end(), buffer.begin()); | ||
Common::SettingsHandler handler(std::move(buffer)); | ||
ASSERT_EQ(handler.GetValue("key"), "val"); | ||
ASSERT_EQ(handler.GetValue("foo"), ""); | ||
|
||
Common::SettingsHandler::Buffer buffer2; | ||
std::copy(BUFFER_CONTENTS_B.begin(), BUFFER_CONTENTS_B.end(), buffer2.begin()); | ||
handler.SetBytes(std::move(buffer2)); | ||
EXPECT_EQ(handler.GetValue("foo"), "bar"); | ||
EXPECT_EQ(handler.GetValue("key"), ""); | ||
} | ||
|
||
TEST(SettingsHandlerTest, GetValueOnSameInstance) | ||
{ | ||
Common::SettingsHandler handler; | ||
handler.AddSetting("key", "val"); | ||
EXPECT_EQ(handler.GetValue("key"), ""); | ||
|
||
Common::SettingsHandler::Buffer buffer = handler.GetBytes(); | ||
handler.SetBytes(std::move(buffer)); | ||
EXPECT_EQ(handler.GetValue("key"), "val"); | ||
} | ||
|
||
TEST(SettingsHandlerTest, GetValueAfterReset) | ||
{ | ||
Common::SettingsHandler::Buffer buffer; | ||
std::copy(BUFFER_CONTENTS_A.begin(), BUFFER_CONTENTS_A.end(), buffer.begin()); | ||
Common::SettingsHandler handler(std::move(buffer)); | ||
ASSERT_EQ(handler.GetValue("key"), "val"); | ||
|
||
handler.Reset(); | ||
EXPECT_EQ(handler.GetValue("key"), ""); | ||
} | ||
|
||
// TODO: Add test coverage of the edge case fixed in | ||
// https://github.com/dolphin-emu/dolphin/pull/8704. |
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