I'm writing some code to perform an integration test of some C code that reads a TCP socket. The code is written in such a way that I can easily pass it any FILE *
so my initial thoughts for testing the code was to use UNIX sockets instead to fake out the network interactions entirely within the test process. My only hang up is that UNIX sockets need a file path to create the socket in, which means I need to deal with the complications of finding/creating/destroying a unique temporary directory and ensuring the individual tests each get a unique socket name in that directory. Pipes have the property that everything is in memory and so they're easy to setup / tear down. But pipes are uni-directional only and they cannot conform to the FILE *
interface I need.
Asked
Active
Viewed 77 times
0

Huckle
- 1,810
- 3
- 26
- 40
-
Unix has built in locations for temp files and apis for creating them. Just google up some sample code. – pvg Jun 29 '17 at 02:02
-
you can use network sockets to test the interactions within the same process. – Michaël Roy Jun 29 '17 at 02:11
-
2You can create a connected pair of sockets using [`socketpair()`](http://man7.org/linux/man-pages/man2/socketpair.2.html). For example, `int sockfd[2], err = socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd);` creates a connected pair of Unix domain stream sockets if `err == 0` (see `strerror(err)` for reason of failure otherwise). One end is `sockfd[0]`, and the other is `sockfd[1]`. – Nominal Animal Jun 29 '17 at 03:07
-
@NominalAnimal That's exactly what I was hoping would exist, If you submit that as an answer I will accept it. – Huckle Jun 29 '17 at 04:07
-
Have you looked at [`fmemopen()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fmemopen.html) or [`opem_memstream()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/opem_memstream.html)? They sound as if they'd fit your question title, but I'm not clear whether the question title really relates to what you're after. A solid wall of text like that is hard to read. – Jonathan Leffler Jun 29 '17 at 04:22
-
@JonathanLeffler `open_memstream()` certainly does sound like it would emulate the file interface but without actually writing a file to disk. I'll keep that in my bookmarks in case I need to test any C code that deals with reading/writing regular files. But for this I need `FILE *` that behave like network sockets, i.e. they are in a pair and writes to one are read in the other. I've successfully used the `socketpair()` method mentioned above. That being said, my question is no more than a paragraph in length, hardly a wall of text IMO. – Huckle Jun 29 '17 at 05:35
1 Answers
0
Quoting Nominal Animal
, if they submit their comment as an answer I'll delete this and accept theirs.
You can create a connected pair of sockets using
socketpair()
. For example,int sockfd[2], err = socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd);
creates a connected pair of Unix domain stream sockets iferr == 0
(seestrerror(err)
for reason of failure otherwise). One end issockfd[0]
, and the other issockfd[1]

Huckle
- 1,810
- 3
- 26
- 40