LoginServiceTests.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Text.RegularExpressions;
  32. using NUnit.Framework;
  33. using NUnit.Framework.SyntaxHelpers;
  34. using Nwc.XmlRpc;
  35. using OpenSim.Framework.Communications.Cache;
  36. using OpenSim.Framework.Communications.Services;
  37. using OpenSim.Region.Communications.Local;
  38. using OpenSim.Tests.Common.Setup;
  39. using OpenSim.Tests.Common.Mock;
  40. using OpenSim.Client.Linden;
  41. using OpenSim.Tests.Common;
  42. using OpenSim.Services.Interfaces;
  43. using OpenMetaverse;
  44. namespace OpenSim.Framework.Communications.Tests
  45. {
  46. /// <summary>
  47. /// Test the login service. For now, most of this will be done through the LocalLoginService as LoginService
  48. /// is abstract
  49. /// </summary>
  50. [TestFixture]
  51. public class LoginServiceTests
  52. {
  53. private string m_firstName = "Adam";
  54. private string m_lastName = "West";
  55. private string m_regionExternalName = "localhost";
  56. private IPEndPoint m_capsEndPoint;
  57. private TestCommunicationsManager m_commsManager;
  58. private TestLoginToRegionConnector m_regionConnector;
  59. private LocalUserServices m_localUserServices;
  60. private LoginService m_loginService;
  61. private UserProfileData m_userProfileData;
  62. private TestScene m_testScene;
  63. [SetUp]
  64. public void SetUpLoginEnviroment()
  65. {
  66. m_capsEndPoint = new IPEndPoint(IPAddress.Loopback, 9123);
  67. m_commsManager = new TestCommunicationsManager(new NetworkServersInfo(42, 43));
  68. m_regionConnector = new TestLoginToRegionConnector();
  69. m_testScene = SceneSetupHelpers.SetupScene(m_commsManager, "");
  70. m_regionConnector.AddRegion(new RegionInfo(42, 43, m_capsEndPoint, m_regionExternalName));
  71. //IInventoryService m_inventoryService = new MockInventoryService();
  72. m_localUserServices = (LocalUserServices) m_commsManager.UserService;
  73. m_localUserServices.AddUser(m_firstName,m_lastName,"boingboing","[email protected]",42,43);
  74. m_loginService = new LLStandaloneLoginService((UserManagerBase) m_localUserServices, "Hello folks", m_testScene.InventoryService,
  75. m_commsManager.NetworkServersInfo, true, new LibraryRootFolder(String.Empty), m_regionConnector);
  76. m_userProfileData = m_localUserServices.GetUserProfile(m_firstName, m_lastName);
  77. }
  78. /// <summary>
  79. /// Test the normal response to a login. Does not test authentication.
  80. /// </summary>
  81. [Test]
  82. public void T010_TestUnauthenticatedLogin()
  83. {
  84. TestHelper.InMethod();
  85. // We want to use our own LoginService for this test, one that
  86. // doesn't require authentication.
  87. new LLStandaloneLoginService(
  88. (UserManagerBase)m_commsManager.UserService, "Hello folks", new MockInventoryService(),
  89. m_commsManager.NetworkServersInfo, false, new LibraryRootFolder(String.Empty), m_regionConnector);
  90. Hashtable loginParams = new Hashtable();
  91. loginParams["first"] = m_firstName;
  92. loginParams["last"] = m_lastName;
  93. loginParams["passwd"] = "boingboing";
  94. ArrayList sendParams = new ArrayList();
  95. sendParams.Add(loginParams);
  96. sendParams.Add(m_capsEndPoint); // is this parameter correct?
  97. sendParams.Add(new Uri("http://localhost:8002/")); // is this parameter correct?
  98. XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams);
  99. IPAddress tmpLocal = Util.GetLocalHost();
  100. IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80);
  101. XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd);
  102. Hashtable responseData = (Hashtable)response.Value;
  103. Assert.That(responseData["first_name"], Is.EqualTo(m_firstName));
  104. Assert.That(responseData["last_name"], Is.EqualTo(m_lastName));
  105. Assert.That(
  106. responseData["circuit_code"], Is.GreaterThanOrEqualTo(0) & Is.LessThanOrEqualTo(Int32.MaxValue));
  107. Regex capsSeedPattern
  108. = new Regex("^http://"
  109. + NetworkUtil.GetHostFor(tmpLocal, m_regionExternalName)
  110. + ":9000/CAPS/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}0000/$");
  111. Assert.That(capsSeedPattern.IsMatch((string)responseData["seed_capability"]), Is.True);
  112. }
  113. [Test]
  114. public void T011_TestAuthenticatedLoginSuccess()
  115. {
  116. TestHelper.InMethod();
  117. // TODO: Not check inventory part of response yet.
  118. // TODO: Not checking all of login response thoroughly yet.
  119. // 1) Test for positive authentication
  120. Hashtable loginParams = new Hashtable();
  121. loginParams["first"] = m_firstName;
  122. loginParams["last"] = m_lastName;
  123. loginParams["passwd"] = "boingboing";
  124. ArrayList sendParams = new ArrayList();
  125. sendParams.Add(loginParams);
  126. sendParams.Add(m_capsEndPoint); // is this parameter correct?
  127. sendParams.Add(new Uri("http://localhost:8002/")); // is this parameter correct?
  128. XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams);
  129. IPAddress tmpLocal = Util.GetLocalHost();
  130. IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80);
  131. XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd);
  132. Hashtable responseData = (Hashtable)response.Value;
  133. UserAgentData uagent = m_userProfileData.CurrentAgent;
  134. Assert.That(uagent,Is.Not.Null);
  135. Assert.That(responseData["first_name"], Is.Not.Null);
  136. Assert.That(responseData["first_name"], Is.EqualTo(m_firstName));
  137. Assert.That(responseData["last_name"], Is.EqualTo(m_lastName));
  138. Assert.That(responseData["agent_id"], Is.EqualTo(uagent.ProfileID.ToString()));
  139. Assert.That(responseData["session_id"], Is.EqualTo(uagent.SessionID.ToString()));
  140. Assert.That(responseData["secure_session_id"], Is.EqualTo(uagent.SecureSessionID.ToString()));
  141. ArrayList invlibroot = (ArrayList) responseData["inventory-lib-root"];
  142. Hashtable invlibroothash = (Hashtable) invlibroot[0];
  143. Assert.That(invlibroothash["folder_id"],Is.EqualTo("00000112-000f-0000-0000-000100bba000"));
  144. Assert.That(
  145. responseData["circuit_code"], Is.GreaterThanOrEqualTo(0) & Is.LessThanOrEqualTo(Int32.MaxValue));
  146. Assert.That(responseData["message"], Is.EqualTo("Hello folks"));
  147. Assert.That(responseData["buddy-list"], Is.Empty);
  148. Assert.That(responseData["start_location"], Is.EqualTo("last"));
  149. Regex capsSeedPattern
  150. = new Regex("^http://"
  151. + NetworkUtil.GetHostFor(tmpLocal, m_regionExternalName)
  152. + ":9000/CAPS/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}0000/$");
  153. Assert.That(capsSeedPattern.IsMatch((string)responseData["seed_capability"]), Is.True);
  154. }
  155. [Test]
  156. public void T012_TestAuthenticatedLoginForBuddies()
  157. {
  158. TestHelper.InMethod();
  159. // 1.1) Test for budddies!
  160. m_localUserServices.AddUser("Friend","Number1","boingboing","[email protected]",42,43);
  161. m_localUserServices.AddUser("Friend","Number2","boingboing","[email protected]",42,43);
  162. UserProfileData friend1 = m_localUserServices.GetUserProfile("Friend","Number1");
  163. UserProfileData friend2 = m_localUserServices.GetUserProfile("Friend","Number2");
  164. m_localUserServices.AddNewUserFriend(friend1.ID,m_userProfileData.ID,1);
  165. m_localUserServices.AddNewUserFriend(friend1.ID,friend2.ID,2);
  166. Hashtable loginParams = new Hashtable();
  167. loginParams["first"] = "Friend";
  168. loginParams["last"] = "Number1";
  169. loginParams["passwd"] = "boingboing";
  170. ArrayList sendParams = new ArrayList();
  171. sendParams.Add(loginParams);
  172. sendParams.Add(m_capsEndPoint); // is this parameter correct?
  173. sendParams.Add(new Uri("http://localhost:8002/")); // is this parameter correct?
  174. XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams);
  175. IPAddress tmpLocal = Util.GetLocalHost();
  176. IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80);
  177. XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd);
  178. Hashtable responseData = (Hashtable)response.Value;
  179. ArrayList friendslist = (ArrayList) responseData["buddy-list"];
  180. Assert.That(friendslist,Is.Not.Null);
  181. Hashtable buddy1 = (Hashtable) friendslist[0];
  182. Hashtable buddy2 = (Hashtable) friendslist[1];
  183. Assert.That(friendslist.Count, Is.EqualTo(2));
  184. Assert.That(m_userProfileData.ID.ToString(), Is.EqualTo(buddy1["buddy_id"]) | Is.EqualTo(buddy2["buddy_id"]));
  185. Assert.That(friend2.ID.ToString(), Is.EqualTo(buddy1["buddy_id"]) | Is.EqualTo(buddy2["buddy_id"]));
  186. }
  187. [Test]
  188. public void T020_TestAuthenticatedLoginBadUsername()
  189. {
  190. TestHelper.InMethod();
  191. // 2) Test for negative authentication
  192. //
  193. string error_auth_message = "Could not authenticate your avatar. Please check your username and password, and check the grid if problems persist.";
  194. //string error_region_unavailable = "The region you are attempting to log into is not responding. Please select another region and try again.";
  195. // 2.1) Test for wrong user name
  196. Hashtable loginParams = new Hashtable();
  197. loginParams["first"] = m_lastName;
  198. loginParams["last"] = m_firstName;
  199. loginParams["passwd"] = "boingboing";
  200. ArrayList sendParams = new ArrayList();
  201. sendParams.Add(loginParams);
  202. sendParams.Add(m_capsEndPoint); // is this parameter correct?
  203. sendParams.Add(new Uri("http://localhost:8002/")); // is this parameter correct?
  204. XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams);
  205. IPAddress tmpLocal = Util.GetLocalHost();
  206. IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80);
  207. XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd);
  208. Hashtable responseData = (Hashtable)response.Value;
  209. Assert.That(responseData["message"], Is.EqualTo(error_auth_message));
  210. }
  211. [Test]
  212. public void T021_TestAuthenticatedLoginBadPassword()
  213. {
  214. TestHelper.InMethod();
  215. string error_auth_message = "Could not authenticate your avatar. Please check your username and password, and check the grid if problems persist.";
  216. // 2.2) Test for wrong password
  217. Hashtable loginParams = new Hashtable();
  218. loginParams["first"] = "Friend";
  219. loginParams["last"] = "Number2";
  220. loginParams["passwd"] = "boing";
  221. ArrayList sendParams = new ArrayList();
  222. sendParams.Add(loginParams);
  223. sendParams.Add(m_capsEndPoint); // is this parameter correct?
  224. sendParams.Add(new Uri("http://localhost:8002/")); // is this parameter correct?
  225. XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams);
  226. IPAddress tmpLocal = Util.GetLocalHost();
  227. IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80);
  228. XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd);
  229. Hashtable responseData = (Hashtable)response.Value;
  230. Assert.That(responseData["message"], Is.EqualTo(error_auth_message));
  231. }
  232. [Test]
  233. public void T022_TestAuthenticatedLoginBadXml()
  234. {
  235. TestHelper.InMethod();
  236. string error_xml_message = "Error connecting to grid. Could not percieve credentials from login XML.";
  237. // 2.3) Bad XML
  238. Hashtable loginParams = new Hashtable();
  239. loginParams["first"] = "Friend";
  240. loginParams["banana"] = "Banana";
  241. loginParams["passwd"] = "boingboing";
  242. ArrayList sendParams = new ArrayList();
  243. sendParams.Add(loginParams);
  244. sendParams.Add(m_capsEndPoint); // is this parameter correct?
  245. sendParams.Add(new Uri("http://localhost:8002/")); // is this parameter correct?
  246. XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams);
  247. IPAddress tmpLocal = Util.GetLocalHost();
  248. IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80);
  249. XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd);
  250. Hashtable responseData = (Hashtable)response.Value;
  251. Assert.That(responseData["message"], Is.EqualTo(error_xml_message));
  252. }
  253. // [Test]
  254. // Commenting out test now that LLStandAloneLoginService no longer replies with message in this case.
  255. // Kept the code for future test with grid mode, which will keep this behavior.
  256. public void T023_TestAuthenticatedLoginAlreadyLoggedIn()
  257. {
  258. TestHelper.InMethod();
  259. //Console.WriteLine("Starting T023_TestAuthenticatedLoginAlreadyLoggedIn()");
  260. //log4net.Config.XmlConfigurator.Configure();
  261. string error_already_logged = "You appear to be already logged in. " +
  262. "If this is not the case please wait for your session to timeout. " +
  263. "If this takes longer than a few minutes please contact the grid owner. " +
  264. "Please wait 5 minutes if you are going to connect to a region nearby to the region you were at previously.";
  265. // 2.4) Already logged in and sucessfull post login
  266. Hashtable loginParams = new Hashtable();
  267. loginParams["first"] = "Adam";
  268. loginParams["last"] = "West";
  269. loginParams["passwd"] = "boingboing";
  270. ArrayList sendParams = new ArrayList();
  271. sendParams.Add(loginParams);
  272. sendParams.Add(m_capsEndPoint); // is this parameter correct?
  273. sendParams.Add(new Uri("http://localhost:8002/")); // is this parameter correct?
  274. // First we log in.
  275. XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams);
  276. IPAddress tmpLocal = Util.GetLocalHost();
  277. IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80);
  278. XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd);
  279. Hashtable responseData = (Hashtable)response.Value;
  280. Assert.That(responseData["message"], Is.EqualTo("Hello folks"));
  281. // Then we try again, this time expecting failure.
  282. request = new XmlRpcRequest("login_to_simulator", sendParams);
  283. response = m_loginService.XmlRpcLoginMethod(request, tmpEnd);
  284. responseData = (Hashtable)response.Value;
  285. Assert.That(responseData["message"], Is.EqualTo(error_already_logged));
  286. // Finally the third time we should be able to get right back in.
  287. request = new XmlRpcRequest("login_to_simulator", sendParams);
  288. response = m_loginService.XmlRpcLoginMethod(request, tmpEnd);
  289. responseData = (Hashtable)response.Value;
  290. Assert.That(responseData["message"], Is.EqualTo("Hello folks"));
  291. //Console.WriteLine("Finished T023_TestAuthenticatedLoginAlreadyLoggedIn()");
  292. }
  293. [TearDown]
  294. public void TearDown()
  295. {
  296. try
  297. {
  298. if (MainServer.Instance != null) MainServer.Instance.Stop();
  299. } catch (NullReferenceException)
  300. {}
  301. }
  302. public class TestLoginToRegionConnector : ILoginServiceToRegionsConnector
  303. {
  304. private List<RegionInfo> m_regionsList = new List<RegionInfo>();
  305. public void AddRegion(RegionInfo regionInfo)
  306. {
  307. lock (m_regionsList)
  308. {
  309. if (!m_regionsList.Contains(regionInfo))
  310. {
  311. m_regionsList.Add(regionInfo);
  312. }
  313. }
  314. }
  315. public void LogOffUserFromGrid(ulong regionHandle, OpenMetaverse.UUID AvatarID, OpenMetaverse.UUID RegionSecret, string message)
  316. {
  317. }
  318. public bool NewUserConnection(ulong regionHandle, AgentCircuitData agent, out string reason)
  319. {
  320. reason = String.Empty;
  321. lock (m_regionsList)
  322. {
  323. foreach (RegionInfo regInfo in m_regionsList)
  324. {
  325. if (regInfo.RegionHandle == regionHandle)
  326. return true;
  327. }
  328. }
  329. reason = "Region not found";
  330. return false;
  331. }
  332. public RegionInfo RequestClosestRegion(string region)
  333. {
  334. lock (m_regionsList)
  335. {
  336. foreach (RegionInfo regInfo in m_regionsList)
  337. {
  338. if (regInfo.RegionName == region)
  339. return regInfo;
  340. }
  341. }
  342. return null;
  343. }
  344. public RegionInfo RequestNeighbourInfo(OpenMetaverse.UUID regionID)
  345. {
  346. lock (m_regionsList)
  347. {
  348. foreach (RegionInfo regInfo in m_regionsList)
  349. {
  350. if (regInfo.RegionID == regionID)
  351. return regInfo;
  352. }
  353. }
  354. return null;
  355. }
  356. public RegionInfo RequestNeighbourInfo(ulong regionHandle)
  357. {
  358. lock (m_regionsList)
  359. {
  360. foreach (RegionInfo regInfo in m_regionsList)
  361. {
  362. if (regInfo.RegionHandle == regionHandle)
  363. return regInfo;
  364. }
  365. }
  366. return null;
  367. }
  368. }
  369. }
  370. }