I have to perform testing of socket related requirements for the software. As part of testing, I have to create a case for socket creation failure. My question is how do I make the C language socket() function return error value -1. The following are the lines of code to create the sockets in the software.
int socket = socket(AF_INET, SOCK_DGRAM, 0);
System configuration:
Socket creation rarely fails except for invalid parameters or extremely low-resources conditions (but in those cases generally nothing works so attempting recovery is an impossible mission).
What can fail at runtime is for example a listen
call because another process already is listening on the same port.
In windows socket creation most commonly fails because the the winsock initialization call has not been performed. This is IMO an horrible design bug in the API (so bad that makes me think it's an intentional attempt at prevent portability for no real reason).