Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions tests/unit-tests/file_tests/deterministic/read.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#undef _GNU_SOURCE
#define _GNU_SOURCE

#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void) {
const char *filename = "read_test.txt";
const size_t payload_size = 4096;
char write_buf[4096];
char read_buf[4096] = {0};

// Create a fixed payload: repeating "ABCD..." pattern
for (size_t i = 0; i < payload_size; i++) {
write_buf[i] = 'A' + (i % 26);
}

// Create a local file "read_test.txt" with O_CREAT|O_TRUNC|O_WRONLY, mode 0644
int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0644);
assert(fd >= 0);

// Write the fixed payload
ssize_t written = write(fd, write_buf, payload_size);
assert(written == (ssize_t)payload_size);

// Close
assert(close(fd) == 0);

// Reopen O_RDONLY
fd = open(filename, O_RDONLY);
assert(fd >= 0);

// Read exactly 4096 bytes into buffer (loop until full)
size_t total_read = 0;
while (total_read < payload_size) {
ssize_t ret = read(fd, read_buf + total_read, payload_size - total_read);
assert(ret > 0);
total_read += ret;
}
assert(total_read == payload_size);

// Assert content matches expected (memcmp with the original payload buffer)
assert(memcmp(read_buf, write_buf, payload_size) == 0);

// Close fd and return
assert(close(fd) == 0);

return 0;
}
44 changes: 0 additions & 44 deletions tests/unit-tests/file_tests/non-deterministic/read.c

This file was deleted.

54 changes: 54 additions & 0 deletions tests/unit-tests/memory_tests/deterministic/mmap_complicated.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <assert.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>

int main() {
size_t mem_size = sizeof(int);

// Create two anonymous mmaps before fork
// 1) shared region
int *shared_int = (int *)mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
assert(shared_int != MAP_FAILED);

// 2) private region
int *private_int = (int *)mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
assert(private_int != MAP_FAILED);

// Initialize
*shared_int = 1;
*private_int = 1;

// Fork
pid_t pid = fork();
assert(pid >= 0);

if (pid == 0) {
// Child: set shared_int = 42, private_int = 99
*shared_int = 42;
*private_int = 99;
_exit(0);
} else {
// Parent: wait for child
int status;
pid_t waited_pid = waitpid(pid, &status, 0);
assert(waited_pid >= 0);
assert(WIFEXITED(status));
assert(WEXITSTATUS(status) == 0);

// Assert shared_int == 42 (should reflect child write)
assert(*shared_int == 42);

// Assert private_int == 1 (should NOT reflect child write)
assert(*private_int == 1);

// Munmap both and assert success
assert(munmap(shared_int, mem_size) == 0);
assert(munmap(private_int, mem_size) == 0);
}

return 0;
}
61 changes: 61 additions & 0 deletions tests/unit-tests/memory_tests/deterministic/segfault.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <assert.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

int main(void) {
int fd[2];
const char *message = "OK\n";
const size_t message_len = 3; // strlen("OK\n")
char buf[4] = {0};
ssize_t total_read = 0;
ssize_t n;

// Create pipe
assert(pipe(fd) == 0);

// Fork
pid_t pid = fork();
assert(pid >= 0);

if (pid == 0) {
// Child: close read end
assert(close(fd[0]) == 0);

// Write fixed message "OK\n" to fd[1]
ssize_t written = write(fd[1], message, message_len);
assert(written == (ssize_t)message_len);

// Close write end
assert(close(fd[1]) == 0);

_exit(0);
} else {
// Parent: close write end
assert(close(fd[1]) == 0);

// Read exactly len("OK\n") bytes (loop if needed)
while (total_read < (ssize_t)message_len) {
n = read(fd[0], buf + total_read, message_len - total_read);
assert(n > 0);
total_read += n;
}
assert(total_read == (ssize_t)message_len);

// Assert memcmp == 0
assert(memcmp(buf, message, message_len) == 0);

// Close read end
assert(close(fd[0]) == 0);

// Wait for child
int status;
pid_t waited_pid = waitpid(pid, &status, 0);
assert(waited_pid >= 0);
assert(WIFEXITED(status));
assert(WEXITSTATUS(status) == 0);
}

return 0;
}
63 changes: 63 additions & 0 deletions tests/unit-tests/memory_tests/deterministic/shm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <assert.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <sys/wait.h>

#define SHM_SIZE 4096

int main() {
key_t key = 1234;
int shmid;
int *flag;

// Try to remove any existing segment first (ignore errors)
int old_shmid = shmget(key, SHM_SIZE, 0666);
if (old_shmid >= 0) {
shmctl(old_shmid, IPC_RMID, NULL);
}

// Create the shared memory segment
shmid = shmget(key, SHM_SIZE, IPC_CREAT | IPC_EXCL | 0666);
assert(shmid >= 0);

// Parent: attach to shared memory and initialize flag to 0
void *shmaddr = shmat(shmid, NULL, 0);
assert(shmaddr != (void *)-1);
flag = (int *)shmaddr;
*flag = 0;

// Fork
pid_t pid = fork();
assert(pid >= 0);

if (pid == 0) {
// Child: attach to shared memory (gets its own pointer to same segment)
void *child_shmaddr = shmat(shmid, NULL, 0);
assert(child_shmaddr != (void *)-1);

// Write flag = 777
int *child_flag = (int *)child_shmaddr;
*child_flag = 777;

// Note: child doesn't need to detach before exit
_exit(0);
} else {
// Parent: wait for child
int status;
pid_t waited_pid = waitpid(pid, &status, 0);
assert(waited_pid >= 0);
assert(WIFEXITED(status));
assert(WEXITSTATUS(status) == 0);

// Assert that the shared memory was updated by child
assert(*flag == 777);

// Remove the shared memory segment (detach happens automatically on process exit)
assert(shmctl(shmid, IPC_RMID, NULL) == 0);
}

return 0;
}
74 changes: 0 additions & 74 deletions tests/unit-tests/memory_tests/non-deterministic/mmap_complicated.c

This file was deleted.

Loading