Skip to content

Commit

Permalink
mkm: add pluggable IO modules for client.c testing
Browse files Browse the repository at this point in the history
  • Loading branch information
spernsteiner committed Dec 21, 2024
1 parent 11efbdc commit b3bff4a
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 7 deletions.
2 changes: 1 addition & 1 deletion components/mission_key_management/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ BUILD_DIR = build$(TARGET_SUFFIX)

MKM_BIN = mkm$(TARGET_SUFFIX)

SRC = mkm.c client.c policy.c sha_256.c hmac_sha256.c
SRC = mkm.c client.c policy.c sha_256.c hmac_sha256.c io_real.c
OBJ = $(SRC:%.c=$(BUILD_DIR)/%.o)
CFLAGS = -I$(ROOT_DIR) -Wall -Wextra -pedantic

Expand Down
9 changes: 5 additions & 4 deletions components/mission_key_management/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ uint32_t client_state_epoll_events(enum client_state state) {
}
}

struct client* client_new(int fd) {
struct client* client_new(struct io_data* io, int fd) {
struct client* c = malloc(sizeof(struct client));
if (c == NULL) {
perror("malloc (client_new)");
return NULL;
}
c->io = io;
c->fd = fd;
c->pos = 0;
c->state = CS_RECV_KEY_ID;
Expand All @@ -50,7 +51,7 @@ struct client* client_new(int fd) {
}

void client_delete(struct client* c) {
int ret = shutdown(c->fd, SHUT_RDWR);
int ret = io_shutdown(c->io, c->fd, SHUT_RDWR);
if (ret != 0) {
perror("shutdown (client_delete)");
// Keep going. Even if TCP shutdown fails, we still need to close the
Expand Down Expand Up @@ -133,7 +134,7 @@ enum client_event_result client_read(struct client* c) {
return RES_DONE;
}

int ret = read(c->fd, buf + c->pos, buf_size - c->pos);
int ret = io_read(c->io, c->fd, buf + c->pos, buf_size - c->pos);
if (ret < 0) {
perror("read (client_read)");
return RES_ERROR;
Expand All @@ -158,7 +159,7 @@ enum client_event_result client_write(struct client* c) {
return RES_DONE;
}

int ret = write(c->fd, buf + c->pos, buf_size - c->pos);
int ret = io_write(c->io, c->fd, buf + c->pos, buf_size - c->pos);
if (ret < 0) {
perror("write (client_write)");
return RES_ERROR;
Expand Down
4 changes: 3 additions & 1 deletion components/mission_key_management/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <stdint.h>
#include "policy.h"
#include "io_real.h"

enum client_state {
// Waiting to receive a request for a specific key ID.
Expand All @@ -23,6 +24,7 @@ enum client_op {
};

struct client {
struct io_data* io;
int fd;
// Buffers for async read/write operations.
uint8_t challenge[NONCE_SIZE];
Expand All @@ -49,7 +51,7 @@ enum client_event_result {
RES_DONE = 1,
};

struct client* client_new(int fd);
struct client* client_new(struct io_data* io, int fd);
// Deallocate client data. The client should be removed from epoll before
// calling this.
void client_delete(struct client* c);
Expand Down
16 changes: 16 additions & 0 deletions components/mission_key_management/io_real.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "io_real.h"
#include <unistd.h>
#include <sys/socket.h>

ssize_t io_read(struct io_data* data, int fd, void* buf, size_t n) {
(void)data;
return read(fd, buf, n);
}
ssize_t io_write(struct io_data* data, int fd, const void* buf, size_t n) {
(void)data;
return write(fd, buf, n);
}
int io_shutdown(struct io_data* data, int fd, int how) {
(void)data;
return shutdown(fd, how);
}
13 changes: 13 additions & 0 deletions components/mission_key_management/io_real.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <stdint.h>
#include <stddef.h>
#include <sys/types.h>

struct io_data {
uint8_t _dummy;
};

ssize_t io_read(struct io_data* data, int fd, void* buf, size_t n);
ssize_t io_write(struct io_data* data, int fd, const void* buf, size_t n);
int io_shutdown(struct io_data* data, int fd, int how);
50 changes: 50 additions & 0 deletions components/mission_key_management/io_trace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "io_trace.h"
#include <string.h>

ssize_t io_read(struct io_data* data, int fd, void* buf, size_t n) {
// Return the next `n` bytes from `data->read_data`. If `n` exceeds the
// amount of data remaining, return as much data as possible.
//
// To be more thorough, we could instead truncate some reads (though not to
// zero, which means EOF) in order to test the caller's handling of short
// reads. We could also return an error code (and set `errno`) in some
// cases.
//
// We could also assert that `fd` is valid to check whether the caller is
// passing bogus file descriptors.
(void)fd;
size_t remaining = data->read_data_len - data->read_pos;
if (remaining == 0) {
return 0;
}
if (n > remaining) {
n = remaining;
}
memcpy(buf, data->read_data + data->read_pos, n);
data->read_pos += n;
return n;
}

ssize_t io_write(struct io_data* data, int fd, const void* buf, size_t n) {
// Ignore inputs and return success (writing all `n` bytes).
//
// To be more thorough, we could instead truncate some writes (though not
// to zero, which means EOF) in order to test the caller's handling of
// short writes. We could also return an error code (and set `errno`) in
// some cases.
(void)data;
(void)fd;
(void)buf;
return n;
}

int io_shutdown(struct io_data* data, int fd, int how) {
// Ignore inputs and return success.
//
// To be more thorough, we could return an error code (and set `errno`) in
// some cases.
(void)data;
(void)fd;
(void)how;
return 0;
}
17 changes: 17 additions & 0 deletions components/mission_key_management/io_trace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <stdint.h>
#include <stddef.h>
#include <sys/types.h>

#define READ_DATA_MAX_SIZE 64

struct io_data {
const uint8_t* read_data;
size_t read_data_len;
size_t read_pos;
};

ssize_t io_read(struct io_data* data, int fd, void* buf, size_t n);
ssize_t io_write(struct io_data* data, int fd, const void* buf, size_t n);
int io_shutdown(struct io_data* data, int fd, int how);
5 changes: 4 additions & 1 deletion components/mission_key_management/mkm.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include "policy.h"


static struct io_data io = {0};


int main() {
int ret;

Expand Down Expand Up @@ -141,7 +144,7 @@ int main() {
if (events & EPOLLIN) {
// TODO: get peer address and log it
int sock_client = accept(sock_listen, NULL, 0);
struct client* c = client_new(sock_client);
struct client* c = client_new(&io, sock_client);
ret = client_epoll_ctl(c, epfd, EPOLL_CTL_ADD);
if (ret != 0) {
perror("epoll_ctl (add)");
Expand Down

0 comments on commit b3bff4a

Please sign in to comment.