Skip to content

Commit

Permalink
Add simple unit tests for SettingsHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
nlebeck committed Mar 1, 2024
1 parent 5090a02 commit 7793cf6
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions Source/UnitTests/Common/CMakeLists.txt
Expand Up @@ -15,6 +15,7 @@ add_dolphin_test(FlagTest FlagTest.cpp)
add_dolphin_test(FloatUtilsTest FloatUtilsTest.cpp)
add_dolphin_test(MathUtilTest MathUtilTest.cpp)
add_dolphin_test(NandPathsTest NandPathsTest.cpp)
add_dolphin_test(SettingsHandlerTest SettingsHandlerTest.cpp)
add_dolphin_test(SPSCQueueTest SPSCQueueTest.cpp)
add_dolphin_test(StringUtilTest StringUtilTest.cpp)
add_dolphin_test(SwapTest SwapTest.cpp)
Expand Down
106 changes: 106 additions & 0 deletions Source/UnitTests/Common/SettingsHandlerTest.cpp
@@ -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.
1 change: 1 addition & 0 deletions Source/UnitTests/UnitTests.vcxproj
Expand Up @@ -54,6 +54,7 @@
<ClCompile Include="Common\FloatUtilsTest.cpp" />
<ClCompile Include="Common\MathUtilTest.cpp" />
<ClCompile Include="Common\NandPathsTest.cpp" />
<ClCompile Include="Common\SettingsHandlerTest.cpp" />
<ClCompile Include="Common\SPSCQueueTest.cpp" />
<ClCompile Include="Common\StringUtilTest.cpp" />
<ClCompile Include="Common\SwapTest.cpp" />
Expand Down

0 comments on commit 7793cf6

Please sign in to comment.