using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Ionic.BZip2;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
namespace Ionic.BZip2.Tests
{
///
/// Summary description for UnitTest1
///
[TestClass]
public class UnitTest1
{
private System.Random rnd;
public UnitTest1()
{
this.rnd = new System.Random();
FilesToRemove = new System.Collections.Generic.List();
}
static UnitTest1()
{
string lorem = TestStrings["LoremIpsum"];
LoremIpsumWords = lorem.Split(" ".ToCharArray(),
System.StringSplitOptions.RemoveEmptyEntries);
}
private TestContext testContextInstance;
///
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
private string CurrentDir = null;
private string TopLevelDir = null;
protected System.Collections.Generic.List FilesToRemove;
// Use TestInitialize to run code before running each test
[TestInitialize()]
public void MyTestInitialize()
{
CurrentDir = System.IO.Directory.GetCurrentDirectory();
Assert.AreNotEqual(System.IO.Path.GetFileName(CurrentDir), "Temp", "at start");
string parentDir = System.Environment.GetEnvironmentVariable("TEMP");
TopLevelDir = System.IO.Path.Combine(parentDir, String.Format("Ionic.ZlibTest-{0}.tmp", System.DateTime.Now.ToString("yyyyMMMdd-HHmmss")));
System.IO.Directory.CreateDirectory(TopLevelDir);
System.IO.Directory.SetCurrentDirectory(TopLevelDir);
FilesToRemove.Add(TopLevelDir);
}
// Use TestCleanup to run code after each test has run
[TestCleanup()]
public void MyTestCleanup()
{
Assert.AreNotEqual(Path.GetFileName(CurrentDir), "Temp", "at finish");
Directory.SetCurrentDirectory(CurrentDir);
IOException GotException = null;
int Tries = 0;
do
{
try
{
GotException = null;
foreach (string filename in FilesToRemove)
{
if (Directory.Exists(filename))
{
// turn off any ReadOnly attributes
ClearReadOnly(filename);
Directory.Delete(filename, true);
}
if (File.Exists(filename))
{
File.Delete(filename);
}
}
Tries++;
}
catch (IOException ioexc)
{
GotException = ioexc;
// use an backoff interval before retry
System.Threading.Thread.Sleep(200 * Tries);
}
} while ((GotException != null) && (Tries < 4));
if (GotException != null) throw GotException;
FilesToRemove.Clear();
}
public static void ClearReadOnly(string dirname)
{
// don't traverse reparse points
if ((File.GetAttributes(dirname) & FileAttributes.ReparsePoint) != 0)
return;
foreach (var d in Directory.GetDirectories(dirname))
{
ClearReadOnly(d); // recurse
}
foreach (var f in Directory.GetFiles(dirname))
{
// clear ReadOnly and System attributes
var a = File.GetAttributes(f);
if ((a & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
a ^= FileAttributes.ReadOnly;
File.SetAttributes(f, a);
}
if ((a & FileAttributes.System) == FileAttributes.System)
{
a ^= FileAttributes.System;
File.SetAttributes(f, a);
}
}
}
#endregion
#region Helpers
private static void CopyStream(System.IO.Stream src, System.IO.Stream dest)
{
byte[] buffer = new byte[4096];
int n;
while ((n = src.Read(buffer, 0, buffer.Length)) > 0)
{
dest.Write(buffer, 0, n);
}
}
private static string GetTestDependentDir(string startingPoint, string subdir)
{
var location = startingPoint;
for (int i = 0; i < 3; i++)
location = Path.GetDirectoryName(location);
location = Path.Combine(location, subdir);
return location;
}
private static string GetTestBinDir(string startingPoint)
{
return GetTestDependentDir(startingPoint, "BZip2 Tests\\bin\\Debug");
}
private string GetContentFile(string fileName)
{
string testBin = GetTestBinDir(CurrentDir);
string path = Path.Combine(testBin, String.Format("Resources\\{0}", fileName));
Assert.IsTrue(File.Exists(path), "file ({0}) does not exist", path);
return path;
}
private static Int32 GetCrc(string fname)
{
using (var fs1 = File.OpenRead(fname))
{
var checker = new Ionic.Crc.CRC32(true);
return checker.GetCrc32(fs1);
}
}
internal string Exec(string program, string args)
{
return Exec(program, args, true);
}
internal string Exec(string program, string args, bool waitForExit)
{
return Exec(program, args, waitForExit, true);
}
internal string Exec(string program, string args, bool waitForExit, bool emitOutput)
{
if (program == null)
throw new ArgumentException("program");
if (args == null)
throw new ArgumentException("args");
// Microsoft.VisualStudio.TestTools.UnitTesting
this.TestContext.WriteLine("running command: {0} {1}", program, args);
string output;
int rc = Exec_NoContext(program, args, waitForExit, out output);
if (rc != 0)
throw new Exception(String.Format("Non-zero RC {0}: {1}", program, output));
if (emitOutput)
this.TestContext.WriteLine("output: {0}", output);
else
this.TestContext.WriteLine("A-OK. (output suppressed)");
return output;
}
internal static int Exec_NoContext(string program, string args, bool waitForExit, out string output)
{
System.Diagnostics.Process p = new System.Diagnostics.Process
{
StartInfo =
{
FileName = program,
CreateNoWindow = true,
Arguments = args,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
UseShellExecute = false,
}
};
if (waitForExit)
{
StringBuilder sb = new StringBuilder();
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
// must read at least one of the stderr or stdout asynchronously,
// to avoid deadlock
Action