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)); }
public virtual void TestMultiChunkFile() { Path testPath = new Path(TestRootDir, "testMultiChunk"); FSDataOutputStream fout = localFs.Create(testPath); for (int i = 0; i < 1000; i++) { fout.Write(Runtime.GetBytesForString(("testing" + i))); } 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); }
public virtual void TestCorruptedChecksum() { Path testPath = new Path(TestRootDir, "testCorruptChecksum"); Path checksumPath = localFs.GetChecksumFile(testPath); // write a file to generate checksum FSDataOutputStream @out = localFs.Create(testPath, true); @out.Write(Runtime.GetBytesForString("testing 1 2 3")); @out.Close(); Assert.True(localFs.Exists(checksumPath)); FileStatus stat = localFs.GetFileStatus(checksumPath); // alter file directly so checksum is invalid @out = localFs.GetRawFileSystem().Create(testPath, true); @out.Write(Runtime.GetBytesForString("testing stale checksum")); @out.Close(); Assert.True(localFs.Exists(checksumPath)); // checksum didn't change on disk Assert.Equal(stat, localFs.GetFileStatus(checksumPath)); Exception e = null; try { localFs.SetVerifyChecksum(true); FileSystemTestHelper.ReadFile(localFs, testPath, 1024); } catch (ChecksumException ce) { e = ce; } finally { NUnit.Framework.Assert.IsNotNull("got checksum error", e); } localFs.SetVerifyChecksum(false); string str = FileSystemTestHelper.ReadFile(localFs, testPath, 1024); Assert.Equal("testing stale checksum", str); }
public virtual void TestTruncatedChecksum() { Path testPath = new Path(TestRootDir, "testtruncatedcrc"); FSDataOutputStream fout = localFs.Create(testPath); fout.Write(Runtime.GetBytesForString("testing truncation")); fout.Close(); // Read in the checksum Path checksumFile = localFs.GetChecksumFile(testPath); FileSystem rawFs = localFs.GetRawFileSystem(); FSDataInputStream checksumStream = rawFs.Open(checksumFile); byte[] buf = new byte[8192]; int read = checksumStream.Read(buf, 0, buf.Length); checksumStream.Close(); // Now rewrite the checksum file with the last byte missing FSDataOutputStream replaceStream = rawFs.Create(checksumFile); replaceStream.Write(buf, 0, read - 1); replaceStream.Close(); // Now reading the file should fail with a ChecksumException try { FileSystemTestHelper.ReadFile(localFs, testPath, 1024); NUnit.Framework.Assert.Fail("Did not throw a ChecksumException when reading truncated " + "crc file"); } catch (ChecksumException) { } // telling it not to verify checksums, should avoid issue. localFs.SetVerifyChecksum(false); string str = FileSystemTestHelper.ReadFile(localFs, testPath, 1024).ToString(); Assert.True("read", "testing truncation".Equals(str)); }