Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Echo" device for Unit Testing

I'm currently writing up some CPPunit tests for a program that tests a hardware communication port (yes, I'm writing unit tests for a tester app ;-) ). One of the classes I'm testing is basically a wrapper around the device's file descriptor; I make read() and write() calls on the file descriptor. Is there a device file/driver on Linux where if you write() to it, when you read() from it you will read back what you wrote (like a FIFO queue device)? If I had this device file, it would be really simple to plop that in place of the actual device file in my wrapper and would make it easy to emulate "loopback" scenarios that might occur on real-world hardware.


Essentially what I'm looking for would be a sort of hypothetical "/dev/echo" device.

Ex: The device would behave as follows:

open("/dev/echo", O_RDRW);

write(fdEcho, 123, sizeof(int));
write(fdEcho, 456, sizeof(int));
write(fdEcho, 789, sizeof(int));

read(fdEcho, iData,  sizeof(int);  // returns 123
read(fdEcho, iData2, sizeof(int);  // returns 456
read(fdEcho, iData3, sizeof(int);  // returns 789
like image 586
J. Polfer Avatar asked Jan 23 '23 14:01

J. Polfer


2 Answers

Why don't you use a UNIX pipe in the filesystem?

mkfifo /dev/test
echo test > /dev/test

on a second terminal:

cat /dev/test

Then the first will unblock and the second will show the text!

like image 199
Christian Avatar answered Feb 02 '23 08:02

Christian


You can make one with

mknod /dev/echo p

First argument is the filename (it can be any filename, doesn't have to be in /dev), second argument is p for "pipe".

Reference: http://www.unix.com/unix-dummies-questions-answers/22205-unix-pipe.html

You could also look into the pipe() C function: http://www.manpagez.com/man/2/pipe/

like image 28
David Z Avatar answered Feb 02 '23 09:02

David Z