Add some unit tests for InternetAddress

This commit is contained in:
Eugen Wissner 2017-06-03 15:18:53 +02:00
parent 260937e4fb
commit dc39efd316

View File

@ -410,7 +410,8 @@ else version (Windows)
return result == TRUE; return result == TRUE;
} }
OverlappedConnectedSocket endAccept(SocketState overlapped) @nogc @trusted OverlappedConnectedSocket endAccept(SocketState overlapped)
@nogc @trusted
{ {
scope (exit) scope (exit)
{ {
@ -480,11 +481,7 @@ else version (D_Ddoc)
* *
* Postcondition: $(D_INLINECODE result >= 0). * Postcondition: $(D_INLINECODE result >= 0).
*/ */
int endReceive(SocketState overlapped) @nogc @trusted int endReceive(SocketState overlapped) @nogc @trusted;
out (count)
{
assert(count >= 0);
}
/** /**
* Sends data asynchronously to a connected socket. * Sends data asynchronously to a connected socket.
@ -515,11 +512,7 @@ else version (D_Ddoc)
* *
* Postcondition: $(D_INLINECODE result >= 0). * Postcondition: $(D_INLINECODE result >= 0).
*/ */
int endSend(SocketState overlapped) @nogc @trusted int endSend(SocketState overlapped) @nogc @trusted;
out (count)
{
assert(count >= 0);
}
} }
/** /**
@ -581,6 +574,57 @@ struct Linger
/// discarded. /// discarded.
LingerField l_linger; LingerField l_linger;
/**
* If $(D_PARAM timeout) is `0`, linger is disabled, otherwise enables the
* linger and sets the timeout.
*
* Params:
* timeout = Timeout, in seconds.
*/
this(const ushort timeout)
{
time = timeout;
}
///
unittest
{
{
auto linger = Linger(5);
assert(linger.enabled);
assert(linger.time == 5);
}
{
auto linger = Linger(0);
assert(!linger.enabled);
}
{ // Default constructor.
Linger linger;
assert(!linger.enabled);
}
}
/**
* System dependent constructor.
*
* Params:
* l_onoff = $(D_PSYMBOL l_onoff) value.
* l_linger = $(D_PSYMBOL l_linger) value.
*/
this(LingerField l_onoff, LingerField l_linger)
{
this.l_onoff = l_onoff;
this.l_linger = l_linger;
}
///
unittest
{
auto linger = Linger(1, 5);
assert(linger.l_onoff == 1);
assert(linger.l_linger == 5);
}
/** /**
* Params: * Params:
* value = Whether to linger after the socket is closed. * value = Whether to linger after the socket is closed.
@ -616,8 +660,9 @@ struct Linger
* Params: * Params:
* timeout = Timeout period, in seconds. * timeout = Timeout period, in seconds.
*/ */
@property void time(ushort timeout) pure nothrow @safe @nogc @property void time(const ushort timeout) pure nothrow @safe @nogc
{ {
this.l_onoff = timeout > 0;
this.l_linger = timeout; this.l_linger = timeout;
} }
} }
@ -1420,12 +1465,9 @@ class InternetAddress : Address
} }
const ushort port_; const ushort port_;
enum enum ushort anyPort = 0;
{
anyPort = 0,
}
this(in string host, ushort port = anyPort) @nogc this(string host, const ushort port = anyPort) @nogc
{ {
if (getaddrinfoPointer is null || freeaddrinfoPointer is null) if (getaddrinfoPointer is null || freeaddrinfoPointer is null)
{ {
@ -1433,7 +1475,7 @@ class InternetAddress : Address
"Address info lookup is not available on this system"); "Address info lookup is not available on this system");
} }
addrinfo* ai_res; addrinfo* ai_res;
port_ = port; this.port_ = port;
// Make C-string from host. // Make C-string from host.
auto node = cast(char[]) allocator.allocate(host.length + 1); auto node = cast(char[]) allocator.allocate(host.length + 1);
@ -1449,16 +1491,17 @@ class InternetAddress : Address
const(char)* servicePointer; const(char)* servicePointer;
if (port) if (port)
{ {
ushort originalPort = port;
ushort start; ushort start;
for (ushort j = 10, i = 4; i > 0; j *= 10, --i) for (ushort j = 10, i = 4; i > 0; j *= 10, --i)
{ {
ushort rest = port % 10; ushort rest = originalPort % 10;
if (rest != 0) if (rest != 0)
{ {
service[i] = cast(char) (rest + '0'); service[i] = cast(char) (rest + '0');
start = i; start = i;
} }
port /= 10; originalPort /= 10;
} }
servicePointer = service[start .. $].ptr; servicePointer = service[start .. $].ptr;
} }
@ -1484,6 +1527,17 @@ class InternetAddress : Address
} }
} }
///
unittest
{
auto address = defaultAllocator.make!InternetAddress("127.0.0.1");
assert(address.port == InternetAddress.anyPort);
assert(address.name !is null);
assert(address.family == AddressFamily.inet);
defaultAllocator.dispose(address);
}
/** /**
* Returns: Pointer to underlying $(D_PSYMBOL sockaddr) structure. * Returns: Pointer to underlying $(D_PSYMBOL sockaddr) structure.
*/ */
@ -1521,6 +1575,15 @@ class InternetAddress : Address
{ {
return port_; return port_;
} }
///
unittest
{
auto address = defaultAllocator.make!InternetAddress("127.0.0.1",
cast(ushort) 1234);
assert(address.port == 1234);
defaultAllocator.dispose(address);
}
} }
/** /**