|
| 1 | +#define _GNU_SOURCE |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | +#include <dlfcn.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <sys/socket.h> |
| 8 | +#include <linux/netlink.h> |
| 9 | +#include <errno.h> |
| 10 | +#include <assert.h> |
| 11 | +#include <stdint.h> |
| 12 | + |
| 13 | +/* Mock libnl structures and functions */ |
| 14 | +#include <netlink/netlink.h> |
| 15 | +#include <netlink/genl/genl.h> |
| 16 | +#include <netlink/genl/ctrl.h> |
| 17 | +#include <netlink/attr.h> |
| 18 | +#include "../../nbd-netlink.h" |
| 19 | + |
| 20 | +/* Real function pointers */ |
| 21 | +static int (*real_genl_connect)(struct nl_sock *sock) = NULL; |
| 22 | +static int (*real_genl_ctrl_resolve)(struct nl_sock *sock, const char *name) = NULL; |
| 23 | +static struct nl_sock *(*real_nl_socket_alloc)(void) = NULL; |
| 24 | +static void (*real_nl_socket_free)(struct nl_sock *sock) = NULL; |
| 25 | +static int (*real_nl_socket_modify_cb)(struct nl_sock *sock, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg) = NULL; |
| 26 | +static struct nl_msg *(*real_nlmsg_alloc)(void) = NULL; |
| 27 | +static void (*real_nlmsg_free)(struct nl_msg *msg) = NULL; |
| 28 | +static void *(*real_genlmsg_put)(struct nl_msg *msg, uint32_t port, uint32_t seq, int family, int hdrlen, int flags, uint8_t cmd, uint8_t version) = NULL; |
| 29 | +static struct nlmsghdr *(*real_nlmsg_hdr)(struct nl_msg *msg) = NULL; |
| 30 | +static int (*real_nl_send_auto)(struct nl_sock *sock, struct nl_msg *msg) = NULL; |
| 31 | +static int (*real_nl_wait_for_ack)(struct nl_sock *sock) = NULL; |
| 32 | + |
| 33 | +/* Mock state */ |
| 34 | +static int mock_family_id = 42; |
| 35 | +static int mock_connected = 0; |
| 36 | +static struct nl_msg *last_sent_msg = NULL; |
| 37 | + |
| 38 | +/* Initialize real function pointers */ |
| 39 | +static void init_real_functions(void) { |
| 40 | + if (!real_nl_socket_alloc) { |
| 41 | + real_nl_socket_alloc = dlsym(RTLD_NEXT, "nl_socket_alloc"); |
| 42 | + real_nl_socket_free = dlsym(RTLD_NEXT, "nl_socket_free"); |
| 43 | + real_genl_connect = dlsym(RTLD_NEXT, "genl_connect"); |
| 44 | + real_genl_ctrl_resolve = dlsym(RTLD_NEXT, "genl_ctrl_resolve"); |
| 45 | + real_nl_socket_modify_cb = dlsym(RTLD_NEXT, "nl_socket_modify_cb"); |
| 46 | + real_nlmsg_alloc = dlsym(RTLD_NEXT, "nlmsg_alloc"); |
| 47 | + real_nlmsg_free = dlsym(RTLD_NEXT, "nlmsg_free"); |
| 48 | + real_genlmsg_put = dlsym(RTLD_NEXT, "genlmsg_put"); |
| 49 | + real_nlmsg_hdr = dlsym(RTLD_NEXT, "nlmsg_hdr"); |
| 50 | + real_nl_send_auto = dlsym(RTLD_NEXT, "nl_send_auto"); |
| 51 | + real_nl_wait_for_ack = dlsym(RTLD_NEXT, "nl_wait_for_ack"); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/* Message validation functions */ |
| 56 | +static int validate_connect_message(struct nl_msg *msg) { |
| 57 | + struct nlmsghdr *nlh; |
| 58 | + struct genlmsghdr *gnlh; |
| 59 | + struct nlattr *attrs[NBD_ATTR_MAX + 1]; |
| 60 | + int ret; |
| 61 | + |
| 62 | + nlh = real_nlmsg_hdr(msg); |
| 63 | + if (!nlh) { |
| 64 | + fprintf(stderr, "MOCK: Failed to get netlink header\n"); |
| 65 | + return -1; |
| 66 | + } |
| 67 | + |
| 68 | + gnlh = nlmsg_data(nlh); |
| 69 | + if (!gnlh) { |
| 70 | + fprintf(stderr, "MOCK: Failed to get genl header\n"); |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + if (gnlh->cmd != NBD_CMD_CONNECT) { |
| 75 | + fprintf(stderr, "MOCK: Expected NBD_CMD_CONNECT, got %d\n", gnlh->cmd); |
| 76 | + return -1; |
| 77 | + } |
| 78 | + |
| 79 | + ret = genlmsg_parse(nlh, 0, attrs, NBD_ATTR_MAX, NULL); |
| 80 | + if (ret != 0) { |
| 81 | + fprintf(stderr, "MOCK: Failed to parse attributes: %d\n", ret); |
| 82 | + return -1; |
| 83 | + } |
| 84 | + |
| 85 | + /* Validate required attributes */ |
| 86 | + if (!attrs[NBD_ATTR_SIZE_BYTES]) { |
| 87 | + fprintf(stderr, "MOCK: Missing required NBD_ATTR_SIZE_BYTES\n"); |
| 88 | + return -1; |
| 89 | + } |
| 90 | + |
| 91 | + if (!attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) { |
| 92 | + fprintf(stderr, "MOCK: Missing required NBD_ATTR_BLOCK_SIZE_BYTES\n"); |
| 93 | + return -1; |
| 94 | + } |
| 95 | + |
| 96 | + if (!attrs[NBD_ATTR_SERVER_FLAGS]) { |
| 97 | + fprintf(stderr, "MOCK: Missing required NBD_ATTR_SERVER_FLAGS\n"); |
| 98 | + return -1; |
| 99 | + } |
| 100 | + |
| 101 | + if (!attrs[NBD_ATTR_SOCKETS]) { |
| 102 | + fprintf(stderr, "MOCK: Missing required NBD_ATTR_SOCKETS\n"); |
| 103 | + return -1; |
| 104 | + } |
| 105 | + |
| 106 | + /* Validate attribute values */ |
| 107 | + uint64_t size = nla_get_u64(attrs[NBD_ATTR_SIZE_BYTES]); |
| 108 | + if (size == 0) { |
| 109 | + fprintf(stderr, "MOCK: Invalid size_bytes: %lu\n", size); |
| 110 | + return -1; |
| 111 | + } |
| 112 | + |
| 113 | + uint64_t block_size = nla_get_u64(attrs[NBD_ATTR_BLOCK_SIZE_BYTES]); |
| 114 | + if (block_size == 0 || (block_size & (block_size - 1)) != 0) { |
| 115 | + fprintf(stderr, "MOCK: Invalid block_size_bytes: %lu (must be power of 2)\n", block_size); |
| 116 | + return -1; |
| 117 | + } |
| 118 | + |
| 119 | + printf("MOCK: ✓ Connect message validation passed\n"); |
| 120 | + printf("MOCK: Size: %lu, Block size: %lu, Flags: %lu\n", |
| 121 | + size, block_size, nla_get_u64(attrs[NBD_ATTR_SERVER_FLAGS])); |
| 122 | + |
| 123 | + return 0; |
| 124 | +} |
| 125 | + |
| 126 | +static int validate_disconnect_message(struct nl_msg *msg) { |
| 127 | + struct nlmsghdr *nlh; |
| 128 | + struct genlmsghdr *gnlh; |
| 129 | + struct nlattr *attrs[NBD_ATTR_MAX + 1]; |
| 130 | + int ret; |
| 131 | + |
| 132 | + nlh = real_nlmsg_hdr(msg); |
| 133 | + if (!nlh) return -1; |
| 134 | + |
| 135 | + gnlh = nlmsg_data(nlh); |
| 136 | + if (!gnlh) return -1; |
| 137 | + |
| 138 | + if (gnlh->cmd != NBD_CMD_DISCONNECT) { |
| 139 | + fprintf(stderr, "MOCK: Expected NBD_CMD_DISCONNECT, got %d\n", gnlh->cmd); |
| 140 | + return -1; |
| 141 | + } |
| 142 | + |
| 143 | + ret = genlmsg_parse(nlh, 0, attrs, NBD_ATTR_MAX, NULL); |
| 144 | + if (ret != 0) return -1; |
| 145 | + |
| 146 | + if (!attrs[NBD_ATTR_INDEX]) { |
| 147 | + fprintf(stderr, "MOCK: Missing required NBD_ATTR_INDEX for disconnect\n"); |
| 148 | + return -1; |
| 149 | + } |
| 150 | + |
| 151 | + printf("MOCK: ✓ Disconnect message validation passed\n"); |
| 152 | + printf("MOCK: Device index: %u\n", nla_get_u32(attrs[NBD_ATTR_INDEX])); |
| 153 | + |
| 154 | + return 0; |
| 155 | +} |
| 156 | + |
| 157 | +static int validate_status_message(struct nl_msg *msg) { |
| 158 | + struct nlmsghdr *nlh; |
| 159 | + struct genlmsghdr *gnlh; |
| 160 | + |
| 161 | + nlh = real_nlmsg_hdr(msg); |
| 162 | + if (!nlh) return -1; |
| 163 | + |
| 164 | + gnlh = nlmsg_data(nlh); |
| 165 | + if (!gnlh) return -1; |
| 166 | + |
| 167 | + if (gnlh->cmd != NBD_CMD_STATUS) { |
| 168 | + fprintf(stderr, "MOCK: Expected NBD_CMD_STATUS, got %d\n", gnlh->cmd); |
| 169 | + return -1; |
| 170 | + } |
| 171 | + |
| 172 | + printf("MOCK: ✓ Status message validation passed\n"); |
| 173 | + |
| 174 | + return 0; |
| 175 | +} |
| 176 | + |
| 177 | +/* Mock implementations */ |
| 178 | +struct nl_sock *nl_socket_alloc(void) { |
| 179 | + init_real_functions(); |
| 180 | + return real_nl_socket_alloc(); |
| 181 | +} |
| 182 | + |
| 183 | +void nl_socket_free(struct nl_sock *sock) { |
| 184 | + init_real_functions(); |
| 185 | + real_nl_socket_free(sock); |
| 186 | +} |
| 187 | + |
| 188 | +int genl_connect(struct nl_sock *sock) { |
| 189 | + init_real_functions(); |
| 190 | + mock_connected = 1; |
| 191 | + printf("MOCK: genl_connect() - success\n"); |
| 192 | + return 0; /* Always succeed in mock */ |
| 193 | +} |
| 194 | + |
| 195 | +int genl_ctrl_resolve(struct nl_sock *sock, const char *name) { |
| 196 | + init_real_functions(); |
| 197 | + |
| 198 | + if (strcmp(name, "nbd") == 0) { |
| 199 | + printf("MOCK: genl_ctrl_resolve(nbd) - returning mock family ID %d\n", mock_family_id); |
| 200 | + return mock_family_id; |
| 201 | + } |
| 202 | + |
| 203 | + printf("MOCK: genl_ctrl_resolve(%s) - not found\n", name); |
| 204 | + return -ENOENT; |
| 205 | +} |
| 206 | + |
| 207 | +int nl_socket_modify_cb(struct nl_sock *sock, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg) { |
| 208 | + init_real_functions(); |
| 209 | + printf("MOCK: nl_socket_modify_cb() - callback registered\n"); |
| 210 | + return real_nl_socket_modify_cb(sock, type, kind, func, arg); |
| 211 | +} |
| 212 | + |
| 213 | +struct nl_msg *nlmsg_alloc(void) { |
| 214 | + init_real_functions(); |
| 215 | + return real_nlmsg_alloc(); |
| 216 | +} |
| 217 | + |
| 218 | +void nlmsg_free(struct nl_msg *msg) { |
| 219 | + init_real_functions(); |
| 220 | + if (msg == last_sent_msg) { |
| 221 | + last_sent_msg = NULL; |
| 222 | + } |
| 223 | + real_nlmsg_free(msg); |
| 224 | +} |
| 225 | + |
| 226 | +void *genlmsg_put(struct nl_msg *msg, uint32_t port, uint32_t seq, int family, int hdrlen, int flags, uint8_t cmd, uint8_t version) { |
| 227 | + init_real_functions(); |
| 228 | + return real_genlmsg_put(msg, port, seq, family, hdrlen, flags, cmd, version); |
| 229 | +} |
| 230 | + |
| 231 | +struct nlmsghdr *nlmsg_hdr(struct nl_msg *msg) { |
| 232 | + init_real_functions(); |
| 233 | + return real_nlmsg_hdr(msg); |
| 234 | +} |
| 235 | + |
| 236 | +int nl_send_auto(struct nl_sock *sock, struct nl_msg *msg) { |
| 237 | + struct nlmsghdr *nlh; |
| 238 | + struct genlmsghdr *gnlh; |
| 239 | + int validation_result = -1; |
| 240 | + |
| 241 | + init_real_functions(); |
| 242 | + |
| 243 | + printf("MOCK: nl_send_auto() - intercepting message\n"); |
| 244 | + |
| 245 | + /* Store the message for validation */ |
| 246 | + if (last_sent_msg) { |
| 247 | + real_nlmsg_free(last_sent_msg); |
| 248 | + } |
| 249 | + last_sent_msg = msg; |
| 250 | + |
| 251 | + /* Validate the message */ |
| 252 | + nlh = real_nlmsg_hdr(msg); |
| 253 | + if (nlh) { |
| 254 | + gnlh = nlmsg_data(nlh); |
| 255 | + if (gnlh) { |
| 256 | + switch (gnlh->cmd) { |
| 257 | + case NBD_CMD_CONNECT: |
| 258 | + validation_result = validate_connect_message(msg); |
| 259 | + break; |
| 260 | + case NBD_CMD_DISCONNECT: |
| 261 | + validation_result = validate_disconnect_message(msg); |
| 262 | + break; |
| 263 | + case NBD_CMD_STATUS: |
| 264 | + validation_result = validate_status_message(msg); |
| 265 | + break; |
| 266 | + default: |
| 267 | + printf("MOCK: Unknown command %d, skipping validation\n", gnlh->cmd); |
| 268 | + validation_result = 0; |
| 269 | + break; |
| 270 | + } |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + if (validation_result != 0) { |
| 275 | + fprintf(stderr, "MOCK: Message validation failed!\n"); |
| 276 | + return -EINVAL; |
| 277 | + } |
| 278 | + |
| 279 | + /* Don't actually send, just pretend it worked */ |
| 280 | + printf("MOCK: nl_send_auto() - success (message not actually sent)\n"); |
| 281 | + return 0; |
| 282 | +} |
| 283 | + |
| 284 | +int nl_wait_for_ack(struct nl_sock *sock) { |
| 285 | + init_real_functions(); |
| 286 | + printf("MOCK: nl_wait_for_ack() - success (mock)\n"); |
| 287 | + return 0; /* Always succeed in mock */ |
| 288 | +} |
0 commit comments