Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/posix-node/src/netdb.test.ts
1067 views
1
import posix from "./index";
2
3
test("gethostbyname consistency checks", () => {
4
const hostent = posix.gethostbyname?.("example.com");
5
if (hostent == null) throw Error("fail");
6
expect(hostent.h_name).toBe("example.com");
7
expect(hostent.h_aliases.length).toBe(0);
8
expect(hostent.h_addrtype).toBe(2);
9
expect(hostent.h_length).toBe(4);
10
expect(hostent.h_addr_list.length).toBeGreaterThan(0);
11
expect(hostent.h_addr_list[0]).toContain(".");
12
});
13
14
/*
15
Example:
16
17
> require('.').gethostbyaddr("64.233.187.99")
18
{
19
h_name: 'tj-in-f99.1e100.net',
20
h_addrtype: 2,
21
h_length: 4,
22
h_addr_list: [ '64.233.187.99' ],
23
h_aliases: [ '99.187.233.64.in-addr.arpa' ]
24
}
25
*/
26
27
test("gethostbyaddr check - v4", () => {
28
const hostent = posix.gethostbyaddr?.("64.233.187.99");
29
expect(hostent?.h_addr_list[0]).toContain(".");
30
expect(hostent?.h_addrtype).toBe(posix.constants?.AF_INET);
31
});
32
33
test("gethostbyaddr check - v6", () => {
34
const hostent = posix.gethostbyaddr?.("2001:4860:4860::8888");
35
expect(hostent?.h_addr_list[0]).toContain(":");
36
expect(hostent?.h_addrtype).toBe(posix.constants?.AF_INET6);
37
// behavior depends on OS
38
// expect(hostent?.h_aliases[0]).toContain("8.8.8.8");
39
});
40
41
test("getaddrinfo canonical name", () => {
42
const addrinfo = posix.getaddrinfo?.("example.com", "http", { flags: 2 });
43
if (addrinfo == null) throw Error("fail");
44
expect(addrinfo[0]?.ai_canonname).toEqual("example.com");
45
});
46
47
test("getaddrinfo isn't random garbled nonsense", () => {
48
const addrinfo = posix.getaddrinfo?.("example.com", "http", { flags: 2 });
49
if (addrinfo == null) throw Error("fail");
50
const addrinfo2 = posix.getaddrinfo?.("example.com", "http", { flags: 2 });
51
if (addrinfo2 == null) throw Error("fail");
52
expect(addrinfo).toEqual(addrinfo2);
53
});
54
55
// very OS dependent
56
test("getting error messages", () => {
57
expect(posix.gai_strerror?.(0)).toEqual("Unknown error");
58
});
59
60
test("getaddrinfo error code", () => {
61
try {
62
posix.getaddrinfo?.("google.com", "x");
63
} catch (err) {
64
expect(err.code != null).toBe(true);
65
}
66
});
67
68