Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

IdentityReference2Tests.cs 5.4 KiB

7 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System.Security.Principal;
  3. namespace Security2.Tests
  4. {
  5. [TestClass()]
  6. public class IdentityReference2Tests
  7. {
  8. IdentityReference2 currentUser;
  9. IdentityReference2 currentUser2;
  10. IdentityReference2 systemAccount;
  11. string invalidSid;
  12. [TestInitialize]
  13. public void Init()
  14. {
  15. currentUser = WindowsIdentity.GetCurrent().User;
  16. currentUser2 = WindowsIdentity.GetCurrent().User;
  17. systemAccount = new IdentityReference2(@"NT AUTHORITY\SYSTEM");
  18. invalidSid = "S-1-5-2-123456789-2021496291-1752113662-1002";
  19. }
  20. [TestMethod()]
  21. public void EqualsToNameStringTest()
  22. {
  23. var name = WindowsIdentity.GetCurrent().User.Translate(typeof(NTAccount)).Value;
  24. Assert.IsTrue(currentUser.Equals(name));
  25. }
  26. [TestMethod()]
  27. public void NotEqualsToNameStringTest()
  28. {
  29. var name = systemAccount.AccountName;
  30. Assert.IsFalse(currentUser.Equals(name));
  31. }
  32. [TestMethod()]
  33. public void EqualsToSidStringTest()
  34. {
  35. var sid = WindowsIdentity.GetCurrent().User.Value;
  36. Assert.IsTrue(currentUser.Equals(sid));
  37. }
  38. [TestMethod()]
  39. public void NotEqualsToSidStringTest()
  40. {
  41. var sid = systemAccount.Sid;
  42. Assert.IsFalse(currentUser.Equals(sid));
  43. }
  44. [TestMethod()]
  45. public void NotEqualsToStringTest()
  46. {
  47. Assert.IsFalse(currentUser.Equals(systemAccount.AccountName));
  48. }
  49. [TestMethod()]
  50. public void EqualsToNTAccount()
  51. {
  52. var ntAccount = WindowsIdentity.GetCurrent().User.Translate(typeof(NTAccount));
  53. Assert.IsTrue(currentUser.Equals(ntAccount));
  54. }
  55. [TestMethod()]
  56. public void NotEqualsToNTAccount()
  57. {
  58. var ntAccount = new NTAccount(systemAccount.AccountName);
  59. Assert.IsFalse(currentUser.Equals(ntAccount));
  60. }
  61. [TestMethod()]
  62. public void EqualsToSecurityIdentifier()
  63. {
  64. var sid = WindowsIdentity.GetCurrent().User;
  65. Assert.IsTrue(currentUser.Equals(sid));
  66. }
  67. [TestMethod()]
  68. public void NotEqualsToSecurityIdentifier()
  69. {
  70. var sid = new SecurityIdentifier(systemAccount.Sid);
  71. Assert.IsFalse(currentUser.Equals(sid));
  72. }
  73. [TestMethod()]
  74. public void EqualsToIdentityReference2()
  75. {
  76. Assert.IsTrue(currentUser.Equals(currentUser2));
  77. }
  78. [TestMethod()]
  79. public void NotEqualsToIdentityReference2()
  80. {
  81. Assert.IsFalse(currentUser.Equals(systemAccount));
  82. }
  83. [TestMethod()]
  84. [ExpectedException(typeof(System.NullReferenceException))]
  85. public void EqualsToNullThrowsException()
  86. {
  87. IdentityReference2 ir2 = null;
  88. ir2.Equals(null);
  89. }
  90. [TestMethod()]
  91. public void NotEqualsToNull()
  92. {
  93. Assert.IsFalse(currentUser.Equals(null));
  94. }
  95. [TestMethod()]
  96. public void GetHashCodeWithCurrentUserTest()
  97. {
  98. var sid = new SecurityIdentifier(currentUser.GetBinaryForm(), 0);
  99. Assert.AreEqual(sid.GetHashCode(), currentUser.GetHashCode());
  100. }
  101. [TestMethod()]
  102. public void GetBinaryFormWithCurrentUserTest()
  103. {
  104. var sid = new SecurityIdentifier(currentUser.GetBinaryForm(), 0);
  105. Assert.AreEqual(sid.Value, currentUser.Sid);
  106. }
  107. [TestMethod()]
  108. public void ToStringWithCurrentUserTest()
  109. {
  110. var sid = WindowsIdentity.GetCurrent().User;
  111. var ntAccount = sid.Translate(typeof(NTAccount));
  112. Assert.AreEqual(currentUser.ToString(), ntAccount.Value);
  113. }
  114. [TestMethod()]
  115. public void ToStringWithInvalidSidEqualsNull()
  116. {
  117. var ir2 = (IdentityReference2)invalidSid;
  118. Assert.AreEqual(ir2.ToString(), invalidSid);
  119. }
  120. [TestMethod()]
  121. public void op_ReferenceEquals()
  122. {
  123. var temp = currentUser;
  124. Assert.IsTrue(currentUser == temp);
  125. }
  126. [TestMethod()]
  127. public void op_EqualsToIdentityReference2()
  128. {
  129. Assert.IsTrue(currentUser == currentUser2);
  130. }
  131. [TestMethod()]
  132. public void op_NotEqualsToIdentityReference2()
  133. {
  134. Assert.IsTrue(currentUser != systemAccount);
  135. }
  136. [TestMethod()]
  137. public void op_EqualsToNTAccout()
  138. {
  139. var ntAccount = new NTAccount(currentUser.AccountName);
  140. Assert.IsTrue(currentUser == ntAccount);
  141. }
  142. [TestMethod()]
  143. public void op_NotEqualsToNTAccount()
  144. {
  145. var ntAccount = new NTAccount(systemAccount.AccountName);
  146. Assert.IsTrue(currentUser != ntAccount);
  147. }
  148. [TestMethod()]
  149. public void op_EqualsToSecurityIdentifier()
  150. {
  151. var sid = new SecurityIdentifier(currentUser.Sid);
  152. Assert.IsTrue(currentUser == sid);
  153. }
  154. [TestMethod()]
  155. public void op_NotEqualsToSecurityIdentifier()
  156. {
  157. var sid = new SecurityIdentifier(systemAccount.Sid);
  158. Assert.IsTrue(currentUser != sid);
  159. }
  160. }
  161. }