FormsAuthenticationTest.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. namespace MonoTests.System.Web.Security {
  16. [TestFixture]
  17. public class FormsAuthenticationTest {
  18. [Test]
  19. #if ONLY_1_1
  20. [ExpectedException (typeof (NullReferenceException))]
  21. #endif
  22. public void DefaultValues ()
  23. {
  24. // MS use ".ASPXAUTH" while Mono use ".MONOAUTH"
  25. string str = FormsAuthentication.FormsCookieName;
  26. Assert.IsTrue ((str.Length == 9 && str [0] == '.' && str.EndsWith ("AUTH")), "FormsCookieName");
  27. Assert.AreEqual ("/", FormsAuthentication.FormsCookiePath, "FormsCookiePath");
  28. Assert.IsFalse (FormsAuthentication.RequireSSL, "RequireSSL");
  29. Assert.IsTrue (FormsAuthentication.SlidingExpiration, "SlidingExpiration");
  30. #if NET_2_0
  31. Assert.AreEqual (String.Empty, FormsAuthentication.CookieDomain, "CookieDomain");
  32. Assert.AreEqual (HttpCookieMode.UseDeviceProfile, FormsAuthentication.CookieMode, "CookieMode");
  33. Assert.IsTrue (FormsAuthentication.CookiesSupported, "CookiesSupported");
  34. Assert.AreEqual ("/default.aspx", FormsAuthentication.DefaultUrl);
  35. Assert.IsFalse (FormsAuthentication.EnableCrossAppRedirects, "EnableCrossAppRedirects");
  36. Assert.AreEqual ("/login.aspx", FormsAuthentication.LoginUrl, "LoginUrl");
  37. #endif
  38. }
  39. [Test]
  40. #if ONLY_1_1
  41. [ExpectedException (typeof (NullReferenceException))]
  42. #endif
  43. public void Initialize ()
  44. {
  45. // calling Initialize without an HttpContext
  46. FormsAuthentication.Initialize ();
  47. // and that doesn't change the default values
  48. DefaultValues ();
  49. }
  50. [Test]
  51. [ExpectedException (typeof (ArgumentNullException))]
  52. public void HashPasswordForStoringInConfigFile_NullPassword ()
  53. {
  54. FormsAuthentication.HashPasswordForStoringInConfigFile (null, "MD5");
  55. }
  56. [Test]
  57. [ExpectedException (typeof (ArgumentNullException))]
  58. public void HashPasswordForStoringInConfigFile_NullPasswordFormat ()
  59. {
  60. FormsAuthentication.HashPasswordForStoringInConfigFile ("Mono", null);
  61. }
  62. [Test]
  63. public void HashPasswordForStoringInConfigFile_MD5 ()
  64. {
  65. // § (C2-A7)
  66. string s = Encoding.UTF8.GetString (new byte [2] { 0xC2, 0xA7 });
  67. Assert.AreEqual ("BD9A4C255DEEC8944D99E01A64C1E322", FormsAuthentication.HashPasswordForStoringInConfigFile (s, "MD5"));
  68. // ä (C3-A4)
  69. s = Encoding.UTF8.GetString (new byte [2] { 0xC3, 0xA4 });
  70. Assert.AreEqual ("8419B71C87A225A2C70B50486FBEE545", FormsAuthentication.HashPasswordForStoringInConfigFile (s, "MD5"));
  71. }
  72. [Test]
  73. public void HashPasswordForStoringInConfigFile_SHA1 ()
  74. {
  75. // § (C2-A7)
  76. string s = Encoding.UTF8.GetString (new byte [2] { 0xC2, 0xA7 });
  77. Assert.AreEqual ("EB2CB244889599F736B6CDD633C5E324F521D1BB", FormsAuthentication.HashPasswordForStoringInConfigFile (s, "SHA1"));
  78. // ä (C3-A4)
  79. s = Encoding.UTF8.GetString (new byte [2] { 0xC3, 0xA4 });
  80. Assert.AreEqual ("961FA22F61A56E19F3F5F8867901AC8CF5E6D11F", FormsAuthentication.HashPasswordForStoringInConfigFile (s, "SHA1"));
  81. }
  82. [Test]
  83. [ExpectedException (typeof (ArgumentException))]
  84. public void HashPasswordForStoringInConfigFile_SHA256 ()
  85. {
  86. FormsAuthentication.HashPasswordForStoringInConfigFile ("mono", "SHA256");
  87. }
  88. #if NET_2_0
  89. [Test]
  90. [ExpectedException (typeof (NullReferenceException))]
  91. public void RedirectToLoginPage ()
  92. {
  93. FormsAuthentication.RedirectToLoginPage ();
  94. }
  95. [Test]
  96. [ExpectedException (typeof (NullReferenceException))]
  97. public void RedirectToLoginPage_XtraQuery_Null ()
  98. {
  99. FormsAuthentication.RedirectToLoginPage (null);
  100. }
  101. [Test]
  102. [ExpectedException (typeof (NullReferenceException))]
  103. public void RedirectToLoginPage_XtraQuery_Empty ()
  104. {
  105. FormsAuthentication.RedirectToLoginPage (String.Empty);
  106. }
  107. #endif
  108. }
  109. }