Exemple #1
0
        internal static string Rewrite(string absoluteSourceDir, string absoluteBinary, Options options)
        {
            var referencedir         = options.MakeAbsolute(Path.Combine(ReferenceDirRoot, options.BuildFramework));
            var contractreferencedir = options.MakeAbsolute(Path.Combine(ContractReferenceDirRoot, options.ContractFramework));
            var absoluteSourcedir    = Path.GetDirectoryName(absoluteBinary);
            var absoluteSource       = absoluteBinary;
            var libPathsString       = FormLibPaths(absoluteSourceDir, contractreferencedir, options);

            var targetName = options.TestName + ".rw" + Path.GetExtension(absoluteBinary);
            var binDir     = options.UseBinDir ? Path.Combine(Path.Combine(absoluteSourcedir, "bin"), options.BuildFramework) : absoluteSourcedir;
            var targetfile = Path.Combine(binDir, targetName);

            Console.WriteLine("Rewriting '{0}' to '{1}'", absoluteBinary, targetfile);
            if (!Directory.Exists(binDir))
            {
                Directory.CreateDirectory(binDir);
            }
            var exitCode = RunProcess(binDir, options.MakeAbsolute(FoxtrotExe),
                                      String.Format("/out:{0} -nobox -libpaths:{3} -libpaths:{4} {5} {1} {2}", targetfile, options.FoxtrotOptions, absoluteSource, referencedir, contractreferencedir, libPathsString),
                                      options.TestName);

            if (exitCode != 0)
            {
                return(null);
            }
            else
            {
                if (options.UseBinDir)
                {
                    File.Copy(targetfile, Path.Combine(absoluteSourcedir, targetName), true);
                    return(Path.Combine(absoluteSourcedir, targetName));
                }
            }
            return(targetfile);
        }
Exemple #2
0
        private static List <string> ResolveReferences(Options options)
        {
            var result = new List <string>();

            foreach (var r in options.References)
            {
                foreach (var root in options.LibPaths)
                {
                    var dir = options.MakeAbsolute(Path.Combine(root, options.BuildFramework));

                    var path = Path.Combine(dir, r);
                    if (File.Exists(path))
                    {
                        result.Add(path);
                        break;
                    }
                }
                foreach (var root in new[] { ReferenceDirRoot, ContractReferenceDirRoot })
                {
                    var dir = options.MakeAbsolute(Path.Combine(root, options.BuildFramework));

                    var path = Path.Combine(dir, r);
                    if (File.Exists(path))
                    {
                        result.Add(path);
                        break;
                    }
                }
            }
            return(result);
        }
    internal static string Rewrite(string absoluteSourceDir, string absoluteBinary, Options options)
    {
      var referencedir = options.MakeAbsolute(Path.Combine(ReferenceDirRoot, options.BuildFramework));
      var contractreferencedir = options.MakeAbsolute(Path.Combine(ContractReferenceDirRoot, options.ContractFramework));
      var absoluteSourcedir = Path.GetDirectoryName(absoluteBinary);
      var absoluteSource = absoluteBinary;
      var libPathsString = FormLibPaths(absoluteSourceDir, contractreferencedir, options);

      var targetName = options.TestName + ".rw" + Path.GetExtension(absoluteBinary);
      var binDir = options.UseBinDir ? Path.Combine(Path.Combine(absoluteSourcedir, "bin"), options.BuildFramework) : absoluteSourcedir;
      var targetfile = Path.Combine(binDir, targetName);
      Console.WriteLine("Rewriting '{0}' to '{1}'", absoluteBinary, targetfile);
      if (!Directory.Exists(binDir))
      {
        Directory.CreateDirectory(binDir);
      }
      var exitCode = RunProcess(binDir, options.MakeAbsolute(FoxtrotExe),
        String.Format("/out:{0} -nobox -libpaths:{3} -libpaths:{4} {5} {1} {2}", targetfile, options.FoxtrotOptions, absoluteSource, referencedir, contractreferencedir, libPathsString),
        options.TestName);
      if (exitCode != 0)
      {
        return null;
      }
      else
      {
        if (options.UseBinDir)
        {
          File.Copy(targetfile, Path.Combine(absoluteSourcedir, targetName), true);
          return Path.Combine(absoluteSourcedir, targetName);
        }
      }
      return targetfile;
    }
    internal static object Rewrite(string absoluteSourceDir, string absoluteBinary, Options options, bool alwaysCapture = false)
    {
      string referencedir;
      if (Path.IsPathRooted(options.ReferencesFramework))
      {
        referencedir = options.ReferencesFramework;
      }
      else
      {
        referencedir = options.MakeAbsolute(Path.Combine(ReferenceDirRoot, options.ReferencesFramework));
      }
      var contractreferencedir = options.MakeAbsolute(Path.Combine(ContractReferenceDirRoot, options.ContractFramework));
      var absoluteSourcedir = Path.GetDirectoryName(absoluteBinary);
      var absoluteSource = absoluteBinary;
      var libPathsString = FormLibPaths(absoluteSourceDir, contractreferencedir, options);

      var targetName = options.TestName + ".rw" + Path.GetExtension(absoluteBinary);
      var binDir = options.UseBinDir ? Path.Combine(Path.Combine(absoluteSourcedir, "bin"), options.BuildFramework) : absoluteSourcedir;
      var targetfile = Path.Combine(binDir, targetName);
      Console.WriteLine("Rewriting '{0}' to '{1}'", absoluteBinary, targetfile);
      if (!Directory.Exists(binDir))
      {
        Directory.CreateDirectory(binDir);
      }
      var optionString = String.Format("/out:{0} -nobox -libpaths:{3} {4} {1} {2}", targetfile, options.FoxtrotOptions, absoluteSource, referencedir, libPathsString);
      if (absoluteBinary.EndsWith("mscorlib.dll"))
        optionString = String.Format("/platform:{0} {1}", absoluteBinary, optionString);
      var capture = RunProcessAndCapture(binDir, options.GetFullExecutablePath(FoxtrotExe), optionString, options.TestName);
      if (capture.ExitCode != 0)
      {
        if (options.MustSucceed)
        {
          Assert.AreEqual(0, capture.ExitCode, "{0} returned an errorcode of {1}.", FoxtrotExe, capture.ExitCode);
        }
        return capture;
      }
      else
      {
        if (alwaysCapture) return capture;

        if (options.UseBinDir)
        {
          var fileName = Path.Combine(absoluteSourcedir, targetName);
          if (File.Exists(fileName))
          {
            try
            {
              File.SetAttributes(fileName, FileAttributes.Normal);
            }
            catch { }
          }
          File.Copy(targetfile, fileName, true);
          return fileName;
        }
      }
      return targetfile;
    }
    internal static int PEVerify(string assemblyFile, Options options)
    {
      var peVerifyPath = options.MakeAbsolute(Path.Combine(ToolsRoot, String.Format(@"{0}\peverify.exe", options.BuildFramework)));
      assemblyFile = options.MakeAbsolute(assemblyFile);
      var path = Path.GetDirectoryName(assemblyFile);
      var file = Path.GetFileName(assemblyFile);
      if (file == "mscorlib.dll") return -1; // peverify returns 0 for mscorlib without verifying.

      var exitCode = RunProcess(path, peVerifyPath, "/unique \"" + file + "\"", "repro");
      return exitCode;
    }
 internal static string RewriteAndVerify(string sourceDir, string binary, Options options)
 {
   if (!Path.IsPathRooted(sourceDir)) { sourceDir = options.MakeAbsolute(sourceDir); }
   if (!Path.IsPathRooted(binary)) { binary = options.MakeAbsolute(binary); }
   string target = Rewrite(sourceDir, binary, options);
   if (target != null)
   {
     PEVerify(target, options);
   }
   return target;
 }
        internal static string Build(Options options, out string absoluteSourceDir)
        {
            var sourceFile           = options.MakeAbsolute(options.SourceFile);
            var compilerpath         = options.MakeAbsolute(Path.Combine(ToolsRoot, String.Format(@"{0}\{1}", options.BuildFramework, options.Compiler)));
            var contractreferencedir = options.MakeAbsolute(Path.Combine(ContractReferenceDirRoot, options.ReferencesFramework));
            var sourcedir            = absoluteSourceDir = Path.GetDirectoryName(sourceFile);
            var outputdir            = Path.Combine(Path.Combine(sourcedir, "bin"), options.BuildFramework);
            var extension            = options.UseExe ? ".exe" : ".dll";
            var targetKind           = options.UseExe ? "exe" : "library";
            var targetfile           = Path.Combine(outputdir, options.TestName + extension);

            // add Microsoft.Contracts reference if needed
            if (!options.BuildFramework.Contains("v4."))
            {
                options.References.Add("Microsoft.Contracts.dll");
            }
            var oldCwd = Environment.CurrentDirectory;

            try
            {
                Environment.CurrentDirectory = sourcedir;

                var resolvedReferences = ResolveReferences(options);
                var referenceString    = ReferenceOptions(resolvedReferences);
                if (!Directory.Exists(outputdir))
                {
                    Directory.CreateDirectory(outputdir);
                }
                var exitCode = RunProcess(sourcedir, compilerpath, String.Format("/debug /t:{4} /out:{0} {3} {2} {1}", targetfile, sourceFile, referenceString, options.CompilerOptions, targetKind), true);
                if (exitCode != 0)
                {
                    return(null);
                }
                // add reference to Microsoft.Contracts.dll if not already done because of transitive closure issues
                if (!options.References.Contains("Microsoft.Contracts.dll"))
                {
                    options.References.Add("Microsoft.Contracts.dll");
                    // recompute resolvedReferences
                    resolvedReferences = ResolveReferences(options);
                }

                CopyReferenceAssemblies(resolvedReferences, outputdir);
                return(targetfile);
            }
            finally
            {
                Environment.CurrentDirectory = oldCwd;
            }
        }
        // [TestMethod] no longer needed as we don't use the 2008 solution anymore.
        public void CheckOOBCProjectSources()
        {
            var v9Sources = new[] {
                @"Microsoft.Research\Contracts\Microsoft.VisualBasic.Compatibility\Microsoft.VisualBasic.Compatibility.csproj",
                @"Microsoft.Research\Contracts\Microsoft.VisualBasic.Contracts\Microsoft.VisualBasic.Contracts.csproj",
                @"Microsoft.Research\Contracts\mscorlib\mscorlib.Contracts.csproj",
                @"Microsoft.Research\Contracts\System\System.Contracts.csproj",
                @"Microsoft.Research\Contracts\System.Configuration\System.Configuration.csproj",
                @"Microsoft.Research\Contracts\System.Configuration.Install\System.Configuration.Install.csproj",
                @"Microsoft.Research\Contracts\System.Core.Contracts\System.Core.Contracts.csproj",
                @"Microsoft.Research\Contracts\System.Data\System.Data\System.Data.csproj",
                @"Microsoft.Research\Contracts\System.Drawing\System.Drawing.csproj",
                @"Microsoft.Research\Contracts\System.Security\System.Security.csproj",
                @"Microsoft.Research\Contracts\System.Web\System.Web.csproj",
                @"Microsoft.Research\Contracts\System.Windows\System.Windows.csproj",
                @"Microsoft.Research\Contracts\System.Windows.Browser\System.Windows.Browser.csproj",
                @"Microsoft.Research\Contracts\System.Windows.Forms\Windows.Forms\System.Windows.Forms.csproj",
                @"Microsoft.Research\Contracts\System.Xml\System.Xml.csproj",
                @"Microsoft.Research\Contracts\System.Xml.Linq\System.Xml.Linq.csproj",
                @"Microsoft.Research\Contracts\WindowsBase\WindowsBase.csproj",
            };
            var v10Sources = new[] {
                @"Microsoft.Research\Contracts\Microsoft.VisualBasic.Compatibility\Microsoft.VisualBasic.Compatibility10.csproj",
                @"Microsoft.Research\Contracts\Microsoft.VisualBasic.Contracts\Microsoft.VisualBasic.Contracts10.csproj",
                @"Microsoft.Research\Contracts\mscorlib\mscorlib.Contracts10.csproj",
                @"Microsoft.Research\Contracts\System\System.Contracts10.csproj",
                @"Microsoft.Research\Contracts\System.Configuration\System.Configuration10.csproj",
                @"Microsoft.Research\Contracts\System.Configuration.Install\System.Configuration.Install10.csproj",
                @"Microsoft.Research\Contracts\System.Core.Contracts\System.Core.Contracts10.csproj",
                @"Microsoft.Research\Contracts\System.Data\System.Data\System.Data10.csproj",
                @"Microsoft.Research\Contracts\System.Drawing\System.Drawing10.csproj",
                @"Microsoft.Research\Contracts\System.Security\System.Security10.csproj",
                @"Microsoft.Research\Contracts\System.Web\System.Web10.csproj",
                @"Microsoft.Research\Contracts\System.Windows\System.Windows10.csproj",
                @"Microsoft.Research\Contracts\System.Windows.Browser\System.Windows.Browser10.csproj",
                @"Microsoft.Research\Contracts\System.Windows.Forms\Windows.Forms\System.Windows.Forms10.csproj",
                @"Microsoft.Research\Contracts\System.Xml\System.Xml10.csproj",
                @"Microsoft.Research\Contracts\System.Xml.Linq\System.Xml.Linq10.csproj",
                @"Microsoft.Research\Contracts\WindowsBase\WindowsBase10.csproj",
                @"Microsoft.Research\Contracts\System.Numerics\System.Numerics.csproj"
            };

            for (int i = 0; i < v9Sources.Length; i++)
            {
                var opt = new Options(TestContext);
                CheckSourceFiles(opt.MakeAbsolute(v9Sources[i]), opt.MakeAbsolute(v10Sources[i]));
            }
        }
Exemple #9
0
        internal static int PEVerify(string assemblyFile, Options options)
        {
            var peVerifyPath = options.MakeAbsolute(Path.Combine(ToolsRoot, String.Format(@"{0}\peverify.exe", options.BuildFramework)));

            assemblyFile = options.MakeAbsolute(assemblyFile);
            var path = Path.GetDirectoryName(assemblyFile);
            var file = Path.GetFileName(assemblyFile);

            if (file == "mscorlib.dll")
            {
                return(-1);                  // peverify returns 0 for mscorlib without verifying.
            }
            var exitCode = RunProcess(path, peVerifyPath, "/unique \"" + file + "\"", "repro");

            return(exitCode);
        }
Exemple #10
0
        internal static void RewriteBinary(ITestOutputHelper testOutputHelper, Options options, string sourceDir)
        {
            var absoluteDir = options.MakeAbsolute(sourceDir);
            var binary      = Path.Combine(absoluteDir, options.SourceFile);

            Rewrite(testOutputHelper, absoluteDir, binary, options);
        }
Exemple #11
0
        private static string FormLibPaths(string contractReferenceDir, Options options)
        {
            // MB: do not change CurrentDirectory because it makes parallel tests fail

            if (options.LibPaths == null)
            {
                return("");
            }

            StringBuilder sb = null;

            if (options.UseContractReferenceAssemblies)
            {
                sb = new StringBuilder("/libpaths:").Append(contractReferenceDir);
            }

            foreach (var path in options.LibPaths)
            {
                if (sb == null)
                {
                    sb = new StringBuilder("/libpaths:");
                }
                else
                {
                    sb.Append(';');
                }

                sb.Append(options.MakeAbsolute(Path.Combine(path, options.ContractFramework)));
            }
            if (sb == null)
            {
                return("");
            }
            return(sb.ToString());
        }
Exemple #12
0
 internal static void Clousot1Slicing(string absoluteSourceDir, string absoluteBinary, Options options, Output output)
 {
     var referencedir = options.MakeAbsolute(Path.Combine(ReferenceDirRoot, options.BuildFramework));
     var contractreferencedir = options.MakeAbsolute(Path.Combine(ContractReferenceDirRoot, options.ContractFramework));
     var absoluteBinaryDir = Path.GetDirectoryName(absoluteBinary);
     var absoluteSource = absoluteBinary;
     var libPathsString = FormLibPaths(contractreferencedir, options) + " /libpaths:.";
     var args = String.Format("{0} -cci1 /regression /define:cci1only;clousot1 -framework:{4} -libpaths:{2} {3} {1}", options.ClousotOptions, absoluteSource, referencedir, libPathsString, options.Framework);
     if (options.Fast || System.Diagnostics.Debugger.IsAttached)
     {
         output.WriteLine("Calling NewCCI2Driver.Main with: {0}", args);
         // Use output to avoid Clousot from closing the Console
         Assert.AreEqual(0, Microsoft.Research.CodeAnalysis.NewCCI2Driver.Main(args.Split(' '), output));
     }
     else
         RunProcess(absoluteBinaryDir, options.GetFullExecutablePath(Clousot2SlicingExe), args, output);
 }
Exemple #13
0
        public void RewriteFrameworkDlls45()
        {
            var options   = new Options(this.TestContext);
            var framework = @".NetFramework\v4.5";

            options.ContractFramework = framework;
            var dllpath           = FrameworkBinariesToRewritePath + framework;
            var extrareferencedir = options.MakeAbsolute(TestDriver.ReferenceDirRoot);

            options.LibPaths.Add(extrareferencedir);
            options.BuildFramework = options.MakeAbsolute(dllpath);
            options.FoxtrotOptions = options.FoxtrotOptions + " /verbose:4";
            options.UseContractReferenceAssemblies = false;
            if (File.Exists(options.MakeAbsolute(Path.Combine(dllpath, options.SourceFile))))
            {
                TestDriver.RewriteBinary(options, dllpath);
            }
        }
        internal static string RewriteAndVerify(string sourceDir, string binary, Options options)
        {
            if (!Path.IsPathRooted(sourceDir))
            {
                sourceDir = options.MakeAbsolute(sourceDir);
            }
            if (!Path.IsPathRooted(binary))
            {
                binary = options.MakeAbsolute(binary);
            }
            string target = Rewrite(sourceDir, binary, options) as string;

            if (target != null)
            {
                PEVerify(target, options);
            }
            return(target);
        }
Exemple #15
0
        internal static string Build(Options options, int testIndex, string extraCompilerOptions, Output output, out string absoluteSourceDir)
        {
            var sourceFile           = options.MakeAbsolute(options.SourceFile);
            var compilerpath         = options.MakeAbsolute(Path.Combine(ToolsRoot, options.BuildFramework, options.Compiler));
            var contractreferencedir = options.MakeAbsolute(Path.Combine(ContractReferenceDirRoot, options.BuildFramework));
            var sourcedir            = absoluteSourceDir = Path.GetDirectoryName(sourceFile);
            var outputdir            = Path.Combine(sourcedir, "bin", options.BuildFramework);
            var extension            = options.UseExe ? ".exe" : ".dll";
            var targetKind           = options.UseExe ? "exe" : "library";
            var suffix = "_" + testIndex;

            if (options.GenerateUniqueOutputName)
            {
                suffix += "." + randGenerator.Next(0x10000).ToString("X4"); // enables concurrent tests on the same source file
            }
            var targetfile = Path.Combine(outputdir, Path.GetFileNameWithoutExtension(sourceFile) + suffix + extension);

            // add Microsoft.Contracts reference if needed
            if (!options.BuildFramework.Contains("v4."))
            {
                options.References.Add("Microsoft.Contracts.dll");
            }

            // MB: do not modify the CurrentDirectory, that could cause parallel tests to fail

            var resolvedReferences = ResolveReferences(options);
            var referenceString    = ReferenceOptions(resolvedReferences);

            if (!Directory.Exists(outputdir))
            {
                Directory.CreateDirectory(outputdir);
            }
            var args     = String.Format("/debug /t:{4} /out:{0} {5} {3} {2} {1}", targetfile, sourceFile, referenceString, options.CompilerOptions(resolvedReferences), targetKind, extraCompilerOptions);
            var exitCode = RunProcess(sourcedir, compilerpath, args, output);

            if (exitCode != 0)
            {
                return(null);
            }
            //CopyReferenceAssemblies(resolvedReferences, outputdir);

            return(targetfile);
        }
Exemple #16
0
        internal static void Clousot1Slicing(string absoluteSourceDir, string absoluteBinary, Options options, Output output)
        {
            var referencedir         = options.MakeAbsolute(Path.Combine(ReferenceDirRoot, options.BuildFramework));
            var contractreferencedir = options.MakeAbsolute(Path.Combine(ContractReferenceDirRoot, options.ContractFramework));
            var absoluteBinaryDir    = Path.GetDirectoryName(absoluteBinary);
            var absoluteSource       = absoluteBinary;
            var libPathsString       = FormLibPaths(contractreferencedir, options) + " /libpaths:.";
            var args = String.Format("{0} -cci1 /regression /define:cci1only;clousot1 -framework:{4} -libpaths:{2} {3} {1}", options.ClousotOptions, absoluteSource, referencedir, libPathsString, options.Framework);

            if (options.Fast || System.Diagnostics.Debugger.IsAttached)
            {
                output.WriteLine("Calling NewCCI2Driver.Main with: {0}", args);
                // Use output to avoid Clousot from closing the Console
                Assert.AreEqual(0, Microsoft.Research.CodeAnalysis.NewCCI2Driver.Main(args.Split(' '), output));
            }
            else
            {
                RunProcess(absoluteBinaryDir, options.GetFullExecutablePath(Clousot2SlicingExe), args, output);
            }
        }
Exemple #17
0
        private static void StartService(Options options)
        {
            if (serviceProcess != null)
            {
                StopService();
            }

            // First make sure another instance is not already running (because we don't know which version is running)
            foreach (var process in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(ClousotServiceHostExe)))
            {
                process.CloseMainWindow();
                if (!process.WaitForExit(1000))
                {
                    process.Kill();
                }
            }

            var serviceHostDir = options.MakeAbsolute(Path.GetDirectoryName(ClousotServiceHostExe));

            // note: we do not want to use ClousotServiceHostExe from the deployment directory because the app.config will be missing
            serviceProcess = StartServiceProcess(serviceHostDir, options.MakeAbsolute(ClousotServiceHostExe), "", Output.Ignore);
        }
        internal static void Clousot(string absoluteSourceDir, string absoluteBinary, Options options, string testName, int testindex, Output output)
        {
            var referencedir = options.MakeAbsolute(Path.Combine(ReferenceDirRoot, options.BuildFramework));
            var contractreferencedir = options.MakeAbsolute(Path.Combine(ContractReferenceDirRoot, options.ContractFramework));
            var absoluteBinaryDir = Path.GetDirectoryName(absoluteBinary);
            var absoluteSource = absoluteBinary;
            var libPathsString = FormLibPaths(contractreferencedir, options);
            var args = string.Format("{0} /regression /define:cci1only;clousot1 -framework:{4} -libpaths:{2} {3} {1}", options.ClousotOptions, absoluteSource, referencedir, libPathsString, options.Framework);

            WriteRSPFile(absoluteBinaryDir, testName, args);
            
            if (options.Fast || Debugger.IsAttached)
            {
                output.WriteLine("Calling CCI1Driver.Main with: {0}", args);
                // Use output to avoid Clousot from closing the Console
                Assert.Equal(0, Microsoft.Research.CodeAnalysis.CCI1Driver.Main(args.Split(' '), output));
            }
            else
            {
                RunProcess(absoluteBinaryDir, options.GetFullExecutablePath(ClousotExe), args, output, testName);
            }
        }
Exemple #19
0
        internal static string RewriteAndVerify(ITestOutputHelper testOutputHelper, string sourceDir, string binary, Options options)
        {
            if (!Path.IsPathRooted(sourceDir))
            {
                sourceDir = options.MakeAbsolute(sourceDir);
            }
            if (!Path.IsPathRooted(binary))
            {
                binary = options.MakeAbsolute(binary);
            }
            string target = Rewrite(testOutputHelper, sourceDir, binary, options) as string;

            if (target != null)
            {
                PEVerify(testOutputHelper, target, options);

                if (options.DeepVerify)
                {
                    ExtractContracts(target, options);
                }
            }
            return(target);
        }
Exemple #20
0
        private static bool FindReference(Options options, List <string> result, string filename, string framework)
        {
            foreach (var root in System.Linq.Enumerable.Concat(options.LibPaths, new[] { ReferenceDirRoot, ContractReferenceDirRoot }))
            {
                var dir = options.MakeAbsolute(Path.Combine(root, framework));

                var path = Path.Combine(dir, filename);
                if (File.Exists(path))
                {
                    result.Add(path);
                    return(true);
                }
            }
            return(false);
        }
Exemple #21
0
        private static void CompareExpectedOutput(Options options, OutputCapture capture)
        {
            var expected      = File.ReadAllLines(options.MakeAbsolute(@"Foxtrot\Tests\CheckerTests\" + Path.GetFileNameWithoutExtension(options.SourceFile) + ".Expected"));
            var expectedIndex = 0;
            var absoluteFile  = options.MakeAbsolute(options.SourceFile);

            for (int i = 0; i < capture.stdOut.Count; i++)
            {
                var actual = capture.stdOut[i];

                if (actual.StartsWith("Trace:"))
                {
                    continue;
                }
                if (actual.StartsWith("elapsed time:"))
                {
                    continue;
                }
                if (expectedIndex >= expected.Length)
                {
                    Assert.Null(actual);
                }
                var expectedLine = expected[expectedIndex++];

                if (actual.StartsWith(absoluteFile, StringComparison.InvariantCultureIgnoreCase))
                {
                    actual       = TrimFilePath(actual, options.SourceFile);
                    expectedLine = TrimFilePath(expectedLine, options.SourceFile);
                }
                Assert.Equal(expectedLine, actual);
            }
            if (expectedIndex < expected.Length)
            {
                Assert.Equal(expected[expectedIndex], null);
            }
        }
Exemple #22
0
        static string FormLibPaths(string absoluteSourceDir, string contractReferenceDir, Options options)
        {
            var oldcwd = Environment.CurrentDirectory;

            try
            {
                Environment.CurrentDirectory = absoluteSourceDir;

                StringBuilder sb = null;
                if (options.UseContractReferenceAssemblies)
                {
                    sb = new StringBuilder("/libpaths:").Append(contractReferenceDir);
                }
                if (options.LibPaths == null)
                {
                    return("");
                }
                foreach (var path in options.LibPaths)
                {
                    if (sb == null)
                    {
                        sb = new StringBuilder("/libpaths:");
                    }
                    else
                    {
                        sb.Append(';');
                    }
                    sb.Append(options.MakeAbsolute(Path.Combine(path, options.ContractFramework)));
                }
                if (sb == null)
                {
                    return("");
                }
                return(sb.ToString());
            }
            finally
            {
                Environment.CurrentDirectory = oldcwd;
            }
        }
Exemple #23
0
        internal static string Build(ITestOutputHelper testOutputHelper, Options options, out string absoluteSourceDir)
        {
            var sourceFile = options.MakeAbsolute(options.SourceFile);

            var compilerPath = options.GetCompilerAbsolutePath(ToolsRoot);

            Assert.True(File.Exists(compilerPath), string.Format("Can't find compiler at '{0}'", compilerPath));

            var contractreferencedir = options.MakeAbsolute(Path.Combine(ContractReferenceDirRoot, options.ReferencesFramework));
            var sourcedir            = absoluteSourceDir = Path.GetDirectoryName(sourceFile);

            var outputdir  = Path.Combine(Path.Combine(sourcedir, "bin"), options.CompilerPath);
            var extension  = options.UseExe ? ".exe" : ".dll";
            var targetKind = options.UseExe ? "exe" : "library";

            var targetfile = Path.Combine(outputdir, options.TestName + extension);

            // add Microsoft.Contracts reference if needed
            if (options.IsV35)
            {
                options.References.Add("Microsoft.Contracts.dll");
            }

            var oldCwd = Environment.CurrentDirectory;

            try
            {
                Environment.CurrentDirectory = sourcedir;

                var resolvedReferences = ResolveReferences(options);
                var referenceString    = ReferenceOptions(resolvedReferences);

                if (!Directory.Exists(outputdir))
                {
                    Directory.CreateDirectory(outputdir);
                }

                string arguments = String.Format("/t:{4} /out:{0} {3} {2} {1}", targetfile, sourceFile, referenceString, options.FinalCompilerOptions, targetKind);

                if (!options.ReleaseMode)
                {
                    if (options.PdbOnly)
                    {
                        arguments = "/debug:pdbonly " + arguments;
                    }
                    else
                    {
                        arguments = "/debug:full " + arguments;
                    }
                }

                if (options.Optimize)
                {
                    arguments = "/optimize " + arguments;
                }

                var exitCode = RunProcess(testOutputHelper, sourcedir, compilerPath, arguments, true);

                if (exitCode != 0)
                {
                    return(null);
                }

                // add reference to Microsoft.Contracts.dll if not already done because of transitive closure issues
                if (!options.References.Contains("Microsoft.Contracts.dll"))
                {
                    options.References.Add("Microsoft.Contracts.dll");

                    // recompute resolvedReferences
                    resolvedReferences = ResolveReferences(options);
                }

                CopyReferenceAssemblies(resolvedReferences, outputdir);

                return(targetfile);
            }
            finally
            {
                Environment.CurrentDirectory = oldCwd;
            }
        }
Exemple #24
0
        static string FormLibPaths(string absoluteSourceDir, string contractReferenceDir, Options options)
        {
            var oldcwd = Environment.CurrentDirectory;
            try
            {
                Environment.CurrentDirectory = absoluteSourceDir;

                StringBuilder sb = null;
                if (options.UseContractReferenceAssemblies)
                {
                    sb = new StringBuilder("/libpaths:").Append(contractReferenceDir);
                }
                if (options.LibPaths == null) return "";
                foreach (var path in options.LibPaths)
                {
                    if (sb == null)
                    {
                        sb = new StringBuilder("/libpaths:");
                    }
                    else
                    {
                        sb.Append(';');
                    }
                    sb.Append(options.MakeAbsolute(Path.Combine(path, options.ContractFramework)));
                }
                if (sb == null) return "";
                return sb.ToString();
            }
            finally
            {
                Environment.CurrentDirectory = oldcwd;
            }
        }
    // [TestMethod] no longer needed as we don't use the 2008 solution anymore.
    public void CheckOOBCProjectSources()
    {
      var v9Sources = new[]{
        @"Microsoft.Research\Contracts\Microsoft.VisualBasic.Compatibility\Microsoft.VisualBasic.Compatibility.csproj",
        @"Microsoft.Research\Contracts\Microsoft.VisualBasic.Contracts\Microsoft.VisualBasic.Contracts.csproj",
        @"Microsoft.Research\Contracts\mscorlib\mscorlib.Contracts.csproj",
        @"Microsoft.Research\Contracts\System\System.Contracts.csproj",
        @"Microsoft.Research\Contracts\System.Configuration\System.Configuration.csproj",
        @"Microsoft.Research\Contracts\System.Configuration.Install\System.Configuration.Install.csproj",
        @"Microsoft.Research\Contracts\System.Core.Contracts\System.Core.Contracts.csproj",
        @"Microsoft.Research\Contracts\System.Data\System.Data\System.Data.csproj",
        @"Microsoft.Research\Contracts\System.Drawing\System.Drawing.csproj",
        @"Microsoft.Research\Contracts\System.Security\System.Security.csproj",
        @"Microsoft.Research\Contracts\System.Web\System.Web.csproj",
        @"Microsoft.Research\Contracts\System.Windows\System.Windows.csproj",
        @"Microsoft.Research\Contracts\System.Windows.Browser\System.Windows.Browser.csproj",
        @"Microsoft.Research\Contracts\System.Windows.Forms\Windows.Forms\System.Windows.Forms.csproj",
        @"Microsoft.Research\Contracts\System.Xml\System.Xml.csproj",
        @"Microsoft.Research\Contracts\System.Xml.Linq\System.Xml.Linq.csproj",
        @"Microsoft.Research\Contracts\WindowsBase\WindowsBase.csproj",
      };
      var v10Sources = new[]{
        @"Microsoft.Research\Contracts\Microsoft.VisualBasic.Compatibility\Microsoft.VisualBasic.Compatibility10.csproj",
        @"Microsoft.Research\Contracts\Microsoft.VisualBasic.Contracts\Microsoft.VisualBasic.Contracts10.csproj",
        @"Microsoft.Research\Contracts\mscorlib\mscorlib.Contracts10.csproj",
        @"Microsoft.Research\Contracts\System\System.Contracts10.csproj",
        @"Microsoft.Research\Contracts\System.Configuration\System.Configuration10.csproj",
        @"Microsoft.Research\Contracts\System.Configuration.Install\System.Configuration.Install10.csproj",
        @"Microsoft.Research\Contracts\System.Core.Contracts\System.Core.Contracts10.csproj",
        @"Microsoft.Research\Contracts\System.Data\System.Data\System.Data10.csproj",
        @"Microsoft.Research\Contracts\System.Drawing\System.Drawing10.csproj",
        @"Microsoft.Research\Contracts\System.Security\System.Security10.csproj",
        @"Microsoft.Research\Contracts\System.Web\System.Web10.csproj",
        @"Microsoft.Research\Contracts\System.Windows\System.Windows10.csproj",
        @"Microsoft.Research\Contracts\System.Windows.Browser\System.Windows.Browser10.csproj",
        @"Microsoft.Research\Contracts\System.Windows.Forms\Windows.Forms\System.Windows.Forms10.csproj",
        @"Microsoft.Research\Contracts\System.Xml\System.Xml10.csproj",
        @"Microsoft.Research\Contracts\System.Xml.Linq\System.Xml.Linq10.csproj",
        @"Microsoft.Research\Contracts\WindowsBase\WindowsBase10.csproj",
        @"Microsoft.Research\Contracts\System.Numerics\System.Numerics.csproj"
      };

      for (int i = 0; i < v9Sources.Length; i++)
      {
        var opt = new Options(TestContext);
        CheckSourceFiles(opt.MakeAbsolute(v9Sources[i]), opt.MakeAbsolute(v10Sources[i]));
      }
    }
Exemple #26
0
        internal static string Build(Options options, out string absoluteSourceDir)
        {
            var sourceFile = options.MakeAbsolute(options.SourceFile);

            var compilerPath = options.GetCompilerAbsolutePath(ToolsRoot);
            Assert.IsTrue(File.Exists(compilerPath), string.Format("Can't find compiler at '{0}'", compilerPath));

            var contractreferencedir = options.MakeAbsolute(Path.Combine(ContractReferenceDirRoot, options.ReferencesFramework));
            var sourcedir = absoluteSourceDir = Path.GetDirectoryName(sourceFile);

            var outputdir = Path.Combine(Path.Combine(sourcedir, "bin"), options.CompilerPath);
            var extension = options.UseExe ? ".exe" : ".dll";
            var targetKind = options.UseExe ? "exe" : "library";

            var targetfile = Path.Combine(outputdir, options.TestName + extension);

            // add Microsoft.Contracts reference if needed
            if (options.IsV35)
            {
                options.References.Add("Microsoft.Contracts.dll");
            }

            var oldCwd = Environment.CurrentDirectory;

            try
            {
                Environment.CurrentDirectory = sourcedir;

                var resolvedReferences = ResolveReferences(options);
                var referenceString = ReferenceOptions(resolvedReferences);

                if (!Directory.Exists(outputdir))
                {
                    Directory.CreateDirectory(outputdir);
                }

                string arguments = String.Format("/t:{4} /out:{0} {3} {2} {1}", targetfile, sourceFile, referenceString, options.FinalCompilerOptions, targetKind);

                if (!options.ReleaseMode)
                {
                    arguments = "/debug " + arguments;
                }

                var exitCode = RunProcess(sourcedir, compilerPath, arguments, true);

                if (exitCode != 0)
                {
                    return null;
                }

                // add reference to Microsoft.Contracts.dll if not already done because of transitive closure issues
                if (!options.References.Contains("Microsoft.Contracts.dll"))
                {
                    options.References.Add("Microsoft.Contracts.dll");

                    // recompute resolvedReferences
                    resolvedReferences = ResolveReferences(options);
                }

                CopyReferenceAssemblies(resolvedReferences, outputdir);

                return targetfile;
            }
            finally
            {
                Environment.CurrentDirectory = oldCwd;
            }
        }
Exemple #27
0
        private static bool FindReference(Options options, List<string> result, string filename, string framework)
        {
            foreach (var root in System.Linq.Enumerable.Concat(options.LibPaths, new[] { ReferenceDirRoot, ContractReferenceDirRoot }))
            {
                var dir = options.MakeAbsolute(Path.Combine(root, framework));

                var path = Path.Combine(dir, filename);
                if (File.Exists(path))
                {
                    result.Add(path);
                    return true;
                }
            }
            return false;
        }
Exemple #28
0
        private static void CompareExpectedOutput(Options options, OutputCapture capture)
        {
            var expected = File.ReadAllLines(options.MakeAbsolute(@"Foxtrot\Tests\CheckerTests\" + Path.GetFileNameWithoutExtension(options.SourceFile) + ".Expected"));
            var expectedIndex = 0;
            var absoluteFile = options.MakeAbsolute(options.SourceFile);
            for (int i = 0; i < capture.stdOut.Count; i++)
            {
                var actual = capture.stdOut[i];

                if (actual.StartsWith("Trace:")) continue;
                if (actual.StartsWith("elapsed time:")) continue;
                if (expectedIndex >= expected.Length)
                {
                    Assert.AreEqual(null, actual);
                }
                var expectedLine = expected[expectedIndex++];

                if (actual.StartsWith(absoluteFile, StringComparison.InvariantCultureIgnoreCase))
                {
                    actual = TrimFilePath(actual, options.SourceFile);
                    expectedLine = TrimFilePath(expectedLine, options.SourceFile);
                }
                Assert.AreEqual(expectedLine, actual);
            }
            if (expectedIndex < expected.Length)
            {
                Assert.AreEqual(expected[expectedIndex], null);
            }
        }
Exemple #29
0
 internal static void RewriteBinary(Options options, string sourceDir)
 {
     var absoluteDir = options.MakeAbsolute(sourceDir);
     var binary = Path.Combine(absoluteDir, options.SourceFile);
     Rewrite(absoluteDir, binary, options);
 }
Exemple #30
0
        private static string FormLibPaths(string contractReferenceDir, Options options)
        {
            // MB: do not change CurrentDirectory because it makes parallel tests fail

            if (options.LibPaths == null)
                return "";

            StringBuilder sb = null;
            if (options.UseContractReferenceAssemblies)
                sb = new StringBuilder("/libpaths:").Append(contractReferenceDir);

            foreach (var path in options.LibPaths)
            {
                if (sb == null)
                    sb = new StringBuilder("/libpaths:");
                else
                    sb.Append(';');

                sb.Append(options.MakeAbsolute(Path.Combine(path, options.ContractFramework)));
            }
            if (sb == null)
                return "";
            return sb.ToString();
        }
Exemple #31
0
        private static List<string> ResolveReferences(Options options)
        {
            var result = new List<string>();
            foreach (var r in options.References)
            {
                foreach (var root in options.LibPaths)
                {
                    var dir = options.MakeAbsolute(Path.Combine(root, options.BuildFramework));

                    var path = Path.Combine(dir, r);
                    if (File.Exists(path))
                    {
                        result.Add(path);
                        break;
                    }
                }
                foreach (var root in new[] { ReferenceDirRoot, ContractReferenceDirRoot })
                {
                    var dir = options.MakeAbsolute(Path.Combine(root, options.BuildFramework));

                    var path = Path.Combine(dir, r);
                    if (File.Exists(path))
                    {
                        result.Add(path);
                        break;
                    }
                }
            }
            return result;
        }
Exemple #32
0
        internal static object Rewrite(ITestOutputHelper testOutputHelper, string absoluteSourceDir, string absoluteBinary, Options options, bool alwaysCapture = false)
        {
            string referencedir;

            if (Path.IsPathRooted(options.ReferencesFramework))
            {
                referencedir = options.ReferencesFramework;
            }
            else
            {
                referencedir = options.MakeAbsolute(Path.Combine(ReferenceDirRoot, options.ReferencesFramework));
            }

            var contractreferencedir = options.MakeAbsolute(Path.Combine(ContractReferenceDirRoot, options.ContractFramework));
            var absoluteSourcedir    = Path.GetDirectoryName(absoluteBinary);
            var absoluteSource       = absoluteBinary;
            var libPathsString       = FormLibPaths(absoluteSourceDir, contractreferencedir, options);

            var targetName = options.TestName + ".rw" + Path.GetExtension(absoluteBinary);
            var binDir     = options.UseBinDir ? Path.Combine(Path.Combine(absoluteSourcedir, "bin"), options.BuildFramework) : absoluteSourcedir;
            var targetfile = Path.Combine(binDir, targetName);

            testOutputHelper.WriteLine("Rewriting '{0}' to '{1}'", absoluteBinary, targetfile);

            if (!Directory.Exists(binDir))
            {
                Directory.CreateDirectory(binDir);
            }
            var optionString = String.Format("/out:{0} -nobox -libpaths:{3} {4} {1} {2}", targetfile, options.FoxtrotOptions, absoluteSource, referencedir, libPathsString);

            if (absoluteBinary.EndsWith("mscorlib.dll"))
            {
                optionString = String.Format("/platform:{0} {1}", absoluteBinary, optionString);
            }

            var capture = RunProcessAndCapture(testOutputHelper, binDir, options.GetFullExecutablePath(FoxtrotExe), optionString, options.TestName);

            if (capture.ExitCode != 0)
            {
                if (options.MustSucceed)
                {
                    if (capture.ExitCode != 0)
                    {
                        testOutputHelper.WriteLine("");
                    }

                    Assert.Equal(0, capture.ExitCode);
                }
                return(capture);
            }
            else
            {
                if (alwaysCapture)
                {
                    return(capture);
                }

                if (options.UseBinDir)
                {
                    var fileName = Path.Combine(absoluteSourcedir, targetName);
                    if (File.Exists(fileName))
                    {
                        try
                        {
                            File.SetAttributes(fileName, FileAttributes.Normal);
                        }
                        catch { }
                    }

                    File.Copy(targetfile, fileName, true);
                    return(fileName);
                }
            }
            return(targetfile);
        }
Exemple #33
0
        private static void StartService(Options options)
        {
            if (serviceProcess != null)
                StopService();

            // First make sure another instance is not already running (because we don't know which version is running)
            foreach (var process in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(ClousotServiceHostExe)))
            {
                process.CloseMainWindow();
                if (!process.WaitForExit(1000))
                    process.Kill();
            }

            var serviceHostDir = options.MakeAbsolute(Path.GetDirectoryName(ClousotServiceHostExe));

            // note: we do not want to use ClousotServiceHostExe from the deployment directory because the app.config will be missing
            serviceProcess = StartServiceProcess(serviceHostDir, options.MakeAbsolute(ClousotServiceHostExe), "", Output.Ignore);
        }
 public void RewriteFrameworkDlls40()
 {
   var options = new Options(this.TestContext);
   var framework = @".NetFramework\v4.0";
   options.ContractFramework = framework;
   var dllpath = FrameworkBinariesToRewritePath + framework;
   options.BuildFramework = options.MakeAbsolute(dllpath);
   var extrareferencedir = options.MakeAbsolute(TestDriver.ReferenceDirRoot);
   options.LibPaths.Add(extrareferencedir);
   options.FoxtrotOptions = options.FoxtrotOptions + " /verbose:4";
   options.UseContractReferenceAssemblies = false;
   if (File.Exists(options.MakeAbsolute(Path.Combine(dllpath, options.SourceFile))))
   {
     TestDriver.RewriteBinary(options, dllpath);
   }
 }
        internal static string RewriteAndVerify(ITestOutputHelper testOutputHelper, string sourceDir, string binary, Options options)
        {
            if (!Path.IsPathRooted(sourceDir)) { sourceDir = options.MakeAbsolute(sourceDir); }
            if (!Path.IsPathRooted(binary)) { binary = options.MakeAbsolute(binary); }
            string target = Rewrite(testOutputHelper, sourceDir, binary, options) as string;
            if (target != null)
            {
                PEVerify(testOutputHelper, target, options);

                if (options.DeepVerify)
                    ExtractContracts(target, options);
            }
            return target;
        }
Exemple #36
0
        internal static string Build(Options options, string extraCompilerOptions, Output output, out string absoluteSourceDir)
        {
            var sourceFile = options.MakeAbsolute(options.SourceFile);
            var compilerpath = options.MakeAbsolute(Path.Combine(ToolsRoot, options.BuildFramework, options.Compiler));
            var contractreferencedir = options.MakeAbsolute(Path.Combine(ContractReferenceDirRoot, options.BuildFramework));
            var sourcedir = absoluteSourceDir = Path.GetDirectoryName(sourceFile);
            var outputdir = Path.Combine(sourcedir, "bin", options.BuildFramework);
            var extension = options.UseExe ? ".exe" : ".dll";
            var targetKind = options.UseExe ? "exe" : "library";
            var suffix = "_" + options.TestInstance;
            if (options.GenerateUniqueOutputName)
                suffix += "." + randGenerator.Next(0x10000).ToString("X4"); // enables concurrent tests on the same source file
            var targetfile = Path.Combine(outputdir, Path.GetFileNameWithoutExtension(sourceFile) + suffix + extension);
            // add Microsoft.Contracts reference if needed
            if (!options.BuildFramework.Contains("v4."))
            {
                options.References.Add("Microsoft.Contracts.dll");
            }

            // MB: do not modify the CurrentDirectory, that could cause parallel tests to fail

            var resolvedReferences = ResolveReferences(options);
            var referenceString = ReferenceOptions(resolvedReferences);
            if (!Directory.Exists(outputdir))
            {
                Directory.CreateDirectory(outputdir);
            }
            var args = String.Format("/debug /t:{4} /out:{0} {5} {3} {2} {1}", targetfile, sourceFile, referenceString, options.CompilerOptions(resolvedReferences), targetKind, extraCompilerOptions);
            var exitCode = RunProcess(sourcedir, compilerpath, args, output);
            if (exitCode != 0)
            {
                return null;
            }
            //CopyReferenceAssemblies(resolvedReferences, outputdir);

            return targetfile;
        }
    internal static string Build(Options options, out string absoluteSourceDir)
    {
      var sourceFile = options.MakeAbsolute(options.SourceFile);
      var compilerpath = options.MakeAbsolute(Path.Combine(ToolsRoot, String.Format(@"{0}\{1}", options.BuildFramework, options.Compiler)));
      var contractreferencedir = options.MakeAbsolute(Path.Combine(ContractReferenceDirRoot, options.BuildFramework));
      var sourcedir = absoluteSourceDir = Path.GetDirectoryName(sourceFile);
      var outputdir = Path.Combine(Path.Combine(sourcedir, "bin"), options.BuildFramework);
      var extension = options.UseExe ? ".exe" : ".dll";
      var targetKind = options.UseExe ? "exe" : "library";
      var targetfile = Path.Combine(outputdir, Path.GetFileNameWithoutExtension(sourceFile) + extension);
      // add Microsoft.Contracts reference if needed
      if (!options.BuildFramework.Contains("v4."))
      {
        options.References.Add("Microsoft.Contracts.dll");
      }
      var oldCwd = Environment.CurrentDirectory;
      try
      {
        Environment.CurrentDirectory = sourcedir;

        var resolvedReferences = ResolveReferences(options);
        var referenceString = ReferenceOptions(resolvedReferences);
        if (!Directory.Exists(outputdir))
        {
          Directory.CreateDirectory(outputdir);
        }
        var exitCode = RunProcess(sourcedir, compilerpath, String.Format("/debug /t:{4} /out:{0} {3} {2} {1}", targetfile, sourceFile, referenceString, options.CompilerOptions, targetKind));
        if (exitCode != 0)
        {
          return null;
        }
        CopyReferenceAssemblies(resolvedReferences, outputdir);
        return targetfile;
      }
      finally
      {
        Environment.CurrentDirectory = oldCwd;
      }
    }