NetworkCredentialTest.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Unit tests for System.Net.NetworkCredential
  3. //
  4. // Contact:
  5. // Moonlight List ([email protected])
  6. //
  7. // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Net;
  30. using System.Security;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Net {
  33. [TestFixture]
  34. public class NetworkCredentialTest {
  35. void CheckDefaults (NetworkCredential nc)
  36. {
  37. Assert.AreSame (String.Empty, nc.Domain, "Domain");
  38. Assert.AreSame (String.Empty, nc.Password, "Password");
  39. Assert.AreSame (String.Empty, nc.UserName, "UserName");
  40. Assert.AreSame (nc, nc.GetCredential (null, null), "GetCredential");
  41. }
  42. void CheckCustom (NetworkCredential nc)
  43. {
  44. Assert.AreEqual ("dom", nc.Domain, "Domain");
  45. Assert.AreEqual ("********", nc.Password, "Password");
  46. Assert.AreEqual ("user", nc.UserName, "UserName");
  47. Assert.AreSame (nc, nc.GetCredential (new Uri ("http://www.mono-project.com"), "basic"), "GetCredential");
  48. }
  49. [Test]
  50. public void Constructor_0 ()
  51. {
  52. NetworkCredential nc = new NetworkCredential ();
  53. CheckDefaults (nc);
  54. nc.UserName = null;
  55. nc.Domain = null;
  56. nc.Password = null;
  57. CheckDefaults (nc);
  58. nc.UserName = "user";
  59. nc.Domain = "dom";
  60. nc.Password = "********";
  61. CheckCustom (nc);
  62. }
  63. [Test]
  64. public void Constructor_2 ()
  65. {
  66. NetworkCredential nc = new NetworkCredential ((string)null, (string)null);
  67. CheckDefaults (nc);
  68. nc.UserName = String.Empty;
  69. nc.Domain = String.Empty;
  70. nc.Password = null;
  71. CheckDefaults (nc);
  72. nc = new NetworkCredential ("user", "********");
  73. nc.Domain = "dom";
  74. CheckCustom (nc);
  75. }
  76. [Test]
  77. public void Constructor_3 ()
  78. {
  79. NetworkCredential nc = new NetworkCredential ((string)null, (string)null, (string)null);
  80. CheckDefaults (nc);
  81. nc.UserName = String.Empty;
  82. nc.Domain = null;
  83. nc.Password = String.Empty;
  84. CheckDefaults (nc);
  85. nc = new NetworkCredential ("user", "********", "dom");
  86. CheckCustom (nc);
  87. }
  88. [Test]
  89. public void DecipherSecureString ()
  90. {
  91. // many code snippets suggest using the following to get the decrypted string from a SecureString
  92. var ss = new SecureString ();
  93. ss.AppendChar('h');
  94. ss.AppendChar('e');
  95. ss.AppendChar('l');
  96. ss.AppendChar('l');
  97. ss.AppendChar('o');
  98. string plain = new NetworkCredential (string.Empty, ss).Password;
  99. Assert.AreEqual ("hello", plain);
  100. }
  101. }
  102. }