1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// Copyright 2024 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <gtest/gtest.h>
#include "Common/SettingsHandler.h"
namespace
{
// The encrypted bytes corresponding to the following settings, in order:
// "key" = "val"
Common::SettingsHandler::Buffer BUFFER_A{0x91, 0x91, 0x90, 0xEE, 0xD1, 0x2F, 0xF0, 0x34, 0x79};
// The encrypted bytes corresponding to the following settings, in order:
// "key1" = "val1"
// "key2" = "val2"
// "foo" = "bar"
Common::SettingsHandler::Buffer BUFFER_B{
0x91, 0x91, 0x90, 0xE2, 0x9A, 0x38, 0xFD, 0x55, 0x42, 0xEA, 0xC4, 0xF6, 0x5E, 0xF, 0xDF, 0xE7,
0xC3, 0x0A, 0xBB, 0x9C, 0x50, 0xB1, 0x10, 0x82, 0xB4, 0x8A, 0x0D, 0xBE, 0xCD, 0x72, 0xF4};
// The encrypted bytes corresponding to the following setting:
// "\xFA" = "a"
//
// This setting triggers the edge case fixed in PR #8704: the key's first and only byte matches the
// first byte of the initial encryption key, resulting in a null encoded byte on the first attempt
// to encode the line.
Common::SettingsHandler::Buffer BUFFER_C{0xF0, 0x0E, 0xD4, 0xB2, 0xAA, 0x44};
// The encrypted bytes corresponding to the following setting:
// "\xFA\xE9" = "a"
//
// This setting triggers the edge case fixed in PR #8704 twice:
//
// 1. The key's first byte matches the first byte of the initial encryption key, resulting in a null
// encoded byte on the first attempt to encode the line.
//
// 2. The key's second byte matches the first byte of the encryption key after two
// rotations, resulting in a null encoded byte on the second attempt to encode the line (with a
// single LF inserted before the line).
Common::SettingsHandler::Buffer BUFFER_D{0xF0, 0xFE, 0x13, 0x3A, 0x9A, 0x2F, 0x91, 0x33};
} // namespace
TEST(SettingsHandlerTest, EncryptSingleSetting)
{
Common::SettingsHandler handler;
handler.AddSetting("key", "val");
Common::SettingsHandler::Buffer buffer = handler.GetBytes();
EXPECT_TRUE(std::equal(buffer.begin(), buffer.end(), BUFFER_A.begin(), BUFFER_A.end()));
}
TEST(SettingsHandlerTest, DecryptSingleSetting)
{
Common::SettingsHandler::Buffer buffer = BUFFER_A;
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();
EXPECT_TRUE(std::equal(buffer.begin(), buffer.end(), BUFFER_B.begin(), BUFFER_B.end()));
}
TEST(SettingsHandlerTest, DecryptMultipleSettings)
{
Common::SettingsHandler::Buffer buffer = BUFFER_B;
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 = BUFFER_A;
Common::SettingsHandler handler(std::move(buffer));
ASSERT_EQ(handler.GetValue("key"), "val");
ASSERT_EQ(handler.GetValue("foo"), "");
Common::SettingsHandler::Buffer buffer2 = BUFFER_B;
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 = BUFFER_A;
Common::SettingsHandler handler(std::move(buffer));
ASSERT_EQ(handler.GetValue("key"), "val");
handler.Reset();
EXPECT_EQ(handler.GetValue("key"), "");
}
TEST(SettingsHandlerTest, EncryptAddsLFOnNullChar)
{
Common::SettingsHandler handler;
handler.AddSetting("\xFA", "a");
Common::SettingsHandler::Buffer buffer = handler.GetBytes();
EXPECT_TRUE(std::equal(buffer.begin(), buffer.end(), BUFFER_C.begin(), BUFFER_C.end()));
}
TEST(SettingsHandlerTest, EncryptAddsLFOnNullCharTwice)
{
Common::SettingsHandler handler;
handler.AddSetting("\xFA\xE9", "a");
Common::SettingsHandler::Buffer buffer = handler.GetBytes();
EXPECT_TRUE(std::equal(buffer.begin(), buffer.end(), BUFFER_D.begin(), BUFFER_D.end()));
}
TEST(SettingsHandlerTest, DecryptSingleAddedLF)
{
Common::SettingsHandler::Buffer buffer = BUFFER_C;
Common::SettingsHandler handler(std::move(buffer));
EXPECT_EQ(handler.GetValue("\xFA"), "a");
}
TEST(SettingsHandlerTest, DecryptTwoAddedLFs)
{
Common::SettingsHandler::Buffer buffer = BUFFER_D;
Common::SettingsHandler handler(std::move(buffer));
EXPECT_EQ(handler.GetValue("\xFA\xE9"), "a");
}
|