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