LicenseManagerTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //
  2. // System.ComponentModel.LicenseManagerTests test cases
  3. //
  4. // Authors:
  5. // Ivan Hamilton ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Martin Willemoes Hansen ([email protected])
  8. //
  9. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  10. // (c) 2003 Martin Willemoes Hansen
  11. // (c) 2004 Ivan Hamilton
  12. #define NUNIT // Comment out this one if you wanna play with the test without using NUnit
  13. #if NUNIT
  14. using NUnit.Framework;
  15. #else
  16. using System.Reflection;
  17. #endif
  18. using System;
  19. using System.ComponentModel;
  20. using System.ComponentModel.Design;
  21. namespace MonoTests.System.ComponentModel
  22. {
  23. public class UnlicensedObject
  24. {
  25. }
  26. [LicenseProvider (typeof (TestLicenseProvider))]
  27. public class LicensedObject
  28. {
  29. }
  30. [LicenseProvider (typeof (TestLicenseProvider))]
  31. public class InvalidLicensedObject
  32. {
  33. }
  34. [LicenseProvider (typeof (TestLicenseProvider))]
  35. public class RuntimeLicensedObject
  36. {
  37. public RuntimeLicensedObject ()
  38. {
  39. LicenseManager.Validate (typeof (RuntimeLicensedObject));
  40. }
  41. public RuntimeLicensedObject (int a): this () {}
  42. }
  43. [LicenseProvider (typeof (TestLicenseProvider))]
  44. public class DesigntimeLicensedObject
  45. {
  46. public DesigntimeLicensedObject ()
  47. {
  48. LicenseManager.Validate (typeof (DesigntimeLicensedObject));
  49. }
  50. }
  51. public class TestLicenseProvider : LicenseProvider
  52. {
  53. private class TestLicense : License
  54. {
  55. public override void Dispose ()
  56. {
  57. }
  58. public override string LicenseKey
  59. {
  60. get { return "YourLicenseKey"; }
  61. }
  62. }
  63. public TestLicenseProvider () : base ()
  64. {
  65. }
  66. public override License GetLicense (LicenseContext context,
  67. Type type,
  68. object instance,
  69. bool allowExceptions)
  70. {
  71. if (type.Name.Equals ("RuntimeLicensedObject")) {
  72. if (context.UsageMode != LicenseUsageMode.Runtime)
  73. if (allowExceptions)
  74. throw new LicenseException (type, instance, "License fails because this is a Runtime only license");
  75. else
  76. return null;
  77. return new TestLicense ();
  78. }
  79. if (type.Name.Equals ("DesigntimeLicensedObject"))
  80. {
  81. if (context.UsageMode != LicenseUsageMode.Designtime)
  82. if (allowExceptions)
  83. throw new LicenseException (type, instance, "License fails because this is a Designtime only license");
  84. else
  85. return null;
  86. return new TestLicense ();
  87. }
  88. if (type.Name.Equals ("LicensedObject"))
  89. return new TestLicense ();
  90. if (allowExceptions)
  91. throw new LicenseException (type, instance, "License fails because of class name.");
  92. else
  93. return null;
  94. }
  95. }
  96. #if NUNIT
  97. [TestFixture]
  98. public class LicenseManagerTests : Assertion
  99. {
  100. [SetUp]
  101. public void GetReady ()
  102. {
  103. #else
  104. public class LicenseManagerTests
  105. {
  106. static LicenseManagerTests ()
  107. {
  108. #endif
  109. }
  110. #if NUNIT
  111. [Test]
  112. #endif
  113. public void Test ()
  114. {
  115. object lockObject = new object ();
  116. //**DEFAULT CONTEXT & LicenseUsageMode**
  117. //Get CurrentContext, check default type
  118. AssertEquals ("LicenseManager #1", "System.ComponentModel.Design.RuntimeLicenseContext", LicenseManager.CurrentContext.GetType().ToString());
  119. //Read default LicenseUsageMode, check against CurrentContext (LicCont).UsageMode
  120. AssertEquals ("LicenseManager #2", LicenseManager.CurrentContext.UsageMode, LicenseManager.UsageMode);
  121. //**CHANGING CONTEXT**
  122. //Change the context and check it changes
  123. LicenseContext oldcontext = LicenseManager.CurrentContext;
  124. LicenseContext newcontext = new DesigntimeLicenseContext();
  125. LicenseManager.CurrentContext = newcontext;
  126. AssertEquals ("LicenseManager #3", newcontext, LicenseManager.CurrentContext);
  127. //Check the UsageMode changed too
  128. AssertEquals ("LicenseManager #4", newcontext.UsageMode, LicenseManager.UsageMode);
  129. //Set Context back to original
  130. LicenseManager.CurrentContext = oldcontext;
  131. //Check it went back
  132. AssertEquals ("LicenseManager #5", oldcontext, LicenseManager.CurrentContext);
  133. //Check the UsageMode changed too
  134. AssertEquals ("LicenseManager #6", oldcontext.UsageMode, LicenseManager.UsageMode);
  135. //**CONTEXT LOCKING**
  136. //Lock the context
  137. LicenseManager.LockContext(lockObject);
  138. //Try and set new context again, should throw System.InvalidOperationException: The CurrentContext property of the LicenseManager is currently locked and cannot be changed.
  139. bool exceptionThrown = false;
  140. try
  141. {
  142. LicenseManager.CurrentContext=newcontext;
  143. }
  144. catch (Exception e)
  145. {
  146. AssertEquals ("LicenseManager #7",typeof(InvalidOperationException), e.GetType());
  147. exceptionThrown = true;
  148. }
  149. //Check the exception was thrown
  150. AssertEquals ("LicenseManager #8", true, exceptionThrown);
  151. //Check the context didn't change
  152. AssertEquals ("LicenseManager #9", oldcontext, LicenseManager.CurrentContext);
  153. //Unlock it
  154. LicenseManager.UnlockContext(lockObject);
  155. //Now's unlocked, change it
  156. LicenseManager.CurrentContext=newcontext;
  157. AssertEquals ("LicenseManager #10", newcontext, LicenseManager.CurrentContext);
  158. //Change it back
  159. LicenseManager.CurrentContext = oldcontext;
  160. //Lock the context
  161. LicenseManager.LockContext(lockObject);
  162. //Unlock with different "user" should throw System.ArgumentException: The CurrentContext property of the LicenseManager can only be unlocked with the same contextUser.
  163. object wrongLockObject = new object();
  164. exceptionThrown = false;
  165. try
  166. {
  167. LicenseManager.UnlockContext(wrongLockObject);
  168. }
  169. catch (Exception e)
  170. {
  171. AssertEquals ("LicenseManager #11",typeof(ArgumentException), e.GetType());
  172. exceptionThrown = true;
  173. }
  174. AssertEquals ("LicenseManager #12",true, exceptionThrown);
  175. //Unlock it
  176. LicenseManager.UnlockContext(lockObject);
  177. //** bool IsValid(Type);
  178. AssertEquals ("LicenseManager #13", true, LicenseManager.IsLicensed (typeof (UnlicensedObject)));
  179. AssertEquals ("LicenseManager #14", true, LicenseManager.IsLicensed (typeof (LicensedObject)));
  180. AssertEquals ("LicenseManager #15", false, LicenseManager.IsLicensed (typeof (InvalidLicensedObject)));
  181. AssertEquals ("LicenseManager #16", true, LicenseManager.IsValid (typeof (UnlicensedObject)));
  182. AssertEquals ("LicenseManager #17", true, LicenseManager.IsValid (typeof (LicensedObject)));
  183. AssertEquals ("LicenseManager #18", false, LicenseManager.IsValid (typeof (InvalidLicensedObject)));
  184. UnlicensedObject unlicensedObject = new UnlicensedObject ();
  185. LicensedObject licensedObject = new LicensedObject ();
  186. InvalidLicensedObject invalidLicensedObject = new InvalidLicensedObject ();
  187. //** bool IsValid(Type, object, License);
  188. License license=null;
  189. AssertEquals ("LicenseManager #19", true, LicenseManager.IsValid (unlicensedObject.GetType (), unlicensedObject, out license));
  190. AssertEquals ("LicenseManager #20", null, license);
  191. license=null;
  192. AssertEquals ("LicenseManager #21",true, LicenseManager.IsValid (licensedObject.GetType (), licensedObject,out license));
  193. AssertEquals ("LicenseManager #22", "TestLicense", license.GetType ().Name);
  194. license=null;
  195. AssertEquals ("LicenseManager #23",false, LicenseManager.IsValid (invalidLicensedObject.GetType (), invalidLicensedObject, out license));
  196. AssertEquals ("LicenseManager #24",null, license);
  197. //** void Validate(Type);
  198. //Shouldn't throw exception
  199. LicenseManager.Validate (typeof (UnlicensedObject));
  200. //Shouldn't throw exception
  201. LicenseManager.Validate (typeof (LicensedObject));
  202. //Should throw exception
  203. exceptionThrown=false;
  204. try
  205. {
  206. LicenseManager.Validate(typeof(InvalidLicensedObject));
  207. }
  208. catch (Exception e)
  209. {
  210. AssertEquals ("LicenseManager #25",typeof(LicenseException), e.GetType());
  211. exceptionThrown=true;
  212. }
  213. //Check the exception was thrown
  214. AssertEquals ("LicenseManager #26",true, exceptionThrown);
  215. //** License Validate(Type, object);
  216. //Shouldn't throw exception, returns null license
  217. license=LicenseManager.Validate (typeof (UnlicensedObject), unlicensedObject);
  218. AssertEquals ("LicenseManager #27", null, license);
  219. //Shouldn't throw exception, returns TestLicense license
  220. license=LicenseManager.Validate(typeof(LicensedObject), licensedObject);
  221. AssertEquals ("LicenseManager #28", "TestLicense", license.GetType ().Name);
  222. //Should throw exception, returns null license
  223. exceptionThrown=false;
  224. try
  225. {
  226. license=null;
  227. license=LicenseManager.Validate (typeof (InvalidLicensedObject), invalidLicensedObject);
  228. }
  229. catch (Exception e)
  230. {
  231. AssertEquals ("LicenseManager #29",typeof (LicenseException), e.GetType ());
  232. exceptionThrown=true;
  233. }
  234. //Check the exception was thrown
  235. AssertEquals ("LicenseManager #30",true, exceptionThrown);
  236. AssertEquals ("LicenseManager #31",null, license);
  237. //** object CreateWithContext (Type, LicenseContext);
  238. object cwc = null;
  239. //Test we can create an unlicensed object with no context
  240. cwc = LicenseManager.CreateWithContext (typeof (UnlicensedObject), null);
  241. AssertEquals ("LicenseManager #32", "UnlicensedObject", cwc.GetType ().Name);
  242. //Test we can create RunTime with CurrentContext (runtime)
  243. cwc = null;
  244. cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
  245. LicenseManager.CurrentContext);
  246. AssertEquals ("LicenseManager #33", "RuntimeLicensedObject", cwc.GetType ().Name);
  247. //Test we can't create DesignTime with CurrentContext (runtime)
  248. exceptionThrown=false;
  249. try
  250. {
  251. cwc = null;
  252. cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject), LicenseManager.CurrentContext);
  253. }
  254. catch (Exception e)
  255. {
  256. AssertEquals ("LicenseManager #34",typeof (LicenseException), e.GetType ());
  257. exceptionThrown=true;
  258. }
  259. //Check the exception was thrown
  260. AssertEquals ("LicenseManager #35",true, exceptionThrown);
  261. //Test we can create DesignTime with A new DesignTimeContext
  262. cwc = null;
  263. cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject),
  264. new DesigntimeLicenseContext ());
  265. AssertEquals ("LicenseManager #36", "DesigntimeLicensedObject", cwc.GetType ().Name);
  266. //** object CreateWithContext(Type, LicenseContext, object[]);
  267. //Test we can create RunTime with CurrentContext (runtime)
  268. cwc = null;
  269. cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
  270. LicenseManager.CurrentContext, new object [] { 7 });
  271. AssertEquals ("LicenseManager #37", "RuntimeLicensedObject", cwc.GetType ().Name);
  272. }
  273. #if !NUNIT
  274. void Assert (string msg, bool result)
  275. {
  276. if (!result)
  277. Console.WriteLine (msg);
  278. }
  279. void AssertEquals (string msg, object expected, object real)
  280. {
  281. if (expected == null && real == null)
  282. return;
  283. if (expected != null && expected.Equals (real))
  284. return;
  285. Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
  286. }
  287. void Fail (string msg)
  288. {
  289. Console.WriteLine ("Failed: {0}", msg);
  290. }
  291. static void Main ()
  292. {
  293. LicenseManagerTests p = new LicenseManagerTests ();
  294. Type t = p.GetType ();
  295. MethodInfo [] methods = t.GetMethods ();
  296. foreach (MethodInfo m in methods)
  297. {
  298. if (m.Name.Substring (0, 4) == "Test")
  299. {
  300. m.Invoke (p, null);
  301. }
  302. }
  303. }
  304. #endif
  305. }
  306. }