Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pistache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
pistache
Commits
b6491159
Unverified
Commit
b6491159
authored
Aug 28, 2018
by
Dennis Jenkins
Committed by
GitHub
Aug 28, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #279 from knowledge4igor/tests_and_refactoring_net_utils
Tests and refactoring net utils
parents
e7946337
a10b68f0
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
9 deletions
+66
-9
include/pistache/net.h
include/pistache/net.h
+4
-1
src/common/net.cc
src/common/net.cc
+8
-8
tests/CMakeLists.txt
tests/CMakeLists.txt
+1
-0
tests/net_test.cc
tests/net_test.cc
+53
-0
No files found.
include/pistache/net.h
View file @
b6491159
...
...
@@ -35,6 +35,9 @@ public:
bool
isReserved
()
const
;
bool
isUsed
()
const
;
static
constexpr
uint16_t
min
()
{
return
std
::
numeric_limits
<
uint16_t
>::
min
();
}
static
constexpr
uint16_t
max
()
{
return
std
::
numeric_limits
<
uint16_t
>::
max
();
}
...
...
@@ -77,7 +80,7 @@ public:
Port
port
()
const
;
private:
void
init
(
std
::
string
addr
);
void
init
(
const
std
::
string
&
addr
);
std
::
string
host_
;
Port
port_
;
};
...
...
src/common/net.cc
View file @
b6491159
...
...
@@ -104,22 +104,22 @@ Address::port() const {
}
void
Address
::
init
(
std
::
string
addr
)
{
auto
pos
=
addr
.
find
(
':'
);
Address
::
init
(
const
std
::
string
&
addr
)
{
const
auto
pos
=
addr
.
find
(
':'
);
if
(
pos
==
std
::
string
::
npos
)
throw
std
::
invalid_argument
(
"Invalid address"
);
std
::
string
host
=
addr
.
substr
(
0
,
pos
);
char
*
end
;
host_
=
addr
.
substr
(
0
,
pos
);
char
*
end
;
const
std
::
string
portPart
=
addr
.
substr
(
pos
+
1
);
if
(
portPart
.
empty
())
throw
std
::
invalid_argument
(
"Invalid port"
);
long
port
=
strtol
(
portPart
.
c_str
(),
&
end
,
10
);
if
(
*
end
!=
0
||
port
>
Port
::
max
())
if
(
*
end
!=
0
||
port
<
Port
::
min
()
||
port
>
Port
::
max
())
throw
std
::
invalid_argument
(
"Invalid port"
);
host_
=
std
::
move
(
host
);
port_
=
port
;
port_
=
static_cast
<
uint16_t
>
(
port
);
}
Error
::
Error
(
const
char
*
message
)
...
...
tests/CMakeLists.txt
View file @
b6491159
...
...
@@ -17,3 +17,4 @@ pistache_test(cookie_test)
pistache_test
(
view_test
)
pistache_test
(
http_parsing_test
)
pistache_test
(
http_client_test
)
pistache_test
(
net_test
)
\ No newline at end of file
tests/net_test.cc
0 → 100644
View file @
b6491159
#include "gtest/gtest.h"
#include <pistache/net.h>
#include <stdexcept>
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using
namespace
Pistache
;
TEST
(
net_test
,
port_creation
)
{
Port
port1
(
3000
);
ASSERT_FALSE
(
port1
.
isReserved
());
uint16_t
value1
=
port1
;
ASSERT_EQ
(
value1
,
3000
);
Port
port2
(
80
);
ASSERT_TRUE
(
port2
.
isReserved
());
uint16_t
value2
=
port2
;
ASSERT_EQ
(
value2
,
80
);
}
TEST
(
net_test
,
address_creation
)
{
Address
address1
(
"127.0.0.1:8080"
);
ASSERT_EQ
(
address1
.
host
(),
"127.0.0.1"
);
ASSERT_EQ
(
address1
.
port
(),
8080
);
std
::
string
addr
=
"127.0.0.1"
;
Address
address2
(
addr
,
Port
(
8080
));
ASSERT_EQ
(
address2
.
host
(),
"127.0.0.1"
);
ASSERT_EQ
(
address2
.
port
(),
8080
);
Address
address3
(
Ipv4
(
127
,
0
,
0
,
1
),
Port
(
8080
));
ASSERT_EQ
(
address3
.
host
(),
"127.0.0.1"
);
ASSERT_EQ
(
address3
.
port
(),
8080
);
Address
address4
(
Ipv4
::
any
(),
Port
(
8080
));
ASSERT_EQ
(
address4
.
host
(),
"0.0.0.0"
);
ASSERT_EQ
(
address4
.
port
(),
8080
);
}
TEST
(
net_test
,
invalid_address
)
{
ASSERT_THROW
(
Address
(
"127.0.0.1"
),
std
::
invalid_argument
);
ASSERT_THROW
(
Address
(
"127.0.0.1:9999999"
),
std
::
invalid_argument
);
ASSERT_THROW
(
Address
(
"127.0.0.1:"
),
std
::
invalid_argument
);
ASSERT_THROW
(
Address
(
"127.0.0.1:-10"
),
std
::
invalid_argument
);
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment