#include #include #include #include #include #include #include #include "../include/chardev.h" #define DEV0 "./dev0" #define DEV1 "./dev1" // basic write and read void test_1_write_read(void) { int fd = open(DEV0, O_RDWR); char *msg_1 = "Hello-1"; char *data_1 = (char *)calloc(strlen(msg_1), sizeof(char)); char *msg_2 = "Hello-2"; char *data_2 = (char *)calloc(strlen(msg_2), sizeof(char)); if (fd < 0) { printf("[ERROR]: Could not open device!\n"); } write(fd, msg_1, strlen(msg_1)); write(fd, msg_2, strlen(msg_2)); read(fd, data_1, strlen(msg_1)); printf("%s\n", data_1); read(fd, data_2, strlen(msg_2)); printf("%s\n", data_2); if (close(fd) < 0) { printf("[ERROR]: Could not close!\n"); } } // basic write with timeout void test_2_delayed_write(void) { int fd, ret; char *msg_1 = "Hello-1"; unsigned long nsecs = 3000000000; fd = open(DEV0, O_RDWR); if (fd < 0) { printf("[ERROR]: Could not open device!\n"); } ret = ioctl(fd, IOCTL_SET_SEND_TIMEOUT, nsecs); write(fd, msg_1, strlen(msg_1)); write(fd, msg_1, strlen(msg_1)); write(fd, msg_1, strlen(msg_1)); if (close(fd) < 0) { printf("[ERROR]: Could not close!\n"); } } void test_3_delayed_read() { int fd, ret, size = 1024; char *msg_1 = "Hello-1"; char *data_1 = (char *)calloc(size, sizeof(char)); char *data_2 = (char *)calloc(size, sizeof(char)); char *data_3 = (char *)calloc(size, sizeof(char)); unsigned long nsecs = 3000000000; fd = open(DEV0, O_RDWR); if (fd < 0) { printf("[ERROR]: Could not open device!\n"); } ret = ioctl(fd, IOCTL_SET_RECV_TIMEOUT, nsecs); // write(fd, msg_1, strlen(msg_1)); read(fd, data_1, size); printf("%s\n", data_1); read(fd, data_2, size); printf("%s\n", data_2); read(fd, data_3, size); printf("%s\n", data_3); if (close(fd) < 0) { printf("[ERROR]: Could not close!\n"); } } void *_test_4_thread_job(void *arg) { int fd = open(DEV0, O_RDWR); int id = (int)arg; char *msg_1 = "Hello-1"; char *data_1 = (char *)calloc(strlen(msg_1), sizeof(char)); char *msg_2 = "Hello-2"; char *data_2 = (char *)calloc(strlen(msg_2), sizeof(char)); if (fd < 0) { printf("[ERROR]: Could not open device!\n"); } write(fd, msg_1, strlen(msg_1)); write(fd, msg_2, strlen(msg_2)); read(fd, data_1, strlen(msg_1)); printf("[%d]: %s\n", id, data_1); read(fd, data_2, strlen(msg_2)); printf("[%d]: %s\n", id, data_2); if (close(fd) < 0) { printf("[ERROR]: Could not close!\n"); } return NULL; } void test_4_concurrent_sessions(void) { pthread_t tid1, tid2; if (pthread_create(&tid1, NULL, _test_4_thread_job, (void *)1) != 0) { fprintf(stderr, "Error during creation of thread\n"); exit(1); } if (pthread_create(&tid2, NULL, _test_4_thread_job, (void *)2) != 0) { fprintf(stderr, "Error during creation of thread\n"); exit(1); } pthread_join(tid1, NULL); pthread_join(tid2, NULL); return; } void test_5_delayed_write_cancelled() { int fd, ret; char *msg_1 = "Hello-1"; unsigned long nsecs = 3000000000; fd = open(DEV0, O_RDWR); if (fd < 0) { printf("[ERROR]: Could not open device!\n"); } ret = ioctl(fd, IOCTL_SET_SEND_TIMEOUT, nsecs); write(fd, msg_1, strlen(msg_1)); write(fd, msg_1, strlen(msg_1)); write(fd, msg_1, strlen(msg_1)); ret = ioctl(fd, IOCTL_REVOKE_DELAYED_MESSAGES); if (close(fd) < 0) { printf("[ERROR]: Could not close!\n"); } } int main(int argc, char **argv) { // test_1_write_read(); // test_2_delayed_write(); // test_3_delayed_read(); // test_4_concurrent_sessions(); // test_5_delayed_write_cancelled(); return 0; }