summaryrefslogtreecommitdiff
path: root/indra/newview/tests/lllogininstance_test.cpp
blob: ef93586c6e852c6b713f477639184321e6a02368 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
 * @file   lllogininstance_test.cpp
 * @brief  Test for lllogininstance.cpp.
 * 
 * $LicenseInfo:firstyear=2008&license=viewergpl$
 * Copyright (c) 2008, Linden Research, Inc.
 * $/LicenseInfo$
 */

// Precompiled header
#include "../llviewerprecompiledheaders.h"
// Own header
#include "../lllogininstance.h"
// STL headers
// std headers
// external library headers
// other Linden headers
#include "../test/lltut.h"
#include "llevents.h"

#if defined(LL_WINDOWS)
#pragma warning(disable: 4355)      // using 'this' in base-class ctor initializer expr
#endif

// Constants
const std::string VIEWERLOGIN_URI("viewerlogin_uri");
const std::string VIEWERLOGIN_GRIDLABEL("viewerlogin_grid");

const std::string APPVIEWER_SERIALNUMBER("appviewer_serialno");

// Link seams.

//-----------------------------------------------------------------------------
static LLEventStream gTestPump("test_pump");

#include "lllogin.h"
static std::string gLoginURI;
static LLSD gLoginCreds;
static bool gDisconnectCalled = false;
class LLLogin::Impl
{
};
LLLogin::LLLogin() {}
LLLogin::~LLLogin() {}
LLEventPump& LLLogin::getEventPump() { return gTestPump; }
void LLLogin::connect(const std::string& uri, const LLSD& credentials) 
{
	gLoginURI = uri;
	gLoginCreds = credentials;
}

void LLLogin::disconnect() 
{
	gDisconnectCalled = true;
}

//-----------------------------------------------------------------------------
#include "../llviewernetwork.h"
unsigned char gMACAddress[MAC_ADDRESS_BYTES] = {'1','2','3','4','5','6'};

LLViewerLogin::LLViewerLogin() : mGridChoice(GRID_INFO_NONE) {}
LLViewerLogin::~LLViewerLogin() {}
void LLViewerLogin::getLoginURIs(std::vector<std::string>& uris) const 
{
	uris.push_back(VIEWERLOGIN_URI);
}
std::string LLViewerLogin::getGridLabel() const { return VIEWERLOGIN_GRIDLABEL; }

//-----------------------------------------------------------------------------
#include "../llviewercontrol.h"
LLControlGroup gSavedSettings("Global");
std::string gCurrentVersion = "invalid_version";

LLControlGroup::LLControlGroup(const std::string& name) :
	LLInstanceTracker<LLControlGroup, std::string>(name){}
LLControlGroup::~LLControlGroup() {}
void LLControlGroup::setBOOL(const std::string& name, BOOL val) {}
BOOL LLControlGroup::getBOOL(const std::string& name) { return FALSE; }
F32 LLControlGroup::getF32(const std::string& name) { return 0.0f; }
U32 LLControlGroup::saveToFile(const std::string& filename, BOOL nondefault_only) { return 1; }
void LLControlGroup::setString(const std::string& name, const std::string& val) {}
std::string LLControlGroup::getString(const std::string& name) { return "test_string"; }
BOOL LLControlGroup::declareBOOL(const std::string& name, BOOL initial_val, const std::string& comment, BOOL persist) { return TRUE; }
BOOL LLControlGroup::declareString(const std::string& name, const std::string &initial_val, const std::string& comment, BOOL persist) { return TRUE; }

#include "lluicolortable.h"
void LLUIColorTable::saveUserSettings(void)const {}

//-----------------------------------------------------------------------------
#include "../llurlsimstring.h"
LLURLSimString LLURLSimString::sInstance;
bool LLURLSimString::parse() { return true; }

//-----------------------------------------------------------------------------
#include "llnotifications.h"
#include "llfloaterreg.h"
static std::string gTOSType;
static LLEventPump * gTOSReplyPump = NULL;

//static
LLFloater* LLFloaterReg::showInstance(const std::string& name, const LLSD& key, BOOL focus)
{
	gTOSType = name;
	gTOSReplyPump = &LLEventPumps::instance().obtain(key["reply_pump"]);
	return NULL;
}

//-----------------------------------------------------------------------------
// LLNotifications
class MockNotifications : public LLNotificationsInterface
{
	boost::function<void (const LLSD&, const LLSD&)> mResponder;
	int mAddedCount;

public: 
	MockNotifications() :
		mResponder(0),
		mAddedCount(0)
	{
	}

	virtual ~MockNotifications() {}

	/* virtual */ LLNotificationPtr add(
					const std::string& name,
					const LLSD& substitutions,
					const LLSD& payload, 
					LLNotificationFunctorRegistry::ResponseFunctor functor)
	{
		mResponder = functor;
		mAddedCount++;
		return LLNotificationPtr((LLNotification*)NULL);
	}

	void sendYesResponse()
	{
		LLSD notification;
		LLSD response;
		response = 1;
		mResponder(notification, response);
	}

	void sendNoResponse()
	{
		LLSD notification;
		LLSD response;
		response = 2;
		mResponder(notification, response);
	}

	void sendBogusResponse()
	{
		LLSD notification;
		LLSD response;
		response = 666;
		mResponder(notification, response);
	}

	int addedCount() { return mAddedCount; }
};

S32 LLNotification::getSelectedOption(const LLSD& notification, const LLSD& response)
{
	return response.asInteger();
}

// misc
std::string xml_escape_string(const std::string& in)
{
	return in;
}

/*****************************************************************************
*   TUT
*****************************************************************************/
namespace tut
{
    struct lllogininstance_data
    {
		lllogininstance_data() : logininstance(LLLoginInstance::getInstance())
		{
			// Global initialization
			gLoginURI.clear();
			gLoginCreds.clear();
			gDisconnectCalled = false;

			gTOSType = ""; // Set to invalid value.
			gTOSReplyPump = 0; // clear the callback.


			gSavedSettings.declareBOOL("NoInventoryLibrary", FALSE, "", FALSE);
			gSavedSettings.declareBOOL("ConnectAsGod", FALSE, "", FALSE);
			gSavedSettings.declareBOOL("UseDebugMenus", FALSE, "", FALSE);
			gSavedSettings.declareBOOL("ForceMandatoryUpdate", FALSE, "", FALSE);
			gSavedSettings.declareString("ClientSettingsFile", "test_settings.xml", "", FALSE);
			gSavedSettings.declareString("VersionChannelName", "test_version_string", "", FALSE);
			gSavedSettings.declareString("NextLoginLocation", "", "", FALSE);
			gSavedSettings.declareBOOL("LoginLastLocation", FALSE, "", FALSE);

			credentials["first"] = "testfirst";
			credentials["last"] = "testlast";
			credentials["passwd"] = "testpass";

			logininstance->setNotificationsInterface(&notifications);
		}

		LLLoginInstance* logininstance;
		LLSD credentials;
		MockNotifications notifications;
    };

    typedef test_group<lllogininstance_data> lllogininstance_group;
    typedef lllogininstance_group::object lllogininstance_object;
    lllogininstance_group llsdmgr("lllogininstance");

    template<> template<>
    void lllogininstance_object::test<1>()
    {
		set_test_name("Test Simple Success And Disconnect");

		// Test default connect.
		logininstance->connect(credentials);

		ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI); 

		// Dummy success response.
		LLSD response;
		response["state"] = "online";
		response["change"] = "connect";
		response["progress"] = 1.0;
		response["transfer_rate"] = 7;
		response["data"] = "test_data";

		gTestPump.post(response);

		ensure("Success response", logininstance->authSuccess());
		ensure_equals("Test Response Data", logininstance->getResponse().asString(), "test_data");

		logininstance->disconnect();

		ensure_equals("Called Login Module Disconnect", gDisconnectCalled, true);

		response.clear();
		response["state"] = "offline";
		response["change"] = "disconnect";
		response["progress"] = 0.0;
		response["transfer_rate"] = 0;
		response["data"] = "test_data";

		gTestPump.post(response);

		ensure("Disconnected", !(logininstance->authSuccess()));
    }

    template<> template<>
    void lllogininstance_object::test<2>()
    {
		set_test_name("Test User TOS/Critical message Interaction");

		const std::string test_uri = "testing-uri";

		// Test default connect.
		logininstance->connect(test_uri, credentials);

		// connect should call LLLogin::connect to init gLoginURI and gLoginCreds.
		ensure_equals("Default connect uri", gLoginURI, "testing-uri"); 
		ensure_equals("Default for agree to tos", gLoginCreds["params"]["agree_to_tos"].asBoolean(), false);
		ensure_equals("Default for read critical", gLoginCreds["params"]["read_critical"].asBoolean(), false);

		// TOS failure response.
		LLSD response;
		response["state"] = "offline";
		response["change"] = "fail.login";
		response["progress"] = 0.0;
		response["transfer_rate"] = 7;
		response["data"]["reason"] = "tos";
		gTestPump.post(response);

		ensure_equals("TOS Dialog type", gTOSType, "message_tos");
		ensure("TOS callback given", gTOSReplyPump != 0);
		gTOSReplyPump->post(false); // Call callback denying TOS.
		ensure("No TOS, failed auth", logininstance->authFailure());

		// Start again.
		logininstance->connect(test_uri, credentials);
		gTestPump.post(response); // Fail for tos again.
		gTOSReplyPump->post(true); // Accept tos, should reconnect w/ agree_to_tos.
		ensure_equals("Accepted agree to tos", gLoginCreds["params"]["agree_to_tos"].asBoolean(), true);
		ensure("Incomplete login status", !logininstance->authFailure() && !logininstance->authSuccess());
	
		// Fail connection, attempt connect again.
		// The new request should have reset agree to tos to default.
		response["data"]["reason"] = "key"; // bad creds.
		gTestPump.post(response);
		ensure("TOS auth failure", logininstance->authFailure());

		logininstance->connect(test_uri, credentials);
		ensure_equals("Reset to default for agree to tos", gLoginCreds["params"]["agree_to_tos"].asBoolean(), false);

		// Critical Message failure response.
		logininstance->connect(test_uri, credentials);
		response["data"]["reason"] = "critical"; // Change response to "critical message"
		gTestPump.post(response);

		ensure_equals("TOS Dialog type", gTOSType, "message_critical");
		ensure("TOS callback given", gTOSReplyPump != 0);
		gTOSReplyPump->post(true); 
		ensure_equals("Accepted read critical message", gLoginCreds["params"]["read_critical"].asBoolean(), true);
		ensure("Incomplete login status", !logininstance->authFailure() && !logininstance->authSuccess());

		// Fail then attempt new connection
		response["data"]["reason"] = "key"; // bad creds.
		gTestPump.post(response);
		ensure("TOS auth failure", logininstance->authFailure());
		logininstance->connect(test_uri, credentials);
		ensure_equals("Default for agree to tos", gLoginCreds["params"]["read_critical"].asBoolean(), false);
	}

    template<> template<>
    void lllogininstance_object::test<3>()
    {
		set_test_name("Test Mandatory Update User Accepts");

		// Part 1 - Mandatory Update, with User accepts response.
		// Test connect with update needed.
		logininstance->connect(credentials);

		ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI); 

		// Update needed failure response.
		LLSD response;
		response["state"] = "offline";
		response["change"] = "fail.login";
		response["progress"] = 0.0;
		response["transfer_rate"] = 7;
		response["data"]["reason"] = "update";
		gTestPump.post(response);

		ensure_equals("Notification added", notifications.addedCount(), 1);

		notifications.sendYesResponse();

		ensure("Disconnected", !(logininstance->authSuccess()));
	}

	template<> template<>
    void lllogininstance_object::test<4>()
    {
		set_test_name("Test Mandatory Update User Decline");

		// Test connect with update needed.
		logininstance->connect(credentials);

		ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI); 

		// Update needed failure response.
		LLSD response;
		response["state"] = "offline";
		response["change"] = "fail.login";
		response["progress"] = 0.0;
		response["transfer_rate"] = 7;
		response["data"]["reason"] = "update";
		gTestPump.post(response);

		ensure_equals("Notification added", notifications.addedCount(), 1);
		notifications.sendNoResponse();

		ensure("Disconnected", !(logininstance->authSuccess()));
	}

	template<> template<>
    void lllogininstance_object::test<6>()
    {
		set_test_name("Test Optional Update User Accept");

		// Part 3 - Mandatory Update, with bogus response.
		// Test connect with update needed.
		logininstance->connect(credentials);

		ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI); 

		// Update needed failure response.
		LLSD response;
		response["state"] = "offline";
		response["change"] = "fail.login";
		response["progress"] = 0.0;
		response["transfer_rate"] = 7;
		response["data"]["reason"] = "optional";
		gTestPump.post(response);

		ensure_equals("Notification added", notifications.addedCount(), 1);
		notifications.sendYesResponse();

		ensure("Disconnected", !(logininstance->authSuccess()));
	}

	template<> template<>
    void lllogininstance_object::test<7>()
    {
		set_test_name("Test Optional Update User Denies");

		// Part 3 - Mandatory Update, with bogus response.
		// Test connect with update needed.
		logininstance->connect(credentials);

		ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI); 

		// Update needed failure response.
		LLSD response;
		response["state"] = "offline";
		response["change"] = "fail.login";
		response["progress"] = 0.0;
		response["transfer_rate"] = 7;
		response["data"]["reason"] = "optional";
		gTestPump.post(response);

		ensure_equals("Notification added", notifications.addedCount(), 1);
		notifications.sendNoResponse();

		// User skips, should be reconnecting.
		ensure_equals("reconnect uri", gLoginURI, VIEWERLOGIN_URI); 
		ensure_equals("skipping optional update", gLoginCreds["params"]["skipoptional"].asBoolean(), true); 
	}
}