Skip to content

Commit

Permalink
tests: Communicate bind() operation over test socket
Browse files Browse the repository at this point in the history
Populate the implemenetation for the bind callback, sending the address
data to the test socket.

Signed-off-by: Jeremy Kerr <[email protected]>
  • Loading branch information
jk-ozlabs committed Jun 26, 2024
1 parent 5acab74 commit 01c129e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ async def recv_msg(self, data):
level = int.from_bytes(level, byteorder = sys.byteorder)
optname = int.from_bytes(optname, byteorder = sys.byteorder)
await self.handle_setsockopt(level, optname, optval)
elif typ == 3:
# bind
addr = data[:MAX_SOCKADDR_SIZE]
addrlen = int.from_bytes(
data[MAX_SOCKADDR_SIZE:MAX_SOCKADDR_SIZE+4],
byteorder = sys.byteorder
)
addr = addr[:addrlen]
await self.handle_bind(addr)

else:
print(f"unknown message type {typ}")

Expand All @@ -332,6 +342,9 @@ async def send(self, addr, data):
buf = struct.pack("@I", 0) + addr + struct.pack("@I", addrlen) + data
await self.sock.send(buf)

async def handle_bind(self, addr):
pass

class MCTPSockAddr:
base_addr_fmt = "@HHiBBBB"
ext_addr_fmt = "@iB3c" # just the header here, we append the lladdr data
Expand Down
24 changes: 24 additions & 0 deletions tests/mctp-ops-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ static int mctp_op_netlink_socket(void)

static int mctp_op_bind(int sd, struct sockaddr *addr, socklen_t addrlen)
{
struct msghdr msg = { 0 };
struct sock_msg sock_msg;
struct iovec iov;
ssize_t rc;

sock_msg.type = SOCK_BIND;
sock_msg.bind.addrlen = addrlen;
memcpy(&sock_msg.bind.addr.buf, addr, addrlen);

iov.iov_base = &sock_msg;
iov.iov_len = sizeof(sock_msg);

msg.msg_iov = &iov;
msg.msg_iovlen = 1;

rc = sendmsg(sd, &msg, 0);
if (rc < 0)
return rc;

if (rc < (int)sizeof(sock_msg)) {
errno = EPROTO;
return -1;
}

return 0;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/test-proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct sock_msg {
SOCK_RECV,
SOCK_SEND,
SOCK_SETSOCKOPT,
SOCK_BIND,
} type;
union {
struct sock_msg_recv {
Expand All @@ -48,6 +49,10 @@ struct sock_msg {
int optname;
uint8_t optdata[];
} setsockopt;
struct sock_msg_bind {
union sock_msg_sockaddr addr;
socklen_t addrlen;
} bind;
};
};

Expand Down

0 comments on commit 01c129e

Please sign in to comment.