Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_socket.d

raw
1 @safe unittest
2 {
3 import std.socket;
4
5 InternetHost ih = new InternetHost;
6
7 ih.getHostByAddr(0x7F_00_00_01);
8 assert(ih.addrList[0] == 0x7F_00_00_01);
9 ih.getHostByAddr("127.0.0.1");
10 assert(ih.addrList[0] == 0x7F_00_00_01);
11
12 if (!ih.getHostByName("www.digitalmars.com"))
13 return; // don't fail if not connected to internet
14
15 assert(ih.addrList.length);
16 InternetAddress ia = new InternetAddress(ih.addrList[0], InternetAddress.PORT_ANY);
17 assert(ih.name == "www.digitalmars.com" || ih.name == "digitalmars.com",
18 ih.name);
19
20 /* The following assert randomly fails in the test suite.
21 * https://issues.dlang.org/show_bug.cgi?id=22791
22 * So just ignore it when it fails.
23 */
24 //assert(ih.getHostByAddr(ih.addrList[0]));
25 if (ih.getHostByAddr(ih.addrList[0]))
26 {
27 string getHostNameFromInt = ih.name.dup;
28
29 // This randomly fails in the compiler test suite
30 //assert(ih.getHostByAddr(ia.toAddrString()));
31
32 if (ih.getHostByAddr(ia.toAddrString()))
33 {
34 string getHostNameFromStr = ih.name.dup;
35 assert(getHostNameFromInt == getHostNameFromStr);
36 }
37 }
38 }
39
40 @system unittest
41 {
42 import std.socket;
43
44 auto addr1 = new InternetAddress("127.0.0.1", 80);
45 auto addr2 = new InternetAddress("127.0.0.2", 80);
46
47 assert(addr1 == addr1);
48 assert(addr1 != addr2);
49
50 }
51
52 @safe unittest
53 {
54 import std.socket;
55
56 immutable ubyte[4] data = [1, 2, 3, 4];
57 auto pair = socketPair();
58 scope(exit) foreach (s; pair) s.close();
59
60 pair[0].send(data[]);
61
62 auto buf = new ubyte[data.length];
63 pair[1].receive(buf);
64 assert(buf == data);
65 }
66