/*********************************************************************** * testFifo.c * * Perform some rudimentary testing of the fifo device. Emphasis on * the rudimentary. Asserts are used to confirm correct functionality. * This program should be able to successfully run an arbitrary number * of times without unloading and reloading the module. If not, your * fifo is faulty. * * Running the program without the fifo loaded should result in a * failure of the first assertion ('file >= 0'). * * Add your tests below, as indicated by the pair of comments near the * end of the file. ***********************************************************************/ #include #include #include #include #include #include #include #include "fifodev.h" /*********************************************************************** * main() ***********************************************************************/ int main() { int file; /* File descriptor for fifo device. */ /* Just a string used for testing * later. */ char hw[] = "Hello world.\n"; /* Buffer0 and bd0 (buffer descriptor). This is used for receiving * data from the fifo. */ char buffer0[FIFO_LEN + 1]; buffer_descriptor bd0 = { .buffer = buffer0, .len = FIFO_LEN }; /* Buffer1 and bd1. This is used for writing data to the fifo. */ char buffer1[FIFO_LEN] = "DATA data\n"; buffer_descriptor bd1 = { .buffer = buffer1, .len = 10 }; /* Check that device can be opened correctly and that we can't open an * open device. */ file = open(DEVICE_FILE_NAME, O_RDWR); assert(file >= 0); assert(open(DEVICE_FILE_NAME, O_RDWR) < 0); /* Return the fifo to a known initial state to begin testing. */ ioctl(file, IOCTL_READ_FIFO, &bd0); ioctl(file, IOCTL_SET_IMODE, STOP); ioctl(file, IOCTL_SET_PMODE, NORMAL); /* Check processing mode functionality. */ assert(ioctl(file, IOCTL_WRITE_FIFO, &bd1) == 10); ioctl(file, IOCTL_SET_PMODE, WHISPER); assert(ioctl(file, IOCTL_WRITE_FIFO, &bd1) == 10); ioctl(file, IOCTL_SET_PMODE, SHOUT); assert(ioctl(file, IOCTL_WRITE_FIFO, &bd1) == 10); assert(ioctl(file, IOCTL_READ_FIFO, &bd0) == 30); buffer0[30] = '\0'; printf("%s", buffer0); assert(strcmp(buffer0, "DATA data\ndata data\nDATA DATA\n") == 0); /* Check input mode functionality, STOP mode first. */ strcpy(buffer1, "12345678901234567890"); bd1.len = 20; assert(ioctl(file, IOCTL_WRITE_FIFO, &bd1) == 20); assert(ioctl(file, IOCTL_WRITE_FIFO, &bd1) == 12); assert(ioctl(file, IOCTL_READ_FIFO, &bd0) == 32); buffer0[32] = '\0'; printf("%s\n", buffer0); assert(strcmp(buffer0, "12345678901234567890123456789012") == 0); /* Now check OVERWRITE mode. */ ioctl(file, IOCTL_SET_IMODE, OVERWRITE); assert(ioctl(file, IOCTL_WRITE_FIFO, &bd1) == 20); assert(ioctl(file, IOCTL_WRITE_FIFO, &bd1) == 20); assert(ioctl(file, IOCTL_READ_FIFO, &bd0) == 32); buffer0[32] = '\0'; printf("%s\n", buffer0); assert(strcmp(buffer0, "90123456789012345678901234567890") == 0); /* Perform a few filesystem access tests. */ ioctl(file, IOCTL_SET_PMODE, NORMAL); assert(write(file, hw, strlen(hw)) == strlen(hw)); assert(read(file, buffer0, 6) == 6); assert(read(file, buffer0, FIFO_LEN) == 7); buffer0[7] = '\0'; printf("%s", buffer0); assert(strcmp(buffer0, "world.\n") == 0); /*********** Add your tests below this line. ***********/ /*********** Add your tests above this line. ***********/ /* Close the device and confirm that we can't close a closed device. */ assert(close(file) == 0); assert(close(file) == -1); return 0; }