-
Notifications
You must be signed in to change notification settings - Fork 0
/
addrinfo_test.cpp
144 lines (129 loc) · 4.82 KB
/
addrinfo_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "addrinfo.hpp"
#include "socket.hpp"
#include "gtest/gtest.h"
#include <arpa/inet.h>
#include <memory>
#include <iostream>
#include <sys/socket.h>
TEST(AddrinfoTest, getaddrinfo) {
std::vector<Addrinfo *> v = Addrinfo::getaddrinfo("moonrock.jp", "http");
for (std::vector<Addrinfo *>::iterator i = v.begin(); i != v.end(); ++i) {
Addrinfo *ai = *i;
int sock = socket(ai->pfamily(), ai->socktype(), ai->protocol());
ASSERT_NE(-1, sock);
ASSERT_EQ(80, ntohs(ai->addr_in()->sin_port));
ASSERT_EQ(0, connect(sock, ai->addr(), ai->addrlen()));
ASSERT_EQ(0, close(sock));
delete ai;
}
}
TEST(AddrinfoTest, getaddrinfo2) {
std::vector<Addrinfo *> v = Addrinfo::getaddrinfo("moonrock.jp", 80);
for (std::vector<Addrinfo *>::iterator i = v.begin(); i != v.end(); ++i) {
Addrinfo *ai = *i;
int sock = socket(ai->pfamily(), ai->socktype(), ai->protocol());
ASSERT_NE(-1, sock);
ASSERT_EQ(80, ntohs(ai->addr_in()->sin_port));
ASSERT_EQ(0, connect(sock, ai->addr(), ai->addrlen()));
ASSERT_EQ(0, close(sock));
delete ai;
}
}
TEST(AddrinfoTest, tcp) {
std::shared_ptr<Addrinfo> ai(Addrinfo::tcp("localhost", "http"));
ASSERT_TRUE(ai != NULL);
EXPECT_EQ(0, ai->flags());
EXPECT_EQ(PF_INET, ai->pfamily());
EXPECT_EQ(SOCK_STREAM, ai->socktype());
EXPECT_EQ(IPPROTO_TCP, ai->protocol());
EXPECT_EQ(sizeof (sockaddr_in), ai->addrlen());
EXPECT_EQ(AF_INET, ai->addr_in()->sin_family);
EXPECT_EQ(80, ntohs(ai->addr_in()->sin_port));
EXPECT_EQ(0, memcmp(&ai->addr_in()->sin_addr, "\x7f\x00\x00\x01", 4));
}
TEST(AddrinfoTest, tcp_2) {
std::shared_ptr<Addrinfo> ai(Addrinfo::tcp("localhost", 80));
ASSERT_TRUE(ai != NULL);
EXPECT_EQ(0, ai->flags());
EXPECT_EQ(PF_INET, ai->pfamily());
EXPECT_EQ(SOCK_STREAM, ai->socktype());
EXPECT_EQ(IPPROTO_TCP, ai->protocol());
EXPECT_EQ(sizeof (sockaddr_in), ai->addrlen());
EXPECT_EQ(AF_INET, ai->addr_in()->sin_family);
EXPECT_EQ(80, ntohs(ai->addr_in()->sin_port));
EXPECT_EQ(0, memcmp(&ai->addr_in()->sin_addr, "\x7f\x00\x00\x01", 4));
}
TEST(AddrinfoTest, udp) {
std::shared_ptr<Addrinfo> ai(Addrinfo::udp("localhost", "http"));
ASSERT_TRUE(ai != NULL);
EXPECT_EQ(0, ai->flags());
EXPECT_EQ(PF_INET, ai->pfamily());
EXPECT_EQ(SOCK_DGRAM, ai->socktype());
EXPECT_EQ(IPPROTO_UDP, ai->protocol());
EXPECT_EQ(sizeof (sockaddr_in), ai->addrlen());
EXPECT_EQ(AF_INET, ai->addr_in()->sin_family);
EXPECT_EQ(80, ntohs(ai->addr_in()->sin_port));
EXPECT_EQ(0, memcmp(&ai->addr_in()->sin_addr, "\x7f\x00\x00\x01", 4));
}
TEST(AddrinfoTest, udp_2) {
std::shared_ptr<Addrinfo> ai(Addrinfo::udp("localhost", 80));
ASSERT_TRUE(ai != NULL);
EXPECT_EQ(0, ai->flags());
EXPECT_EQ(PF_INET, ai->pfamily());
EXPECT_EQ(SOCK_DGRAM, ai->socktype());
EXPECT_EQ(IPPROTO_UDP, ai->protocol());
EXPECT_EQ(sizeof (sockaddr_in), ai->addrlen());
EXPECT_EQ(AF_INET, ai->addr_in()->sin_family);
EXPECT_EQ(80, ntohs(ai->addr_in()->sin_port));
EXPECT_EQ(0, memcmp(&ai->addr_in()->sin_addr, "\x7f\x00\x00\x01", 4));
}
TEST(AddrinfoTest, ctor) {
sockaddr_in sin;
ASSERT_TRUE(Socket::get_sockaddr_in(sin, "http", "localhost"));
Addrinfo ai(&sin);
EXPECT_EQ(0, ai.flags());
EXPECT_EQ(PF_INET, ai.pfamily());
EXPECT_EQ(0, ai.socktype());
EXPECT_EQ(0, ai.protocol());
EXPECT_EQ(sizeof (sockaddr_in), ai.addrlen());
EXPECT_EQ(AF_INET, ai.addr_in()->sin_family);
EXPECT_EQ(80, ntohs(ai.addr_in()->sin_port));
EXPECT_EQ(0, memcmp(&ai.addr_in()->sin_addr, "\x7f\x00\x00\x01", 4));
}
TEST(AddrinfoTest, bind) {
std::shared_ptr<Addrinfo> ai(Addrinfo::udp("localhost", 7777));
ASSERT_TRUE(ai != NULL);
Socket *sock = ai->bind();
ASSERT_TRUE(sock != NULL);
delete sock;
}
TEST(AddrinfoTest, inspect) {
sockaddr_in sin1;
ASSERT_TRUE(Socket::get_sockaddr_in(sin1, 80, "127.0.0.1"));
Addrinfo ai1(&sin1);
EXPECT_EQ("#<Addrinfo: 127.0.0.1:80>", ai1.inspect());
std::shared_ptr<Addrinfo> ai2(Addrinfo::tcp("127.0.0.1", 80));
EXPECT_EQ("#<Addrinfo: 127.0.0.1:80 TCP>", ai2->inspect());
std::shared_ptr<Addrinfo> ai3(Addrinfo::udp("127.0.0.1", 80));
EXPECT_EQ("#<Addrinfo: 127.0.0.1:80 UDP>", ai3->inspect());
sockaddr_in sin4;
ASSERT_TRUE(Socket::get_sockaddr_in(sin4, 0, ""));
Addrinfo ai4(&sin4);
EXPECT_EQ("#<Addrinfo: 0.0.0.0>", ai4.inspect());
sockaddr_in sin5;
ASSERT_TRUE(Socket::get_sockaddr_in(sin5, 1, ""));
Addrinfo ai5(&sin5);
EXPECT_EQ("#<Addrinfo: 0.0.0.0:1>", ai5.inspect());
sockaddr_in sin6;
ASSERT_TRUE(Socket::get_sockaddr_in(sin6, "", ""));
Addrinfo ai6(&sin6);
EXPECT_EQ("#<Addrinfo: 0.0.0.0>", ai6.inspect());
}
TEST(AddrinfoTest, operator_LSHIFT) {
sockaddr_in sin;
Socket::get_sockaddr_in(sin, 80, "127.0.0.1");
Addrinfo ai(&sin);
testing::internal::CaptureStdout();
std::cout << ai;
EXPECT_STREQ("#<Addrinfo: 127.0.0.1:80>", testing::internal::GetCapturedStdout().c_str());
}