From 10696f384bac54648d9399c69f11e468aadefa38 Mon Sep 17 00:00:00 2001 From: OAI Date: Sun, 31 May 2026 05:09:38 +0000 Subject: [PATCH] tests: add AFP file fork I/O smoke coverage --- tests/afp/CMakeLists.txt | 4 + tests/afp/afp_file_fork_io_smoke.c | 443 +++++++++++++++++++++++++++++ tests/afp/afp_smoke_suite.sh | 33 +++ 3 files changed, 480 insertions(+) create mode 100644 tests/afp/afp_file_fork_io_smoke.c diff --git a/tests/afp/CMakeLists.txt b/tests/afp/CMakeLists.txt index 00be83d..7af5cf4 100644 --- a/tests/afp/CMakeLists.txt +++ b/tests/afp/CMakeLists.txt @@ -62,6 +62,10 @@ add_executable(afp_open_file_fork_smoke afp_open_file_fork_smoke.c) target_include_directories(afp_open_file_fork_smoke PRIVATE ${NCPFS_INCLUDE_DIR}) target_link_libraries(afp_open_file_fork_smoke ${NCPFS_LIBRARY}) +add_executable(afp_file_fork_io_smoke afp_file_fork_io_smoke.c) +target_include_directories(afp_file_fork_io_smoke PRIVATE ${NCPFS_INCLUDE_DIR}) +target_link_libraries(afp_file_fork_io_smoke ${NCPFS_LIBRARY}) + add_executable(afp_create_directory_smoke afp_create_directory_smoke.c) target_include_directories(afp_create_directory_smoke PRIVATE ${NCPFS_INCLUDE_DIR}) diff --git a/tests/afp/afp_file_fork_io_smoke.c b/tests/afp/afp_file_fork_io_smoke.c new file mode 100644 index 0000000..475a48c --- /dev/null +++ b/tests/afp/afp_file_fork_io_smoke.c @@ -0,0 +1,443 @@ +/* + * Smoke test for NetWare AFP data-fork I/O. + * + * AFP Open File Fork returns a normal mars_nwe/NetWare file handle. The + * actual fork I/O is intentionally exercised through the existing NetWare + * Read File (0x48) and Write File (0x49) NCPs so the server continues to share + * normal mars_nwe handle, share, trustee and file-size semantics. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include + +#ifndef NCPC_SUBFUNCTION +#define NCPC_SUBFUNCTION 0x10000 +#endif +#ifndef NCPC_SFN +#define NCPC_SFN(FN, SFN) ((FN) | ((SFN) << 8) | NCPC_SUBFUNCTION) +#endif + +#define AFP_OPEN_FILE_FORK 0x08 +#define AFP_DATA_FORK 0x00 +#define AFP_ACCESS_READ 0x01 +#define AFP_ACCESS_WRITE 0x02 +#define NWE_INVALID_PATH 0x9c + +static void usage(const char *prog) +{ + fprintf(stderr, + "Usage: %s [--entry-id-only] [--write TEXT] [--read-size N] " + "[--offset N] [--volume N] [ncpfs options] PATH\n" + "\n" + "Examples:\n" + " %s -S MARS -U SUPERVISOR -P secret SYS:PUBLIC/pmdflts.ini\n" + " %s --entry-id-only -S MARS -U SUPERVISOR -P secret SYS:PUBLIC/pmdflts.ini\n" + " %s --write MARS-AFP -S MARS -U SUPERVISOR -P secret SYS:PUBLIC/test.dat\n", + prog, prog, prog, prog); +} + +static int parse_u32(const char *text, uint32_t *value) +{ + char *end = NULL; + unsigned long v; + + errno = 0; + v = strtoul(text, &end, 0); + if (errno || !end || *end || v > 0xffffffffUL) + return -1; + *value = (uint32_t)v; + return 0; +} + +static void cpu_to_be16(uint16_t v, uint8_t p[2]) +{ + p[0] = (uint8_t)(v >> 8); + p[1] = (uint8_t)v; +} + +static void cpu_to_be32(uint32_t v, uint8_t p[4]) +{ + p[0] = (uint8_t)(v >> 24); + p[1] = (uint8_t)(v >> 16); + p[2] = (uint8_t)(v >> 8); + p[3] = (uint8_t)v; +} + +static void cpu_to_le32(uint32_t v, uint8_t p[4]) +{ + p[0] = (uint8_t)v; + p[1] = (uint8_t)(v >> 8); + p[2] = (uint8_t)(v >> 16); + p[3] = (uint8_t)(v >> 24); +} + +static uint16_t be16_to_cpu(const uint8_t p[2]) +{ + return ((uint16_t)p[0] << 8) | (uint16_t)p[1]; +} + +static uint32_t be32_to_cpu(const uint8_t p[4]) +{ + return ((uint32_t)p[0] << 24) | + ((uint32_t)p[1] << 16) | + ((uint32_t)p[2] << 8) | + (uint32_t)p[3]; +} + +static uint32_t le32_to_cpu(const uint8_t p[4]) +{ + return ((uint32_t)p[0]) | + ((uint32_t)p[1] << 8) | + ((uint32_t)p[2] << 16) | + ((uint32_t)p[3] << 24); +} + +static NWCCODE afp_get_entry_id_from_path(NWCONN_HANDLE conn, const char *path, + uint32_t *entry_id) +{ + NW_FRAGMENT reply; + uint8_t request[1 + 1 + 255]; + uint8_t reply_buf[4]; + size_t path_len = strlen(path); + NWCCODE err; + + if (path_len > 255) + return NWE_INVALID_PATH; + + request[0] = 0; /* directory handle 0, raw VOL:path */ + request[1] = (uint8_t)path_len; + memcpy(request + 2, path, path_len); + + memset(reply_buf, 0, sizeof(reply_buf)); + reply.fragAddr.rw = reply_buf; + reply.fragSize = sizeof(reply_buf); + + err = NWRequestSimple(conn, NCPC_SFN(0x23, 0x0c), + request, 2 + path_len, &reply); + if (err) + return err; + if (reply.fragSize < 4) + return NWE_INVALID_PATH; + + *entry_id = be32_to_cpu(reply_buf); + return 0; +} + +static void close_file_handle(NWCONN_HANDLE conn, uint32_t fhandle) +{ + uint8_t rq[7]; + + memset(rq, 0, sizeof(rq)); + cpu_to_le32(fhandle, rq + 3); + (void)NWRequestSimple(conn, 0x42, rq, sizeof(rq), NULL); +} + +static NWCCODE afp_open_file_fork(NWCONN_HANDLE conn, const char *path, + uint32_t volume_number, int entry_id_only, + uint8_t access_mode, uint32_t *entry_id, + uint32_t *fhandle, uint32_t *fork_len) +{ + NW_FRAGMENT reply; + uint8_t request[1 + 4 + 1 + 1 + 1 + 255]; + uint8_t reply_buf[10]; + size_t path_len; + NWCCODE err; + + if (entry_id_only && !*entry_id) { + err = afp_get_entry_id_from_path(conn, path, entry_id); + if (err) + return err; + if (!*entry_id) + return NWE_INVALID_PATH; + } + + path_len = entry_id_only ? 0 : strlen(path); + if (path_len > 255) + return NWE_INVALID_PATH; + + request[0] = (uint8_t)volume_number; + cpu_to_be32(*entry_id, request + 1); + request[5] = AFP_DATA_FORK; + request[6] = access_mode; + request[7] = (uint8_t)path_len; + if (path_len) + memcpy(request + 8, path, path_len); + + memset(reply_buf, 0, sizeof(reply_buf)); + reply.fragAddr.rw = reply_buf; + reply.fragSize = sizeof(reply_buf); + + err = NWRequestSimple(conn, NCPC_SFN(0x23, AFP_OPEN_FILE_FORK), + request, 8 + path_len, &reply); + if (err) + return err; + if (reply.fragSize < 10) + return NWE_INVALID_PATH; + + *fhandle = le32_to_cpu(reply_buf + 2); + *fork_len = be32_to_cpu(reply_buf + 6); + return 0; +} + +static NWCCODE ncp_read_file(NWCONN_HANDLE conn, uint32_t fhandle, + uint32_t offset, uint16_t max_size, + uint8_t *data, uint16_t *read_size) +{ + NW_FRAGMENT reply; + uint8_t request[13]; + uint8_t reply_buf[2 + 4096 + 1]; + NWCCODE err; + + if (max_size > 4096) + return NWE_INVALID_PATH; + + memset(request, 0, sizeof(request)); + cpu_to_le32(fhandle, request + 3); + cpu_to_be32(offset, request + 7); + cpu_to_be16(max_size, request + 11); + + memset(reply_buf, 0, sizeof(reply_buf)); + reply.fragAddr.rw = reply_buf; + reply.fragSize = sizeof(reply_buf); + + err = NWRequestSimple(conn, 0x48, request, sizeof(request), &reply); + if (err) + return err; + if (reply.fragSize < 2) + return NWE_INVALID_PATH; + + *read_size = be16_to_cpu(reply_buf); + if (*read_size > max_size || (size_t)*read_size + 2 > reply.fragSize) + return NWE_INVALID_PATH; + memcpy(data, reply_buf + 2, *read_size); + return 0; +} + +static NWCCODE ncp_write_file(NWCONN_HANDLE conn, uint32_t fhandle, + uint32_t offset, const uint8_t *data, + uint16_t size) +{ + uint8_t request[13 + 4096]; + + if (size > 4096) + return NWE_INVALID_PATH; + + memset(request, 0, sizeof(request)); + cpu_to_le32(fhandle, request + 3); + cpu_to_be32(offset, request + 7); + cpu_to_be16(size, request + 11); + memcpy(request + 13, data, size); + + return NWRequestSimple(conn, 0x49, request, 13 + size, NULL); +} + +int main(int argc, char **argv) +{ + NWCONN_HANDLE conn; + long init_err = 0; + const char *path = NULL; + const char *write_text = NULL; + uint32_t volume_number = 0; + uint32_t entry_id = 0; + uint32_t fhandle = 0; + uint32_t fork_len = 0; + uint32_t offset = 0; + uint32_t read_size_arg = 16; + uint8_t read_buf[4096]; + uint16_t read_size = 0; + int entry_id_only = 0; + int i; + NWCCODE err; + + if (NWCallsInit(NULL, NULL)) { + fprintf(stderr, "NWCallsInit failed\n"); + return 2; + } + + conn = ncp_initialize(&argc, argv, 1, &init_err); + if (!conn) { + fprintf(stderr, "ncp_initialize/login failed: %ld\n", init_err); + usage(argv[0]); + return 2; + } + + for (i = 1; i < argc; i++) { + if (!strcmp(argv[i], "--entry-id-only")) { + entry_id_only = 1; + } else if (!strcmp(argv[i], "--entry-id")) { + if (++i >= argc || parse_u32(argv[i], &entry_id)) { + fprintf(stderr, "invalid --entry-id value\n"); + ncp_close(conn); + return 2; + } + entry_id_only = 1; + } else if (!strcmp(argv[i], "--volume")) { + if (++i >= argc || parse_u32(argv[i], &volume_number) || + volume_number > 255) { + fprintf(stderr, "invalid --volume value\n"); + ncp_close(conn); + return 2; + } + } else if (!strcmp(argv[i], "--write")) { + if (++i >= argc) { + fprintf(stderr, "missing --write text\n"); + ncp_close(conn); + return 2; + } + write_text = argv[i]; + } else if (!strcmp(argv[i], "--read-size")) { + if (++i >= argc || parse_u32(argv[i], &read_size_arg) || + read_size_arg > sizeof(read_buf)) { + fprintf(stderr, "invalid --read-size value\n"); + ncp_close(conn); + return 2; + } + } else if (!strcmp(argv[i], "--offset")) { + if (++i >= argc || parse_u32(argv[i], &offset)) { + fprintf(stderr, "invalid --offset value\n"); + ncp_close(conn); + return 2; + } + } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + usage(argv[0]); + ncp_close(conn); + return 0; + } else if (!path) { + path = argv[i]; + } else { + fprintf(stderr, "unexpected argument: %s\n", argv[i]); + usage(argv[0]); + ncp_close(conn); + return 2; + } + } + + if (!path && !entry_id) { + fprintf(stderr, "missing PATH or --entry-id\n"); + usage(argv[0]); + ncp_close(conn); + return 2; + } + + if (write_text) { + size_t write_len = strlen(write_text); + if (!write_len || write_len > sizeof(read_buf)) { + fprintf(stderr, "invalid --write length: %zu\n", write_len); + ncp_close(conn); + return 2; + } + + err = afp_open_file_fork(conn, path, volume_number, entry_id_only, + AFP_ACCESS_WRITE, &entry_id, &fhandle, + &fork_len); + if (err) { + fprintf(stderr, + "AFP Open File Fork for write failed: completion=0x%02x (%u) path=%s entry_id=0x%08x\n", + (unsigned int)err & 0xff, (unsigned int)err, + path ? path : "(entry-id-only)", (unsigned int)entry_id); + ncp_close(conn); + return 1; + } + + err = ncp_write_file(conn, fhandle, offset, + (const uint8_t *)write_text, (uint16_t)write_len); + close_file_handle(conn, fhandle); + fhandle = 0; + if (err) { + fprintf(stderr, + "NCP Write File failed after AFP open: completion=0x%02x (%u) path=%s entry_id=0x%08x\n", + (unsigned int)err & 0xff, (unsigned int)err, + path ? path : "(entry-id-only)", (unsigned int)entry_id); + ncp_close(conn); + return 1; + } + + err = afp_open_file_fork(conn, path, volume_number, entry_id_only, + AFP_ACCESS_READ, &entry_id, &fhandle, + &fork_len); + if (err) { + fprintf(stderr, + "AFP Open File Fork for verify read failed: completion=0x%02x (%u) path=%s entry_id=0x%08x\n", + (unsigned int)err & 0xff, (unsigned int)err, + path ? path : "(entry-id-only)", (unsigned int)entry_id); + ncp_close(conn); + return 1; + } + + err = ncp_read_file(conn, fhandle, offset, (uint16_t)write_len, + read_buf, &read_size); + close_file_handle(conn, fhandle); + fhandle = 0; + if (err) { + fprintf(stderr, + "NCP Read File verify failed after AFP write: completion=0x%02x (%u) path=%s entry_id=0x%08x\n", + (unsigned int)err & 0xff, (unsigned int)err, + path ? path : "(entry-id-only)", (unsigned int)entry_id); + ncp_close(conn); + return 1; + } + if (read_size != write_len || memcmp(read_buf, write_text, write_len)) { + fprintf(stderr, + "AFP fork write verify mismatch: path=%s entry_id=0x%08x wrote=%zu read=%u\n", + path ? path : "(entry-id-only)", (unsigned int)entry_id, + write_len, (unsigned int)read_size); + ncp_close(conn); + return 1; + } + + printf("AFP File Fork I/O write path=%s entry_id=0x%08x offset=%u size=%zu verified%s\n", + path ? path : "(entry-id-only)", (unsigned int)entry_id, + (unsigned int)offset, write_len, + entry_id_only ? " entry-id-only" : ""); + ncp_close(conn); + return 0; + } + + err = afp_open_file_fork(conn, path, volume_number, entry_id_only, + AFP_ACCESS_READ, &entry_id, &fhandle, &fork_len); + if (err) { + fprintf(stderr, + "AFP Open File Fork for read failed: completion=0x%02x (%u) path=%s entry_id=0x%08x\n", + (unsigned int)err & 0xff, (unsigned int)err, + path ? path : "(entry-id-only)", (unsigned int)entry_id); + ncp_close(conn); + return 1; + } + + err = ncp_read_file(conn, fhandle, offset, (uint16_t)read_size_arg, + read_buf, &read_size); + close_file_handle(conn, fhandle); + fhandle = 0; + if (err) { + fprintf(stderr, + "NCP Read File failed after AFP open: completion=0x%02x (%u) path=%s entry_id=0x%08x\n", + (unsigned int)err & 0xff, (unsigned int)err, + path ? path : "(entry-id-only)", (unsigned int)entry_id); + ncp_close(conn); + return 1; + } + if (!read_size) { + fprintf(stderr, + "AFP fork read returned zero bytes: path=%s entry_id=0x%08x fork_len=%u\n", + path ? path : "(entry-id-only)", (unsigned int)entry_id, + (unsigned int)fork_len); + ncp_close(conn); + return 1; + } + + printf("AFP File Fork I/O read path=%s entry_id=0x%08x offset=%u requested=%u read=%u fork_len=%u%s\n", + path ? path : "(entry-id-only)", (unsigned int)entry_id, + (unsigned int)offset, (unsigned int)read_size_arg, + (unsigned int)read_size, (unsigned int)fork_len, + entry_id_only ? " entry-id-only" : ""); + + ncp_close(conn); + return 0; +} diff --git a/tests/afp/afp_smoke_suite.sh b/tests/afp/afp_smoke_suite.sh index eea3acd..28ae907 100755 --- a/tests/afp/afp_smoke_suite.sh +++ b/tests/afp/afp_smoke_suite.sh @@ -611,6 +611,39 @@ run_cmd \ "$SCRIPT_DIR/afp_open_file_fork_smoke" --expect-completion 0x9c --fork 1 \ -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$NETWARE_PATH" +run_cmd \ + "AFP File Fork Read via NetWare handle" \ + "./afp_file_fork_io_smoke $COMMON_PRINT '$NETWARE_PATH'" \ + "$SCRIPT_DIR/afp_file_fork_io_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$NETWARE_PATH" +run_cmd \ + "AFP File Fork Read by Entry ID via NetWare handle" \ + "./afp_file_fork_io_smoke --entry-id-only $COMMON_PRINT '$NETWARE_PATH'" \ + "$SCRIPT_DIR/afp_file_fork_io_smoke" --entry-id-only \ + -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$NETWARE_PATH" + +run_optional_cmd \ + "Prepare AFP File Fork I/O write cleanup" \ + "./afp_delete_smoke $COMMON_PRINT '$CREATE_FILE_PATH'" \ + "$SCRIPT_DIR/afp_delete_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$CREATE_FILE_PATH" || true +run_cmd \ + "AFP File Fork I/O write create file" \ + "./afp_create_file_smoke $COMMON_PRINT '$CREATE_FILE_PATH'" \ + "$SCRIPT_DIR/afp_create_file_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$CREATE_FILE_PATH" +run_cmd \ + "AFP File Fork Write via NetWare handle" \ + "./afp_file_fork_io_smoke --write MARS-AFP $COMMON_PRINT '$CREATE_FILE_PATH'" \ + "$SCRIPT_DIR/afp_file_fork_io_smoke" --write MARS-AFP \ + -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$CREATE_FILE_PATH" +run_cmd \ + "AFP File Fork Write by Entry ID via NetWare handle" \ + "./afp_file_fork_io_smoke --entry-id-only --write MARS-AFP-ID $COMMON_PRINT '$CREATE_FILE_PATH'" \ + "$SCRIPT_DIR/afp_file_fork_io_smoke" --entry-id-only --write MARS-AFP-ID \ + -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$CREATE_FILE_PATH" +run_cmd \ + "AFP File Fork I/O write cleanup" \ + "./afp_delete_smoke $COMMON_PRINT '$CREATE_FILE_PATH'" \ + "$SCRIPT_DIR/afp_delete_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$CREATE_FILE_PATH" + run_cmd \ "AFP Get File Information by Entry ID" \ "./afp_file_info_smoke --entry-id-only $COMMON_PRINT '$NETWARE_PATH'" \