| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <gtest/gtest.h> |
| 5 | |
| 6 | #include <vector> |
| 7 | |
| 8 | #include "Core/CommonTitles.h" |
| 9 | #include "Core/IOS/ES/Formats.h" |
| 10 | #include "TestBinaryData.h" |
| 11 | |
| 12 | TEST(ESFormats, TitleType) |
| 13 | { |
| 14 | EXPECT_TRUE(IOS::ES::IsTitleType(Titles::SYSTEM_MENU, IOS::ES::TitleType::System)); |
| 15 | EXPECT_FALSE(IOS::ES::IsDiscTitle(Titles::SYSTEM_MENU)); |
| 16 | |
| 17 | constexpr u64 ios59_title_id = 0x000000010000003b; |
| 18 | EXPECT_TRUE(IOS::ES::IsTitleType(ios59_title_id, IOS::ES::TitleType::System)); |
| 19 | EXPECT_FALSE(IOS::ES::IsDiscTitle(ios59_title_id)); |
| 20 | |
| 21 | constexpr u64 ztp_title_id = 0x00010000525a4445; |
| 22 | EXPECT_TRUE(IOS::ES::IsTitleType(ztp_title_id, IOS::ES::TitleType::Game)); |
| 23 | EXPECT_TRUE(IOS::ES::IsDiscTitle(ztp_title_id)); |
| 24 | } |
| 25 | |
| 26 | TEST(TMDReader, Validity) |
| 27 | { |
| 28 | IOS::ES::TMDReader tmd; |
| 29 | EXPECT_FALSE(tmd.IsValid()) << "An empty TMDReader should be invalid"; |
| 30 | |
| 31 | tmd = IOS::ES::TMDReader{{1, 2, 3}}; |
| 32 | EXPECT_FALSE(tmd.IsValid()) << "IsValid should be false when reading an invalid TMD"; |
| 33 | |
| 34 | tmd = IOS::ES::TMDReader{std::vector(soup01_tmd.cbegin(), soup01_tmd.cend() - 1)}; |
| 35 | EXPECT_FALSE(tmd.IsValid()) << "IsValid should be false when reading an invalid TMD"; |
| 36 | |
| 37 | tmd = IOS::ES::TMDReader{std::vector(soup01_tmd.cbegin(), soup01_tmd.cend())}; |
| 38 | EXPECT_TRUE(tmd.IsValid()) << "IsValid should be true when reading a valid TMD"; |
| 39 | |
| 40 | tmd = IOS::ES::TMDReader{std::vector(ios59_tmd.cbegin(), ios59_tmd.cend())}; |
| 41 | EXPECT_TRUE(tmd.IsValid()) << "IsValid should be true when reading a valid TMD"; |
| 42 | } |
| 43 | |
| 44 | class TMDReaderTest : public ::testing::Test |
| 45 | { |
| 46 | protected: |
| 47 | void SetUp() override = 0; |
| 48 | |
| 49 | // Common tests. |
| 50 | void TestGeneralInfo(); |
| 51 | void TestRawTMDAndView(); |
| 52 | |
| 53 | // Expected data. |
| 54 | virtual u64 GetTitleId() const = 0; |
| 55 | virtual u64 GetIOSId() const = 0; |
| 56 | virtual std::string GetGameID() const = 0; |
| 57 | virtual std::vector<u8> GetRawView() const = 0; |
| 58 | |
| 59 | std::vector<u8> m_raw_tmd; |
| 60 | IOS::ES::TMDReader m_tmd; |
| 61 | }; |
| 62 | |
| 63 | void TMDReaderTest::SetUp() |
| 64 | { |
| 65 | m_tmd.SetBytes(m_raw_tmd); |
| 66 | ASSERT_TRUE(m_tmd.IsValid()) << "BUG: The test TMD should be valid."; |
| 67 | } |
| 68 | |
| 69 | void TMDReaderTest::TestGeneralInfo() |
| 70 | { |
| 71 | EXPECT_EQ(m_tmd.GetTitleId(), GetTitleId()); |
| 72 | EXPECT_EQ(m_tmd.GetIOSId(), GetIOSId()); |
| 73 | EXPECT_EQ(m_tmd.GetGameID(), GetGameID()); |
| 74 | } |
| 75 | |
| 76 | void TMDReaderTest::TestRawTMDAndView() |
| 77 | { |
| 78 | const std::vector<u8>& dolphin_tmd_bytes = m_tmd.GetBytes(); |
| 79 | // Separate check because gtest prints neither the size nor the full buffer. |
| 80 | EXPECT_EQ(m_raw_tmd.size(), dolphin_tmd_bytes.size()); |
| 81 | EXPECT_EQ(m_raw_tmd, dolphin_tmd_bytes); |
| 82 | |
| 83 | const std::vector<u8> tmd_view = GetRawView(); |
| 84 | const std::vector<u8> dolphin_tmd_view = m_tmd.GetRawView(); |
| 85 | EXPECT_EQ(dolphin_tmd_view.size(), tmd_view.size()); |
| 86 | EXPECT_EQ(dolphin_tmd_view, tmd_view); |
| 87 | } |
| 88 | |
| 89 | class GameTMDReaderTest : public TMDReaderTest |
| 90 | { |
| 91 | protected: |
| 92 | void SetUp() override |
| 93 | { |
| 94 | m_raw_tmd = std::vector(soup01_tmd.cbegin(), soup01_tmd.cend()); |
| 95 | TMDReaderTest::SetUp(); |
| 96 | } |
| 97 | u64 GetTitleId() const override { return 0x00010000534f5550; } |
| 98 | u64 GetIOSId() const override { return 0x0000000100000038; } |
| 99 | std::string GetGameID() const override { return "SOUP01"; } |
| 100 | std::vector<u8> GetRawView() const override |
| 101 | { |
| 102 | return std::vector(soup01_tmd_view.cbegin(), soup01_tmd_view.cend()); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | TEST_F(GameTMDReaderTest, GeneralInfo) |
| 107 | { |
| 108 | TestGeneralInfo(); |
| 109 | } |
| 110 | |
| 111 | TEST_F(GameTMDReaderTest, RawTMDAndView) |
| 112 | { |
| 113 | TestRawTMDAndView(); |
| 114 | } |
| 115 | |
| 116 | TEST_F(GameTMDReaderTest, ContentInfo) |
| 117 | { |
| 118 | EXPECT_EQ(m_tmd.GetNumContents(), 1U); |
| 119 | |
| 120 | const auto check_is_expected_content = [](const IOS::ES::Content& content) { |
| 121 | EXPECT_EQ(content.id, 0U); |
| 122 | EXPECT_EQ(content.index, 0U); |
| 123 | EXPECT_EQ(content.type, 3U); |
| 124 | EXPECT_EQ(content.size, 0xff7c0000ULL); |
| 125 | constexpr std::array<u8, 20> expected_hash = {{ |
| 126 | 0x77, 0x13, 0xe0, 0xac, 0xef, 0xc1, 0x01, 0x51, 0xe7, 0x8b, |
| 127 | 0x0b, 0x01, 0xa2, 0xfb, 0x03, 0xdb, 0x45, 0x8a, 0x0e, 0x18, |
| 128 | }}; |
| 129 | EXPECT_EQ(content.sha1, expected_hash); |
| 130 | }; |
| 131 | |
| 132 | IOS::ES::Content content; |
| 133 | |
| 134 | EXPECT_TRUE(m_tmd.FindContentById(0, &content)) << "Content 0 should exist"; |
| 135 | check_is_expected_content(content); |
| 136 | |
| 137 | EXPECT_TRUE(m_tmd.GetContent(0, &content)) << "Content with index 0 should exist"; |
| 138 | check_is_expected_content(content); |
| 139 | |
| 140 | EXPECT_FALSE(m_tmd.FindContentById(1, &content)) << "Content 1 should not exist"; |
| 141 | EXPECT_FALSE(m_tmd.GetContent(1, &content)) << "Content with index 1 should not exist"; |
| 142 | |
| 143 | const std::vector<IOS::ES::Content> contents = m_tmd.GetContents(); |
| 144 | ASSERT_EQ(contents.size(), size_t(1)); |
| 145 | check_is_expected_content(contents.at(0)); |
| 146 | } |
| 147 | |
| 148 | class IOSTMDReaderTest : public TMDReaderTest |
| 149 | { |
| 150 | protected: |
| 151 | void SetUp() override |
| 152 | { |
| 153 | m_raw_tmd = std::vector(ios59_tmd.cbegin(), ios59_tmd.cend()); |
| 154 | TMDReaderTest::SetUp(); |
| 155 | } |
| 156 | u64 GetTitleId() const override { return 0x000000010000003b; } |
| 157 | u64 GetIOSId() const override { return 0; } |
| 158 | std::string GetGameID() const override { return "000000010000003b"; } |
| 159 | std::vector<u8> GetRawView() const override |
| 160 | { |
| 161 | return std::vector(ios59_tmd_view.cbegin(), ios59_tmd_view.cend()); |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | TEST_F(IOSTMDReaderTest, GeneralInfo) |
| 166 | { |
| 167 | TestGeneralInfo(); |
| 168 | EXPECT_EQ(m_tmd.GetBootIndex(), 22U); |
| 169 | EXPECT_EQ(m_tmd.GetTitleVersion(), 0x2421U); |
| 170 | } |
| 171 | |
| 172 | TEST_F(IOSTMDReaderTest, RawTMDAndView) |
| 173 | { |
| 174 | TestRawTMDAndView(); |
| 175 | } |
| 176 | |
| 177 | TEST_F(IOSTMDReaderTest, ContentInfo) |
| 178 | { |
| 179 | EXPECT_EQ(m_tmd.GetNumContents(), 23U); |
| 180 | |
| 181 | const auto check_is_first_content = [](const IOS::ES::Content& content) { |
| 182 | EXPECT_EQ(content.id, 0x00000020U); |
| 183 | EXPECT_EQ(content.index, 0U); |
| 184 | EXPECT_EQ(content.type, 1U); |
| 185 | EXPECT_FALSE(content.IsShared()); |
| 186 | EXPECT_EQ(content.size, 0x40ULL); |
| 187 | constexpr std::array<u8, 20> expected_hash = {{ |
| 188 | 0x2c, 0x96, 0x97, 0x6d, 0x25, 0x2b, 0x2e, 0xa0, 0xcd, 0xc1, |
| 189 | 0xea, 0x16, 0x57, 0x7f, 0x3d, 0x90, 0x82, 0x59, 0xf1, 0x53, |
| 190 | }}; |
| 191 | EXPECT_EQ(content.sha1, expected_hash); |
| 192 | }; |
| 193 | |
| 194 | const auto check_is_kernel = [](const IOS::ES::Content& content) { |
| 195 | EXPECT_EQ(content.id, 0x00000018U); |
| 196 | EXPECT_EQ(content.index, 22U); |
| 197 | EXPECT_EQ(content.type, 0x8001U); |
| 198 | EXPECT_TRUE(content.IsShared()); |
| 199 | EXPECT_EQ(content.size, 0x293d8ULL); |
| 200 | constexpr std::array<u8, 20> expected_hash = {{ |
| 201 | 0xfd, 0x77, 0x46, 0xac, 0x47, 0x00, 0xd5, 0x86, 0xa9, 0x32, |
| 202 | 0x84, 0xfb, 0xe3, 0x12, 0xc5, 0x2b, 0x2a, 0xc6, 0x33, 0xd3, |
| 203 | }}; |
| 204 | EXPECT_EQ(content.sha1, expected_hash); |
| 205 | }; |
| 206 | |
| 207 | const std::vector<IOS::ES::Content> contents = m_tmd.GetContents(); |
| 208 | ASSERT_EQ(contents.size(), size_t(23)); |
| 209 | |
| 210 | IOS::ES::Content content; |
| 211 | |
| 212 | EXPECT_FALSE(m_tmd.FindContentById(0x23, &content)) << "Content 00000023 should not exist"; |
| 213 | EXPECT_FALSE(m_tmd.GetContent(23, &content)) << "Content with index 23 should not exist"; |
| 214 | |
| 215 | EXPECT_TRUE(m_tmd.FindContentById(0x20, &content)) << "Content 00000020 should exist"; |
| 216 | check_is_first_content(content); |
| 217 | EXPECT_TRUE(m_tmd.GetContent(0, &content)) << "Content with index 0 should exist"; |
| 218 | check_is_first_content(content); |
| 219 | check_is_first_content(contents.at(0)); |
| 220 | |
| 221 | EXPECT_TRUE(m_tmd.FindContentById(0x18, &content)) << "Content 00000018 should exist"; |
| 222 | check_is_kernel(content); |
| 223 | EXPECT_TRUE(m_tmd.GetContent(22, &content)) << "Content with index 22 should exist"; |
| 224 | check_is_kernel(content); |
| 225 | check_is_kernel(contents.at(22)); |
| 226 | } |