1 //
2 // VersioningHelperTest.cs -
3 //	Unit tests for System.Runtime.Versioning.VersioningHelper
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 
30 
31 using System;
32 using System.Runtime.Versioning;
33 
34 using NUnit.Framework;
35 
36 namespace MonoTests.System.Runtime.Versioning {
37 
38 	[TestFixture]
39 	public class VersioningHelperTest {
40 
41 		[Test]
Name_Null()42 		public void Name_Null ()
43 		{
44 			string s = VersioningHelper.MakeVersionSafeName (null, ResourceScope.AppDomain, ResourceScope.AppDomain);
45 			Assert.AreEqual (String.Empty, s, "Result");
46 		}
47 
48 		[Test]
49 		[ExpectedException (typeof (ArgumentException))]
From_Invalid()50 		public void From_Invalid ()
51 		{
52 			ResourceScope bad = (ResourceScope) Int32.MinValue;
53 			string s = VersioningHelper.MakeVersionSafeName (null, bad, ResourceScope.None);
54 		}
55 
56 		[Test]
57 		[ExpectedException (typeof (ArgumentException))]
To_Invalid()58 		public void To_Invalid ()
59 		{
60 			ResourceScope bad = (ResourceScope) Int32.MinValue;
61 			string s = VersioningHelper.MakeVersionSafeName (null, ResourceScope.AppDomain, bad);
62 		}
63 
64 		[Test]
Type_Null()65 		public void Type_Null ()
66 		{
67 			string s = VersioningHelper.MakeVersionSafeName (null, ResourceScope.AppDomain, ResourceScope.AppDomain, null);
68 			Assert.AreEqual (String.Empty, s, "Result");
69 		}
70 
71 		// ResourceScope is encoded 6 bits for "from" and 6 bits for "to"
72 		static int[] ValidValues = { 65, 66, 68, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
73 			143, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 1089, 1090, 1092, 1105, 1106,
74 			1108, 1121, 1122, 1124, 1137, 1138, 1140, 1154, 1155, 1156, 1157, 1158, 1159, 1160, 1161, 1162,
75 			1163, 1164, 1165, 1166, 1167, 1170, 1171, 1172, 1173, 1174, 1175, 1176, 1177, 1178, 1179, 1180,
76 			1181, 1182, 1183, 1186, 1187, 1188, 1189, 1190, 1191, 1192, 1193, 1194, 1195, 1196, 1197, 1198,
77 			1199, 1202, 1203, 1204, 1205, 1206, 1207, 1208, 1209, 1210, 1211, 1212, 1213, 1214, 1215, 1284,
78 			1285, 1286, 1287, 1288, 1289, 1290, 1291, 1292, 1293, 1294, 1295, 1300, 1301, 1302, 1303, 1304,
79 			1305, 1306, 1307, 1308, 1309, 1310, 1311, 1316, 1317, 1318, 1319, 1320, 1321, 1322, 1323, 1324,
80 			1325, 1326, 1327, 1332, 1333, 1334, 1335, 1336, 1337, 1338, 1339, 1340, 1341, 1342, 1343, 2113,
81 			2114, 2116, 2145, 2146, 2148, 2178, 2179, 2180, 2181, 2182, 2183, 2184, 2185, 2186, 2187, 2188,
82 			2189, 2190, 2191, 2210, 2211, 2212, 2213, 2214, 2215, 2216, 2217, 2218, 2219, 2220, 2221, 2222,
83 			2223, 2308, 2309, 2310, 2311, 2312, 2313, 2314, 2315, 2316, 2317, 2318, 2319, 2340, 2341, 2342,
84 			2343, 2344, 2345, 2346, 2347, 2348, 2349, 2350, 2351 };
85 
IsValid(int n)86 		private bool IsValid (int n)
87 		{
88 			return (Array.BinarySearch (ValidValues, n) >= 0);
89 		}
90 
91 		[Test]
92 		// note: this one can be _very_ slow under a debugger due to the high number of exceptions
MakeVersionSafeName()93 		public void MakeVersionSafeName ()
94 		{
95 			for (int i = 0; i < 64; i++) {
96 				ResourceScope from = (ResourceScope) i;
97 				for (int j = 0; j < 64; j++) {
98 					ResourceScope to = (ResourceScope) j;
99 					bool valid = IsValid ((i << 6) | j);
100 					string from_to = String.Format ("From ({2}) {0} to ({3}) {1}", from, to, (int)from, (int)to);
101 					try {
102 						string s = VersioningHelper.MakeVersionSafeName ("a", from, to);
103 						Assert.IsTrue (valid, from_to);
104 					}
105 					catch (ArgumentException) {
106 						Assert.IsFalse (valid, from_to);
107 					}
108 				}
109 			}
110 		}
111 
112 		[Test]
113 		[ExpectedException (typeof (ArgumentException))]
MakeVersionSafeName_SingleUnWorking()114 		public void MakeVersionSafeName_SingleUnWorking ()
115 		{
116 			VersioningHelper.MakeVersionSafeName ("a", ResourceScope.None, ResourceScope.None);
117 		}
118 
119 		[Test]
ConvertTo_AppDomain()120 		public void ConvertTo_AppDomain ()
121 		{
122 			// if appdomain is present in "from" then the ouput is unchanged
123 			Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.AppDomain, ResourceScope.Library), "1a");
124 			Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.AppDomain, ResourceScope.AppDomain), "1b");
125 			Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.AppDomain, ResourceScope.AppDomain | ResourceScope.Machine), "1c");
126 
127 			// if "from" doesn't have appdomain then the appdomain id is appended
128 			Assert.IsTrue (VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Machine, ResourceScope.AppDomain).StartsWith ("a_"), "2");
129 		}
130 
131 		[Test]
ConvertTo_Process()132 		public void ConvertTo_Process ()
133 		{
134 			// if process is present in "from" then the ouput is unchanged
135 			Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Process, ResourceScope.Library), "1a");
136 			Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Process, ResourceScope.Process), "1b");
137 			Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Process, ResourceScope.Process | ResourceScope.Library), "1c");
138 
139 			// if "from" doesn't have process then the process id is appended
140 			Assert.IsTrue (VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Machine, ResourceScope.Process).StartsWith ("a_"), "2");
141 		}
142 
143 		[Test]
ConvertTo_AppDomain_Process()144 		public void ConvertTo_AppDomain_Process ()
145 		{
146 			// if the appdomain is present then the output is unchanged (process isn't appended)
147 			Assert.AreEqual ("a", VersioningHelper.MakeVersionSafeName ("a", ResourceScope.AppDomain, ResourceScope.AppDomain | ResourceScope.Process), "1a");
148 			// if the process is present then appdomain is appended to the output
149 			Assert.IsTrue (VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Process, ResourceScope.AppDomain | ResourceScope.Process).StartsWith ("a_"), "1b");
150 
151 			// if "from" doesn't have process nor appdomain then both id are appended
152 			string s = VersioningHelper.MakeVersionSafeName ("a", ResourceScope.Machine, ResourceScope.AppDomain);
153 			int p1 = s.IndexOf ("_");
154 			Assert.IsTrue ((p1 > 0), "first _");
155 			int p2 = s.LastIndexOf ("_");
156 			Assert.IsTrue (((p2 > 0) && (p2 != p1)), "second _");
157 		}
158 	}
159 }
160 
161