From 8360edfff748dfd7d611623c60eb585eb7e42d69 Mon Sep 17 00:00:00 2001 From: a Date: Sat, 30 May 2026 20:09:23 +0000 Subject: [PATCH] tests: add AFP rename smoke --- tests/linux/CMakeLists.txt | 4 + tests/linux/afp_rename_smoke.c | 337 +++++++++++++++++++++++++++++++++ tests/linux/afp_smoke_suite.sh | 74 ++++++++ 3 files changed, 415 insertions(+) create mode 100644 tests/linux/afp_rename_smoke.c diff --git a/tests/linux/CMakeLists.txt b/tests/linux/CMakeLists.txt index 48ea5e0..a3c1ad8 100644 --- a/tests/linux/CMakeLists.txt +++ b/tests/linux/CMakeLists.txt @@ -75,6 +75,10 @@ add_executable(afp_delete_smoke afp_delete_smoke.c) target_include_directories(afp_delete_smoke PRIVATE ${NCPFS_INCLUDE_DIR}) target_link_libraries(afp_delete_smoke ${NCPFS_LIBRARY}) +add_executable(afp_rename_smoke afp_rename_smoke.c) +target_include_directories(afp_rename_smoke PRIVATE ${NCPFS_INCLUDE_DIR}) +target_link_libraries(afp_rename_smoke ${NCPFS_LIBRARY}) + add_executable(afp_temp_dir_handle_smoke afp_temp_dir_handle_smoke.c) target_include_directories(afp_temp_dir_handle_smoke PRIVATE ${NCPFS_INCLUDE_DIR}) target_link_libraries(afp_temp_dir_handle_smoke ${NCPFS_LIBRARY}) diff --git a/tests/linux/afp_rename_smoke.c b/tests/linux/afp_rename_smoke.c new file mode 100644 index 0000000..27ba4b1 --- /dev/null +++ b/tests/linux/afp_rename_smoke.c @@ -0,0 +1,337 @@ +/* + * Linux smoke test for NetWare AFP Rename. + * + * The helper sends the WebSDK/nwafp.h AFP Rename request through ncpfs/libncp + * and verifies that the source path disappears while the destination path gains + * an AFP Entry ID. + */ + +#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_RENAME 0x07 +#define AFP_GET_ENTRY_ID_FROM_PATH 0x0c +#define NWE_INVALID_PATH 0x9c +#ifndef NWE_INVALID_NCP_PACKET_LENGTH +#define NWE_INVALID_NCP_PACKET_LENGTH 0x7e +#endif + +static void usage(const char *prog) +{ + fprintf(stderr, + "Usage: %s [--expect-completion CODE] [--volume N] " + "[--source-entry-id ID] [--dest-entry-id ID] [ncpfs options] SOURCE DEST\n" + "\n" + "ncpfs options are parsed by ncp_initialize(), for example:\n" + " -S SERVER -U USER -P PASSWORD -n\n" + "\n" + "Example:\n" + " %s -S MARS -U SUPERVISOR -P secret SYS:PUBLIC/OLD SYS:PUBLIC/NEW\n", + 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_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 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) | + p[3]; +} + +static int split_parent_leaf(const char *path, char *parent, size_t parent_size, + char *leaf, size_t leaf_size) +{ + const char *slash = strrchr(path, '/'); + const char *backslash = strrchr(path, '\\'); + const char *sep = slash; + size_t parent_len; + size_t leaf_len; + + if (backslash && (!sep || backslash > sep)) + sep = backslash; + + if (!sep) { + const char *colon = strchr(path, ':'); + if (!colon || colon[1] == '\0') + return -1; + parent_len = (size_t)(colon - path + 1); + leaf_len = strlen(colon + 1); + if (parent_len >= parent_size || leaf_len >= leaf_size || !leaf_len) + return -1; + memcpy(parent, path, parent_len); + parent[parent_len] = '\0'; + memcpy(leaf, colon + 1, leaf_len + 1); + return 0; + } + + parent_len = (size_t)(sep - path); + leaf_len = strlen(sep + 1); + if (!parent_len || !leaf_len || parent_len >= parent_size || + leaf_len >= leaf_size) + return -1; + + memcpy(parent, path, parent_len); + parent[parent_len] = '\0'; + memcpy(leaf, sep + 1, leaf_len + 1); + return 0; +} + +static NWCCODE afp_get_entry_id_from_path(NWCONN_HANDLE conn, + const char *path, + uint32_t *entry_id) +{ + size_t path_len = strlen(path); + uint8_t request[1 + 1 + 255]; + uint8_t reply_buf[4]; + NW_FRAGMENT reply; + NWCCODE err; + + if (path_len > 255) + return NWE_INVALID_PATH; + + request[0] = 0; + 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, AFP_GET_ENTRY_ID_FROM_PATH), + request, + 2 + path_len, + &reply); + if (err) + return err; + if (reply.fragSize < 4) + return NWE_INVALID_NCP_PACKET_LENGTH; + + *entry_id = be32_to_cpu(reply_buf); + return 0; +} + +int main(int argc, char **argv) +{ + NWCONN_HANDLE conn; + NW_FRAGMENT reply; + long init_err = 0; + const char *source_path = NULL; + const char *dest_path = NULL; + uint32_t volume_number = 0; + uint32_t source_entry_id = 0; + uint32_t dest_entry_id = 0; + uint32_t expect_completion = 0xffffffffU; + int i; + char source_parent[256]; + char source_leaf[256]; + char dest_parent[256]; + char dest_leaf[256]; + size_t source_leaf_len; + size_t dest_leaf_len; + uint8_t request[1 + 4 + 4 + 1 + 255 + 1 + 255]; + size_t req_len; + uint8_t reply_buf[1]; + uint32_t lookup_entry_id = 0; + 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], "--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], "--source-entry-id")) { + if (++i >= argc || parse_u32(argv[i], &source_entry_id)) { + fprintf(stderr, "invalid --source-entry-id value\n"); + ncp_close(conn); + return 2; + } + } else if (!strcmp(argv[i], "--dest-entry-id")) { + if (++i >= argc || parse_u32(argv[i], &dest_entry_id)) { + fprintf(stderr, "invalid --dest-entry-id value\n"); + ncp_close(conn); + return 2; + } + } else if (!strcmp(argv[i], "--expect-completion")) { + if (++i >= argc || parse_u32(argv[i], &expect_completion) || + expect_completion > 255) { + fprintf(stderr, "invalid --expect-completion 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 (!source_path) { + source_path = argv[i]; + } else if (!dest_path) { + dest_path = argv[i]; + } else { + fprintf(stderr, "unexpected argument: %s\n", argv[i]); + usage(argv[0]); + ncp_close(conn); + return 2; + } + } + + if (!source_path || !dest_path) { + usage(argv[0]); + ncp_close(conn); + return 2; + } + + if (split_parent_leaf(source_path, source_parent, sizeof(source_parent), + source_leaf, sizeof(source_leaf)) || + split_parent_leaf(dest_path, dest_parent, sizeof(dest_parent), + dest_leaf, sizeof(dest_leaf))) { + fprintf(stderr, "both SOURCE and DEST must include a parent path\n"); + ncp_close(conn); + return 2; + } + + if (!source_entry_id) { + err = afp_get_entry_id_from_path(conn, source_parent, &source_entry_id); + if (err) { + fprintf(stderr, "AFP source parent lookup failed: parent=%s error=0x%02x\n", + source_parent, (unsigned)err & 0xff); + ncp_close(conn); + return 1; + } + } + if (!dest_entry_id) { + err = afp_get_entry_id_from_path(conn, dest_parent, &dest_entry_id); + if (err) { + fprintf(stderr, "AFP dest parent lookup failed: parent=%s error=0x%02x\n", + dest_parent, (unsigned)err & 0xff); + ncp_close(conn); + return 1; + } + } + + source_leaf_len = strlen(source_leaf); + dest_leaf_len = strlen(dest_leaf); + if (source_leaf_len > 255 || dest_leaf_len > 255) { + fprintf(stderr, "source and destination leaf names must be <=255 bytes\n"); + ncp_close(conn); + return 2; + } + + request[0] = (uint8_t)volume_number; + cpu_to_be32(source_entry_id, request + 1); + cpu_to_be32(dest_entry_id, request + 5); + request[9] = (uint8_t)source_leaf_len; + memcpy(request + 10, source_leaf, source_leaf_len); + request[10 + source_leaf_len] = (uint8_t)dest_leaf_len; + memcpy(request + 11 + source_leaf_len, dest_leaf, dest_leaf_len); + req_len = 11 + source_leaf_len + dest_leaf_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_RENAME), + request, + req_len, + &reply); + + if (expect_completion != 0xffffffffU) { + if (((unsigned)err & 0xff) != expect_completion) { + fprintf(stderr, + "AFP Rename expected completion 0x%02x but got 0x%02x: source=%s dest=%s\n", + (unsigned)expect_completion & 0xff, + (unsigned)err & 0xff, source_path, dest_path); + ncp_close(conn); + return 1; + } + printf("AFP Rename returned expected completion 0x%02x: source=%s dest=%s\n", + (unsigned)expect_completion & 0xff, source_path, dest_path); + ncp_close(conn); + return 0; + } + + if (err) { + fprintf(stderr, + "AFP Rename failed: source=%s dest=%s source_entry_id=0x%08x dest_entry_id=0x%08x error=0x%02x\n", + source_path, dest_path, source_entry_id, dest_entry_id, + (unsigned)err & 0xff); + ncp_close(conn); + return 1; + } + + err = afp_get_entry_id_from_path(conn, source_path, &lookup_entry_id); + if (!err) { + fprintf(stderr, + "AFP Rename verify failed: source still exists source=%s entry_id=0x%08x\n", + source_path, lookup_entry_id); + ncp_close(conn); + return 1; + } + + err = afp_get_entry_id_from_path(conn, dest_path, &lookup_entry_id); + if (err) { + fprintf(stderr, + "AFP Rename verify failed: destination missing dest=%s error=0x%02x\n", + dest_path, (unsigned)err & 0xff); + ncp_close(conn); + return 1; + } + + printf("AFP Rename source=%s dest=%s source_parent=%s dest_parent=%s source_entry_id=0x%08x dest_entry_id=0x%08x renamed_entry_id=0x%08x verified\n", + source_path, dest_path, source_parent, dest_parent, + source_entry_id, dest_entry_id, lookup_entry_id); + + ncp_close(conn); + return 0; +} diff --git a/tests/linux/afp_smoke_suite.sh b/tests/linux/afp_smoke_suite.sh index f6c154d..ee70007 100755 --- a/tests/linux/afp_smoke_suite.sh +++ b/tests/linux/afp_smoke_suite.sh @@ -28,6 +28,8 @@ READONLY_RIGHTS="[RF]" KEEP_GOING=1 CAPTURE_LOG=1 LOG_WINDOW_SECONDS=120 +RENAME_DIR_NAME="" +RENAME_FILE_NAME="" usage() { cat <&2; exit 2 ;; @@ -152,6 +166,14 @@ case "$CREATE_FILE_NAME" in ???????|??????|?????|????|???|??|?) ;; *) echo "--create-file-name must be at most seven characters so the AFP 2.0 companion name stays DOS-compatible" >&2; exit 2 ;; esac +case "$RENAME_DIR_NAME" in + ???????|??????|?????|????|???|??|?) ;; + *) echo "--rename-dir-name must be at most seven characters" >&2; exit 2 ;; +esac +case "$RENAME_FILE_NAME" in + ???????|??????|?????|????|???|??|?) ;; + *) echo "--rename-file-name must be at most seven characters" >&2; exit 2 ;; +esac DIR_PATH=$NETWARE_PATH case "$NETWARE_PATH" in @@ -175,6 +197,11 @@ UNIX_FILE20_PATH=$UNIX_PARENT_PATH/${CREATE_FILE_NAME}2 UNIX_FILE_PATH_DOS=$UNIX_PARENT_PATH/$CREATE_FILE_NAME_DOS UNIX_FILE20_PATH_DOS=$UNIX_PARENT_PATH/$CREATE_FILE20_NAME_DOS +RENAME_DIR_FROM_PATH="$DIR_PATH/$RENAME_DIR_NAME" +RENAME_DIR_TO_PATH="$DIR_PATH/${RENAME_DIR_NAME}r" +RENAME_FILE_FROM_PATH="$DIR_PATH/$RENAME_FILE_NAME" +RENAME_FILE_TO_PATH="$DIR_PATH/${RENAME_FILE_NAME}r" + REPORT_TMP=$(mktemp "${TMPDIR:-/tmp}/mars-afp-smoke.XXXXXX") LOG_TMP=$(mktemp "${TMPDIR:-/tmp}/mars-afp-log.XXXXXX") LOG_RECENT_TMP=$(mktemp "${TMPDIR:-/tmp}/mars-afp-log-recent.XXXXXX") @@ -337,6 +364,10 @@ emit "unix_dir_path_dos=$UNIX_DIR_PATH_DOS" emit "unix_dir20_path_dos=$UNIX_DIR20_PATH_DOS" emit "create_file_path=$CREATE_FILE_PATH" emit "create_file20_path=$CREATE_FILE20_PATH" +emit "rename_dir_from_path=$RENAME_DIR_FROM_PATH" +emit "rename_dir_to_path=$RENAME_DIR_TO_PATH" +emit "rename_file_from_path=$RENAME_FILE_FROM_PATH" +emit "rename_file_to_path=$RENAME_FILE_TO_PATH" emit "unix_file_path=$UNIX_FILE_PATH" emit "unix_file20_path=$UNIX_FILE20_PATH" emit "unix_file_path_dos=$UNIX_FILE_PATH_DOS" @@ -498,6 +529,49 @@ run_cmd \ "./afp_delete_smoke $COMMON_PRINT '$CREATE_FILE20_PATH'" \ "$SCRIPT_DIR/afp_delete_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$CREATE_FILE20_PATH" + +run_optional_cmd \ + "Prepare AFP Rename Directory cleanup source" \ + "./afp_delete_smoke $COMMON_PRINT '$RENAME_DIR_FROM_PATH'" \ + "$SCRIPT_DIR/afp_delete_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$RENAME_DIR_FROM_PATH" || true +run_optional_cmd \ + "Prepare AFP Rename Directory cleanup destination" \ + "./afp_delete_smoke $COMMON_PRINT '$RENAME_DIR_TO_PATH'" \ + "$SCRIPT_DIR/afp_delete_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$RENAME_DIR_TO_PATH" || true +run_cmd \ + "AFP Rename Directory create source" \ + "./afp_create_directory_smoke $COMMON_PRINT '$RENAME_DIR_FROM_PATH'" \ + "$SCRIPT_DIR/afp_create_directory_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$RENAME_DIR_FROM_PATH" +run_cmd \ + "AFP Rename Directory" \ + "./afp_rename_smoke $COMMON_PRINT '$RENAME_DIR_FROM_PATH' '$RENAME_DIR_TO_PATH'" \ + "$SCRIPT_DIR/afp_rename_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$RENAME_DIR_FROM_PATH" "$RENAME_DIR_TO_PATH" +run_cmd \ + "AFP Rename Directory cleanup" \ + "./afp_delete_smoke $COMMON_PRINT '$RENAME_DIR_TO_PATH'" \ + "$SCRIPT_DIR/afp_delete_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$RENAME_DIR_TO_PATH" + +run_optional_cmd \ + "Prepare AFP Rename File cleanup source" \ + "./afp_delete_smoke $COMMON_PRINT '$RENAME_FILE_FROM_PATH'" \ + "$SCRIPT_DIR/afp_delete_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$RENAME_FILE_FROM_PATH" || true +run_optional_cmd \ + "Prepare AFP Rename File cleanup destination" \ + "./afp_delete_smoke $COMMON_PRINT '$RENAME_FILE_TO_PATH'" \ + "$SCRIPT_DIR/afp_delete_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$RENAME_FILE_TO_PATH" || true +run_cmd \ + "AFP Rename File create source" \ + "./afp_create_file_smoke $COMMON_PRINT '$RENAME_FILE_FROM_PATH'" \ + "$SCRIPT_DIR/afp_create_file_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$RENAME_FILE_FROM_PATH" +run_cmd \ + "AFP Rename File" \ + "./afp_rename_smoke $COMMON_PRINT '$RENAME_FILE_FROM_PATH' '$RENAME_FILE_TO_PATH'" \ + "$SCRIPT_DIR/afp_rename_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$RENAME_FILE_FROM_PATH" "$RENAME_FILE_TO_PATH" +run_cmd \ + "AFP Rename File cleanup" \ + "./afp_delete_smoke $COMMON_PRINT '$RENAME_FILE_TO_PATH'" \ + "$SCRIPT_DIR/afp_delete_smoke" -S "$SERVER" -U "$USER_NAME" -P "$PASSWORD" "$RENAME_FILE_TO_PATH" + run_cmd \ "AFP Open File Fork" \ "./afp_open_file_fork_smoke $COMMON_PRINT '$NETWARE_PATH'" \