public void TestGenerateDirectoryHashNoDirectory() { string nonExistentDir = Path.Combine(Path.GetFullPath(tempFolder), "temsp"); Utils.GenerateDirectoryHash(null, nonExistentDir, ""); Assert.Fail("Expected an IOException as the directory does not exist"); }
public void TestGenerateDirectoryHashEmptyDirectory() { string emptydir = Path.Combine(tempFolder, "subfolder"); Directory.CreateDirectory(emptydir); Utils.GenerateDirectoryHash(null, emptydir, ""); Assert.Fail("Expected an IOException as the directory is empty"); }
public void TestGenerateDirectoryHashWithFile() { // Create a temp file string tempfile = Path.Combine(tempFolder, "temp.txt"); File.OpenWrite(tempfile).Close(); Utils.GenerateDirectoryHash(null, tempfile, ""); Assert.Fail("Expected an IOException as we passed it a file"); }
// ========================================================================================== // Helper methods // ========================================================================================== // Helper method to allow tests of multiple code paths through generateDirectoryHash public void DoGenerateDirectoryHash(bool useRootDir, bool usePreviousHash) { // Use any old hash value string previousHashToUse = "3c08029b52176eacf802dee93129a9f1fd115008950e1bb968465dcd51bbbb9d"; // The hashes expected when we 1: do not pass a previousHash and 2: pass // the previousHash string expectedHash1 = "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"; string expectedHash2 = "6c9f96b2dd87d7a02fd3b7cc6026a6a96d21c4c53aaf5777439151690c48c7b8"; string expectedHash = usePreviousHash ? expectedHash2 : expectedHash1; string chaincodeSubDirString = "chaincode/example/java"; // Create the temp directories string rootDir = Path.GetFullPath(tempFolder); string chaincodeDir = Path.Combine(rootDir, chaincodeSubDirString); Directory.CreateDirectory(chaincodeDir); string rootDirString = null; string chaincodeDirString; if (useRootDir) { // Pass both a RootDir and a chaincodeDir to the function rootDirString = rootDir; chaincodeDirString = chaincodeSubDirString; } else { // Pass just a chaincodeDir to the function chaincodeDirString = chaincodeDir; } // Create a dummy file in the chaincode directory string tempfile = Path.Combine(tempFolder, "temp.txt"); File.OpenWrite(tempfile).Close(); string previousHash = usePreviousHash ? previousHashToUse : ""; string hash = Utils.GenerateDirectoryHash(rootDirString, chaincodeDirString, previousHash); Assert.AreEqual(expectedHash, hash); }