summaryrefslogtreecommitdiff
path: root/mcs/class/System/Test/System.ComponentModel/LicenseManagerTests.cs
blob: edef6e098547bd639e4d02fa016f9b7caa6a1f01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
// System.ComponentModel.LicenseManagerTests test cases
//
// Authors:
//	Ivan Hamilton (ivan@chimerical.com.au)
//	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//	Martin Willemoes Hansen (mwh@sysrq.dk)
//
// (c) 2002 Ximian, Inc. (http://www.ximian.com)
// (c) 2003 Martin Willemoes Hansen
// (c) 2004 Ivan Hamilton

#if !MOBILE

using NUnit.Framework;
using System;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace MonoTests.System.ComponentModel 
{
	public class UnlicensedObject 
	{
	}

	[LicenseProvider (typeof (TestLicenseProvider))]
	public class LicensedObject
	{
	}

	[LicenseProvider (typeof (TestLicenseProvider))]
	public class InvalidLicensedObject
	{
	}

	[LicenseProvider (typeof (TestLicenseProvider))]
	public class RuntimeLicensedObject
	{
		public RuntimeLicensedObject () 
		{
			LicenseManager.Validate (typeof (RuntimeLicensedObject));
		}
		public RuntimeLicensedObject (int a): this () {}
	}

	[LicenseProvider (typeof (TestLicenseProvider))]
	public class DesigntimeLicensedObject
	{
		public DesigntimeLicensedObject () 
		{
			LicenseManager.Validate (typeof (DesigntimeLicensedObject));
		}
	}

	public class TestLicenseProvider : LicenseProvider 
	{

		private class TestLicense : License 
		{
			public override void Dispose ()
			{
			}

			public override string LicenseKey 
			{
				get { return "YourLicenseKey"; }
			}
		}

		public TestLicenseProvider () : base ()
		{
		}

		public override License GetLicense (LicenseContext context,
			Type type,
			object instance,
			bool allowExceptions)
		{
			if (type.Name.Equals ("RuntimeLicensedObject")) {
				if (context.UsageMode != LicenseUsageMode.Runtime)
					if (allowExceptions)
						throw new LicenseException (type, instance, "License fails because this is a Runtime only license");
					else
						return null;
				return new TestLicense ();
			}

			if (type.Name.Equals ("DesigntimeLicensedObject")) 
			{
				if (context.UsageMode != LicenseUsageMode.Designtime)
					if (allowExceptions)
						throw new LicenseException (type, instance, "License fails because this is a Designtime only license");
					else
						return null;
				return new TestLicense ();
			}

			if (type.Name.Equals ("LicensedObject"))
				return new TestLicense ();

			if (allowExceptions)
				throw new LicenseException (type, instance, "License fails because of class name.");
			else
				return null;
		}
	}


	[TestFixture]
	public class LicenseManagerTests
	{
		[SetUp]
		public void GetReady ()
		{
		}

		[Test]
		public void Test () 
		{
			object lockObject = new object ();
			//**DEFAULT CONTEXT & LicenseUsageMode**
			//Get CurrentContext, check default type
			Assert.AreEqual ("System.ComponentModel.Design.RuntimeLicenseContext", LicenseManager.CurrentContext.GetType().ToString(), "LicenseManager #1");
			//Read default LicenseUsageMode, check against CurrentContext (LicCont).UsageMode
			Assert.AreEqual (LicenseManager.CurrentContext.UsageMode, LicenseManager.UsageMode, "LicenseManager #2");

			//**CHANGING CONTEXT**
			//Change the context and check it changes
			LicenseContext oldcontext = LicenseManager.CurrentContext;
			LicenseContext newcontext = new DesigntimeLicenseContext();
			LicenseManager.CurrentContext = newcontext;
			Assert.AreEqual (newcontext, LicenseManager.CurrentContext, "LicenseManager #3");
			//Check the UsageMode changed too
			Assert.AreEqual (newcontext.UsageMode, LicenseManager.UsageMode, "LicenseManager #4");
			//Set Context back to original
			LicenseManager.CurrentContext = oldcontext;
			//Check it went back
			Assert.AreEqual (oldcontext, LicenseManager.CurrentContext, "LicenseManager #5");
			//Check the UsageMode changed too
			Assert.AreEqual (oldcontext.UsageMode, LicenseManager.UsageMode, "LicenseManager #6");

			//**CONTEXT LOCKING**
			//Lock the context
			LicenseManager.LockContext(lockObject);
			//Try and set new context again, should throw System.InvalidOperationException: The CurrentContext property of the LicenseManager is currently locked and cannot be changed.
			bool exceptionThrown = false;
			try 
			{
				LicenseManager.CurrentContext=newcontext;
			} 
			catch (Exception e) 
			{
				Assert.AreEqual (typeof(InvalidOperationException), e.GetType(), "LicenseManager #7");
				exceptionThrown = true;
			} 
			//Check the exception was thrown
			Assert.AreEqual (true, exceptionThrown, "LicenseManager #8");
			//Check the context didn't change
			Assert.AreEqual (oldcontext, LicenseManager.CurrentContext, "LicenseManager #9");
			//Unlock it
			LicenseManager.UnlockContext(lockObject);
			//Now's unlocked, change it
			LicenseManager.CurrentContext=newcontext;
			Assert.AreEqual (newcontext, LicenseManager.CurrentContext, "LicenseManager #10");
			//Change it back
			LicenseManager.CurrentContext = oldcontext;


			//Lock the context
			LicenseManager.LockContext(lockObject);
			//Unlock with different "user" should throw System.ArgumentException: The CurrentContext property of the LicenseManager can only be unlocked with the same contextUser.
			object wrongLockObject = new object();
			exceptionThrown = false;
			try 
			{
				LicenseManager.UnlockContext(wrongLockObject);
			} 
			catch (Exception e) 
			{
				Assert.AreEqual (typeof(ArgumentException), e.GetType(), "LicenseManager #11");
				exceptionThrown = true;
			} 
			Assert.AreEqual (true, exceptionThrown, "LicenseManager #12");
			//Unlock it
			LicenseManager.UnlockContext(lockObject);

			//** bool IsValid(Type);
			Assert.AreEqual (true, LicenseManager.IsLicensed (typeof (UnlicensedObject)), "LicenseManager #13");
			Assert.AreEqual (true, LicenseManager.IsLicensed (typeof (LicensedObject)), "LicenseManager #14");
			Assert.AreEqual (false, LicenseManager.IsLicensed (typeof (InvalidLicensedObject)), "LicenseManager #15");

			Assert.AreEqual (true, LicenseManager.IsValid (typeof (UnlicensedObject)), "LicenseManager #16");
			Assert.AreEqual (true, LicenseManager.IsValid (typeof (LicensedObject)), "LicenseManager #17");
			Assert.AreEqual (false, LicenseManager.IsValid (typeof (InvalidLicensedObject)), "LicenseManager #18");

			UnlicensedObject unlicensedObject = new UnlicensedObject ();
			LicensedObject licensedObject = new LicensedObject ();
			InvalidLicensedObject invalidLicensedObject = new InvalidLicensedObject ();

			//** bool IsValid(Type, object, License);
			License license=null;
			Assert.AreEqual (true, LicenseManager.IsValid (unlicensedObject.GetType (), unlicensedObject, out license), "LicenseManager #19");
			Assert.AreEqual (null, license, "LicenseManager #20");

			license=null;
			Assert.AreEqual (true, LicenseManager.IsValid (licensedObject.GetType (), licensedObject,out license), "LicenseManager #21");
			Assert.AreEqual ("TestLicense", license.GetType ().Name, "LicenseManager #22");

			license=null;
			Assert.AreEqual (false, LicenseManager.IsValid (invalidLicensedObject.GetType (), invalidLicensedObject, out license), "LicenseManager #23");
			Assert.AreEqual (null, license, "LicenseManager #24");

			//** void Validate(Type);
			//Shouldn't throw exception
			LicenseManager.Validate (typeof (UnlicensedObject));
			//Shouldn't throw exception
			LicenseManager.Validate (typeof (LicensedObject));
			//Should throw exception
			exceptionThrown=false;
			try 
			{
				LicenseManager.Validate(typeof(InvalidLicensedObject));
			} 
			catch (Exception e) 
			{
				Assert.AreEqual (typeof(LicenseException), e.GetType(), "LicenseManager #25");
				exceptionThrown=true;
			} 
			//Check the exception was thrown
			Assert.AreEqual (true, exceptionThrown, "LicenseManager #26");

			//** License Validate(Type, object);
			//Shouldn't throw exception, returns null license
			license=LicenseManager.Validate (typeof (UnlicensedObject), unlicensedObject);
			Assert.AreEqual (null, license, "LicenseManager #27");

			//Shouldn't throw exception, returns TestLicense license
			license=LicenseManager.Validate(typeof(LicensedObject), licensedObject);
			Assert.AreEqual ("TestLicense", license.GetType ().Name, "LicenseManager #28");

			//Should throw exception, returns null license
			exceptionThrown=false;
			try 
			{
				license=null;
				license=LicenseManager.Validate (typeof (InvalidLicensedObject), invalidLicensedObject);
			} 
			catch (Exception e) 
			{
				Assert.AreEqual (typeof (LicenseException), e.GetType (), "LicenseManager #29");
				exceptionThrown=true;
			} 
			//Check the exception was thrown
			Assert.AreEqual (true, exceptionThrown, "LicenseManager #30");
			Assert.AreEqual (null, license, "LicenseManager #31");


			//** object CreateWithContext (Type, LicenseContext);
			object cwc = null;
			//Test we can create an unlicensed object with no context
			cwc = LicenseManager.CreateWithContext (typeof (UnlicensedObject), null);
			Assert.AreEqual ("UnlicensedObject", cwc.GetType ().Name, "LicenseManager #32");
			//Test we can create RunTime with CurrentContext (runtime)
			cwc = null;
			cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
				LicenseManager.CurrentContext);
			Assert.AreEqual ("RuntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #33");
			//Test we can't create DesignTime with CurrentContext (runtime)
			exceptionThrown=false;
			try 
			{
				cwc = null;
				cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject), LicenseManager.CurrentContext);
			} 
			catch (Exception e) 
			{
				Assert.AreEqual (typeof (LicenseException), e.GetType (), "LicenseManager #34");
				exceptionThrown=true;
			} 
			//Check the exception was thrown
			Assert.AreEqual (true, exceptionThrown, "LicenseManager #35");
			//Test we can create DesignTime with A new DesignTimeContext 
			cwc = null;
			cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject),
				new DesigntimeLicenseContext ());
			Assert.AreEqual ("DesigntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #36");

			//** object CreateWithContext(Type, LicenseContext, object[]);
			//Test we can create RunTime with CurrentContext (runtime)
			cwc = null;
			cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
				LicenseManager.CurrentContext, new object [] { 7 });
			Assert.AreEqual ("RuntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #37");

		}
	}
}

#endif