PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. // Precompiled header
  27. #include "../llviewerprecompiledheaders.h"
  28. // Own header
  29. #include "../llsecapi.h"
  30. #include "../llviewernetwork.h"
  31. #include "../lllogininstance.h"
  32. // STL headers
  33. // std headers
  34. // external library headers
  35. // other Linden headers
  36. #include "../test/lltut.h"
  37. #include "llevents.h"
  38. #if defined(LL_WINDOWS)
  39. #pragma warning(disable: 4355) // using 'this' in base-class ctor initializer expr
  40. #pragma warning(disable: 4702) // disable 'unreachable code' so we can safely use skip().
  41. #endif
  42. // Constants
  43. const std::string VIEWERLOGIN_URI("viewerlogin_uri");
  44. const std::string VIEWERLOGIN_GRIDLABEL("viewerlogin_grid");
  45. const std::string APPVIEWER_SERIALNUMBER("appviewer_serialno");
  46. const std::string VIEWERLOGIN_CHANNEL("invalid_channel");
  47. const std::string VIEWERLOGIN_VERSION_CHANNEL("invalid_version");
  48. // Link seams.
  49. //-----------------------------------------------------------------------------
  50. static LLEventStream gTestPump("test_pump");
  51. #include "../llslurl.h"
  52. #include "../llstartup.h"
  53. LLSLURL LLStartUp::sStartSLURL;
  54. #include "lllogin.h"
  55. static std::string gLoginURI;
  56. static LLSD gLoginCreds;
  57. static bool gDisconnectCalled = false;
  58. #include "../llviewerwindow.h"
  59. void LLViewerWindow::setShowProgress(BOOL show) {}
  60. LLProgressView * LLViewerWindow::getProgressView(void) const { return 0; }
  61. LLViewerWindow* gViewerWindow;
  62. class LLLogin::Impl
  63. {
  64. };
  65. LLLogin::LLLogin() {}
  66. LLLogin::~LLLogin() {}
  67. LLEventPump& LLLogin::getEventPump() { return gTestPump; }
  68. void LLLogin::connect(const std::string& uri, const LLSD& credentials)
  69. {
  70. gLoginURI = uri;
  71. gLoginCreds = credentials;
  72. }
  73. void LLLogin::disconnect()
  74. {
  75. gDisconnectCalled = true;
  76. }
  77. LLSD LLCredential::getLoginParams()
  78. {
  79. LLSD result = LLSD::emptyMap();
  80. // legacy credential
  81. result["passwd"] = "$1$testpasssd";
  82. result["first"] = "myfirst";
  83. result["last"] ="mylast";
  84. return result;
  85. }
  86. void LLCredential::identifierType(std::string &idType)
  87. {
  88. }
  89. void LLCredential::authenticatorType(std::string &idType)
  90. {
  91. }
  92. //-----------------------------------------------------------------------------
  93. #include "../llviewernetwork.h"
  94. LLGridManager::~LLGridManager()
  95. {
  96. }
  97. void LLGridManager::addGrid(LLSD& grid_data)
  98. {
  99. }
  100. LLGridManager::LLGridManager()
  101. :
  102. mIsInProductionGrid(false)
  103. {
  104. }
  105. void LLGridManager::getLoginURIs(std::vector<std::string>& uris)
  106. {
  107. uris.push_back(VIEWERLOGIN_URI);
  108. }
  109. void LLGridManager::addSystemGrid(const std::string& label,
  110. const std::string& name,
  111. const std::string& login,
  112. const std::string& helper,
  113. const std::string& login_page,
  114. const std::string& login_id)
  115. {
  116. }
  117. std::map<std::string, std::string> LLGridManager::getKnownGrids(bool favorite_only)
  118. {
  119. std::map<std::string, std::string> result;
  120. return result;
  121. }
  122. void LLGridManager::setGridChoice(const std::string& grid_name)
  123. {
  124. }
  125. bool LLGridManager::isInProductionGrid()
  126. {
  127. return false;
  128. }
  129. void LLGridManager::saveFavorites()
  130. {}
  131. std::string LLGridManager::getSLURLBase(const std::string& grid_name)
  132. {
  133. return "myslurl";
  134. }
  135. std::string LLGridManager::getAppSLURLBase(const std::string& grid_name)
  136. {
  137. return "myappslurl";
  138. }
  139. //-----------------------------------------------------------------------------
  140. #include "../llviewercontrol.h"
  141. LLControlGroup gSavedSettings("Global");
  142. LLControlGroup::LLControlGroup(const std::string& name) :
  143. LLInstanceTracker<LLControlGroup, std::string>(name){}
  144. LLControlGroup::~LLControlGroup() {}
  145. void LLControlGroup::setBOOL(const std::string& name, BOOL val) {}
  146. BOOL LLControlGroup::getBOOL(const std::string& name) { return FALSE; }
  147. F32 LLControlGroup::getF32(const std::string& name) { return 0.0f; }
  148. U32 LLControlGroup::saveToFile(const std::string& filename, BOOL nondefault_only) { return 1; }
  149. void LLControlGroup::setString(const std::string& name, const std::string& val) {}
  150. std::string LLControlGroup::getString(const std::string& name) { return "test_string"; }
  151. BOOL LLControlGroup::declareBOOL(const std::string& name, BOOL initial_val, const std::string& comment, BOOL persist) { return TRUE; }
  152. BOOL LLControlGroup::declareString(const std::string& name, const std::string &initial_val, const std::string& comment, BOOL persist) { return TRUE; }
  153. #include "lluicolortable.h"
  154. void LLUIColorTable::saveUserSettings(void)const {}
  155. //-----------------------------------------------------------------------------
  156. #include "../llversioninfo.h"
  157. const std::string &LLVersionInfo::getChannelAndVersion() { return VIEWERLOGIN_VERSION_CHANNEL; }
  158. const std::string &LLVersionInfo::getChannel() { return VIEWERLOGIN_CHANNEL; }
  159. //-----------------------------------------------------------------------------
  160. #include "../llappviewer.h"
  161. void LLAppViewer::forceQuit(void) {}
  162. LLAppViewer * LLAppViewer::sInstance = 0;
  163. //-----------------------------------------------------------------------------
  164. #include "llnotificationsutil.h"
  165. LLNotificationPtr LLNotificationsUtil::add(const std::string& name,
  166. const LLSD& substitutions,
  167. const LLSD& payload,
  168. boost::function<void (const LLSD&, const LLSD&)> functor) { return LLNotificationPtr((LLNotification*)0); }
  169. //-----------------------------------------------------------------------------
  170. #include "llupdaterservice.h"
  171. std::string const & LLUpdaterService::pumpName(void)
  172. {
  173. static std::string wakka = "wakka wakka wakka";
  174. return wakka;
  175. }
  176. bool LLUpdaterService::updateReadyToInstall(void) { return false; }
  177. void LLUpdaterService::initialize(const std::string& protocol_version,
  178. const std::string& url,
  179. const std::string& path,
  180. const std::string& channel,
  181. const std::string& version) {}
  182. void LLUpdaterService::setCheckPeriod(unsigned int seconds) {}
  183. void LLUpdaterService::startChecking(bool install_if_ready) {}
  184. void LLUpdaterService::stopChecking() {}
  185. bool LLUpdaterService::isChecking() { return false; }
  186. LLUpdaterService::eUpdaterState LLUpdaterService::getState() { return INITIAL; }
  187. std::string LLUpdaterService::updatedVersion() { return ""; }
  188. //-----------------------------------------------------------------------------
  189. #include "llnotifications.h"
  190. #include "llfloaterreg.h"
  191. static std::string gTOSType;
  192. static LLEventPump * gTOSReplyPump = NULL;
  193. //static
  194. LLFloater* LLFloaterReg::showInstance(const std::string& name, const LLSD& key, BOOL focus)
  195. {
  196. gTOSType = name;
  197. gTOSReplyPump = &LLEventPumps::instance().obtain(key["reply_pump"]);
  198. return NULL;
  199. }
  200. //----------------------------------------------------------------------------
  201. #include "../llprogressview.h"
  202. void LLProgressView::setText(std::string const &){}
  203. void LLProgressView::setPercent(float){}
  204. void LLProgressView::setMessage(std::string const &){}
  205. //-----------------------------------------------------------------------------
  206. // LLNotifications
  207. class MockNotifications : public LLNotificationsInterface
  208. {
  209. boost::function<void (const LLSD&, const LLSD&)> mResponder;
  210. int mAddedCount;
  211. public:
  212. MockNotifications() :
  213. mResponder(0),
  214. mAddedCount(0)
  215. {
  216. }
  217. virtual ~MockNotifications() {}
  218. /* virtual */ LLNotificationPtr add(
  219. const std::string& name,
  220. const LLSD& substitutions,
  221. const LLSD& payload,
  222. LLNotificationFunctorRegistry::ResponseFunctor functor)
  223. {
  224. mResponder = functor;
  225. mAddedCount++;
  226. return LLNotificationPtr((LLNotification*)NULL);
  227. }
  228. void sendYesResponse()
  229. {
  230. LLSD notification;
  231. LLSD response;
  232. response = 1;
  233. mResponder(notification, response);
  234. }
  235. void sendNoResponse()
  236. {
  237. LLSD notification;
  238. LLSD response;
  239. response = 2;
  240. mResponder(notification, response);
  241. }
  242. void sendBogusResponse()
  243. {
  244. LLSD notification;
  245. LLSD response;
  246. response = 666;
  247. mResponder(notification, response);
  248. }
  249. int addedCount() { return mAddedCount; }
  250. };
  251. S32 LLNotification::getSelectedOption(const LLSD& notification, const LLSD& response)
  252. {
  253. return response.asInteger();
  254. }
  255. //-----------------------------------------------------------------------------
  256. #include "../llmachineid.h"
  257. unsigned char gMACAddress[MAC_ADDRESS_BYTES] = {77,21,46,31,89,2};
  258. S32 LLMachineID::getUniqueID(unsigned char *unique_id, size_t len)
  259. {
  260. memcpy(unique_id, gMACAddress, len);
  261. return 1;
  262. }
  263. //-----------------------------------------------------------------------------
  264. // misc
  265. std::string xml_escape_string(const std::string& in)
  266. {
  267. return in;
  268. }
  269. /*****************************************************************************
  270. * TUT
  271. *****************************************************************************/
  272. namespace tut
  273. {
  274. struct lllogininstance_data
  275. {
  276. lllogininstance_data() : logininstance(LLLoginInstance::getInstance())
  277. {
  278. // Global initialization
  279. gLoginURI.clear();
  280. gLoginCreds.clear();
  281. gDisconnectCalled = false;
  282. gTOSType = ""; // Set to invalid value.
  283. gTOSReplyPump = 0; // clear the callback.
  284. gSavedSettings.declareBOOL("NoInventoryLibrary", FALSE, "", FALSE);
  285. gSavedSettings.declareBOOL("ConnectAsGod", FALSE, "", FALSE);
  286. gSavedSettings.declareBOOL("UseDebugMenus", FALSE, "", FALSE);
  287. gSavedSettings.declareBOOL("ForceMandatoryUpdate", FALSE, "", FALSE);
  288. gSavedSettings.declareString("ClientSettingsFile", "test_settings.xml", "", FALSE);
  289. gSavedSettings.declareString("NextLoginLocation", "", "", FALSE);
  290. gSavedSettings.declareBOOL("LoginLastLocation", FALSE, "", FALSE);
  291. LLSD authenticator = LLSD::emptyMap();
  292. LLSD identifier = LLSD::emptyMap();
  293. identifier["type"] = "agent";
  294. identifier["first_name"] = "testfirst";
  295. identifier["last_name"] = "testlast";
  296. authenticator["passwd"] = "testpass";
  297. agentCredential = new LLCredential();
  298. agentCredential->setCredentialData(identifier, authenticator);
  299. authenticator = LLSD::emptyMap();
  300. identifier = LLSD::emptyMap();
  301. identifier["type"] = "account";
  302. identifier["username"] = "testuser";
  303. authenticator["secret"] = "testsecret";
  304. accountCredential = new LLCredential();
  305. accountCredential->setCredentialData(identifier, authenticator);
  306. logininstance->setNotificationsInterface(&notifications);
  307. }
  308. LLLoginInstance* logininstance;
  309. LLPointer<LLCredential> agentCredential;
  310. LLPointer<LLCredential> accountCredential;
  311. MockNotifications notifications;
  312. };
  313. typedef test_group<lllogininstance_data> lllogininstance_group;
  314. typedef lllogininstance_group::object lllogininstance_object;
  315. lllogininstance_group llsdmgr("LLLoginInstance");
  316. template<> template<>
  317. void lllogininstance_object::test<1>()
  318. {
  319. set_test_name("Test Simple Success And Disconnect");
  320. // Test default connect.
  321. logininstance->connect(agentCredential);
  322. ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI);
  323. // Dummy success response.
  324. LLSD response;
  325. response["state"] = "online";
  326. response["change"] = "connect";
  327. response["progress"] = 1.0;
  328. response["transfer_rate"] = 7;
  329. response["data"] = "test_data";
  330. gTestPump.post(response);
  331. ensure("Success response", logininstance->authSuccess());
  332. ensure_equals("Test Response Data", logininstance->getResponse().asString(), "test_data");
  333. logininstance->disconnect();
  334. ensure_equals("Called Login Module Disconnect", gDisconnectCalled, true);
  335. response.clear();
  336. response["state"] = "offline";
  337. response["change"] = "disconnect";
  338. response["progress"] = 0.0;
  339. response["transfer_rate"] = 0;
  340. response["data"] = "test_data";
  341. gTestPump.post(response);
  342. ensure("Disconnected", !(logininstance->authSuccess()));
  343. }
  344. template<> template<>
  345. void lllogininstance_object::test<2>()
  346. {
  347. set_test_name("Test User TOS/Critical message Interaction");
  348. const std::string test_uri = "testing-uri";
  349. // Test default connect.
  350. logininstance->connect(test_uri, agentCredential);
  351. // connect should call LLLogin::connect to init gLoginURI and gLoginCreds.
  352. ensure_equals("Default connect uri", gLoginURI, "testing-uri");
  353. ensure_equals("Default for agree to tos", gLoginCreds["params"]["agree_to_tos"].asBoolean(), false);
  354. ensure_equals("Default for read critical", gLoginCreds["params"]["read_critical"].asBoolean(), false);
  355. // TOS failure response.
  356. LLSD response;
  357. response["state"] = "offline";
  358. response["change"] = "fail.login";
  359. response["progress"] = 0.0;
  360. response["transfer_rate"] = 7;
  361. response["data"]["reason"] = "tos";
  362. gTestPump.post(response);
  363. ensure_equals("TOS Dialog type", gTOSType, "message_tos");
  364. ensure("TOS callback given", gTOSReplyPump != 0);
  365. gTOSReplyPump->post(false); // Call callback denying TOS.
  366. ensure("No TOS, failed auth", logininstance->authFailure());
  367. // Start again.
  368. logininstance->connect(test_uri, agentCredential);
  369. gTestPump.post(response); // Fail for tos again.
  370. gTOSReplyPump->post(true); // Accept tos, should reconnect w/ agree_to_tos.
  371. ensure_equals("Accepted agree to tos", gLoginCreds["params"]["agree_to_tos"].asBoolean(), true);
  372. ensure("Incomplete login status", !logininstance->authFailure() && !logininstance->authSuccess());
  373. // Fail connection, attempt connect again.
  374. // The new request should have reset agree to tos to default.
  375. response["data"]["reason"] = "key"; // bad creds.
  376. gTestPump.post(response);
  377. ensure("TOS auth failure", logininstance->authFailure());
  378. logininstance->connect(test_uri, agentCredential);
  379. ensure_equals("Reset to default for agree to tos", gLoginCreds["params"]["agree_to_tos"].asBoolean(), false);
  380. // Critical Message failure response.
  381. logininstance->connect(test_uri, agentCredential);
  382. response["data"]["reason"] = "critical"; // Change response to "critical message"
  383. gTestPump.post(response);
  384. ensure_equals("TOS Dialog type", gTOSType, "message_critical");
  385. ensure("TOS callback given", gTOSReplyPump != 0);
  386. gTOSReplyPump->post(true);
  387. ensure_equals("Accepted read critical message", gLoginCreds["params"]["read_critical"].asBoolean(), true);
  388. ensure("Incomplete login status", !logininstance->authFailure() && !logininstance->authSuccess());
  389. // Fail then attempt new connection
  390. response["data"]["reason"] = "key"; // bad creds.
  391. gTestPump.post(response);
  392. ensure("TOS auth failure", logininstance->authFailure());
  393. logininstance->connect(test_uri, agentCredential);
  394. ensure_equals("Default for agree to tos", gLoginCreds["params"]["read_critical"].asBoolean(), false);
  395. }
  396. template<> template<>
  397. void lllogininstance_object::test<3>()
  398. {
  399. skip();
  400. set_test_name("Test Mandatory Update User Accepts");
  401. // Part 1 - Mandatory Update, with User accepts response.
  402. // Test connect with update needed.
  403. logininstance->connect(agentCredential);
  404. ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI);
  405. // Update needed failure response.
  406. LLSD response;
  407. response["state"] = "offline";
  408. response["change"] = "fail.login";
  409. response["progress"] = 0.0;
  410. response["transfer_rate"] = 7;
  411. response["data"]["reason"] = "update";
  412. gTestPump.post(response);
  413. ensure_equals("Notification added", notifications.addedCount(), 1);
  414. notifications.sendYesResponse();
  415. ensure("Disconnected", !(logininstance->authSuccess()));
  416. }
  417. template<> template<>
  418. void lllogininstance_object::test<4>()
  419. {
  420. skip();
  421. set_test_name("Test Mandatory Update User Decline");
  422. // Test connect with update needed.
  423. logininstance->connect(agentCredential);
  424. ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI);
  425. // Update needed failure response.
  426. LLSD response;
  427. response["state"] = "offline";
  428. response["change"] = "fail.login";
  429. response["progress"] = 0.0;
  430. response["transfer_rate"] = 7;
  431. response["data"]["reason"] = "update";
  432. gTestPump.post(response);
  433. ensure_equals("Notification added", notifications.addedCount(), 1);
  434. notifications.sendNoResponse();
  435. ensure("Disconnected", !(logininstance->authSuccess()));
  436. }
  437. template<> template<>
  438. void lllogininstance_object::test<6>()
  439. {
  440. set_test_name("Test Optional Update User Accept");
  441. // Part 3 - Mandatory Update, with bogus response.
  442. // Test connect with update needed.
  443. logininstance->connect(agentCredential);
  444. ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI);
  445. // Update needed failure response.
  446. LLSD response;
  447. response["state"] = "offline";
  448. response["change"] = "fail.login";
  449. response["progress"] = 0.0;
  450. response["transfer_rate"] = 7;
  451. response["data"]["reason"] = "optional";
  452. gTestPump.post(response);
  453. ensure_equals("Notification added", notifications.addedCount(), 1);
  454. notifications.sendYesResponse();
  455. ensure("Disconnected", !(logininstance->authSuccess()));
  456. }
  457. template<> template<>
  458. void lllogininstance_object::test<7>()
  459. {
  460. set_test_name("Test Optional Update User Denies");
  461. // Part 3 - Mandatory Update, with bogus response.
  462. // Test connect with update needed.
  463. logininstance->connect(agentCredential);
  464. ensure_equals("Default connect uri", gLoginURI, VIEWERLOGIN_URI);
  465. // Update needed failure response.
  466. LLSD response;
  467. response["state"] = "offline";
  468. response["change"] = "fail.login";
  469. response["progress"] = 0.0;
  470. response["transfer_rate"] = 7;
  471. response["data"]["reason"] = "optional";
  472. gTestPump.post(response);
  473. ensure_equals("Notification added", notifications.addedCount(), 1);
  474. notifications.sendNoResponse();
  475. // User skips, should be reconnecting.
  476. ensure_equals("reconnect uri", gLoginURI, VIEWERLOGIN_URI);
  477. ensure_equals("skipping optional update", gLoginCreds["params"]["skipoptional"].asBoolean(), true);
  478. }
  479. }