Skip to content

Commit

Permalink
Merge pull request #10 from fxhash/test/creating-directory-with-nonex…
Browse files Browse the repository at this point in the history
…istent-inode

test: revert when creating directory with nonexistent inode
  • Loading branch information
stevennevins authored Nov 29, 2023
2 parents 9495755 + 178322a commit 3374f56
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ out/
node_modules/

# Ignores development broadcast logs
!/broadcast
/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

Expand Down
12 changes: 8 additions & 4 deletions src/FileSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,23 @@ contract FileSystem is IFileSystem {
*/
function createDirectory(
string[] calldata _fileNames,
bytes32[] calldata _fileChecksums
bytes32[] calldata _inodeChecksums
) external returns (bytes32 directoryChecksum) {
if (_fileNames.length != _fileChecksums.length) revert LengthMismatch();
if (_fileNames.length != _inodeChecksums.length) revert LengthMismatch();
bytes32[] memory hashedFilenames = hashFileNames(_fileNames);

for (uint256 i; i < _inodeChecksums.length; i++) {
if (!inodeExists(_inodeChecksums[i])) revert InodeNotFound();
}
directoryChecksum = keccak256(
bytes.concat(
bytes1(uint8(InodeType.Directory)),
keccak256(abi.encodePacked(hashedFilenames)),
keccak256(abi.encodePacked(_fileChecksums))
keccak256(abi.encodePacked(_inodeChecksums))
)
);
if (inodeExists(directoryChecksum)) revert InodeAlreadyExists();
Directory memory newDirectory = Directory(_fileNames, _fileChecksums);
Directory memory newDirectory = Directory(_fileNames, _inodeChecksums);
inodes[directoryChecksum] = Inode(InodeType.Directory, File(bytes(""), new bytes32[](0)), newDirectory);
}

Expand Down
25 changes: 15 additions & 10 deletions test/FileSystem/CreateDirectory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ pragma solidity 0.8.23;
import "test/FileSystem/FileSystemTest.t.sol";

contract CreateDirectory is FileSystemTest {
bytes internal metadata;
bytes internal fileContent;
bytes32[] internal chunkChecksums;
bytes32[] internal hashedNames;
bytes32 internal fileChecksum;

function setUp() public override {
super.setUp();
fileNames = new string[](2);
fileNames[0] = "file1";
fileNames[1] = "file2";
filePointers = new bytes32[](2);
filePointers[0] = bytes32(uint256(1));
filePointers[1] = bytes32(uint256(2));
metadata = "file metadata";
fileContent = bytes("asdf");
(bytes32 checksum, ) = IContentStore(contentStore).addContent(fileContent);
chunkChecksums.push(checksum);
fileChecksum = fileSystem.createFile(metadata, chunkChecksums);
fileNames.push("file1");
filePointers.push(fileChecksum);
}

function test_CreateDirectory() public {
Expand All @@ -36,12 +41,11 @@ contract CreateDirectory is FileSystemTest {
}

function test_RevertsWhen_INodeNotFound() public {
/// when file checksum doesnt exist
filePointers[0] = keccak256(abi.encode("NON_EXISTENT"));
vm.expectRevert(INODE_NOT_FOUND_ERROR);
fileSystem.createDirectory(fileNames, filePointers);
}

function test_RevertsWhen_FileNotFound() public {}

function test_RevertsWhen_InvalidCharacters() public {
fileNames[0] = "/";
vm.expectRevert(INVALID_CHARACTER_ERROR);
Expand All @@ -50,7 +54,8 @@ contract CreateDirectory is FileSystemTest {

function test_NestedDirectories() public {
bytes32 checksum = fileSystem.createDirectory(fileNames, filePointers);
filePointers[1] = checksum;
fileNames.push("file2");
filePointers.push(checksum);
fileSystem.createDirectory(fileNames, filePointers);
}
}
27 changes: 14 additions & 13 deletions test/FileSystem/ReadDirectory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import "test/FileSystem/FileSystemTest.t.sol";

contract ReadDirectory is FileSystemTest {
bytes32[] internal hashedFileNames;
bytes internal metadata;
bytes internal fileContent;
bytes32[] internal chunkChecksums;
bytes32[] internal hashedNames;
bytes32 internal fileChecksum;

function setUp() public override {
super.setUp();
fileNames = new string[](2);
fileNames[0] = "file1";
fileNames[1] = "file2";
filePointers = new bytes32[](2);
filePointers[0] = bytes32(uint256(1));
filePointers[1] = bytes32(uint256(2));
metadata = "file metadata";
fileContent = bytes("asdf");
(bytes32 checksum, ) = IContentStore(contentStore).addContent(fileContent);
chunkChecksums.push(checksum);
fileChecksum = fileSystem.createFile(metadata, chunkChecksums);
fileNames.push("file1");
filePointers.push(fileChecksum);
}

function test_ReadDirectory() public {
Expand Down Expand Up @@ -41,7 +47,8 @@ contract ReadDirectory is FileSystemTest {

function test_NestedDirectory() public {
bytes32 checksum = fileSystem.createDirectory(fileNames, filePointers);
filePointers[1] = checksum;
fileNames.push("file2");
filePointers.push(checksum);
checksum = fileSystem.createDirectory(fileNames, filePointers);
}

Expand All @@ -51,12 +58,6 @@ contract ReadDirectory is FileSystemTest {
}

function test_RevertsWhen_ReadingFile() public {
bytes memory metadata = "file metadata";
bytes memory fileContent = bytes("asdf");
(bytes32 checksum, ) = IContentStore(contentStore).addContent(fileContent);
bytes32[] memory chunkChecksums = new bytes32[](1);
chunkChecksums[0] = checksum;
bytes32 fileChecksum = fileSystem.createFile(metadata, chunkChecksums);
vm.expectRevert(DIRECTORY_NOT_FOUND_ERROR);
fileSystem.readDirectory(fileChecksum);
}
Expand Down
6 changes: 4 additions & 2 deletions test/FileSystem/ReadFile.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ contract ReadFile is FileSystemTest {
}

function test_RevertsWhen_ReadingDirectory() public {
bytes32 checksum1 = fileSystem.createFile(bytes("file1"), chunkChecksums);
bytes32 checksum2 = fileSystem.createFile(bytes("file2"), chunkChecksums);
fileNames = new string[](2);
fileNames[0] = "file1";
fileNames[1] = "file2";
filePointers = new bytes32[](2);
filePointers[0] = bytes32(uint256(1));
filePointers[1] = bytes32(uint256(2));
filePointers[0] = checksum1;
filePointers[1] = checksum2;
bytes32 checksum = fileSystem.createDirectory(fileNames, filePointers);
vm.expectRevert(FILE_NOT_FOUND_ERROR);
fileSystem.readFile(checksum);
Expand Down

0 comments on commit 3374f56

Please sign in to comment.