16 wrapper_ = std::make_unique<AsarWrapper>();
26 test_dir_ = std::filesystem::temp_directory_path() /
"yaze_asar_test";
27 std::filesystem::create_directories(test_dir_);
30 test_asm_path_ = test_dir_ /
"test_patch.asm";
31 std::ofstream asm_file(test_asm_path_);
33; Test assembly patch for yaze
48 invalid_asm_path_ = test_dir_ /
"invalid_patch.asm";
49 std::ofstream invalid_file(invalid_asm_path_);
51; Invalid assembly that should cause errors
53invalid_instruction_here
62 void CleanupTestFiles() {
64 if (std::filesystem::exists(test_dir_)) {
65 std::filesystem::remove_all(test_dir_);
67 }
catch (
const std::exception& e) {
72 std::unique_ptr<AsarWrapper> wrapper_;
73 std::filesystem::path test_dir_;
74 std::filesystem::path test_asm_path_;
75 std::filesystem::path invalid_asm_path_;
76 std::vector<uint8_t> test_rom_;
81 ASSERT_FALSE(wrapper_->IsInitialized());
83 auto status = wrapper_->Initialize();
84 EXPECT_TRUE(status.ok()) << status.message();
85 EXPECT_TRUE(wrapper_->IsInitialized());
88 std::string version = wrapper_->GetVersion();
89 EXPECT_FALSE(version.empty());
90 EXPECT_NE(version,
"Not initialized");
92 int api_version = wrapper_->GetApiVersion();
93 EXPECT_GT(api_version, 0);
97 EXPECT_FALSE(wrapper_->IsInitialized());
101 auto status1 = wrapper_->Initialize();
102 EXPECT_TRUE(status1.ok());
104 auto status2 = wrapper_->Initialize();
105 EXPECT_TRUE(status2.ok());
106 EXPECT_TRUE(wrapper_->IsInitialized());
111 ASSERT_FALSE(wrapper_->IsInitialized());
113 std::vector<uint8_t> rom_copy = test_rom_;
114 auto patch_result = wrapper_->ApplyPatch(test_asm_path_.string(), rom_copy);
115 EXPECT_FALSE(patch_result.ok());
116 EXPECT_THAT(patch_result.status().message(),
117 testing::HasSubstr(
"not initialized"));
119 auto symbols_result = wrapper_->ExtractSymbols(test_asm_path_.string());
120 EXPECT_FALSE(symbols_result.ok());
121 EXPECT_THAT(symbols_result.status().message(),
122 testing::HasSubstr(
"not initialized"));
126 ASSERT_TRUE(wrapper_->Initialize().ok());
128 std::vector<uint8_t> rom_copy = test_rom_;
129 size_t original_size = rom_copy.size();
131 auto patch_result = wrapper_->ApplyPatch(test_asm_path_.string(), rom_copy);
132 ASSERT_TRUE(patch_result.ok()) << patch_result.status().message();
134 const auto& result = patch_result.value();
135 EXPECT_TRUE(result.success) <<
"Patch failed: "
136 << testing::PrintToString(result.errors);
137 EXPECT_GT(result.rom_size, 0);
138 EXPECT_EQ(rom_copy.size(), result.rom_size);
141 EXPECT_NE(rom_copy, test_rom_);
145 ASSERT_TRUE(wrapper_->Initialize().ok());
147 std::vector<uint8_t> rom_copy = test_rom_;
149 auto patch_result = wrapper_->ApplyPatch(invalid_asm_path_.string(), rom_copy);
150 EXPECT_FALSE(patch_result.ok());
151 EXPECT_THAT(patch_result.status().message(),
152 testing::HasSubstr(
"Patch failed"));
156 ASSERT_TRUE(wrapper_->Initialize().ok());
158 std::vector<uint8_t> rom_copy = test_rom_;
159 std::string nonexistent_path = test_dir_.string() +
"/nonexistent.asm";
161 auto patch_result = wrapper_->ApplyPatch(nonexistent_path, rom_copy);
162 EXPECT_FALSE(patch_result.ok());
166 ASSERT_TRUE(wrapper_->Initialize().ok());
168 auto symbols_result = wrapper_->ExtractSymbols(test_asm_path_.string());
169 ASSERT_TRUE(symbols_result.ok()) << symbols_result.status().message();
171 const auto& symbols = symbols_result.value();
172 EXPECT_GT(symbols.size(), 0);
175 bool found_testlabel =
false;
176 bool found_anotherlabel =
false;
178 for (
const auto& symbol : symbols) {
179 EXPECT_FALSE(symbol.name.empty());
180 EXPECT_GT(symbol.address, 0);
182 if (symbol.name ==
"testlabel") {
183 found_testlabel =
true;
184 EXPECT_EQ(symbol.address, 0x008000);
185 }
else if (symbol.name ==
"anotherlabel") {
186 found_anotherlabel =
true;
190 EXPECT_TRUE(found_testlabel) <<
"Expected 'testlabel' symbol not found";
191 EXPECT_TRUE(found_anotherlabel) <<
"Expected 'anotherlabel' symbol not found";
195 ASSERT_TRUE(wrapper_->Initialize().ok());
197 std::vector<uint8_t> rom_copy = test_rom_;
198 auto patch_result = wrapper_->ApplyPatch(test_asm_path_.string(), rom_copy);
199 ASSERT_TRUE(patch_result.ok());
202 auto symbol_table = wrapper_->GetSymbolTable();
203 EXPECT_GT(symbol_table.size(), 0);
206 auto testlabel_symbol = wrapper_->FindSymbol(
"testlabel");
207 EXPECT_TRUE(testlabel_symbol.has_value());
208 if (testlabel_symbol) {
209 EXPECT_EQ(testlabel_symbol->name,
"testlabel");
210 EXPECT_GT(testlabel_symbol->address, 0);
214 auto nonexistent_symbol = wrapper_->FindSymbol(
"nonexistent_symbol");
215 EXPECT_FALSE(nonexistent_symbol.has_value());
218 if (testlabel_symbol) {
219 auto symbols_at_addr = wrapper_->GetSymbolsAtAddress(testlabel_symbol->address);
220 EXPECT_GT(symbols_at_addr.size(), 0);
223 for (
const auto& symbol : symbols_at_addr) {
224 if (symbol.name ==
"testlabel") {
234 ASSERT_TRUE(wrapper_->Initialize().ok());
236 std::string patch_content = R
"(
244 std::vector<uint8_t> rom_copy = test_rom_;
245 auto patch_result = wrapper_->ApplyPatchFromString(
246 patch_content, rom_copy, test_dir_.string());
248 ASSERT_TRUE(patch_result.ok()) << patch_result.status().message();
250 const auto& result = patch_result.value();
251 EXPECT_TRUE(result.success);
252 EXPECT_GT(result.symbols.size(), 0);
255 bool found_symbol =
false;
256 for (
const auto& symbol : result.symbols) {
257 if (symbol.name ==
"stringpatchlabel") {
259 EXPECT_EQ(symbol.address, 0x009000);
263 EXPECT_TRUE(found_symbol);
267 ASSERT_TRUE(wrapper_->Initialize().ok());
270 auto valid_status = wrapper_->ValidateAssembly(test_asm_path_.string());
271 EXPECT_TRUE(valid_status.ok()) << valid_status.message();
274 auto invalid_status = wrapper_->ValidateAssembly(invalid_asm_path_.string());
275 EXPECT_FALSE(invalid_status.ok());
276 EXPECT_THAT(invalid_status.message(),
277 testing::AnyOf(testing::HasSubstr(
"validation failed"),
278 testing::HasSubstr(
"Patch failed"),
279 testing::HasSubstr(
"Unknown command"),
280 testing::HasSubstr(
"Label")));
284 ASSERT_TRUE(wrapper_->Initialize().ok());
287 std::vector<uint8_t> rom_copy = test_rom_;
288 auto patch_result = wrapper_->ApplyPatch(test_asm_path_.string(), rom_copy);
289 ASSERT_TRUE(patch_result.ok());
292 auto symbol_table_before = wrapper_->GetSymbolTable();
293 EXPECT_GT(symbol_table_before.size(), 0);
298 auto symbol_table_after = wrapper_->GetSymbolTable();
299 EXPECT_EQ(symbol_table_after.size(), 0);
301 auto errors = wrapper_->GetLastErrors();
302 auto warnings = wrapper_->GetLastWarnings();
303 EXPECT_EQ(errors.size(), 0);
304 EXPECT_EQ(warnings.size(), 0);
308 ASSERT_TRUE(wrapper_->Initialize().ok());
310 std::vector<uint8_t> original_rom = test_rom_;
311 std::vector<uint8_t> modified_rom = test_rom_;
312 modified_rom[100] = 0x42;
314 std::string patch_path = test_dir_.string() +
"/generated.asm";
315 auto status = wrapper_->CreatePatch(original_rom, modified_rom, patch_path);
317 EXPECT_FALSE(status.ok());
318 EXPECT_THAT(status.message(), testing::HasSubstr(
"not yet implemented"));