1 module during.tests.msg;
2 
3 import during;
4 import during.tests.base;
5 
6 import core.stdc.stdlib;
7 import core.sys.linux.errno;
8 import core.sys.posix.sys.socket;
9 import core.sys.posix.sys.uio : iovec;
10 import core.sys.posix.unistd;
11 
12 import std.algorithm : copy, equal, map;
13 import std.range;
14 
15 @("send/recv")
16 unittest
17 {
18     int[2] fd;
19     int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
20     assert(ret == 0, "socketpair()");
21 
22     Uring io;
23     auto res = io.setup();
24     assert(res >= 0, "Error initializing IO");
25 
26     // 0 - read, 1 - write
27     iovec[2] v;
28     msghdr[2] msg;
29 
30     foreach (i; 0..2)
31     {
32         v[i].iov_base = malloc(256);
33         v[i].iov_len = 256;
34 
35         msg[i].msg_iov = &v[i];
36         msg[i].msg_iovlen = 1;
37     }
38     scope (exit) foreach (i; 0..2) free(v[i].iov_base);
39 
40     iota(0, 256)
41         .map!(a => cast(ubyte)a)
42         .copy((cast(ubyte*)v[1].iov_base)[0..256]);
43 
44     // add recvmsg
45     io.putWith!(
46         (ref SubmissionEntry e, int fd, ref msghdr m)
47         {
48             e.prepRecvMsg(fd, m);
49             e.user_data = 0;
50         })(fd[0], msg[0]);
51 
52     // add sendmsg
53     io.putWith!(
54         (ref SubmissionEntry e, int fd, ref msghdr m)
55         {
56             e.prepSendMsg(fd, m);
57             e.user_data = 1;
58         })(fd[1], msg[1]);
59 
60     ret = io.submit(2);
61     assert(ret == 2);
62     assert(io.length == 2);
63 
64     foreach (i; 0..2)
65     {
66         if (io.front.res == -EINVAL)
67         {
68             version (D_BetterC)
69             {
70                 errmsg = "kernel doesn't support SEND/RECVMSG";
71                 return;
72             }
73             else throw new Exception("kernel doesn't support SEND/RECVMSG");
74         }
75         assert(io.front.res >= 0);
76         if (io.front.user_data == 1) continue; // write done
77         else assert((cast(ubyte*)v[0].iov_base)[0..256].equal(iota(0, 256)));
78     }
79 }