Ticket #18653: 18653-test.cpp

File 18653-test.cpp, 3.9 KB (added by nielx, 7 months ago)
Line 
1#include <filesystem>
2#include <iostream>
3#include <cstring>
4#include <fcntl.h>
5#include <sys/socket.h>
6#include <sys/un.h>
7#include <vector>
8#include <unistd.h>
9
10
11namespace fs = std::filesystem;
12
13
14class TemporaryDirectory
15{
16public:
17 TemporaryDirectory();
18 ~TemporaryDirectory();
19
20 const fs::path& Path() const;
21private:
22 fs::path fPath;
23};
24
25
26TemporaryDirectory::TemporaryDirectory()
27{
28 fPath = fs::temp_directory_path();
29 auto subdir = std::string("haiku-test-");
30 srand(time(NULL));
31 subdir += std::to_string(rand());
32 fPath = fPath / subdir;
33 fs::create_directories(fPath);
34 std::cout << "Temp directory is " << fPath << '\n';
35}
36
37
38TemporaryDirectory::~TemporaryDirectory()
39{
40 fs::remove_all(fPath);
41}
42
43
44const std::filesystem::path &
45TemporaryDirectory::Path() const
46{
47 return fPath;
48}
49
50
51void
52set_cloexec(int fd)
53{
54 auto previous = fcntl(fd, F_GETFD);
55 auto newflags = previous | FD_CLOEXEC;
56 if (previous != newflags) {
57 if (auto status = fcntl(fd, F_SETFD, newflags); status != 0)
58 throw std::runtime_error("unable to set FD_CLOEXEC on sock1");
59 }
60}
61
62/*
63 * fn test_unix_datagram_peek() {
64 let dir = tmpdir();
65 let path1 = dir.path().join("sock");
66
67 let sock1 = or_panic!(UnixDatagram::bind(&path1));
68 let sock2 = or_panic!(UnixDatagram::unbound());
69 or_panic!(sock2.connect(&path1));
70
71 let msg = b"hello world";
72 or_panic!(sock2.send(msg));
73 for _ in 0..2 {
74 let mut buf = [0; 11];
75 let size = or_panic!(sock1.peek(&mut buf));
76 assert_eq!(size, 11);
77 assert_eq!(msg, &buf[..]);
78 }
79
80 let mut buf = [0; 11];
81 let size = or_panic!(sock1.recv(&mut buf));
82 assert_eq!(size, 11);
83 assert_eq!(msg, &buf[..]);
84}
85*/
86
87void
88test_unix_datagram_peek()
89{
90 // let dir = tmpdir();
91 TemporaryDirectory dir;
92 // let path1 = dir.path().join("sock");
93 auto path1 = dir.Path() / "sock";
94
95 // let sock1 = or_panic!(UnixDatagram::bind(&path1));
96 auto sock1 = socket(AF_UNIX, SOCK_DGRAM, 0);
97 if (sock1 == -1)
98 throw std::runtime_error("Cannot create sock1");
99 set_cloexec(sock1);
100
101 sockaddr_un socketaddress = {};
102 socketaddress.sun_family = AF_UNIX;
103 std::strncpy(socketaddress.sun_path, path1.c_str(), std::strlen(path1.c_str()));
104 if (auto status = bind(sock1, (const sockaddr *) &socketaddress, sizeof(socketaddress)); status == -1){
105 std::cout << strerror(errno) << std::endl;
106 throw std::runtime_error("unable to bind sock1 to path");
107 }
108
109 // let sock2 = or_panic!(UnixDatagram::unbound());
110 auto sock2 = socket(AF_UNIX, SOCK_DGRAM, 0);
111 if (sock2 == -1)
112 throw std::runtime_error("Cannot create sock2");
113 set_cloexec(sock2);
114
115 // or_panic!(sock2.connect(&path1))
116 if (auto status = connect(sock2, (const sockaddr *) &socketaddress, sizeof(socketaddress)); status != 0)
117 throw std::runtime_error("Cannot connect sock2 to socket");
118
119 // let msg = b"hello world"
120 const char* msg = "hello world";
121
122 // or_panic!(sock2.send(msg))
123 if (auto status = write(sock2, msg, strlen(msg)); status == -1)
124 throw std::runtime_error("Cannot write message to sock2");
125
126 std::cout << "Message written to sock2" << std::endl;
127
128 /*
129 for _ in 0..2 {
130 let mut buf = [0; 11];
131 let size = or_panic!(sock1.peek(&mut buf));
132 assert_eq!(size, 11);
133 assert_eq!(msg, &buf[..]);
134 }
135 */
136
137 for (int i = 0; i < 2; i++){
138 std::vector<std::byte> buf;
139 buf.reserve(11);
140 if (auto ret = recv(sock1, buf.data(), buf.capacity(), MSG_PEEK); ret != 11)
141 throw std::runtime_error("Could not peek 11 bytes from sock1");
142 buf.resize(11);
143 std::cout << "peek " << i + 1 << " complete" << std::endl;
144 }
145
146 // let mut buf = [0; 11];
147 std::vector<std::byte> buf;
148 buf.reserve(11);
149 // let size = or_panic!(sock1.recv(&mut buf));
150 // assert_eq!(size, 11);
151
152 if (auto ret = recv(sock1, buf.data(), buf.capacity(), 0); ret != 11)
153 throw std::runtime_error("Could not receive 11 bytes from sock1");
154 std::cout << "recv complete" << std::endl;
155}
156
157
158int main()
159{
160 std::cout << "Running test_unix_datagram_peek!" << std::endl;
161 test_unix_datagram_peek();
162 return 0;
163}