PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/tools/misc/verifier.cs

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