PageRenderTime 163ms CodeModel.GetById 80ms app.highlight 6ms RepoModel.GetById 75ms app.codeStats 0ms

/src/UnitTests/Reflection/BasePEVerifyTestCase.cs

http://github.com/philiplaureano/LinFu
C# | 138 lines | 91 code | 29 blank | 18 comment | 8 complexity | c454ee4d7afc9f624b424acad51a34e2 MD5 | raw file
  1// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
  2// 
  3// Licensed under the Apache License, Version 2.0 (the "License");
  4// you may not use this file except in compliance with the License.
  5// You may obtain a copy of the License at
  6// 
  7//     http://www.apache.org/licenses/LICENSE-2.0
  8// 
  9// Unless required by applicable law or agreed to in writing, software
 10// distributed under the License is distributed on an "AS IS" BASIS,
 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12// See the License for the specific language governing permissions and
 13// limitations under the License.
 14
 15// LEGAL NOTICE: The original BasePEVerifyTestCase code was taken from Castle
 16// and modified to suit LinFu's purposes. The following code
 17// is still subject to the terms of the Apache License and retains
 18// the rights of its original owners.
 19
 20using System;
 21using System.Collections.Generic;
 22using System.Configuration;
 23using System.Diagnostics;
 24using System.IO;
 25using Xunit;
 26
 27namespace LinFu.UnitTests.Reflection
 28{
 29    public abstract class BasePEVerifyTestCase : BaseTestFixture
 30    {
 31        private static readonly List<string> _disposalList = new List<string>();
 32
 33        protected override void Init()
 34        {
 35            OnInit();
 36        }
 37
 38        protected override void Term()
 39        {
 40            OnTerm();
 41
 42            lock (_disposalList)
 43            {
 44                // Delete the files tagged for removal
 45                foreach (var file in _disposalList)
 46                {
 47                    if (!File.Exists(file))
 48                        continue;
 49
 50                    File.Delete(file);
 51                }
 52
 53                _disposalList.Clear();
 54            }
 55        }
 56
 57        protected virtual void OnInit()
 58        {
 59        }
 60
 61        protected virtual void OnTerm()
 62        {
 63        }
 64
 65        protected static void AutoDelete(string filename)
 66        {
 67            lock (_disposalList)
 68            {
 69                if (_disposalList.Contains(filename) || !File.Exists(filename))
 70                    return;
 71
 72
 73                _disposalList.Add(filename);
 74            }
 75        }
 76
 77        protected void PEVerify(string assemblyLocation)
 78        {
 79            var pathKeys = new[]
 80            {
 81                "sdkDir",
 82                "x86SdkDir",
 83                "sdkDirUnderVista"
 84            };
 85
 86            var process = new Process();
 87            var peVerifyLocation = string.Empty;
 88
 89
 90            peVerifyLocation = GetPEVerifyLocation(pathKeys, peVerifyLocation);
 91
 92            if (!File.Exists(peVerifyLocation))
 93            {
 94                Console.WriteLine("Warning: PEVerify.exe could not be found. Skipping test.");
 95                return;
 96            }
 97
 98            process.StartInfo.FileName = peVerifyLocation;
 99            process.StartInfo.RedirectStandardOutput = true;
100            process.StartInfo.UseShellExecute = false;
101            process.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
102            process.StartInfo.Arguments = "\"" + assemblyLocation + "\" /VERBOSE";
103            process.StartInfo.CreateNoWindow = true;
104            process.Start();
105
106            var processOutput = process.StandardOutput.ReadToEnd();
107            process.WaitForExit();
108
109            var result = string.Format("PEVerify Exit Code: {0}", process.ExitCode);
110
111            Console.WriteLine(GetType().FullName + ": " + result);
112
113            if (process.ExitCode == 0)
114                return;
115
116            Console.WriteLine(processOutput);
117            Assert.True(false, $"PEVerify output: {Environment.NewLine}{processOutput}{result}");
118        }
119
120        private static string GetPEVerifyLocation(IEnumerable<string> pathKeys, string peVerifyLocation)
121        {
122            foreach (var key in pathKeys)
123            {
124                var directory = ConfigurationManager.AppSettings[key];
125
126                if (string.IsNullOrEmpty(directory))
127                    continue;
128
129                peVerifyLocation = Path.Combine(directory, "peverify.exe");
130
131                if (File.Exists(peVerifyLocation))
132                    break;
133            }
134
135            return peVerifyLocation;
136        }
137    }
138}