Skip to content

Commit

Permalink
Add CorruptCommandTest
Browse files Browse the repository at this point in the history
  • Loading branch information
evrignaud committed Oct 11, 2015
1 parent df3b213 commit 826e9df
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/test/java/org/fim/FullScenarioTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private void doSomeModifications() throws IOException

Files.move(rootDir.resolve("file01"), dir01.resolve("file01"));

tool.touch("file02");
tool.touchLastModified("file02");

Files.copy(rootDir.resolve("file03"), rootDir.resolve("file03.dup1"));
Files.copy(rootDir.resolve("file03"), rootDir.resolve("file03.dup2"));
Expand Down
129 changes: 129 additions & 0 deletions src/test/java/org/fim/command/CorruptCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* This file is part of Fim - File Integrity Manager
*
* Copyright (C) 2015 Etienne Vrignaud
*
* Fim is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fim is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fim. If not, see <http://www.gnu.org/licenses/>.
*/
package org.fim.command;

import static java.nio.file.StandardOpenOption.CREATE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.fim.model.HashMode.dontHash;
import static org.fim.model.HashMode.hashAll;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.fim.command.exception.BadFimUsageException;
import org.fim.model.CompareResult;
import org.fim.model.Context;
import org.fim.model.Difference;
import org.fim.model.FileState;
import org.fim.model.State;
import org.fim.tooling.RepositoryTool;
import org.junit.Before;
import org.junit.Test;

public class CorruptCommandTest
{
private static Path rootDir = Paths.get("target/" + CorruptCommandTest.class.getSimpleName());

private InitCommand initCommand;
private DiffCommand diffCommand;
private CommitCommand commitCommand;
private CorruptCommand corruptCommand;

private RepositoryTool tool;

@Before
public void setup() throws IOException
{
FileUtils.deleteDirectory(rootDir.toFile());
Files.createDirectories(rootDir);

initCommand = new InitCommand();
diffCommand = new DiffCommand();
commitCommand = new CommitCommand();
corruptCommand = new CorruptCommand();

tool = new RepositoryTool(rootDir);
}

@Test(expected = BadFimUsageException.class)
public void fileContentHashingIsMandatory() throws Exception
{
Context context = tool.createContext(dontHash, false);
corruptCommand.execute(context);
}

@Test
public void weCanDetectHardwareCorruption() throws Exception
{
Context context = tool.createContext(hashAll, true);

tool.createASetOfFiles(5);

State state = (State) initCommand.execute(context);

assertThat(state.getModificationCounts().getAdded()).isEqualTo(5);

doSomeModifications();

CompareResult compareResult = (CompareResult) diffCommand.execute(context);
assertThat(compareResult.modifiedCount()).isEqualTo(3);

compareResult = (CompareResult) corruptCommand.execute(context);
List<Difference> corrupted = compareResult.getCorrupted();
assertThat(corrupted.size()).isEqualTo(1);
FileState fileState = corrupted.get(0).getFileState();
assertThat(fileState.getFileName()).isEqualTo("file04");
}

private void doSomeModifications() throws IOException
{
tool.touchCreationTime("file01");

tool.touchLastModified("file02");

tool.setFileContent("file03", "file03 new content");

simulateHardwareCorruption("file04");

// Do nothing on file05
}

private void simulateHardwareCorruption(String fileName) throws IOException
{
Path file = rootDir.resolve(fileName);
// Keep original timestamps
BasicFileAttributes attributes = Files.readAttributes(file, BasicFileAttributes.class);

// Modify the file content
byte[] bytes = Files.readAllBytes(file);
bytes[bytes.length / 2] = 12;

Files.delete(file);
Files.write(file, bytes, CREATE);

// Restore the original timestamps
Files.getFileAttributeView(file, BasicFileAttributeView.class).setTimes(attributes.lastModifiedTime(), attributes.lastAccessTime(), attributes.creationTime());
}
}
3 changes: 1 addition & 2 deletions src/test/java/org/fim/internal/FimIgnoreManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.fim.internal;

import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;
import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -105,7 +104,7 @@ public void weCanLoadCorrectlyAFimIgnore() throws IOException
"**/.git\n" +
"foo\n" +
"**/bar";
Files.write(rootDir.resolve(".fimignore"), fileContent.getBytes(), CREATE, APPEND);
Files.write(rootDir.resolve(".fimignore"), fileContent.getBytes(), CREATE);

fimIgnore = cut.loadFimIgnore(rootDir);
assertThat(fimIgnore.getFilesToIgnoreLocally().toString()).isEqualTo(
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/org/fim/internal/hash/FileHasherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.fim.internal.hash;

import static java.lang.Math.min;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.fim.model.HashMode.dontHash;
Expand Down Expand Up @@ -372,7 +371,7 @@ private Path createFileWithSize(int fileSize) throws IOException

byte[] fileContent = out.toByteArray();
assertThat(fileContent.length).isEqualTo(fileSize);
Files.write(newFile, fileContent, CREATE, APPEND);
Files.write(newFile, fileContent, CREATE);
}

return newFile;
Expand Down
28 changes: 24 additions & 4 deletions src/test/java/org/fim/tooling/RepositoryTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
*/
package org.fim.tooling;

import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;

import org.fim.model.Context;
Expand Down Expand Up @@ -62,7 +63,15 @@ public void createASetOfFiles(int fileCount) throws IOException
}
}

public void touch(String fileName) throws IOException
public void touchCreationTime(String fileName) throws IOException
{
Path file = rootDir.resolve(fileName);
long timeStamp = Math.max(System.currentTimeMillis(), getCreationTime(file).toMillis());
timeStamp += 1_000;
setCreationTime(file, FileTime.fromMillis(timeStamp));
}

public void touchLastModified(String fileName) throws IOException
{
Path file = rootDir.resolve(fileName);
long timeStamp = Math.max(System.currentTimeMillis(), Files.getLastModifiedTime(file).toMillis());
Expand All @@ -77,7 +86,7 @@ public void createFimIgnore(Path directory, String content) throws IOException
{
Files.delete(file);
}
Files.write(file, content.getBytes(), CREATE, APPEND);
Files.write(file, content.getBytes(), CREATE);
}

public void createFile(String fileName) throws IOException
Expand Down Expand Up @@ -115,6 +124,17 @@ public void setFileContent(Path file, String content) throws IOException
sb.append("b_").append(index).append(": ").append(content).append('\n');
}

Files.write(file, sb.toString().getBytes(), CREATE, APPEND);
Files.write(file, sb.toString().getBytes(), CREATE);
}

private FileTime getCreationTime(Path file) throws IOException
{
BasicFileAttributes attributes = Files.readAttributes(file, BasicFileAttributes.class);
return attributes.creationTime();
}

private void setCreationTime(Path file, FileTime creationTime) throws IOException
{
Files.getFileAttributeView(file, BasicFileAttributeView.class).setTimes(null, null, creationTime);
}
}

0 comments on commit 826e9df

Please sign in to comment.