PageRenderTime 69ms CodeModel.GetById 23ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/tests/lllogininstance_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 587 lines | 404 code | 108 blank | 75 comment | 4 complexity | 5c86ce066ea70f522d7bc6a09ffb8b59 MD5 | raw file
  1/**
  2 * @file   lllogininstance_test.cpp
  3 * @brief  Test for lllogininstance.cpp.
  4 * 
  5 * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27// Precompiled header
 28#include "../llviewerprecompiledheaders.h"
 29// Own header
 30#include "../llsecapi.h"
 31#include "../llviewernetwork.h"
 32#include "../lllogininstance.h"
 33
 34// STL headers
 35// std headers
 36// external library headers
 37// other Linden headers
 38#include "../test/lltut.h"
 39#include "llevents.h"
 40
 41#if defined(LL_WINDOWS)
 42#pragma warning(disable: 4355)      // using 'this' in base-class ctor initializer expr
 43#pragma warning(disable: 4702)      // disable 'unreachable code' so we can safely use skip().
 44#endif
 45
 46// Constants
 47const std::string VIEWERLOGIN_URI("viewerlogin_uri");
 48const std::string VIEWERLOGIN_GRIDLABEL("viewerlogin_grid");
 49
 50const std::string APPVIEWER_SERIALNUMBER("appviewer_serialno");
 51
 52const std::string VIEWERLOGIN_CHANNEL("invalid_channel");
 53const std::string VIEWERLOGIN_VERSION_CHANNEL("invalid_version");
 54
 55// Link seams.
 56
 57//-----------------------------------------------------------------------------
 58static LLEventStream gTestPump("test_pump");
 59
 60#include "../llslurl.h"
 61#include "../llstartup.h"
 62LLSLURL LLStartUp::sStartSLURL;
 63
 64#include "lllogin.h"
 65
 66static std::string gLoginURI;
 67static LLSD gLoginCreds;
 68static bool gDisconnectCalled = false;
 69
 70#include "../llviewerwindow.h"
 71void LLViewerWindow::setShowProgress(BOOL show) {}
 72LLProgressView * LLViewerWindow::getProgressView(void) const { return 0; }
 73
 74LLViewerWindow* gViewerWindow;
 75
 76class LLLogin::Impl
 77{
 78};
 79LLLogin::LLLogin() {}
 80LLLogin::~LLLogin() {}
 81LLEventPump& LLLogin::getEventPump() { return gTestPump; }
 82void LLLogin::connect(const std::string& uri, const LLSD& credentials) 
 83{
 84	gLoginURI = uri;
 85	gLoginCreds = credentials;
 86}
 87
 88void LLLogin::disconnect() 
 89{
 90	gDisconnectCalled = true;
 91}
 92
 93LLSD LLCredential::getLoginParams()
 94{
 95	LLSD result = LLSD::emptyMap();
 96
 97	// legacy credential
 98	result["passwd"] = "$1$testpasssd";
 99	result["first"] = "myfirst";
100	result["last"] ="mylast";
101	return result;
102}
103void LLCredential::identifierType(std::string &idType)
104{
105}
106
107void LLCredential::authenticatorType(std::string &idType)
108{
109}
110
111//-----------------------------------------------------------------------------
112#include "../llviewernetwork.h"
113LLGridManager::~LLGridManager()
114{
115}
116
117void LLGridManager::addGrid(LLSD& grid_data)
118{
119}
120LLGridManager::LLGridManager()
121:
122	mIsInProductionGrid(false)
123{	
124}
125
126void LLGridManager::getLoginURIs(std::vector<std::string>& uris)
127{
128	uris.push_back(VIEWERLOGIN_URI);
129}
130
131void LLGridManager::addSystemGrid(const std::string& label, 
132								  const std::string& name, 
133								  const std::string& login, 
134								  const std::string& helper,
135								  const std::string& login_page,
136								  const std::string& login_id)
137{
138}
139std::map<std::string, std::string> LLGridManager::getKnownGrids(bool favorite_only)
140{
141	std::map<std::string, std::string> result;
142	return result;
143}
144
145void LLGridManager::setGridChoice(const std::string& grid_name)
146{
147}
148
149bool LLGridManager::isInProductionGrid()
150{
151	return false;
152}
153
154void LLGridManager::saveFavorites()
155{}
156std::string LLGridManager::getSLURLBase(const std::string& grid_name)
157{
158	return "myslurl";
159}
160std::string LLGridManager::getAppSLURLBase(const std::string& grid_name)
161{
162	return "myappslurl";
163}
164
165//-----------------------------------------------------------------------------
166#include "../llviewercontrol.h"
167LLControlGroup gSavedSettings("Global");
168
169LLControlGroup::LLControlGroup(const std::string& name) :
170	LLInstanceTracker<LLControlGroup, std::string>(name){}
171LLControlGroup::~LLControlGroup() {}
172void LLControlGroup::setBOOL(const std::string& name, BOOL val) {}
173BOOL LLControlGroup::getBOOL(const std::string& name) { return FALSE; }
174F32 LLControlGroup::getF32(const std::string& name) { return 0.0f; }
175U32 LLControlGroup::saveToFile(const std::string& filename, BOOL nondefault_only) { return 1; }
176void LLControlGroup::setString(const std::string& name, const std::string& val) {}
177std::string LLControlGroup::getString(const std::string& name) { return "test_string"; }
178BOOL LLControlGroup::declareBOOL(const std::string& name, BOOL initial_val, const std::string& comment, BOOL persist) { return TRUE; }
179BOOL LLControlGroup::declareString(const std::string& name, const std::string &initial_val, const std::string& comment, BOOL persist) { return TRUE; }
180
181#include "lluicolortable.h"
182void LLUIColorTable::saveUserSettings(void)const {}
183
184//-----------------------------------------------------------------------------
185#include "../llversioninfo.h"
186const std::string &LLVersionInfo::getChannelAndVersion() { return VIEWERLOGIN_VERSION_CHANNEL; }
187const std::string &LLVersionInfo::getChannel() { return VIEWERLOGIN_CHANNEL; }
188
189//-----------------------------------------------------------------------------
190#include "../llappviewer.h"
191void LLAppViewer::forceQuit(void) {}
192LLAppViewer * LLAppViewer::sInstance = 0;
193
194//-----------------------------------------------------------------------------
195#include "llnotificationsutil.h"
196LLNotificationPtr LLNotificationsUtil::add(const std::string& name, 
197					  const LLSD& substitutions, 
198					  const LLSD& payload, 
199					  boost::function<void (const LLSD&, const LLSD&)> functor) { return LLNotificationPtr((LLNotification*)0); }
200
201
202//-----------------------------------------------------------------------------
203#include "llupdaterservice.h"
204
205std::string const & LLUpdaterService::pumpName(void)
206{
207	static std::string wakka = "wakka wakka wakka";
208	return wakka;
209}
210bool LLUpdaterService::updateReadyToInstall(void) { return false; }
211void LLUpdaterService::initialize(const std::string& protocol_version,
212				const std::string& url, 
213				const std::string& path,
214				const std::string& channel,
215								  const std::string& version) {}
216
217void LLUpdaterService::setCheckPeriod(unsigned int seconds) {}
218void LLUpdaterService::startChecking(bool install_if_ready) {}
219void LLUpdaterService::stopChecking() {}
220bool LLUpdaterService::isChecking() { return false; }
221LLUpdaterService::eUpdaterState LLUpdaterService::getState() { return INITIAL; }
222std::string LLUpdaterService::updatedVersion() { return ""; }
223
224//-----------------------------------------------------------------------------
225#include "llnotifications.h"
226#include "llfloaterreg.h"
227static std::string gTOSType;
228static LLEventPump * gTOSReplyPump = NULL;
229
230//static
231LLFloater* LLFloaterReg::showInstance(const std::string& name, const LLSD& key, BOOL focus)
232{
233	gTOSType = name;
234	gTOSReplyPump = &LLEventPumps::instance().obtain(key["reply_pump"]);
235	return NULL;
236}
237
238//----------------------------------------------------------------------------
239#include "../llprogressview.h"
240void LLProgressView::setText(std::string const &){}
241void LLProgressView::setPercent(float){}
242void LLProgressView::setMessage(std::string const &){}
243
244//-----------------------------------------------------------------------------
245// LLNotifications
246class MockNotifications : public LLNotificationsInterface
247{
248	boost::function<void (const LLSD&, const LLSD&)> mResponder;
249	int mAddedCount;
250
251public: 
252	MockNotifications() :
253		mResponder(0),
254		mAddedCount(0)
255	{
256	}
257
258	virtual ~MockNotifications() {}
259
260	/* virtual */ LLNotificationPtr add(
261					const std::string& name,
262					const LLSD& substitutions,
263					const LLSD& payload, 
264					LLNotificationFunctorRegistry::ResponseFunctor functor)
265	{
266		mResponder = functor;
267		mAddedCount++;
268		return LLNotificationPtr((LLNotification*)NULL);
269	}
270
271	void sendYesResponse()
272	{
273		LLSD notification;
274		LLSD response;
275		response = 1;
276		mResponder(notification, response);
277	}
278
279	void sendNoResponse()
280	{
281		LLSD notification;
282		LLSD response;
283		response = 2;
284		mResponder(notification, response);
285	}
286
287	void sendBogusResponse()
288	{
289		LLSD notification;
290		LLSD response;
291		response = 666;
292		mResponder(notification, response);
293	}
294
295	int addedCount() { return mAddedCount; }
296};
297
298S32 LLNotification::getSelectedOption(const LLSD& notification, const LLSD& response)
299{
300	return response.asInteger();
301}
302
303//-----------------------------------------------------------------------------
304#include "../llmachineid.h"
305unsigned char gMACAddress[MAC_ADDRESS_BYTES] = {77,21,46,31,89,2};
306
307S32 LLMachineID::getUniqueID(unsigned char *unique_id, size_t len)
308{
309	memcpy(unique_id, gMACAddress, len);
310	return 1;
311}
312//-----------------------------------------------------------------------------
313// misc
314std::string xml_escape_string(const std::string& in)
315{
316	return in;
317}
318
319/*****************************************************************************
320*   TUT
321*****************************************************************************/
322namespace tut
323{
324    struct lllogininstance_data
325    {
326		lllogininstance_data() : logininstance(LLLoginInstance::getInstance())
327		{
328			// Global initialization
329			gLoginURI.clear();
330			gLoginCreds.clear();
331			gDisconnectCalled = false;
332
333			gTOSType = ""; // Set to invalid value.
334			gTOSReplyPump = 0; // clear the callback.
335
336
337			gSavedSettings.declareBOOL("NoInventoryLibrary", FALSE, "", FALSE);
338			gSavedSettings.declareBOOL("ConnectAsGod", FALSE, "", FALSE);
339			gSavedSettings.declareBOOL("UseDebugMenus", FALSE, "", FALSE);
340			gSavedSettings.declareBOOL("ForceMandatoryUpdate", FALSE, "", FALSE);
341			gSavedSettings.declareString("ClientSettingsFile", "test_settings.xml", "", FALSE);
342			gSavedSettings.declareString("NextLoginLocation", "", "", FALSE);
343			gSavedSettings.declareBOOL("LoginLastLocation", FALSE, "", FALSE);
344
345			LLSD authenticator = LLSD::emptyMap();
346			LLSD identifier = LLSD::emptyMap();
347			identifier["type"] = "agent";
348			identifier["first_name"] = "testfirst";
349			identifier["last_name"] = "testlast";
350			authenticator["passwd"] = "testpass";
351			agentCredential = new LLCredential();
352			agentCredential->setCredentialData(identifier, authenticator);
353			
354			authenticator = LLSD::emptyMap();
355			identifier = LLSD::emptyMap();
356			identifier["type"] = "account";
357			identifier["username"] = "testuser";
358			authenticator["secret"] = "testsecret";
359			accountCredential = new LLCredential();
360			accountCredential->setCredentialData(identifier, authenticator);			
361
362			logininstance->setNotificationsInterface(&notifications);
363		}
364
365		LLLoginInstance* logininstance;
366		LLPointer<LLCredential> agentCredential;
367		LLPointer<LLCredential> accountCredential;
368		MockNotifications notifications;
369    };
370
371    typedef test_group<lllogininstance_data> lllogininstance_group;
372    typedef lllogininstance_group::object lllogininstance_object;
373    lllogininstance_group llsdmgr("LLLoginInstance");
374
375    template<> template<>
376    void lllogininstance_object::test<1>()
377    {
378		set_test_name("Test Simple Success And Disconnect");
379
380		// Test default connect.
381		logininstance->connect(agentCredential);
382
383		ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI); 
384
385		// Dummy success response.
386		LLSD response;
387		response["state"] = "online";
388		response["change"] = "connect";
389		response["progress"] = 1.0;
390		response["transfer_rate"] = 7;
391		response["data"] = "test_data";
392
393		gTestPump.post(response);
394
395		ensure("Success response", logininstance->authSuccess());
396		ensure_equals("Test Response Data", logininstance->getResponse().asString(), "test_data");
397
398		logininstance->disconnect();
399
400		ensure_equals("Called Login Module Disconnect", gDisconnectCalled, true);
401
402		response.clear();
403		response["state"] = "offline";
404		response["change"] = "disconnect";
405		response["progress"] = 0.0;
406		response["transfer_rate"] = 0;
407		response["data"] = "test_data";
408
409		gTestPump.post(response);
410
411		ensure("Disconnected", !(logininstance->authSuccess()));
412    }
413
414    template<> template<>
415    void lllogininstance_object::test<2>()
416    {
417		set_test_name("Test User TOS/Critical message Interaction");
418
419		const std::string test_uri = "testing-uri";
420
421		// Test default connect.
422		logininstance->connect(test_uri, agentCredential);
423
424		// connect should call LLLogin::connect to init gLoginURI and gLoginCreds.
425		ensure_equals("Default connect uri", gLoginURI, "testing-uri"); 
426		ensure_equals("Default for agree to tos", gLoginCreds["params"]["agree_to_tos"].asBoolean(), false);
427		ensure_equals("Default for read critical", gLoginCreds["params"]["read_critical"].asBoolean(), false);
428
429		// TOS failure response.
430		LLSD response;
431		response["state"] = "offline";
432		response["change"] = "fail.login";
433		response["progress"] = 0.0;
434		response["transfer_rate"] = 7;
435		response["data"]["reason"] = "tos";
436		gTestPump.post(response);
437
438		ensure_equals("TOS Dialog type", gTOSType, "message_tos");
439		ensure("TOS callback given", gTOSReplyPump != 0);
440		gTOSReplyPump->post(false); // Call callback denying TOS.
441		ensure("No TOS, failed auth", logininstance->authFailure());
442
443		// Start again.
444		logininstance->connect(test_uri, agentCredential);
445		gTestPump.post(response); // Fail for tos again.
446		gTOSReplyPump->post(true); // Accept tos, should reconnect w/ agree_to_tos.
447		ensure_equals("Accepted agree to tos", gLoginCreds["params"]["agree_to_tos"].asBoolean(), true);
448		ensure("Incomplete login status", !logininstance->authFailure() && !logininstance->authSuccess());
449	
450		// Fail connection, attempt connect again.
451		// The new request should have reset agree to tos to default.
452		response["data"]["reason"] = "key"; // bad creds.
453		gTestPump.post(response);
454		ensure("TOS auth failure", logininstance->authFailure());
455
456		logininstance->connect(test_uri, agentCredential);
457		ensure_equals("Reset to default for agree to tos", gLoginCreds["params"]["agree_to_tos"].asBoolean(), false);
458
459		// Critical Message failure response.
460		logininstance->connect(test_uri, agentCredential);
461		response["data"]["reason"] = "critical"; // Change response to "critical message"
462		gTestPump.post(response);
463
464		ensure_equals("TOS Dialog type", gTOSType, "message_critical");
465		ensure("TOS callback given", gTOSReplyPump != 0);
466		gTOSReplyPump->post(true); 
467		ensure_equals("Accepted read critical message", gLoginCreds["params"]["read_critical"].asBoolean(), true);
468		ensure("Incomplete login status", !logininstance->authFailure() && !logininstance->authSuccess());
469
470		// Fail then attempt new connection
471		response["data"]["reason"] = "key"; // bad creds.
472		gTestPump.post(response);
473		ensure("TOS auth failure", logininstance->authFailure());
474		logininstance->connect(test_uri, agentCredential);
475		ensure_equals("Default for agree to tos", gLoginCreds["params"]["read_critical"].asBoolean(), false);
476	}
477
478    template<> template<>
479    void lllogininstance_object::test<3>()
480    {
481		skip();
482		
483		set_test_name("Test Mandatory Update User Accepts");
484
485		// Part 1 - Mandatory Update, with User accepts response.
486		// Test connect with update needed.
487		logininstance->connect(agentCredential);
488
489		ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI); 
490
491		// Update needed failure response.
492		LLSD response;
493		response["state"] = "offline";
494		response["change"] = "fail.login";
495		response["progress"] = 0.0;
496		response["transfer_rate"] = 7;
497		response["data"]["reason"] = "update";
498		gTestPump.post(response);
499
500		ensure_equals("Notification added", notifications.addedCount(), 1);
501
502		notifications.sendYesResponse();
503
504		ensure("Disconnected", !(logininstance->authSuccess()));
505	}
506
507	template<> template<>
508    void lllogininstance_object::test<4>()
509    {
510		skip();
511		
512		set_test_name("Test Mandatory Update User Decline");
513
514		// Test connect with update needed.
515		logininstance->connect(agentCredential);
516
517		ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI); 
518
519		// Update needed failure response.
520		LLSD response;
521		response["state"] = "offline";
522		response["change"] = "fail.login";
523		response["progress"] = 0.0;
524		response["transfer_rate"] = 7;
525		response["data"]["reason"] = "update";
526		gTestPump.post(response);
527
528		ensure_equals("Notification added", notifications.addedCount(), 1);
529		notifications.sendNoResponse();
530
531		ensure("Disconnected", !(logininstance->authSuccess()));
532	}
533
534	template<> template<>
535    void lllogininstance_object::test<6>()
536    {
537		set_test_name("Test Optional Update User Accept");
538
539		// Part 3 - Mandatory Update, with bogus response.
540		// Test connect with update needed.
541		logininstance->connect(agentCredential);
542
543		ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI); 
544
545		// Update needed failure response.
546		LLSD response;
547		response["state"] = "offline";
548		response["change"] = "fail.login";
549		response["progress"] = 0.0;
550		response["transfer_rate"] = 7;
551		response["data"]["reason"] = "optional";
552		gTestPump.post(response);
553
554		ensure_equals("Notification added", notifications.addedCount(), 1);
555		notifications.sendYesResponse();
556
557		ensure("Disconnected", !(logininstance->authSuccess()));
558	}
559
560	template<> template<>
561    void lllogininstance_object::test<7>()
562    {
563		set_test_name("Test Optional Update User Denies");
564
565		// Part 3 - Mandatory Update, with bogus response.
566		// Test connect with update needed.
567		logininstance->connect(agentCredential);
568
569		ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI); 
570
571		// Update needed failure response.
572		LLSD response;
573		response["state"] = "offline";
574		response["change"] = "fail.login";
575		response["progress"] = 0.0;
576		response["transfer_rate"] = 7;
577		response["data"]["reason"] = "optional";
578		gTestPump.post(response);
579
580		ensure_equals("Notification added", notifications.addedCount(), 1);
581		notifications.sendNoResponse();
582
583		// User skips, should be reconnecting.
584		ensure_equals("reconnect uri", gLoginURI, VIEWERLOGIN_URI); 
585		ensure_equals("skipping optional update", gLoginCreds["params"]["skipoptional"].asBoolean(), true); 
586	}
587}