verifier.cs 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  1. //
  2. // verifier.cs: compares two assemblies and reports differences.
  3. //
  4. // Author:
  5. // Sergey Chaban ([email protected])
  6. //
  7. // (C) Sergey Chaban ([email protected])
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Collections;
  12. using System.Reflection;
  13. namespace Mono.Verifier {
  14. ////////////////////////////////
  15. // Collections
  16. ////////////////////////////////
  17. public abstract class MemberCollection : IEnumerable {
  18. public delegate MemberInfo [] InfoQuery (Type type, BindingFlags bindings);
  19. public delegate bool MemberComparer (MemberInfo mi1, MemberInfo mi2);
  20. protected SortedList list;
  21. protected MemberComparer comparer;
  22. protected BindingFlags bindings;
  23. protected MemberCollection (Type type, InfoQuery query, MemberComparer comparer, BindingFlags bindings)
  24. {
  25. if (query == null)
  26. throw new NullReferenceException ("Invalid query delegate.");
  27. if (comparer == null)
  28. throw new NullReferenceException ("Invalid comparer.");
  29. this.comparer = comparer;
  30. this.bindings = bindings;
  31. this.list = new SortedList ();
  32. MemberInfo [] data = query (type, bindings);
  33. foreach (MemberInfo info in data) {
  34. this.list [info.Name] = info;
  35. }
  36. }
  37. public MemberInfo this [string name] {
  38. get {
  39. return list [name] as MemberInfo;
  40. }
  41. }
  42. public override int GetHashCode ()
  43. {
  44. return list.GetHashCode ();
  45. }
  46. public override bool Equals (object o)
  47. {
  48. bool res = (o is MemberCollection);
  49. if (res) {
  50. MemberCollection another = o as MemberCollection;
  51. IEnumerator it = GetEnumerator ();
  52. while (it.MoveNext () && res) {
  53. MemberInfo inf1 = it.Current as MemberInfo;
  54. MemberInfo inf2 = another [inf1.Name];
  55. res &= comparer (inf1, inf2);
  56. }
  57. }
  58. return res;
  59. }
  60. public static bool operator == (MemberCollection c1, MemberCollection c2)
  61. {
  62. return c1.Equals (c2);
  63. }
  64. public static bool operator != (MemberCollection c1, MemberCollection c2)
  65. {
  66. return !(c1 == c2);
  67. }
  68. public IEnumerator GetEnumerator()
  69. {
  70. return new Iterator (this);
  71. }
  72. internal class Iterator : IEnumerator {
  73. private MemberCollection host;
  74. private int pos;
  75. internal Iterator (MemberCollection host)
  76. {
  77. this.host=host;
  78. this.Reset ();
  79. }
  80. /// <summary></summary>
  81. public object Current
  82. {
  83. get {
  84. if (host != null && pos >=0 && pos < host.list.Count) {
  85. return host.list.GetByIndex (pos);
  86. } else {
  87. return null;
  88. }
  89. }
  90. }
  91. /// <summary></summary>
  92. public bool MoveNext ()
  93. {
  94. if (host!=null) {
  95. return (++pos) < host.list.Count;
  96. } else {
  97. return false;
  98. }
  99. }
  100. /// <summary></summary>
  101. public void Reset ()
  102. {
  103. this.pos = -1;
  104. }
  105. }
  106. }
  107. //--- Method collections
  108. public abstract class MethodCollectionBase : MemberCollection {
  109. protected MethodCollectionBase (Type type, BindingFlags bindings)
  110. : base (type, new InfoQuery (Query), new MemberComparer (Comparer), bindings)
  111. {
  112. }
  113. private static MemberInfo [] Query (Type type, BindingFlags bindings)
  114. {
  115. // returns MethodInfo []
  116. return type.GetMethods (bindings);
  117. }
  118. private static bool Comparer (MemberInfo mi1, MemberInfo mi2)
  119. {
  120. bool res = false;
  121. if (mi1 is MethodInfo && (mi2 == null || mi2 is MethodInfo)) {
  122. MethodInfo inf1 = mi1 as MethodInfo;
  123. MethodInfo inf2 = mi2 as MethodInfo;
  124. res = Compare.Methods (inf1, inf2);
  125. } else {
  126. Verifier.log.Write ("internal-error", "Wrong comparer arguments.", ImportanceLevel.HIGH);
  127. }
  128. return res;
  129. }
  130. }
  131. public class PublicMethods : MethodCollectionBase {
  132. public PublicMethods (Type type)
  133. : base (type, BindingFlags.Public | BindingFlags.Instance)
  134. {
  135. }
  136. }
  137. public class PublicStaticMethods : MethodCollectionBase {
  138. public PublicStaticMethods (Type type)
  139. : base (type, BindingFlags.Public | BindingFlags.Static)
  140. {
  141. }
  142. }
  143. public class NonPublicMethods : MethodCollectionBase {
  144. public NonPublicMethods (Type type)
  145. : base (type, BindingFlags.NonPublic | BindingFlags.Instance)
  146. {
  147. }
  148. }
  149. public class NonPublicStaticMethods : MethodCollectionBase {
  150. public NonPublicStaticMethods (Type type)
  151. : base (type, BindingFlags.NonPublic | BindingFlags.Static)
  152. {
  153. }
  154. }
  155. //--- Field collections
  156. public abstract class FieldCollectionBase : MemberCollection {
  157. protected FieldCollectionBase (Type type, BindingFlags bindings)
  158. : base (type, new InfoQuery (Query), new MemberComparer (Comparer), bindings)
  159. {
  160. }
  161. private static MemberInfo [] Query (Type type, BindingFlags bindings)
  162. {
  163. // returns FieldInfo []
  164. return type.GetFields (bindings);
  165. }
  166. private static bool Comparer (MemberInfo mi1, MemberInfo mi2)
  167. {
  168. bool res = false;
  169. if (mi1 is FieldInfo && (mi2 == null || mi2 is FieldInfo)) {
  170. FieldInfo inf1 = mi1 as FieldInfo;
  171. FieldInfo inf2 = mi2 as FieldInfo;
  172. res = Compare.Fields (inf1, inf2);
  173. } else {
  174. Verifier.log.Write ("internal-error", "Wrong comparer arguments.", ImportanceLevel.HIGH);
  175. }
  176. return res;
  177. }
  178. }
  179. public class PublicFields : FieldCollectionBase {
  180. public PublicFields (Type type)
  181. : base (type, BindingFlags.Public | BindingFlags.Instance)
  182. {
  183. }
  184. }
  185. public class PublicStaticFields : FieldCollectionBase {
  186. public PublicStaticFields (Type type)
  187. : base (type, BindingFlags.Public | BindingFlags.Static)
  188. {
  189. }
  190. }
  191. public class NonPublicFields : FieldCollectionBase {
  192. public NonPublicFields (Type type)
  193. : base (type, BindingFlags.NonPublic | BindingFlags.Instance)
  194. {
  195. }
  196. }
  197. public class NonPublicStaticFields : FieldCollectionBase {
  198. public NonPublicStaticFields (Type type)
  199. : base (type, BindingFlags.NonPublic | BindingFlags.Static)
  200. {
  201. }
  202. }
  203. public abstract class AbstractTypeStuff {
  204. public readonly Type type;
  205. public AbstractTypeStuff (Type type)
  206. {
  207. if (type == null)
  208. throw new NullReferenceException ("Invalid type.");
  209. this.type = type;
  210. }
  211. public override int GetHashCode ()
  212. {
  213. return type.GetHashCode ();
  214. }
  215. public static bool operator == (AbstractTypeStuff t1, AbstractTypeStuff t2)
  216. {
  217. if ((t1 as object) == null) {
  218. if ((t2 as object) == null) return true;
  219. return false;
  220. }
  221. return t1.Equals (t2);
  222. }
  223. public static bool operator != (AbstractTypeStuff t1, AbstractTypeStuff t2)
  224. {
  225. return !(t1 == t2);
  226. }
  227. public override bool Equals (object o)
  228. {
  229. return (o is AbstractTypeStuff && CompareTypes (o as AbstractTypeStuff));
  230. }
  231. protected virtual bool CompareTypes (AbstractTypeStuff that)
  232. {
  233. Verifier.Log.Write ("info", "Comparing types.", ImportanceLevel.LOW);
  234. bool res;
  235. res = Compare.Types (this.type, that.type);
  236. return res;
  237. }
  238. }
  239. /// <summary>
  240. /// Represents a class.
  241. /// </summary>
  242. public class ClassStuff : AbstractTypeStuff {
  243. public PublicMethods publicMethods;
  244. public PublicStaticMethods publicStaticMethods;
  245. public NonPublicMethods nonpublicMethods;
  246. public NonPublicStaticMethods nonpublicStaticMethods;
  247. public PublicFields publicFields;
  248. public PublicStaticFields publicStaticFields;
  249. public NonPublicFields nonpublicFields;
  250. public NonPublicStaticFields nonpublicStaticFields;
  251. public ClassStuff (Type type) : base (type)
  252. {
  253. publicMethods = new PublicMethods (type);
  254. publicStaticMethods = new PublicStaticMethods (type);
  255. nonpublicMethods = new NonPublicMethods (type);
  256. nonpublicStaticMethods = new NonPublicStaticMethods (type);
  257. publicFields = new PublicFields (type);
  258. publicStaticFields = new PublicStaticFields (type);
  259. nonpublicFields = new NonPublicFields (type);
  260. nonpublicStaticFields = new NonPublicStaticFields (type);
  261. }
  262. public override int GetHashCode ()
  263. {
  264. return base.GetHashCode ();
  265. }
  266. private bool CompareMethods (ClassStuff that)
  267. {
  268. bool res = true;
  269. bool ok;
  270. Verifier.Log.Write ("info", "Comparing public instance methods.", ImportanceLevel.LOW);
  271. ok = (this.publicMethods == that.publicMethods);
  272. res &= ok;
  273. if (!ok && Verifier.stopOnError) return res;
  274. Verifier.Log.Write ("info", "Comparing public static methods.", ImportanceLevel.LOW);
  275. ok = (this.publicStaticMethods == that.publicStaticMethods);
  276. res &= ok;
  277. if (!ok && Verifier.stopOnError) return res;
  278. Verifier.Log.Write ("info", "Comparing non-public instance methods.", ImportanceLevel.LOW);
  279. ok = (this.nonpublicMethods == that.nonpublicMethods);
  280. res &= ok;
  281. if (!ok && Verifier.stopOnError) return res;
  282. Verifier.Log.Write ("info", "Comparing non-public static methods.", ImportanceLevel.LOW);
  283. ok = (this.nonpublicStaticMethods == that.nonpublicStaticMethods);
  284. res &= ok;
  285. if (!ok && Verifier.stopOnError) return res;
  286. return res;
  287. }
  288. private bool CompareFields (ClassStuff that)
  289. {
  290. bool res = true;
  291. bool ok;
  292. Verifier.Log.Write ("info", "Comparing public instance fields.", ImportanceLevel.LOW);
  293. ok = (this.publicFields == that.publicFields);
  294. res &= ok;
  295. if (!ok && Verifier.stopOnError) return res;
  296. Verifier.Log.Write ("info", "Comparing public static fields.", ImportanceLevel.LOW);
  297. ok = (this.publicStaticFields == that.publicStaticFields);
  298. res &= ok;
  299. if (!ok && Verifier.stopOnError) return res;
  300. Verifier.Log.Write ("info", "Comparing non-public instance fields.", ImportanceLevel.LOW);
  301. ok = (this.nonpublicFields == that.nonpublicFields);
  302. res &= ok;
  303. if (!ok && Verifier.stopOnError) return res;
  304. Verifier.Log.Write ("info", "Comparing non-public static fields.", ImportanceLevel.LOW);
  305. ok = (this.nonpublicStaticFields == that.nonpublicStaticFields);
  306. res &= ok;
  307. if (!ok && Verifier.stopOnError) return res;
  308. return res;
  309. }
  310. public override bool Equals (object o)
  311. {
  312. bool res = (o is ClassStuff);
  313. if (res) {
  314. ClassStuff that = o as ClassStuff;
  315. res &= this.CompareTypes (that);
  316. if (!res && Verifier.stopOnError) return res;
  317. res &= this.CompareMethods (that);
  318. if (!res && Verifier.stopOnError) return res;
  319. res &= this.CompareFields (that);
  320. if (!res && Verifier.stopOnError) return res;
  321. }
  322. return res;
  323. }
  324. }
  325. /// <summary>
  326. /// Represents an interface.
  327. /// </summary>
  328. public class InterfaceStuff : AbstractTypeStuff {
  329. public PublicMethods publicMethods;
  330. public InterfaceStuff (Type type) : base (type)
  331. {
  332. publicMethods = new PublicMethods (type);
  333. }
  334. public override int GetHashCode ()
  335. {
  336. return base.GetHashCode ();
  337. }
  338. public override bool Equals (object o)
  339. {
  340. bool res = (o is InterfaceStuff);
  341. if (res) {
  342. bool ok;
  343. InterfaceStuff that = o as InterfaceStuff;
  344. res = this.CompareTypes (that);
  345. if (!res && Verifier.stopOnError) return res;
  346. Verifier.Log.Write ("info", "Comparing interface methods.", ImportanceLevel.LOW);
  347. ok = (this.publicMethods == that.publicMethods);
  348. res &= ok;
  349. if (!ok && Verifier.stopOnError) return res;
  350. }
  351. return res;
  352. }
  353. }
  354. /// <summary>
  355. /// Represents an enumeration.
  356. /// </summary>
  357. public class EnumStuff : AbstractTypeStuff {
  358. //public FieldInfo [] members;
  359. public string baseType;
  360. public Hashtable enumTable;
  361. public bool isFlags;
  362. public EnumStuff (Type type) : base (type)
  363. {
  364. //members = type.GetFields (BindingFlags.Public | BindingFlags.Static);
  365. Array values = Enum.GetValues (type);
  366. Array names = Enum.GetNames (type);
  367. baseType = Enum.GetUnderlyingType (type).Name;
  368. enumTable = new Hashtable ();
  369. object [] attrs = type.GetCustomAttributes (false);
  370. isFlags = (attrs != null && attrs.Length > 0);
  371. if (isFlags) {
  372. foreach (object attr in attrs) {
  373. isFlags |= (attr is FlagsAttribute);
  374. }
  375. }
  376. int indx = 0;
  377. foreach (string id in names) {
  378. enumTable [id] = Convert.ToInt64(values.GetValue(indx) as Enum);
  379. ++indx;
  380. }
  381. }
  382. public override int GetHashCode ()
  383. {
  384. return base.GetHashCode ();
  385. }
  386. public override bool Equals (object o)
  387. {
  388. bool res = (o is EnumStuff);
  389. bool ok;
  390. if (res) {
  391. EnumStuff that = o as EnumStuff;
  392. ok = this.CompareTypes (that);
  393. res &= ok;
  394. if (!ok && Verifier.stopOnError) return res;
  395. ok = (this.baseType == that.baseType);
  396. res &= ok;
  397. if (!ok) {
  398. Verifier.log.Write ("error",
  399. String.Format ("Underlying types mismatch [{0}, {1}].", this.baseType, that.baseType),
  400. ImportanceLevel.MEDIUM);
  401. if (Verifier.stopOnError) return res;
  402. }
  403. Verifier.Log.Write ("info", "Comparing [Flags] attribute.");
  404. ok = !(this.isFlags ^ that.isFlags);
  405. res &= ok;
  406. if (!ok) {
  407. Verifier.log.Write ("error",
  408. String.Format ("[Flags] attribute mismatch ({0} : {1}).", this.isFlags ? "Yes" : "No", that.isFlags ? "Yes" : "No"),
  409. ImportanceLevel.MEDIUM);
  410. if (Verifier.stopOnError) return res;
  411. }
  412. Verifier.Log.Write ("info", "Comparing enum values.");
  413. ICollection names = enumTable.Keys;
  414. foreach (string id in names) {
  415. ok = that.enumTable.ContainsKey (id);
  416. res &= ok;
  417. if (!ok) {
  418. Verifier.log.Write ("error", String.Format("{0} absent in enumeration.", id),
  419. ImportanceLevel.MEDIUM);
  420. if (Verifier.stopOnError) return res;
  421. }
  422. if (ok) {
  423. long val1 = (long) this.enumTable [id];
  424. long val2 = (long) that.enumTable [id];
  425. ok = (val1 == val2);
  426. res &= ok;
  427. if (!ok) {
  428. Verifier.log.Write ("error",
  429. String.Format ("Enum values mismatch [{0}: {1} != {2}].", id, val1, val2),
  430. ImportanceLevel.MEDIUM);
  431. if (Verifier.stopOnError) return res;
  432. }
  433. }
  434. }
  435. }
  436. return res;
  437. }
  438. }
  439. public sealed class TypeArray {
  440. public static readonly TypeArray empty = new TypeArray (Type.EmptyTypes);
  441. public Type [] types;
  442. public TypeArray (Type [] types)
  443. {
  444. this.types = new Type [types.Length];
  445. for (int i = 0; i < types.Length; i++) {
  446. this.types.SetValue (types.GetValue (i), i);
  447. }
  448. }
  449. }
  450. public class AssemblyLoader {
  451. public delegate void Hook (TypeArray assemblyTypes);
  452. private static Hashtable cache;
  453. private Hook hook;
  454. static AssemblyLoader ()
  455. {
  456. cache = new Hashtable (11);
  457. }
  458. public AssemblyLoader (Hook hook)
  459. {
  460. if (hook == null)
  461. throw new NullReferenceException ("Invalid loader hook.");
  462. this.hook = hook;
  463. }
  464. public bool LoadFrom (string assemblyName)
  465. {
  466. bool res = false;
  467. try {
  468. TypeArray types = TypeArray.empty;
  469. lock (cache) {
  470. if (cache.Contains (assemblyName)) {
  471. types = (cache [assemblyName] as TypeArray);
  472. if (types == null) types = TypeArray.empty;
  473. } else {
  474. Assembly asm = Assembly.LoadFrom (assemblyName);
  475. Type [] allTypes = asm.GetTypes ();
  476. if (allTypes == null) allTypes = Type.EmptyTypes;
  477. types = new TypeArray (allTypes);
  478. cache [assemblyName] = types;
  479. }
  480. }
  481. hook (types);
  482. res = true;
  483. } catch (ReflectionTypeLoadException rtle) {
  484. // FIXME: Should we try to recover? Use loaded portion of types.
  485. Type [] loaded = rtle.Types;
  486. for (int i = 0, xCnt = 0; i < loaded.Length; i++) {
  487. if (loaded [i] == null) {
  488. Verifier.log.Write ("fatal error",
  489. String.Format ("Unable to load {0}, reason - {1}", loaded [i], rtle.LoaderExceptions [xCnt++]),
  490. ImportanceLevel.LOW);
  491. }
  492. }
  493. } catch (FileNotFoundException fnfe) {
  494. Verifier.log.Write ("fatal error", fnfe.ToString (), ImportanceLevel.LOW);
  495. } catch (Exception x) {
  496. Verifier.log.Write ("fatal error", x.ToString (), ImportanceLevel.LOW);
  497. }
  498. return res;
  499. }
  500. }
  501. public abstract class AbstractTypeCollection : SortedList {
  502. private AssemblyLoader loader;
  503. public AbstractTypeCollection ()
  504. {
  505. loader = new AssemblyLoader (new AssemblyLoader.Hook (LoaderHook));
  506. }
  507. public AbstractTypeCollection (string assemblyName) : this ()
  508. {
  509. LoadFrom (assemblyName);
  510. }
  511. public abstract void LoaderHook (TypeArray types);
  512. public bool LoadFrom (string assemblyName)
  513. {
  514. return loader.LoadFrom (assemblyName);
  515. }
  516. }
  517. public class ClassCollection : AbstractTypeCollection {
  518. public ClassCollection () : base ()
  519. {
  520. }
  521. public ClassCollection (string assemblyName)
  522. : base (assemblyName)
  523. {
  524. }
  525. public override void LoaderHook (TypeArray types)
  526. {
  527. foreach (Type type in types.types) {
  528. if (type.IsClass) {
  529. this [type.FullName] = new ClassStuff (type);
  530. }
  531. }
  532. }
  533. }
  534. public class InterfaceCollection : AbstractTypeCollection {
  535. public InterfaceCollection () : base ()
  536. {
  537. }
  538. public InterfaceCollection (string assemblyName)
  539. : base (assemblyName)
  540. {
  541. }
  542. public override void LoaderHook (TypeArray types)
  543. {
  544. foreach (Type type in types.types) {
  545. if (type.IsInterface) {
  546. this [type.FullName] = new InterfaceStuff (type);
  547. }
  548. }
  549. }
  550. }
  551. public class EnumCollection : AbstractTypeCollection {
  552. public EnumCollection () : base ()
  553. {
  554. }
  555. public EnumCollection (string assemblyName)
  556. : base (assemblyName)
  557. {
  558. }
  559. public override void LoaderHook (TypeArray types)
  560. {
  561. foreach (Type type in types.types) {
  562. if (type.IsEnum) {
  563. this [type.FullName] = new EnumStuff (type);
  564. }
  565. }
  566. }
  567. }
  568. public class AssemblyStuff {
  569. public string name;
  570. public bool valid;
  571. public ClassCollection classes;
  572. public InterfaceCollection interfaces;
  573. public EnumCollection enums;
  574. protected delegate bool Comparer (AssemblyStuff asm1, AssemblyStuff asm2);
  575. private static ArrayList comparers;
  576. static AssemblyStuff ()
  577. {
  578. comparers = new ArrayList ();
  579. comparers.Add (new Comparer (CompareNumClasses));
  580. comparers.Add (new Comparer (CompareNumInterfaces));
  581. comparers.Add (new Comparer (CompareClasses));
  582. comparers.Add (new Comparer (CompareInterfaces));
  583. comparers.Add (new Comparer (CompareEnums));
  584. }
  585. protected static bool CompareNumClasses (AssemblyStuff asm1, AssemblyStuff asm2)
  586. {
  587. bool res = (asm1.classes.Count == asm2.classes.Count);
  588. if (!res) Verifier.Log.Write ("error", "Number of classes mismatch.", ImportanceLevel.MEDIUM);
  589. return res;
  590. }
  591. protected static bool CompareNumInterfaces (AssemblyStuff asm1, AssemblyStuff asm2)
  592. {
  593. bool res = (asm1.interfaces.Count == asm2.interfaces.Count);
  594. if (!res) Verifier.Log.Write ("error", "Number of interfaces mismatch.", ImportanceLevel.MEDIUM);
  595. return res;
  596. }
  597. protected static bool CompareClasses (AssemblyStuff asm1, AssemblyStuff asm2)
  598. {
  599. bool res = true;
  600. Verifier.Log.Write ("info", "Comparing classes.");
  601. foreach (DictionaryEntry c in asm1.classes) {
  602. string className = c.Key as string;
  603. Verifier.Log.Write ("class", className);
  604. ClassStuff class1 = c.Value as ClassStuff;
  605. ClassStuff class2 = asm2.classes [className] as ClassStuff;
  606. if (class2 == null) {
  607. Verifier.Log.Write ("error", String.Format ("There is no such class in {0}", asm2.name));
  608. res = false;
  609. if (Verifier.stopOnError || !Verifier.ignoreMissingTypes) return res;
  610. continue;
  611. }
  612. res &= (class1 == class2);
  613. if (!res && Verifier.stopOnError) return res;
  614. }
  615. return res;
  616. }
  617. protected static bool CompareInterfaces (AssemblyStuff asm1, AssemblyStuff asm2)
  618. {
  619. bool res = true;
  620. Verifier.Log.Write ("info", "Comparing interfaces.");
  621. foreach (DictionaryEntry ifc in asm1.interfaces) {
  622. string ifcName = ifc.Key as string;
  623. Verifier.Log.Write ("interface", ifcName);
  624. InterfaceStuff ifc1 = ifc.Value as InterfaceStuff;
  625. InterfaceStuff ifc2 = asm2.interfaces [ifcName] as InterfaceStuff;
  626. if (ifc2 == null) {
  627. Verifier.Log.Write ("error", String.Format ("There is no such interface in {0}", asm2.name));
  628. res = false;
  629. if (Verifier.stopOnError || !Verifier.ignoreMissingTypes) return res;
  630. continue;
  631. }
  632. res &= (ifc1 == ifc2);
  633. if (!res && Verifier.stopOnError) return res;
  634. }
  635. return res;
  636. }
  637. protected static bool CompareEnums (AssemblyStuff asm1, AssemblyStuff asm2)
  638. {
  639. bool res = true;
  640. Verifier.Log.Write ("info", "Comparing enums.");
  641. foreach (DictionaryEntry e in asm1.enums) {
  642. string enumName = e.Key as string;
  643. Verifier.Log.Write ("enum", enumName);
  644. EnumStuff e1 = e.Value as EnumStuff;
  645. EnumStuff e2 = asm2.enums [enumName] as EnumStuff;
  646. if (e2 == null) {
  647. Verifier.Log.Write ("error", String.Format ("There is no such enum in {0}", asm2.name));
  648. res = false;
  649. if (Verifier.stopOnError || !Verifier.ignoreMissingTypes) return res;
  650. continue;
  651. }
  652. res &= (e1 == e2);
  653. if (!res && Verifier.stopOnError) return res;
  654. }
  655. return res;
  656. }
  657. public AssemblyStuff (string assemblyName)
  658. {
  659. this.name = assemblyName;
  660. valid = false;
  661. }
  662. public bool Load ()
  663. {
  664. bool res = true;
  665. bool ok;
  666. classes = new ClassCollection ();
  667. ok = classes.LoadFrom (name);
  668. res &= ok;
  669. if (!ok) Verifier.log.Write ("error", String.Format ("Unable to load classes from {0}.", name), ImportanceLevel.HIGH);
  670. interfaces = new InterfaceCollection ();
  671. ok = interfaces.LoadFrom (name);
  672. res &= ok;
  673. if (!ok) Verifier.log.Write ("error", String.Format ("Unable to load interfaces from {0}.", name), ImportanceLevel.HIGH);
  674. enums = new EnumCollection ();
  675. ok = enums.LoadFrom (name);
  676. res &= ok;
  677. if (!ok) Verifier.log.Write ("error", String.Format ("Unable to load enums from {0}.", name), ImportanceLevel.HIGH);
  678. valid = res;
  679. return res;
  680. }
  681. public override bool Equals (object o)
  682. {
  683. bool res = (o is AssemblyStuff);
  684. if (res) {
  685. AssemblyStuff that = o as AssemblyStuff;
  686. IEnumerator it = comparers.GetEnumerator ();
  687. while ((res || !Verifier.stopOnError) && it.MoveNext ()) {
  688. Comparer compare = it.Current as Comparer;
  689. res &= compare (this, that);
  690. }
  691. }
  692. return res;
  693. }
  694. public static bool operator == (AssemblyStuff asm1, AssemblyStuff asm2)
  695. {
  696. return asm1.Equals (asm2);
  697. }
  698. public static bool operator != (AssemblyStuff asm1, AssemblyStuff asm2)
  699. {
  700. return !(asm1 == asm2);
  701. }
  702. public override int GetHashCode ()
  703. {
  704. return classes.GetHashCode () ^ interfaces.GetHashCode ();
  705. }
  706. public override string ToString ()
  707. {
  708. string res;
  709. if (valid) {
  710. res = String.Format ("Asssembly {0}, valid, {1} classes, {2} interfaces, {3} enums.",
  711. name, classes.Count, interfaces.Count, enums.Count);
  712. } else {
  713. res = String.Format ("Asssembly {0}, invalid.", name);
  714. }
  715. return res;
  716. }
  717. }
  718. ////////////////////////////////
  719. // Compare
  720. ////////////////////////////////
  721. public sealed class Compare {
  722. private Compare ()
  723. {
  724. }
  725. public static bool Parameters (ParameterInfo[] params1, ParameterInfo[] params2)
  726. {
  727. bool res = true;
  728. if (params1.Length != params2.Length) {
  729. Verifier.Log.Write ("Parameter count mismatch.");
  730. return false;
  731. }
  732. int count = params1.Length;
  733. for (int i = 0; i < count && res; i++) {
  734. if (params1 [i].Name != params2 [i].Name) {
  735. Verifier.Log.Write ("error", String.Format ("Parameters names mismatch {0}, {1}.", params1 [i].Name, params2 [i].Name));
  736. res = false;
  737. if (Verifier.stopOnError) break;
  738. }
  739. Verifier.Log.Write ("parameter", params1 [i].Name);
  740. if (!Compare.Types (params1 [i].ParameterType, params2 [i].ParameterType)) {
  741. Verifier.Log.Write ("error", String.Format ("Parameters types mismatch {0}, {1}.", params1 [i].ParameterType, params2 [i].ParameterType));
  742. res = false;
  743. if (Verifier.stopOnError) break;
  744. }
  745. if (Verifier.checkOptionalFlags) {
  746. if (params1 [i].IsIn != params2 [i].IsIn) {
  747. Verifier.Log.Write ("error", "[in] mismatch.");
  748. res = false;
  749. if (Verifier.stopOnError) break;
  750. }
  751. if (params1 [i].IsOut != params2 [i].IsOut) {
  752. Verifier.Log.Write ("error", "[out] mismatch.");
  753. res = false;
  754. if (Verifier.stopOnError) break;
  755. }
  756. if (params1 [i].IsRetval != params2 [i].IsRetval) {
  757. Verifier.Log.Write ("error", "[ref] mismatch.");
  758. res = false;
  759. if (Verifier.stopOnError) break;
  760. }
  761. if (params1 [i].IsOptional != params2 [i].IsOptional) {
  762. Verifier.Log.Write ("error", "Optional flag mismatch.");
  763. res = false;
  764. if (Verifier.stopOnError) break;
  765. }
  766. } // checkOptionalFlags
  767. }
  768. return res;
  769. }
  770. public static bool Methods (MethodInfo mi1, MethodInfo mi2)
  771. {
  772. if (mi2 == null) {
  773. Verifier.Log.Write ("error", String.Format ("There is no such method {0}.", mi1.Name), ImportanceLevel.MEDIUM);
  774. return false;
  775. }
  776. Verifier.Log.Flush ();
  777. Verifier.Log.Write ("method", String.Format ("{0}.", mi1.Name));
  778. bool res = true;
  779. bool ok;
  780. string expected;
  781. ok = Compare.Types (mi1.ReturnType, mi2.ReturnType);
  782. res &= ok;
  783. if (!ok) {
  784. Verifier.Log.Write ("error", "Return types mismatch.", ImportanceLevel.MEDIUM);
  785. if (Verifier.stopOnError) return res;
  786. }
  787. ok = (mi1.IsAbstract == mi2.IsAbstract);
  788. res &= ok;
  789. if (!ok) {
  790. expected = (mi1.IsAbstract) ? "abstract" : "non-abstract";
  791. Verifier.Log.Write ("error", String.Format ("Expected to be {0}.", expected), ImportanceLevel.MEDIUM);
  792. if (Verifier.stopOnError) return res;
  793. }
  794. ok = (mi1.IsVirtual == mi2.IsVirtual);
  795. res &= ok;
  796. if (!ok) {
  797. expected = (mi1.IsVirtual) ? "virtual" : "non-virtual";
  798. Verifier.Log.Write ("error", String.Format ("Expected to be {0}.", expected), ImportanceLevel.MEDIUM);
  799. if (Verifier.stopOnError) return res;
  800. }
  801. ok = (mi1.IsFinal == mi2.IsFinal);
  802. res &= ok;
  803. if (!ok) {
  804. expected = (mi1.IsFinal) ? "final" : "overridable";
  805. Verifier.Log.Write ("error", String.Format ("Expected to be {0}.", expected), ImportanceLevel.MEDIUM);
  806. if (Verifier.stopOnError) return res;
  807. }
  808. // compare access modifiers
  809. ok = (mi1.IsPrivate == mi2.IsPrivate);
  810. res &= ok;
  811. if (!ok) {
  812. expected = (mi1.IsPublic) ? "public" : "private";
  813. Verifier.Log.Write ("error", String.Format ("Accessibility levels mismatch (expected [{0}]).", expected), ImportanceLevel.MEDIUM);
  814. if (Verifier.stopOnError) return res;
  815. }
  816. ok = (mi1.IsFamily == mi2.IsFamily);
  817. res &= ok;
  818. if (!ok) {
  819. expected = (mi1.IsFamily) ? "protected" : "!protected";
  820. Verifier.Log.Write ("error", String.Format ("Accessibility levels mismatch (expected [{0}]).", expected), ImportanceLevel.MEDIUM);
  821. if (Verifier.stopOnError) return res;
  822. }
  823. ok = (mi1.IsAssembly == mi2.IsAssembly);
  824. res &= ok;
  825. if (!ok) {
  826. expected = (mi1.IsAssembly) ? "internal" : "!internal";
  827. Verifier.Log.Write ("error", String.Format ("Accessibility levels mismatch (expected [{0}]).", expected), ImportanceLevel.MEDIUM);
  828. if (Verifier.stopOnError) return res;
  829. }
  830. ok = (mi1.IsStatic == mi2.IsStatic);
  831. res &= ok;
  832. if (!ok) {
  833. expected = (mi1.IsStatic) ? "static" : "instance";
  834. Verifier.Log.Write ("error", String.Format ("Accessibility levels mismatch (expected [{0}]).", expected), ImportanceLevel.MEDIUM);
  835. if (Verifier.stopOnError) return res;
  836. }
  837. // parameters
  838. ok = Compare.Parameters (mi1.GetParameters (), mi2.GetParameters ());
  839. res &= ok;
  840. if (!ok && Verifier.stopOnError) return res;
  841. ok = (mi1.CallingConvention == mi2.CallingConvention);
  842. res &= ok;
  843. if (!ok) {
  844. Verifier.Log.Write ("error", "Calling conventions mismatch.", ImportanceLevel.MEDIUM);
  845. if (Verifier.stopOnError) return res;
  846. }
  847. return res;
  848. }
  849. public static bool Fields (FieldInfo fi1, FieldInfo fi2)
  850. {
  851. if (fi2 == null) {
  852. Verifier.Log.Write ("error", String.Format ("There is no such field {0}.", fi1.Name), ImportanceLevel.MEDIUM);
  853. return false;
  854. }
  855. bool res = true;
  856. bool ok;
  857. string expected;
  858. Verifier.Log.Write ("field", String.Format ("{0}.", fi1.Name));
  859. ok = (fi1.IsPrivate == fi2.IsPrivate);
  860. res &= ok;
  861. if (!ok) {
  862. expected = (fi1.IsPublic) ? "public" : "private";
  863. Verifier.Log.Write ("error", String.Format ("Accessibility levels mismatch (expected [{0}]).", expected), ImportanceLevel.MEDIUM);
  864. if (Verifier.stopOnError) return res;
  865. }
  866. ok = (fi1.IsFamily == fi2.IsFamily);
  867. res &= ok;
  868. if (!ok) {
  869. expected = (fi1.IsFamily) ? "protected" : "!protected";
  870. Verifier.Log.Write ("error", String.Format ("Accessibility levels mismatch (expected [{0}]).", expected), ImportanceLevel.MEDIUM);
  871. if (Verifier.stopOnError) return res;
  872. }
  873. ok = (fi1.IsAssembly == fi2.IsAssembly);
  874. res &= ok;
  875. if (!ok) {
  876. expected = (fi1.IsAssembly) ? "internal" : "!internal";
  877. Verifier.Log.Write ("error", String.Format ("Accessibility levels mismatch (expected [{0}]).", expected), ImportanceLevel.MEDIUM);
  878. if (Verifier.stopOnError) return res;
  879. }
  880. ok = (fi1.IsInitOnly == fi2.IsInitOnly);
  881. res &= ok;
  882. if (!ok) {
  883. expected = (fi1.IsInitOnly) ? "readonly" : "!readonly";
  884. Verifier.Log.Write ("error", String.Format ("Accessibility levels mismatch (expected [{0}]).", expected), ImportanceLevel.MEDIUM);
  885. if (Verifier.stopOnError) return res;
  886. }
  887. ok = (fi1.IsStatic == fi2.IsStatic);
  888. res &= ok;
  889. if (!ok) {
  890. expected = (fi1.IsStatic) ? "static" : "instance";
  891. Verifier.Log.Write ("error", String.Format ("Accessibility levels mismatch (expected [{0}]).", expected), ImportanceLevel.MEDIUM);
  892. if (Verifier.stopOnError) return res;
  893. }
  894. return res;
  895. }
  896. public static bool Types (Type type1, Type type2)
  897. {
  898. // NOTE:
  899. // simply calling type1.Equals (type2) won't work,
  900. // types are in different assemblies hence they have
  901. // different (fully-qualified) names.
  902. int eqFlags = 0;
  903. eqFlags |= (type1.IsAbstract == type2.IsAbstract) ? 0 : 0x001;
  904. eqFlags |= (type1.IsClass == type2.IsClass) ? 0 : 0x002;
  905. eqFlags |= (type1.IsValueType == type2.IsValueType) ? 0 : 0x004;
  906. eqFlags |= (type1.IsPublic == type2.IsPublic) ? 0 : 0x008;
  907. eqFlags |= (type1.IsSealed == type2.IsSealed) ? 0 : 0x010;
  908. eqFlags |= (type1.IsEnum == type2.IsEnum) ? 0 : 0x020;
  909. eqFlags |= (type1.IsPointer == type2.IsPointer) ? 0 : 0x040;
  910. eqFlags |= (type1.IsPrimitive == type2.IsPrimitive) ? 0 : 0x080;
  911. bool res = (eqFlags == 0);
  912. if (!res) {
  913. // TODO: convert flags into descriptive message.
  914. Verifier.Log.Write ("error", "Types mismatch (0x" + eqFlags.ToString("X") + ").", ImportanceLevel.MEDIUM);
  915. }
  916. return res;
  917. }
  918. }
  919. ////////////////////////////////
  920. // Log
  921. ////////////////////////////////
  922. public enum ImportanceLevel : int {
  923. LOW = 0, MEDIUM, HIGH
  924. }
  925. public interface ILogger {
  926. void Write (string tag, string msg, ImportanceLevel importance);
  927. void Write (string msg, ImportanceLevel level);
  928. void Write (string tag, string msg);
  929. void Write (string msg);
  930. ImportanceLevel DefaultImportance {get; set;}
  931. void Flush ();
  932. void Close ();
  933. }
  934. public abstract class AbstractLogger : ILogger {
  935. private ImportanceLevel defImportance = ImportanceLevel.MEDIUM;
  936. public abstract void Write (string tag, string msg, ImportanceLevel importance);
  937. public abstract void Write (string msg, ImportanceLevel level);
  938. public virtual void Write (string tag, string msg)
  939. {
  940. Write (tag, msg, DefaultImportance);
  941. }
  942. public virtual void Write (string msg)
  943. {
  944. Write (msg, DefaultImportance);
  945. }
  946. public virtual ImportanceLevel DefaultImportance {
  947. get {
  948. return defImportance;
  949. }
  950. set {
  951. defImportance = value < ImportanceLevel.LOW
  952. ? ImportanceLevel.LOW
  953. : value > ImportanceLevel.HIGH
  954. ? ImportanceLevel.HIGH
  955. : value;
  956. }
  957. }
  958. public abstract void Flush ();
  959. public abstract void Close ();
  960. }
  961. public class TextLogger : AbstractLogger {
  962. private TextWriter writer;
  963. public TextLogger (TextWriter writer)
  964. {
  965. if (writer == null)
  966. throw new NullReferenceException ();
  967. this.writer = writer;
  968. }
  969. private void DoWrite (string tag, string msg)
  970. {
  971. if (tag != null && tag.Length > 0) {
  972. writer.WriteLine ("[{0}]\t{1}", tag, msg);
  973. } else {
  974. writer.WriteLine ("\t\t" + msg);
  975. }
  976. }
  977. public override void Write (string tag, string msg, ImportanceLevel importance)
  978. {
  979. int v = Log.VerboseLevel;
  980. switch (v) {
  981. case 0 :
  982. break;
  983. case 1 :
  984. if (importance >= ImportanceLevel.HIGH) {
  985. DoWrite (tag, msg);
  986. }
  987. break;
  988. case 2 :
  989. if (importance >= ImportanceLevel.MEDIUM) {
  990. DoWrite (tag, msg);
  991. }
  992. break;
  993. case 3 :
  994. DoWrite (tag, msg);
  995. break;
  996. default:
  997. break;
  998. }
  999. }
  1000. public override void Write (string msg, ImportanceLevel importance)
  1001. {
  1002. Write (null, msg, importance);
  1003. }
  1004. public override void Flush ()
  1005. {
  1006. Console.Out.Flush ();
  1007. }
  1008. public override void Close ()
  1009. {
  1010. if (writer != Console.Out && writer != Console.Error) {
  1011. writer.Close ();
  1012. }
  1013. }
  1014. }
  1015. public sealed class Log {
  1016. private static int verbose = 3;
  1017. private ArrayList consumers;
  1018. public Log (bool useDefault)
  1019. {
  1020. consumers = new ArrayList ();
  1021. if (useDefault) AddConsumer (new TextLogger (Console.Out));
  1022. }
  1023. public Log () : this (true)
  1024. {
  1025. }
  1026. public static int VerboseLevel {
  1027. get {
  1028. return verbose;
  1029. }
  1030. set {
  1031. verbose = (value < 0)
  1032. ? 0
  1033. : (value > 3)
  1034. ? 3 : value;
  1035. }
  1036. }
  1037. public void AddConsumer (ILogger consumer)
  1038. {
  1039. consumers.Add (consumer);
  1040. }
  1041. public void Write (string tag, string msg, ImportanceLevel importance)
  1042. {
  1043. foreach (ILogger logger in consumers) {
  1044. if (tag == null || tag == "") {
  1045. logger.Write (msg, importance);
  1046. } else {
  1047. logger.Write (tag, msg, importance);
  1048. }
  1049. }
  1050. }
  1051. public void Write (string msg, ImportanceLevel importance)
  1052. {
  1053. Write (null, msg, importance);
  1054. }
  1055. public void Write (string tag, string msg)
  1056. {
  1057. foreach (ILogger logger in consumers) {
  1058. if (tag == null || tag == "") {
  1059. logger.Write (msg);
  1060. } else {
  1061. logger.Write (tag, msg);
  1062. }
  1063. }
  1064. }
  1065. public void Write (string msg)
  1066. {
  1067. Write (null, msg);
  1068. }
  1069. public void Flush ()
  1070. {
  1071. foreach (ILogger logger in consumers) {
  1072. logger.Flush ();
  1073. }
  1074. }
  1075. public void Close ()
  1076. {
  1077. foreach (ILogger logger in consumers) {
  1078. logger.Flush ();
  1079. logger.Close ();
  1080. }
  1081. }
  1082. }
  1083. ////////////////////////////////
  1084. // Main
  1085. ////////////////////////////////
  1086. public class Verifier {
  1087. public static Log log = new Log ();
  1088. public static bool stopOnError = false;
  1089. public static bool ignoreMissingTypes = true;
  1090. public static bool checkOptionalFlags = true;
  1091. private Verifier ()
  1092. {
  1093. }
  1094. public static Log Log {
  1095. get {
  1096. return log;
  1097. }
  1098. }
  1099. public static void Main (String [] args) {
  1100. if (args.Length < 2) {
  1101. Console.WriteLine ("Usage: verifier assembly1 assembly2");
  1102. } else {
  1103. string name1 = args [0];
  1104. string name2 = args [1];
  1105. bool ok = false;
  1106. AssemblyStuff asm1 = new AssemblyStuff (name1);
  1107. AssemblyStuff asm2 = new AssemblyStuff (name2);
  1108. ok = asm1.Load ();
  1109. if (!ok) {
  1110. Console.WriteLine ("Unable to load assembly {0}.", name1);
  1111. Environment.Exit (-1);
  1112. }
  1113. ok = asm2.Load ();
  1114. if (!ok) {
  1115. Console.WriteLine ("Unable to load assembly {0}.", name2);
  1116. Environment.Exit (-1);
  1117. }
  1118. try {
  1119. ok = (asm1 == asm2);
  1120. } catch {
  1121. ok = false;
  1122. } finally {
  1123. Log.Close ();
  1124. }
  1125. if (!ok) {
  1126. Console.WriteLine ("--- not equal");
  1127. Environment.Exit (-1);
  1128. }
  1129. }
  1130. }
  1131. }
  1132. }