FormsAuthenticationTest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // FormsAuthenticationTest.cs - NUnit Test Cases for FormsAuthentication
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. using System;
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. using System.Web;
  13. using System.Web.Security;
  14. using NUnit.Framework;
  15. using MonoTests.SystemWeb.Framework;
  16. namespace MonoTests.System.Web.Security
  17. {
  18. [TestFixture]
  19. public class FormsAuthenticationTest
  20. {
  21. [Test]
  22. [Category ("NotDotNet")] // Dot.net url must include Namespace name
  23. [Category("NunitWeb")]
  24. public void DefaultValues ()
  25. {
  26. new WebTest(new HandlerInvoker (new HandlerDelegate(DefaultValues_delegate))).Run ();
  27. }
  28. static public void DefaultValues_delegate ()
  29. {
  30. // MS use ".ASPXAUTH" while Mono use ".MONOAUTH"
  31. string str = FormsAuthentication.FormsCookieName;
  32. Assert.IsTrue ((str.Length == 9 && str [0] == '.' && str.EndsWith ("AUTH")), "FormsCookieName");
  33. Assert.AreEqual ("/", FormsAuthentication.FormsCookiePath, "FormsCookiePath");
  34. Assert.IsFalse (FormsAuthentication.RequireSSL, "RequireSSL");
  35. Assert.IsTrue (FormsAuthentication.SlidingExpiration, "SlidingExpiration");
  36. #if NET_2_0
  37. // MSDN: The default is an empty string ("") but null.
  38. Assert.AreEqual ("", FormsAuthentication.CookieDomain, "CookieDomain");
  39. Assert.AreEqual (HttpCookieMode.UseDeviceProfile, FormsAuthentication.CookieMode, "CookieMode");
  40. Assert.IsTrue (FormsAuthentication.CookiesSupported, "CookiesSupported");
  41. Assert.AreEqual ("/NunitWeb/default.aspx", FormsAuthentication.DefaultUrl);
  42. Assert.IsFalse (FormsAuthentication.EnableCrossAppRedirects, "EnableCrossAppRedirects");
  43. Assert.AreEqual ("/NunitWeb/login.aspx", FormsAuthentication.LoginUrl, "LoginUrl");
  44. #endif
  45. }
  46. [Test]
  47. [Category ("NotDotNet")] // Dot.net url must include Namespace name
  48. [Category("NunitWeb")]
  49. public void Initialize ()
  50. {
  51. new WebTest(new HandlerInvoker (new HandlerDelegate(Initialize_delegate))).Run ();
  52. }
  53. static public void Initialize_delegate ()
  54. {
  55. // calling Initialize without an HttpContext
  56. FormsAuthentication.Initialize ();
  57. // and that doesn't change the default values
  58. DefaultValues_delegate ();
  59. }
  60. [Test]
  61. [ExpectedException (typeof (ArgumentNullException))]
  62. public void HashPasswordForStoringInConfigFile_NullPassword ()
  63. {
  64. FormsAuthentication.HashPasswordForStoringInConfigFile (null, "MD5");
  65. }
  66. [Test]
  67. [ExpectedException (typeof (ArgumentNullException))]
  68. public void HashPasswordForStoringInConfigFile_NullPasswordFormat ()
  69. {
  70. FormsAuthentication.HashPasswordForStoringInConfigFile ("Mono", null);
  71. }
  72. [Test]
  73. public void HashPasswordForStoringInConfigFile_MD5 ()
  74. {
  75. // § (C2-A7)
  76. string s = Encoding.UTF8.GetString (new byte [2] { 0xC2, 0xA7 });
  77. Assert.AreEqual ("BD9A4C255DEEC8944D99E01A64C1E322", FormsAuthentication.HashPasswordForStoringInConfigFile (s, "MD5"));
  78. // ä (C3-A4)
  79. s = Encoding.UTF8.GetString (new byte [2] { 0xC3, 0xA4 });
  80. Assert.AreEqual ("8419B71C87A225A2C70B50486FBEE545", FormsAuthentication.HashPasswordForStoringInConfigFile (s, "MD5"));
  81. }
  82. [Test]
  83. public void HashPasswordForStoringInConfigFile_SHA1 ()
  84. {
  85. // § (C2-A7)
  86. string s = Encoding.UTF8.GetString (new byte [2] { 0xC2, 0xA7 });
  87. Assert.AreEqual ("EB2CB244889599F736B6CDD633C5E324F521D1BB", FormsAuthentication.HashPasswordForStoringInConfigFile (s, "SHA1"));
  88. // ä (C3-A4)
  89. s = Encoding.UTF8.GetString (new byte [2] { 0xC3, 0xA4 });
  90. Assert.AreEqual ("961FA22F61A56E19F3F5F8867901AC8CF5E6D11F", FormsAuthentication.HashPasswordForStoringInConfigFile (s, "SHA1"));
  91. }
  92. [Test]
  93. [ExpectedException (typeof (ArgumentException))]
  94. public void HashPasswordForStoringInConfigFile_SHA256 ()
  95. {
  96. FormsAuthentication.HashPasswordForStoringInConfigFile ("mono", "SHA256");
  97. }
  98. #if NET_2_0
  99. [Test]
  100. [ExpectedException (typeof (NullReferenceException))]
  101. public void RedirectToLoginPage ()
  102. {
  103. FormsAuthentication.RedirectToLoginPage ();
  104. }
  105. [Test]
  106. [ExpectedException (typeof (NullReferenceException))]
  107. public void RedirectToLoginPage_XtraQuery_Null ()
  108. {
  109. FormsAuthentication.RedirectToLoginPage (null);
  110. }
  111. [Test]
  112. [ExpectedException (typeof (NullReferenceException))]
  113. public void RedirectToLoginPage_XtraQuery_Empty ()
  114. {
  115. FormsAuthentication.RedirectToLoginPage (String.Empty);
  116. }
  117. #endif
  118. [TestFixtureTearDown]
  119. public void TestFixtureTearDown()
  120. {
  121. WebTest.Unload();
  122. }
  123. }
  124. }