-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mkm: add pluggable IO modules for client.c testing
- Loading branch information
1 parent
11efbdc
commit b3bff4a
Showing
8 changed files
with
109 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters