LoginServiceTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 OpenSim 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.Net;
  30. using System.Text.RegularExpressions;
  31. using System.Xml;
  32. using Nini;
  33. using NUnit.Framework;
  34. using NUnit.Framework.SyntaxHelpers;
  35. using Nwc.XmlRpc;
  36. using OpenSim.Framework.Communications.Cache;
  37. using OpenSim.Region.Communications.Local;
  38. using OpenSim.Tests.Common.Mock;
  39. namespace OpenSim.Framework.Communications.Tests
  40. {
  41. /// <summary>
  42. /// Test the login service. For now, most of this will be done through the LocalLoginService as LoginService
  43. /// is abstract
  44. /// </summary>
  45. [TestFixture]
  46. public class LoginServiceTests
  47. {
  48. /// <summary>
  49. /// Test the normal response to a login. Does not test authentication.
  50. /// </summary>
  51. [Test]
  52. public void T010_NormalLoginResponse()
  53. {
  54. //log4net.Config.XmlConfigurator.Configure();
  55. string firstName = "Timmy";
  56. string lastName = "Mallet";
  57. string regionExternalName = "localhost";
  58. IPEndPoint capsEndPoint = new IPEndPoint(IPAddress.Loopback, 9123);
  59. CommunicationsManager commsManager
  60. = new TestCommunicationsManager(new OpenSim.Framework.NetworkServersInfo(42, 43));
  61. commsManager.GridService.RegisterRegion(
  62. new RegionInfo(42, 43, capsEndPoint, regionExternalName));
  63. commsManager.GridService.RegionLoginsEnabled = true;
  64. LoginService loginService
  65. = new LocalLoginService(
  66. (UserManagerBase)commsManager.UserService, "Hello folks", commsManager.InterServiceInventoryService,
  67. (LocalBackEndServices)commsManager.GridService,
  68. commsManager.NetworkServersInfo, false, new LibraryRootFolder(String.Empty));
  69. Hashtable loginParams = new Hashtable();
  70. loginParams["first"] = firstName;
  71. loginParams["last"] = lastName;
  72. loginParams["passwd"] = "boingboing";
  73. ArrayList sendParams = new ArrayList();
  74. sendParams.Add(loginParams);
  75. XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams);
  76. XmlRpcResponse response = loginService.XmlRpcLoginMethod(request);
  77. Hashtable responseData = (Hashtable)response.Value;
  78. Assert.That(responseData["first_name"], Is.EqualTo(firstName));
  79. Assert.That(responseData["last_name"], Is.EqualTo(lastName));
  80. Assert.That(
  81. responseData["circuit_code"], Is.GreaterThanOrEqualTo(0) & Is.LessThanOrEqualTo(System.Int32.MaxValue));
  82. Regex capsSeedPattern
  83. = new Regex("^http://"
  84. + regionExternalName
  85. + ":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]{8}0000/$");
  86. Assert.That(capsSeedPattern.IsMatch((string)responseData["seed_capability"]), Is.True);
  87. }
  88. [Test]
  89. public void T011_Auth_Login()
  90. {
  91. string firstName = "Adam";
  92. string lastName = "West";
  93. string regionExternalName = "localhost";
  94. IPEndPoint capsEndPoint = new IPEndPoint(IPAddress.Loopback, 9123);
  95. CommunicationsManager commsManager
  96. = new TestCommunicationsManager(new OpenSim.Framework.NetworkServersInfo(42, 43));
  97. commsManager.GridService.RegisterRegion(
  98. new RegionInfo(42, 43, capsEndPoint, regionExternalName));
  99. commsManager.GridService.RegionLoginsEnabled = true;
  100. LocalUserServices lus = (LocalUserServices)commsManager.UserService;
  101. lus.AddUser(firstName,lastName,"boingboing","[email protected]",42,43);
  102. LoginService loginService
  103. = new LocalLoginService(
  104. (UserManagerBase)lus, "Hello folks", commsManager.InterServiceInventoryService,
  105. (LocalBackEndServices)commsManager.GridService,
  106. commsManager.NetworkServersInfo, true, new LibraryRootFolder(String.Empty));
  107. // TODO: Not check inventory part of response yet.
  108. // TODO: Not checking all of login response thoroughly yet.
  109. // 1) Test for positive authentication
  110. Hashtable loginParams = new Hashtable();
  111. loginParams["first"] = firstName;
  112. loginParams["last"] = lastName;
  113. loginParams["passwd"] = "boingboing";
  114. ArrayList sendParams = new ArrayList();
  115. sendParams.Add(loginParams);
  116. XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams);
  117. XmlRpcResponse response = loginService.XmlRpcLoginMethod(request);
  118. Hashtable responseData = (Hashtable)response.Value;
  119. UserProfileData uprof = lus.GetUserProfile(firstName,lastName);
  120. UserAgentData uagent = uprof.CurrentAgent;
  121. Assert.That(uagent,Is.Not.Null);
  122. Assert.That(responseData["first_name"], Is.Not.Null);
  123. Assert.That(responseData["first_name"], Is.EqualTo(firstName));
  124. Assert.That(responseData["last_name"], Is.EqualTo(lastName));
  125. Assert.That(responseData["agent_id"], Is.EqualTo(uagent.ProfileID.ToString()));
  126. Assert.That(responseData["session_id"], Is.EqualTo(uagent.SessionID.ToString()));
  127. Assert.That(responseData["secure_session_id"], Is.EqualTo(uagent.SecureSessionID.ToString()));
  128. ArrayList invlibroot = (ArrayList) responseData["inventory-lib-root"];
  129. Hashtable invlibroothash = (Hashtable) invlibroot[0];
  130. Assert.That(invlibroothash["folder_id"],Is.EqualTo("00000112-000f-0000-0000-000100bba000"));
  131. Assert.That(
  132. responseData["circuit_code"], Is.GreaterThanOrEqualTo(0) & Is.LessThanOrEqualTo(System.Int32.MaxValue));
  133. Assert.That(responseData["message"], Is.EqualTo("Hello folks"));
  134. Assert.That(responseData["buddy-list"], Is.Empty);
  135. Assert.That(responseData["start_location"], Is.EqualTo("last"));
  136. Regex capsSeedPattern
  137. = new Regex("^http://"
  138. + regionExternalName
  139. + ":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]{8}0000/$");
  140. Assert.That(capsSeedPattern.IsMatch((string)responseData["seed_capability"]), Is.True);
  141. // 1.1) Test for budddies!
  142. lus.AddUser("Friend","Number1","boingboing","[email protected]",42,43);
  143. lus.AddUser("Friend","Number2","boingboing","[email protected]",42,43);
  144. UserProfileData friend1 = lus.GetUserProfile("Friend","Number1");
  145. UserProfileData friend2 = lus.GetUserProfile("Friend","Number2");
  146. lus.AddNewUserFriend(friend1.ID,uprof.ID,1);
  147. lus.AddNewUserFriend(friend1.ID,friend2.ID,2);
  148. loginParams = new Hashtable();
  149. loginParams["first"] = "Friend";
  150. loginParams["last"] = "Number1";
  151. loginParams["passwd"] = "boingboing";
  152. sendParams = new ArrayList();
  153. sendParams.Add(loginParams);
  154. request = new XmlRpcRequest("login_to_simulator", sendParams);
  155. response = loginService.XmlRpcLoginMethod(request);
  156. responseData = (Hashtable)response.Value;
  157. ArrayList friendslist = (ArrayList) responseData["buddy-list"];
  158. Assert.That(friendslist,Is.Not.Null);
  159. Hashtable buddy1 = (Hashtable) friendslist[0];
  160. Hashtable buddy2 = (Hashtable) friendslist[1];
  161. Assert.That(friendslist.Count, Is.EqualTo(2));
  162. Assert.That(uprof.ID.ToString(), Is.EqualTo(buddy1["buddy_id"]) | Is.EqualTo(buddy2["buddy_id"]));
  163. Assert.That(friend2.ID.ToString(), Is.EqualTo(buddy1["buddy_id"]) | Is.EqualTo(buddy2["buddy_id"]));
  164. // 2) Test for negative authentication
  165. //
  166. string error_auth_message = "Could not authenticate your avatar. Please check your username and password, and check the grid if problems persist.";
  167. string error_xml_message = "Error connecting to grid. Could not percieve credentials from login XML.";
  168. string error_already_logged = "You appear to be already logged in. " +
  169. "If this is not the case please wait for your session to timeout. " +
  170. "If this takes longer than a few minutes please contact the grid owner. " +
  171. "Please wait 5 minutes if you are going to connect to a region nearby to the region you were at previously.";
  172. //string error_region_unavailable = "The region you are attempting to log into is not responding. Please select another region and try again.";
  173. // 2.1) Test for wrong user name
  174. loginParams = new Hashtable();
  175. loginParams["first"] = lastName;
  176. loginParams["last"] = firstName;
  177. loginParams["passwd"] = "boingboing";
  178. sendParams = new ArrayList();
  179. sendParams.Add(loginParams);
  180. request = new XmlRpcRequest("login_to_simulator", sendParams);
  181. response = loginService.XmlRpcLoginMethod(request);
  182. responseData = (Hashtable)response.Value;
  183. Assert.That(responseData["message"], Is.EqualTo(error_auth_message));
  184. // 2.2) Test for wrong password
  185. loginParams = new Hashtable();
  186. loginParams["first"] = "Friend";
  187. loginParams["last"] = "Number2";
  188. loginParams["passwd"] = "boing";
  189. sendParams = new ArrayList();
  190. sendParams.Add(loginParams);
  191. request = new XmlRpcRequest("login_to_simulator", sendParams);
  192. response = loginService.XmlRpcLoginMethod(request);
  193. responseData = (Hashtable)response.Value;
  194. Assert.That(responseData["message"], Is.EqualTo(error_auth_message));
  195. // 2.3) Bad XML
  196. loginParams = new Hashtable();
  197. loginParams["first"] = "Friend";
  198. loginParams["banana"] = "Banana";
  199. loginParams["passwd"] = "boingboing";
  200. sendParams = new ArrayList();
  201. sendParams.Add(loginParams);
  202. request = new XmlRpcRequest("login_to_simulator", sendParams);
  203. response = loginService.XmlRpcLoginMethod(request);
  204. responseData = (Hashtable)response.Value;
  205. Assert.That(responseData["message"], Is.EqualTo(error_xml_message));
  206. // 2.4) Already logged in and sucessfull post login
  207. loginParams = new Hashtable();
  208. loginParams["first"] = "Adam";
  209. loginParams["last"] = "West";
  210. loginParams["passwd"] = "boingboing";
  211. sendParams = new ArrayList();
  212. sendParams.Add(loginParams);
  213. request = new XmlRpcRequest("login_to_simulator", sendParams);
  214. response = loginService.XmlRpcLoginMethod(request);
  215. responseData = (Hashtable)response.Value;
  216. Assert.That(responseData["message"], Is.EqualTo(error_already_logged));
  217. request = new XmlRpcRequest("login_to_simulator", sendParams);
  218. response = loginService.XmlRpcLoginMethod(request);
  219. responseData = (Hashtable)response.Value;
  220. Assert.That(responseData["message"], Is.EqualTo("Hello folks"));
  221. }
  222. }
  223. }