2
0

OleDbPermissionTest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // OleDbPermissionTest.cs - NUnit Test Cases for OleDbPermission
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2004 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 NUnit.Framework;
  29. using System;
  30. using System.Data;
  31. using System.Data.Common;
  32. using System.Data.OleDb;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. namespace MonoTests.System.Data.OleDb {
  36. // NOTE: Most tests are are located in the base class, DBDataPermission
  37. [TestFixture]
  38. public class OleDbPermissionTest {
  39. private void Check (string msg, OleDbPermission perm, bool blank, bool unrestricted, int count)
  40. {
  41. Assert.AreEqual (blank, perm.AllowBlankPassword, msg + ".AllowBlankPassword");
  42. Assert.AreEqual (unrestricted, perm.IsUnrestricted (), msg + ".IsUnrestricted");
  43. if (count == 0)
  44. Assert.IsNull (perm.ToXml ().Children, msg + ".Count != 0");
  45. else
  46. Assert.AreEqual (count, perm.ToXml ().Children.Count, msg + ".Count");
  47. Assert.AreEqual (String.Empty, perm.Provider, "Provider");
  48. }
  49. [Test]
  50. #if NET_2_0
  51. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  52. #else
  53. [ExpectedException (typeof (ArgumentException))]
  54. #endif
  55. public void PermissionState_Invalid ()
  56. {
  57. PermissionState ps = (PermissionState)Int32.MinValue;
  58. OleDbPermission perm = new OleDbPermission (ps);
  59. }
  60. [Test]
  61. public void None ()
  62. {
  63. OleDbPermission perm = new OleDbPermission (PermissionState.None);
  64. Check ("None-1", perm, false, false, 0);
  65. perm.AllowBlankPassword = true;
  66. Check ("None-2", perm, true, false, 0);
  67. OleDbPermission copy = (OleDbPermission)perm.Copy ();
  68. Check ("Copy_None-1", copy, true, false, 0);
  69. copy.AllowBlankPassword = false;
  70. Check ("Copy_None-2", copy, false, false, 0);
  71. }
  72. [Test]
  73. public void None_Childs ()
  74. {
  75. OleDbPermission perm = new OleDbPermission (PermissionState.None);
  76. perm.Add ("data source=localhost;", String.Empty, KeyRestrictionBehavior.AllowOnly);
  77. perm.Add ("data source=127.0.0.1;", "password=;", KeyRestrictionBehavior.PreventUsage);
  78. Check ("None-Childs-1", perm, false, false, 2);
  79. perm.AllowBlankPassword = true;
  80. Check ("None-Childs-2", perm, true, false, 2);
  81. OleDbPermission copy = (OleDbPermission)perm.Copy ();
  82. Check ("Copy_None-Childs-1", copy, true, false, 2);
  83. copy.AllowBlankPassword = false;
  84. Check ("Copy_None-Childs-2", copy, false, false, 2);
  85. }
  86. [Test]
  87. public void Unrestricted ()
  88. {
  89. OleDbPermission perm = new OleDbPermission (PermissionState.Unrestricted);
  90. Check ("Unrestricted-1", perm, false, true, 0);
  91. perm.AllowBlankPassword = true;
  92. Check ("Unrestricted-2", perm, true, true, 0);
  93. OleDbPermission copy = (OleDbPermission)perm.Copy ();
  94. // note: Unrestricted is always created with default values (so AllowBlankPassword is false)
  95. Check ("Copy_Unrestricted-1", copy, false, true, 0);
  96. copy.AllowBlankPassword = true;
  97. Check ("Copy_Unrestricted-2", copy, true, true, 0);
  98. }
  99. [Test]
  100. public void Unrestricted_Add ()
  101. {
  102. OleDbPermission perm = new OleDbPermission (PermissionState.Unrestricted);
  103. Check ("Unrestricted-NoChild", perm, false, true, 0);
  104. perm.Add ("data source=localhost;", String.Empty, KeyRestrictionBehavior.AllowOnly);
  105. // note: Lost unrestricted state when children was added
  106. Check ("Unrestricted-WithChild", perm, false, false, 1);
  107. }
  108. [Test]
  109. public void Provider ()
  110. {
  111. OleDbPermission perm = new OleDbPermission (PermissionState.None);
  112. perm.Provider = String.Empty;
  113. Assert.AreEqual (String.Empty, perm.Provider, "Empty");
  114. perm.Provider = "Mono";
  115. Assert.AreEqual ("Mono", perm.Provider, "Mono");
  116. perm.Provider = null;
  117. Assert.AreEqual (String.Empty, perm.Provider, "Empty(null)");
  118. }
  119. }
  120. }