1//
2// DialogSocketTest.cpp
3//
4// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "DialogSocketTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "EchoServer.h"
15#include "Poco/Net/DialogSocket.h"
16#include "Poco/Net/SocketAddress.h"
17#include <cstring>
18
19
20using Poco::Net::DialogSocket;
21using Poco::Net::SocketAddress;
22
23
24DialogSocketTest::DialogSocketTest(const std::string& name): CppUnit::TestCase(name)
25{
26}
27
28
29DialogSocketTest::~DialogSocketTest()
30{
31}
32
33
34void DialogSocketTest::testDialogSocket()
35{
36 EchoServer echoServer;
37 DialogSocket ds;
38 ds.connect(SocketAddress("127.0.0.1", echoServer.port()));
39
40 ds.sendMessage("Hello, world!");
41 std::string str;
42 ds.receiveMessage(str);
43 assertTrue (str == "Hello, world!");
44
45 ds.sendString("Hello, World!\n");
46 ds.receiveMessage(str);
47 assertTrue (str == "Hello, World!");
48
49 ds.sendMessage("EHLO", "appinf.com");
50 ds.receiveMessage(str);
51 assertTrue (str == "EHLO appinf.com");
52
53 ds.sendMessage("PUT", "local.txt", "remote.txt");
54 ds.receiveMessage(str);
55 assertTrue (str == "PUT local.txt remote.txt");
56
57 ds.sendMessage("220 Hello, world!");
58 int status = ds.receiveStatusMessage(str);
59 assertTrue (status == 220);
60 assertTrue (str == "220 Hello, world!");
61
62 ds.sendString("220-line1\r\n220 line2\r\n");
63 status = ds.receiveStatusMessage(str);
64 assertTrue (status == 220);
65 assertTrue (str == "220-line1\n220 line2");
66
67 ds.sendString("220-line1\r\nline2\r\n220 line3\r\n");
68 status = ds.receiveStatusMessage(str);
69 assertTrue (status == 220);
70 assertTrue (str == "220-line1\nline2\n220 line3");
71
72 ds.sendMessage("Hello, world!");
73 status = ds.receiveStatusMessage(str);
74 assertTrue (status == 0);
75 assertTrue (str == "Hello, world!");
76
77 ds.sendString("Header\nMore Bytes");
78 status = ds.receiveStatusMessage(str);
79 assertTrue (status == 0);
80 assertTrue (str == "Header");
81 char buffer[16];
82 int n = ds.receiveRawBytes(buffer, sizeof(buffer));
83 assertTrue (n == 10);
84 assertTrue (std::memcmp(buffer, "More Bytes", 10) == 0);
85
86 ds.sendString("Even More Bytes");
87 n = ds.receiveRawBytes(buffer, sizeof(buffer));
88 assertTrue (n == 15);
89 assertTrue (std::memcmp(buffer, "Even More Bytes", 15) == 0);
90}
91
92
93void DialogSocketTest::setUp()
94{
95}
96
97
98void DialogSocketTest::tearDown()
99{
100}
101
102
103CppUnit::Test* DialogSocketTest::suite()
104{
105 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("DialogSocketTest");
106
107 CppUnit_addTest(pSuite, DialogSocketTest, testDialogSocket);
108
109 return pSuite;
110}
111