Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reliably make send(2) do a short send?

send(2) takes a buffer and a buffer length. It can return either an error, or some number of bytes successfully sent up to the size of the buffer length. In some cases, send will send fewer than the number of bytes requested (e.g. https://stackoverflow.com/a/2618755/939259).

Is there a way to consistently trigger a short send in a unit test, other than sending a big message and firing a signal from another thread and hoping to get lucky?

like image 230
Thomas Johnson Avatar asked Oct 15 '22 07:10

Thomas Johnson


People also ask

Is there a way to send the same email to multiple recipients separately?

Send to multiple recipients using the BCC method in Gmail. Perhaps the simplest method of doing this is the BCC method. This works by sending the email to a single recipient, often even to yourself, and adding every intended recipient of the email as a BCC.

Is there a way to send a mass email individually outlook?

Send an email to multiple recipient separately with Mail Merge feature. In fact, the Mail Merge feature in Outlook can help you to send the same email to multiple recipients individually with their own greeting.

How do I send an email to multiple recipients without showing addresses?

To send emails to small groups where everybody knows each other, use the Cc field. Enter all of the addresses there, separated by commas. To hide addresses, use the Bcc field, just like the Cc field. No one will be able to see the addresses added in this field.

How do I send a mass email individually?

Open a new email and write the message you intend to send to your contact list. Click BCC in the top-right of your Compose window. Add all the email addresses to which you intend to send the message. It might help to copy and paste your list into this field.


2 Answers

Just roll your own:


#include <sys/types.h>
#include <sys/socket.h>

ssize_t mysend(int fd, void * buff, size_t len, int flags)
{

#if WANT_PARTIAL_SEND
len = 1 + urand(len -1);
#endif

return send(fd, buff, len, flags);
}
like image 106
wildplasser Avatar answered Nov 15 '22 12:11

wildplasser


If you pack the code-to-be tested into a shared library (or a static library with the symbols weakened), then your testing executable (which links with the library) will be able to override send for both itself and the libraries it links.

Example (overrides write rather than send):

#!/bin/sh -eu
cat > code.c <<EOF
#include <unistd.h>
#include <stdio.h>
ssize_t hw(void)
{
    static const char b[]="hello world\n";
    return write(1, b, sizeof(b)-1);
}
void libfunc(void)
{
    puts(__func__);
    hw();
}
EOF

cat > test.c <<'EOF'
#include <stdio.h>
void libfunc(void);
ssize_t hw(void);

#if TEST
ssize_t hw(void)
{
    puts("override");
    return 42;
}
#endif
int main()
{
    libfunc();
    puts("====");
    printf("%zu\n", hw());
}

EOF

gcc code.c -fpic -shared -o libcode.so
gcc test.c $PWD/libcode.so -o real
gcc -DTEST test.c $PWD/libcode.so -o mocked
set -x
./real
./mocked

Example output:

hello world
hello world
libfunc
====
12
libfunc
override
====
override
42

This overshadows the libc implementation of the symbol and while there are mechanism for accessing the overridee (namely dlopen and/or -Wl,--wrap), you shouldn't need to access it in a unit test (if you do need it in other unit tests, it's simplest to just put those other unit tests in a different program).

like image 41
PSkocik Avatar answered Nov 15 '22 12:11

PSkocik