diff --git a/Android/jni/jnihooks.c b/Android/jni/jnihooks.c index c0921f9e..1bd301ad 100644 --- a/Android/jni/jnihooks.c +++ b/Android/jni/jnihooks.c @@ -601,13 +601,6 @@ jstring Java_org_deadc0de_apple2ix_Apple2Activity_nativeStateExtractDiskPaths(JN json_destroy(&jsonData); if (fdState >= 0) { - // 2017/06/30 HACK NOTE : if we dup()'d an Android ParcelFileDescriptor ... seek back to beginning here to reset for - // subsequent call to emulator_loadState(). Apparently there is no way to do that via the ParcelFileDescriptor - // API because ... rockstarz and ninjaz - off_t ret = lseek(fdState, 0, SEEK_SET); - if (ret != 0) { - ERRLOG("OOPS : state file lseek() failed!"); - } TEMP_FAILURE_RETRY(close(fdState)); fdState = -1; } diff --git a/src/misc.c b/src/misc.c index 03143a25..41e91de5 100644 --- a/src/misc.c +++ b/src/misc.c @@ -279,6 +279,14 @@ bool emulator_stateExtractDiskPaths(int fd, JSON_ref json) { loaded = true; } while (0); + if (fd >= 0) { + // Ensure that we leave the file descriptor ready for a call to emulator_loadState() + off_t ret = lseek(fd, 0, SEEK_SET); + if (ret != 0) { + ERRLOG("OOPS : state file lseek() failed!"); + } + } + if (!loaded) { LOG("OOPS, problem(s) encountered loading emulator save-state file"); } diff --git a/src/test/testcommon.c b/src/test/testcommon.c index 792f1c28..fb9c90f7 100644 --- a/src/test/testcommon.c +++ b/src/test/testcommon.c @@ -42,7 +42,11 @@ void test_breakpoint(void *arg) { void test_common_init(void) { GREATEST_SET_BREAKPOINT_CB(test_breakpoint, NULL); +#if __ANDROID__ + // tags help us wade through log soup +#else do_logging = false;// silence regular emulator logging +#endif extern void emulator_ctors(void); emulator_ctors(); diff --git a/src/test/testui.c b/src/test/testui.c index 1248d529..2440f3bd 100644 --- a/src/test/testui.c +++ b/src/test/testui.c @@ -138,9 +138,15 @@ TEST test_save_state_1() { char *savData = NULL; ASPRINTF(&savData, "%s/emulator-test.a2state", HOMEDIR); - bool ret = emulator_saveState(savData); + int fd = -1; + TEMP_FAILURE_RETRY(fd = open(savData, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR)); + ASSERT(fd > 0); + + bool ret = emulator_saveState(fd); ASSERT(ret); + TEMP_FAILURE_RETRY(close(fd)); + FREE(savData); PASS(); } @@ -157,18 +163,24 @@ TEST test_load_state_1() { char *savData = NULL; ASPRINTF(&savData, "%s/emulator-test.a2state", HOMEDIR); + int fdState = -1; + TEMP_FAILURE_RETRY(fdState = open(savData, O_RDONLY)); + ASSERT(fdState > 0); + bool ret = false; int fdA = -1; int fdB = -1; { JSON_ref jsonData; - ret = emulator_stateExtractDiskPaths(savData, &jsonData); + int siz = json_createFromString("{}", &jsonData); + ASSERT(siz > 0); + ret = emulator_stateExtractDiskPaths(fdState, jsonData); ASSERT(ret); _get_fds(jsonData, &fdA, &fdB); json_destroy(&jsonData); } - ret = emulator_loadState(savData, fdA, fdB); + ret = emulator_loadState(fdState, fdA, fdB); ASSERT(ret); ASSERT(apple_ii_64k[0][WATCHPOINT_ADDR] != TEST_FINISHED); @@ -176,6 +188,10 @@ TEST test_load_state_1() { _assert_blank_boot(); + TEMP_FAILURE_RETRY(close(fdState)); + TEMP_FAILURE_RETRY(close(fdA)); + TEMP_FAILURE_RETRY(close(fdB)); + unlink(savData); FREE(savData); @@ -212,18 +228,24 @@ TEST test_load_A2VM_good1() { // load state and assert + int fdState = -1; + TEMP_FAILURE_RETRY(fdState = open(savData, O_RDONLY)); + ASSERT(fdState > 0); + bool ret = false; int fdA = -1; int fdB = -1; { JSON_ref jsonData; - ret = emulator_stateExtractDiskPaths(savData, &jsonData); + int siz = json_createFromString("{}", &jsonData); + ASSERT(siz > 0); + ret = emulator_stateExtractDiskPaths(fdState, jsonData); ASSERT(ret); _get_fds(jsonData, &fdA, &fdB); json_destroy(&jsonData); } - ret = emulator_loadState(savData, fdA, fdB); + ret = emulator_loadState(fdState, fdA, fdB); ASSERT(ret); // ASSERT framebuffer matches expected @@ -280,6 +302,10 @@ TEST test_load_A2VM_good1() { ASSERT(cpu65_y == 0x01); ASSERT(cpu65_sp == 0xEA); + TEMP_FAILURE_RETRY(close(fdState)); + TEMP_FAILURE_RETRY(close(fdA)); + TEMP_FAILURE_RETRY(close(fdB)); + unlink(savData); FREE(savData); @@ -316,18 +342,24 @@ TEST test_load_A2V2_good1() { // load state and assert + int fdState = -1; + TEMP_FAILURE_RETRY(fdState = open(savData, O_RDONLY)); + ASSERT(fdState > 0); + bool ret = false; int fdA = -1; int fdB = -1; { JSON_ref jsonData; - ret = emulator_stateExtractDiskPaths(savData, &jsonData); + int siz = json_createFromString("{}", &jsonData); + ASSERT(siz > 0); + ret = emulator_stateExtractDiskPaths(fdState, jsonData); ASSERT(ret); _get_fds(jsonData, &fdA, &fdB); json_destroy(&jsonData); } - ret = emulator_loadState(savData, fdA, fdB); + ret = emulator_loadState(fdState, fdA, fdB); ASSERT(ret); // ASSERT framebuffer matches expected @@ -396,6 +428,10 @@ TEST test_load_A2V2_good1() { size_t mbSiz = sizeof(mbData); mb_testAssertA2V2(mbData, mbSiz); + TEMP_FAILURE_RETRY(close(fdState)); + TEMP_FAILURE_RETRY(close(fdA)); + TEMP_FAILURE_RETRY(close(fdB)); + unlink(savData); FREE(savData); @@ -432,18 +468,24 @@ TEST test_load_A2V2_good2() { // load state and assert + int fdState = -1; + TEMP_FAILURE_RETRY(fdState = open(savData, O_RDONLY)); + ASSERT(fdState > 0); + bool ret = false; int fdA = -1; int fdB = -1; { JSON_ref jsonData; - ret = emulator_stateExtractDiskPaths(savData, &jsonData); + int siz = json_createFromString("{}", &jsonData); + ASSERT(siz > 0); + ret = emulator_stateExtractDiskPaths(fdState, jsonData); ASSERT(ret); _get_fds(jsonData, &fdA, &fdB); json_destroy(&jsonData); } - ret = emulator_loadState(savData, fdA, fdB); + ret = emulator_loadState(fdState, fdA, fdB); ASSERT(ret); // ASSERT framebuffer matches expected @@ -512,6 +554,10 @@ TEST test_load_A2V2_good2() { size_t mbSiz = sizeof(mbData); mb_testAssertA2V2(mbData, mbSiz); + TEMP_FAILURE_RETRY(close(fdState)); + TEMP_FAILURE_RETRY(close(fdA)); + TEMP_FAILURE_RETRY(close(fdB)); + unlink(savData); FREE(savData);