aboutsummaryrefslogtreecommitdiff
path: root/src/unittest/test_socket.cpp
blob: 33e568e79bd4b94a12bc9275c1fc8b6722a9f607 (plain)
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
145
146
147
148
149
150
151
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "test.h"

#include "log.h"
#include "socket.h"
#include "settings.h"

class TestSocket : public TestBase {
public:
	TestSocket()
	{
		if (INTERNET_SIMULATOR == false)
			TestManager::registerTestModule(this);
	}

	const char *getName() { return "TestSocket"; }

	void runTests(IGameDef *gamedef);

	void testIPv4Socket();
	void testIPv6Socket();

	static const int port = 30003;
};

static TestSocket g_test_instance;

void TestSocket::runTests(IGameDef *gamedef)
{
	TEST(testIPv4Socket);

	if (g_settings->getBool("enable_ipv6"))
		TEST(testIPv6Socket);
}

////////////////////////////////////////////////////////////////////////////////

void TestSocket::testIPv4Socket()
{
	Address address(0, 0, 0, 0, port);
	Address bind_addr(0, 0, 0, 0, port);

	/*
	 * Try to use the bind_address for servers with no localhost address
	 * For example: FreeBSD jails
	 */
	std::string bind_str = g_settings->get("bind_address");
	try {
		bind_addr.Resolve(bind_str.c_str());

		if (!bind_addr.isIPv6()) {
			address = bind_addr;
		}
	} catch (ResolveError &e) {
	}

	UDPSocket socket(false);
	socket.Bind(address);

	const char sendbuffer[] = "hello world!";
	/*
	 * If there is a bind address, use it.
	 * It's useful in container environments
	 */
	if (address != Address(0, 0, 0, 0, port))
		socket.Send(address, sendbuffer, sizeof(sendbuffer));
	else
		socket.Send(Address(127, 0, 0, 1, port), sendbuffer, sizeof(sendbuffer));

	sleep_ms(50);

	char rcvbuffer[256] = { 0 };
	Address sender;
	for (;;) {
		if (socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer)) < 0)
			break;
	}
	//FIXME: This fails on some systems
	UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer)) == 0);

	if (address != Address(0, 0, 0, 0, port)) {
		UASSERT(sender.getAddress().sin_addr.s_addr ==
				address.getAddress().sin_addr.s_addr);
	} else {
		UASSERT(sender.getAddress().sin_addr.s_addr ==
				Address(127, 0, 0, 1, 0).getAddress().sin_addr.s_addr);
	}
}

void TestSocket::testIPv6Socket()
{
	Address address6((IPv6AddressBytes *)NULL, port);
	UDPSocket socket6;

	if (!socket6.init(true, true)) {
		/* Note: Failing to create an IPv6 socket is not technically an
		   error because the OS may not support IPv6 or it may
		   have been disabled. IPv6 is not /required/ by
		   minetest and therefore this should not cause the unit
		   test to fail
		*/
		dstream << "WARNING: IPv6 socket creation failed (unit test)"
			<< std::endl;
		return;
	}

	const char sendbuffer[] = "hello world!";
	IPv6AddressBytes bytes;
	bytes.bytes[15] = 1;

	socket6.Bind(address6);

	try {
		socket6.Send(Address(&bytes, port), sendbuffer, sizeof(sendbuffer));

		sleep_ms(50);

		char rcvbuffer[256] = { 0 };
		Address sender;

		for(;;) {
			if (socket6.Receive(sender, rcvbuffer, sizeof(rcvbuffer)) < 0)
				break;
		}
		//FIXME: This fails on some systems
		UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer)) == 0);
		UASSERT(memcmp(sender.getAddress6().sin6_addr.s6_addr,
				Address(&bytes, 0).getAddress6().sin6_addr.s6_addr, 16) == 0);
	} catch (SendFailedException &e) {
		errorstream << "IPv6 support enabled but not available!"
					<< std::endl;
	}
}
hl kwd">wgettext("New Password"); Environment->addStaticText(text, rect, false, true, this, -1); delete[] text; } { core::rect<s32> rect(0, 0, 230, 30); rect += topleft_client + v2s32(160, ypos); gui::IGUIEditBox *e = Environment->addEditBox(L"", rect, true, this, ID_newPassword1); e->setPasswordBox(true); } ypos += 50; { core::rect<s32> rect(0, 0, 150, 20); rect += topleft_client + v2s32(25, ypos+6); text = wgettext("Confirm Password"); Environment->addStaticText(text, rect, false, true, this, -1); delete[] text; } { core::rect<s32> rect(0, 0, 230, 30); rect += topleft_client + v2s32(160, ypos); gui::IGUIEditBox *e = Environment->addEditBox(L"", rect, true, this, ID_newPassword2); e->setPasswordBox(true); } ypos += 50; { core::rect<s32> rect(0, 0, 140, 30); rect = rect + v2s32(size.X/2-140/2, ypos); text = wgettext("Change"); Environment->addButton(rect, this, ID_change, text); delete[] text; } ypos += 50; { core::rect<s32> rect(0, 0, 300, 20); rect += topleft_client + v2s32(35, ypos); text = wgettext("Passwords do not match!"); IGUIElement *e = Environment->addStaticText( text, rect, false, true, this, ID_message); e->setVisible(false); delete[] text; } } void GUIPasswordChange::drawMenu() { gui::IGUISkin* skin = Environment->getSkin(); if (!skin) return; video::IVideoDriver* driver = Environment->getVideoDriver(); video::SColor bgcolor(140,0,0,0); driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect); gui::IGUIElement::draw(); } bool GUIPasswordChange::acceptInput() { std::wstring oldpass; std::wstring newpass; gui::IGUIElement *e; e = getElementFromId(ID_oldPassword); if(e != NULL) oldpass = e->getText(); e = getElementFromId(ID_newPassword1); if(e != NULL) newpass = e->getText(); e = getElementFromId(ID_newPassword2); if(e != NULL && newpass != e->getText()) { e = getElementFromId(ID_message); if(e != NULL) e->setVisible(true); return false; } m_client->sendChangePassword(wide_to_utf8(oldpass), wide_to_utf8(newpass)); return true; } bool GUIPasswordChange::OnEvent(const SEvent& event) { if(event.EventType==EET_KEY_INPUT_EVENT) { if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown) { quitMenu(); return true; } if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown) { if(acceptInput()) quitMenu(); return true; } } if(event.EventType==EET_GUI_EVENT) { if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST && isVisible()) { if(!canTakeFocus(event.GUIEvent.Element)) { dstream<<"GUIPasswordChange: Not allowing focus change." <<std::endl; // Returning true disables focus change return true; } } if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED) { switch(event.GUIEvent.Caller->getID()) { case ID_change: if(acceptInput()) quitMenu(); return true; } } if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER) { switch(event.GUIEvent.Caller->getID()) { case ID_oldPassword: case ID_newPassword1: case ID_newPassword2: if(acceptInput()) quitMenu(); return true; } } } return Parent ? Parent->OnEvent(event) : false; }