public virtual void PrepFiles() { lfs.SetVerifyChecksum(true); lfs.SetWriteChecksum(true); lfs.Delete(srcPath, true); lfs.Delete(dstPath, true); FSDataOutputStream @out = lfs.Create(srcPath); @out.WriteChars("hi"); @out.Close(); Assert.True(lfs.Exists(lfs.GetChecksumFile(srcPath))); }
/// <exception cref="System.IO.IOException"/> public virtual void TestReportChecksumFailure() { @base.Mkdirs(); Assert.True(@base.Exists() && @base.IsDirectory()); FilePath dir1 = new FilePath(@base, "dir1"); FilePath dir2 = new FilePath(dir1, "dir2"); dir2.Mkdirs(); Assert.True(dir2.Exists() && FileUtil.CanWrite(dir2)); string dataFileName = "corruptedData"; Path dataPath = new Path(new FilePath(dir2, dataFileName).ToURI()); Path checksumPath = fileSys.GetChecksumFile(dataPath); FSDataOutputStream fsdos = fileSys.Create(dataPath); try { fsdos.WriteUTF("foo"); } finally { fsdos.Close(); } Assert.True(fileSys.PathToFile(dataPath).Exists()); long dataFileLength = fileSys.GetFileStatus(dataPath).GetLen(); Assert.True(dataFileLength > 0); // check the the checksum file is created and not empty: Assert.True(fileSys.PathToFile(checksumPath).Exists()); long checksumFileLength = fileSys.GetFileStatus(checksumPath).GetLen(); Assert.True(checksumFileLength > 0); // this is a hack to force the #reportChecksumFailure() method to stop // climbing up at the 'base' directory and use 'dir1/bad_files' as the // corrupted files storage: FileUtil.SetWritable(@base, false); FSDataInputStream dataFsdis = fileSys.Open(dataPath); FSDataInputStream checksumFsdis = fileSys.Open(checksumPath); bool retryIsNecessary = fileSys.ReportChecksumFailure(dataPath, dataFsdis, 0, checksumFsdis , 0); Assert.True(!retryIsNecessary); // the data file should be moved: Assert.True(!fileSys.PathToFile(dataPath).Exists()); // the checksum file should be moved: Assert.True(!fileSys.PathToFile(checksumPath).Exists()); // check that the files exist in the new location where they were moved: FilePath[] dir1files = dir1.ListFiles(new _FileFilter_352()); Assert.True(dir1files != null); Assert.True(dir1files.Length == 1); FilePath badFilesDir = dir1files[0]; FilePath[] badFiles = badFilesDir.ListFiles(); Assert.True(badFiles != null); Assert.True(badFiles.Length == 2); bool dataFileFound = false; bool checksumFileFound = false; foreach (FilePath badFile in badFiles) { if (badFile.GetName().StartsWith(dataFileName)) { Assert.True(dataFileLength == badFile.Length()); dataFileFound = true; } else { if (badFile.GetName().Contains(dataFileName + ".crc")) { Assert.True(checksumFileLength == badFile.Length()); checksumFileFound = true; } } } Assert.True(dataFileFound); Assert.True(checksumFileFound); }
public virtual void TestVerifyChecksum() { Path testPath = new Path(TestRootDir, "testPath"); Path testPath11 = new Path(TestRootDir, "testPath11"); FSDataOutputStream fout = localFs.Create(testPath); fout.Write(Runtime.GetBytesForString("testing")); fout.Close(); fout = localFs.Create(testPath11); fout.Write(Runtime.GetBytesForString("testing you")); fout.Close(); // Exercise some boundary cases - a divisor of the chunk size // the chunk size, 2x chunk size, and +/-1 around these. FileSystemTestHelper.ReadFile(localFs, testPath, 128); FileSystemTestHelper.ReadFile(localFs, testPath, 511); FileSystemTestHelper.ReadFile(localFs, testPath, 512); FileSystemTestHelper.ReadFile(localFs, testPath, 513); FileSystemTestHelper.ReadFile(localFs, testPath, 1023); FileSystemTestHelper.ReadFile(localFs, testPath, 1024); FileSystemTestHelper.ReadFile(localFs, testPath, 1025); localFs.Delete(localFs.GetChecksumFile(testPath), true); Assert.True("checksum deleted", !localFs.Exists(localFs.GetChecksumFile (testPath))); //copying the wrong checksum file FileUtil.Copy(localFs, localFs.GetChecksumFile(testPath11), localFs, localFs.GetChecksumFile (testPath), false, true, localFs.GetConf()); Assert.True("checksum exists", localFs.Exists(localFs.GetChecksumFile (testPath))); bool errorRead = false; try { FileSystemTestHelper.ReadFile(localFs, testPath, 1024); } catch (ChecksumException) { errorRead = true; } Assert.True("error reading", errorRead); //now setting verify false, the read should succeed localFs.SetVerifyChecksum(false); string str = FileSystemTestHelper.ReadFile(localFs, testPath, 1024).ToString(); Assert.True("read", "testing".Equals(str)); }