142 lines
5.8 KiB
C
142 lines
5.8 KiB
C
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// The host checks every final file byte against its own fixtures and compares
|
|
// the raw volume before and after a guest flush on write-protected media.
|
|
#include <stdio.h>
|
|
#include <cmoc.h>
|
|
#include <coco.h>
|
|
#include <decbfile.h>
|
|
|
|
#define CHECK(test) do { if (!(test)) { printf("FAIL LINE %u ERR %u\n", (unsigned)__LINE__, (unsigned)vcc_file_error()); return 1; } } while (0)
|
|
|
|
int formattedChecks(void);
|
|
int formattedProtectedChecks(void);
|
|
|
|
static int binaryUpdate(void)
|
|
{
|
|
FILE *f, *reader;
|
|
byte pair[2];
|
|
CHECK(!fopen("MISSING.DAT", "r+b") && vcc_file_error() == DECB_ERR_NOT_FOUND);
|
|
CHECK(!fopen("SAFE.DAT", "r++") && vcc_file_error() == DECB_ERR_INVALID_ARGUMENT);
|
|
CHECK(!fopen("SAFE.DAT", "rb+b") && vcc_file_error() == DECB_ERR_INVALID_ARGUMENT);
|
|
reader = fopen("distant.dat", "rb");
|
|
CHECK(reader && !fopen("DISTANT.DAT:0", "rb+") && vcc_file_error() == DECB_ERR_ALREADY_EXISTS);
|
|
CHECK(fclose(reader) == 0);
|
|
|
|
f = fopen("DISTANT.DAT", "rb+");
|
|
CHECK(f && ftell(f) == 0L && fgetc(f) == 0);
|
|
CHECK(!fopen("distant.dat:0", "r+b") && vcc_file_error() == DECB_ERR_ALREADY_EXISTS);
|
|
CHECK(!fopen("distant.dat:0", "w") && vcc_file_error() == DECB_ERR_ALREADY_EXISTS);
|
|
// An all-stream flush cannot authorize a write after reading.
|
|
CHECK(fflush(f) == EOF && vcc_file_error() == DECB_ERR_INVALID_ARGUMENT);
|
|
CHECK(fflush(NULL) == 0);
|
|
clearerr(f); // Clearing flags is not a direction change.
|
|
CHECK(fputc('!', f) == EOF && vcc_file_error() == DECB_ERR_INVALID_ARGUMENT && ftell(f) == 1L);
|
|
CHECK(fseek(f, 255L, SEEK_SET) == 0);
|
|
CHECK(fwrite("SECT", 2, 2, f) == 2);
|
|
// A rejected seek cannot authorize reading after writing.
|
|
clearerr(f);
|
|
CHECK(fseek(f, -1L, SEEK_SET) == EOF);
|
|
CHECK(fread(pair, 1, 2, f) == 0 && vcc_file_error() == DECB_ERR_INVALID_ARGUMENT && ftell(f) == 259L);
|
|
CHECK(fflush(NULL) == 0);
|
|
CHECK(fgetc(f) == 22 && ftell(f) == 260L);
|
|
reader = fopen("DISTANT.DAT", "rb");
|
|
CHECK(reader && fseek(reader, 255L, SEEK_SET) == 0);
|
|
CHECK(fgetc(reader) == 'S' && fgetc(reader) == 'E' && fclose(reader) == 0);
|
|
|
|
// Logical byte 2304 is in the deliberately relocated second granule.
|
|
CHECK(fseek(f, 2302L, SEEK_SET) == 0 && fputs("GRAN", f) >= 0);
|
|
CHECK(fflush(f) == 0 && fgetc(f) == 23);
|
|
CHECK(fseek(f, -1L, SEEK_END) == 0 && ftell(f) == 65536L);
|
|
CHECK(fread(pair, 1, 2, f) == 1 && pair[0] == 0 && feof(f));
|
|
clearerr(f);
|
|
CHECK(!feof(f) && !ferror(f) && ftell(f) == 65537L);
|
|
// Clearing the indicator does not undo the read that encountered EOF.
|
|
CHECK(fwrite("END", 1, 3, f) == 3 && !feof(f));
|
|
CHECK(fseek(f, -3L, SEEK_END) == 0 && fgetc(f) == 'E' && ftell(f) == 65538L);
|
|
CHECK(fclose(f) == 0);
|
|
return 0;
|
|
}
|
|
|
|
static int textUpdate(void)
|
|
{
|
|
FILE *f = fopen("RECORDS.TXT", "r+");
|
|
long bookmark;
|
|
char line[16];
|
|
unsigned i;
|
|
CHECK(f);
|
|
for (i = 0; i < 255; ++i) CHECK(fgetc(f) == 'X');
|
|
bookmark = ftell(f);
|
|
CHECK(fgetc(f) == '\n');
|
|
CHECK(fseek(f, bookmark, SEEK_SET) == 0);
|
|
// Replace a CR at the end of one sector; its paired LF begins the next.
|
|
CHECK(fputc('\n', f) == '\n' && fflush(f) == 0);
|
|
CHECK(fgets(line, sizeof(line), f) && !strcmp(line, "MARS\n"));
|
|
// Overwrite the next planet's name after positioning at the same cursor.
|
|
CHECK(fseek(f, 0L, SEEK_CUR) == 0 && fputs("PHOBOS", f) >= 0);
|
|
CHECK(fflush(f) == 0 && fgetc(f) == '\n');
|
|
rewind(f);
|
|
CHECK(ftell(f) == 0L && fgetc(f) == 'X' && fclose(f) == 0);
|
|
return 0;
|
|
}
|
|
|
|
static int createAppendUpdate(void)
|
|
{
|
|
FILE *f;
|
|
CHECK(!fopen("SAFE.DAT", "wb+b") && !fopen("SAFE.DAT", "a++"));
|
|
f = fopen("TRIPS.DAT", "w+b");
|
|
CHECK(f && fputc(0, f) == 0 && fputc(255, f) == 255);
|
|
rewind(f);
|
|
CHECK(fgetc(f) == 0 && fgetc(f) == 255 && fclose(f) == 0);
|
|
|
|
// Replace an existing multi-granule file and release its old chain.
|
|
f = fopen("RESET.DAT", "wb+");
|
|
CHECK(f && fgetc(f) == EOF && feof(f));
|
|
CHECK(fputs("NEW", f) >= 0 && fseek(f, 1L, SEEK_SET) == 0);
|
|
CHECK(fputc('O', f) == 'O' && fclose(f) == 0);
|
|
f = fopen("EMPTY.TXT", "w+");
|
|
CHECK(f && fgetc(f) == EOF && feof(f) && fclose(f) == 0);
|
|
|
|
f = fopen("TRIPS.DAT", "a+b");
|
|
CHECK(f && ftell(f) == 0L && fputc('A', f) == 'A' && ftell(f) == 3L);
|
|
rewind(f);
|
|
CHECK(fgetc(f) == 0 && fputc('X', f) == EOF && ftell(f) == 1L);
|
|
CHECK(fseek(f, 0L, SEEK_CUR) == 0 && fwrite("BC", 1, 2, f) == 2);
|
|
CHECK(fclose(f) == 0);
|
|
f = fopen("DISTANT.DAT", "ab+");
|
|
CHECK(f && fgetc(f) == 0 && fseek(f, 256L, SEEK_SET) == 0);
|
|
CHECK(fputc('!', f) == '!' && ftell(f) == 65541L && fclose(f) == 0);
|
|
f = fopen("CREATED.TXT", "a+");
|
|
CHECK(f && fgetc(f) == EOF && fputs("LOG\n", f) >= 0);
|
|
rewind(f);
|
|
CHECK(fgetc(f) == 'L' && fclose(f) == 0);
|
|
return 0;
|
|
}
|
|
|
|
static int protectedUpdate(void)
|
|
{
|
|
printf("PROTECT NOW\n");
|
|
while (waitkey(0) != 'X') {}
|
|
// The Disk BASIC ROM reports write protection at the actual sector write.
|
|
FILE *f = fopen("SAFE.DAT", "r+b");
|
|
CHECK(f && fgetc(f) == 'p' && fseek(f, 0L, SEEK_SET) == 0);
|
|
CHECK(fputc('!', f) == '!' && fflush(f) == EOF && ferror(f) == DECB_ERR_IO);
|
|
CHECK(fclose(f) == EOF && vcc_file_error() == DECB_ERR_IO);
|
|
f = fopen("SAFE.DAT", "r+b");
|
|
CHECK(f && fgetc(f) == 'p' && fclose(f) == 0);
|
|
CHECK(!fopen("SAFE.DAT", "w+") && !fopen("MISSING.DAT", "a+"));
|
|
f = fopen("SAFE.DAT", "a+");
|
|
CHECK(f && fgetc(f) == 'p' && fseek(f, 0L, SEEK_SET) == 0);
|
|
CHECK(fputc('!', f) == '!' && fflush(f) == EOF && fclose(f) == EOF);
|
|
return formattedProtectedChecks();
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
cls(1);
|
|
printf("SELECT CHECK\n");
|
|
if (waitkey(0) != 'U' || binaryUpdate() || textUpdate()
|
|
|| createAppendUpdate() || formattedChecks() || protectedUpdate()) return 1;
|
|
printf("UPDATES PASSED\n");
|
|
return 0;
|
|
}
|