import { FileSystem } from "@core/filesystem/filesystem"; import { NodeType, type FileNode } from "@core/filesystem/node"; import type { IDataStore, IMetaStore, VDiskConfig } from "@core/filesystem/vdisk"; import type { UserInfo } from "@core/session_manager"; import { createImmutableBuffer } from "@core/util/immutable_buffer"; import { MemDataStore, MemMetaStore } from "@core/util/temp_mem_stores"; import { deserializeFileSystem } from "@shell/serializer"; import { expectAlreadyExistsError, expectDirectoryLoopError, expectIllegalMutationError, expectNodeNotFoundError, expectNotADirectoryError, } from "@tests/helpers/error_matchers"; import { buildTestFS } from "@tests/helpers/filesystems_fixture"; import { Blob } from "node:buffer"; import { describe, expect, it } from "vitest"; const fs = buildTestFS(); describe("FileSystem.init", () => { const rawJson = `{"dev": [{"id": 0, "tree": {"type": "directory", "inode": 0, "mode": 443, "owner": 0, "group": 0, "parent": 0, "atime": 0, "mtime": 0, "ctime": 0, "children": []}}]}`; const arr = deserializeFileSystem(rawJson); const devices: { dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }[] = []; const data = new MemDataStore(); const meta = new MemMetaStore(); const config: VDiskConfig = { id: 0, label: `disk-0`, index: 0, readonly: false, size: 1024 * 1024 * 1024 * 8, //8 GiB base: arr[0]!.map, dataC: { chunkSize: 1024 * 8, //8 KiB cacheSize: 1024 * 1024 * 64, //64 MiB cacheTTL: 1000 * 60, // 1 min }, }; devices.push({ dstore: data, mstore: meta, config }); it("initializes correctly", () => { const fs = FileSystem.init(devices); expect(fs).toBeInstanceOf(FileSystem); }); }); describe("createNode", () => { const content = new Blob([new TextEncoder().encode("Lorem ipsum")]); it("creates a file and updates parent", () => { fs.createNode("0:0", "readme", { type: NodeType.File, mode: 0o644, owner: 0, group: 0, content: content, size: content.size, }); expect(fs).toBeInstanceOf(FileSystem); const newNode = fs.getNode("0") as FileNode; expect(newNode.type).toBe(TreeNodeType.File); expect(newNode.name).toBe("readme"); expect(new TextDecoder().decode(newNode.content.toArray())).toBe("Lorem ipsum"); expect(newNode.parent).toBe(0); expect(newNode.mode).toBe(0o644); const root = newFS.getNode(0) as DirectoryNode; expect(root.children["readme"]).toBe(5); expect(newFS.INodeIndex).toBe(5); expect(newFS.Overlay.size).toBe(2); expect(fs.getNode(5)).toBeUndefined(); const oldRoot = fs.getNode(0) as DirectoryNode; expect(oldRoot.children).toEqual({ "hello.txt": 1, sub: 2 }); }); it("fails to create a file as it alread exists", () => { const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content); expectAlreadyExistsError(() => newFS.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content)); }); it("fails to create a file because the parent is not a directory", () => { const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content); expectNotADirectoryError(() => newFS.createNode(1, "test", TreeNodeType.File, 0o644, 0, 0, content)); }); it("fails to create a file because the specified parent doesnt exist", () => { expectNodeNotFoundError(() => fs.createNode(999, "readme", TreeNodeType.File, 0o644, 0, 0, content)); }); }); describe("permission method group", () => { const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum")); it("returns true for user read permissions", () => { const user: UserInfo = { username: "root", uid: 1000, gid: 1000, group: [1000], home: 0, }; const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o400, 1000, 1000, content); const perms = newFS.canRead(4, user); expect(perms).toBe(true); }); it("returns false for user read permissions", () => { const user: UserInfo = { username: "root", uid: 1000, gid: 1000, group: [1000], home: 0, }; const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o200, 1000, 1000, content); const perms = newFS.canRead(5, user); expect(perms).toBe(false); }); it("returns true for group read permissions", () => { const user: UserInfo = { username: "root", uid: 999, gid: 2000, group: [1000], home: 0, }; const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o040, 1000, 2000, content); const perms = newFS.canRead(5, user); expect(perms).toBe(true); }); it("returns false for group read permissions", () => { const user: UserInfo = { username: "root", uid: 999, gid: 2000, group: [1000], home: 0, }; const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o020, 1000, 2000, content); const perms = newFS.canRead(5, user); expect(perms).toBe(false); }); it("returns true for other read permissions", () => { const user: UserInfo = { username: "root", uid: 999, gid: 3000, group: [1000], home: 0, }; const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o004, 1000, 2000, content); const perms = newFS.canRead(5, user); expect(perms).toBe(true); }); it("returns false for other read permissions", () => { const user: UserInfo = { username: "root", uid: 999, gid: 3000, group: [1000], home: 0, }; const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o002, 1000, 2000, content); const perms = newFS.canRead(5, user); expect(perms).toBe(false); }); it("returns true for user execute permissions", () => { const user: UserInfo = { username: "root", uid: 1000, gid: 2000, group: [1000], home: 0, }; const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o100, 1000, 2000, content); const perms = newFS.canExecute(5, user); expect(perms).toBe(true); }); }); describe("deleteNode", () => { const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum")); const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content); const newNode = newFS.getNode(1) as FileNode; it("fails to delete the root node", () => { expectIllegalMutationError(() => fs.deleteNode(0)); }); it("fails to delete a non existant node", () => { expectNodeNotFoundError(() => fs.deleteNode(999)); }); it("successfully deletes a single child node", () => { const newnewFS = newFS.deleteNode(1); const overlay = newnewFS.Overlay; const parent = newnewFS.getNode(0) as DirectoryNode; expect(overlay.get(1)).toBe(null); expect(parent.children).not.toHaveProperty(newNode.name); }); it("successfully deletes a directory recursively", () => { const newFS = fs.deleteNode(2); const parent = newFS.getNode(0) as DirectoryNode; expect(newFS.getNode(2)).toBe(undefined); expect(newFS.getNode(3)).toBe(undefined); expect(newFS.Overlay.size).toBeGreaterThanOrEqual(3); expect(newFS.Overlay.get(2)).toBe(null); expect(newFS.Overlay.get(3)).toBe(null); expect(parent.children).not.toHaveProperty("sub"); expect(fs.getNode(2)).not.toBe(undefined); expect(fs.Overlay.size).toBe(0); }); }); describe("modifyNode", () => { it("modifies content", () => { const changes = { content: createImmutableBuffer(new TextEncoder().encode("Hello, World!")), }; const before = Date.now(); const newFS = fs.modifyNode(1, changes); const node = newFS.getNode(1) as FileNode; const after = Date.now(); expect(new TextDecoder().decode(node.content.toArray())).toBe("Hello, World!"); expect(newFS.Overlay.size).toBe(1); expect(node.ctime).toBeGreaterThanOrEqual(before); expect(node.ctime).toBeLessThanOrEqual(after); expect(node.mtime).toBeGreaterThanOrEqual(before); expect(node.mtime).toBeLessThanOrEqual(after); }); it("modifies mode", () => { const changes = { mode: 0o222, }; const before = Date.now(); const newFS = fs.modifyNode(1, changes); const node = newFS.getNode(1) as FileNode; const oldNode = fs.getNode(1) as FileNode; const after = Date.now(); expect(node.mode).toEqual(0o222); expect(newFS.Overlay.size).toBe(1); expect(node.ctime).toBeGreaterThanOrEqual(before); expect(node.ctime).toBeLessThanOrEqual(after); expect(node.mtime).toEqual(oldNode.mtime); }); it("modifies owner and group", () => { const changes = { owner: 1, group: 2, }; const before = Date.now(); const newFS = fs.modifyNode(1, changes); const node = newFS.getNode(1) as FileNode; const oldNode = fs.getNode(1) as FileNode; const after = Date.now(); expect(node.owner).toBe(1); expect(node.group).toBe(2); expect(newFS.Overlay.size).toBe(1); expect(node.ctime).toBeGreaterThanOrEqual(before); expect(node.ctime).toBeLessThanOrEqual(after); expect(node.mtime).toEqual(oldNode.mtime); }); it("calls modify node with empty changes", () => { const changes = {}; const before = Date.now(); const newFS = fs.modifyNode(1, changes); const node = newFS.getNode(1) as FileNode; const oldNode = fs.getNode(1) as FileNode; const after = Date.now(); expect(node.content).toEqual(oldNode.content); expect(node.mode).toEqual(oldNode.mode); expect(node.owner).toEqual(oldNode.owner); expect(node.group).toEqual(oldNode.group); expect(node.content).toEqual(oldNode.content); expect(node.ctime).toBeGreaterThanOrEqual(before); expect(node.ctime).toBeLessThanOrEqual(after); expect(node.mtime).toEqual(oldNode.mtime); }); it("fails", () => { const changes = { owner: 1, group: 2, }; expectNodeNotFoundError(() => fs.modifyNode(999, changes)); }); }); describe("moveNode", () => { it("throws because of invalid destination node", () => { expectNodeNotFoundError(() => fs.moveNode(1, 999)); }); it("throws because of invalid source node", () => { expectNodeNotFoundError(() => fs.moveNode(999, 2)); }); it("throws because of invalid destination type", () => { expectNotADirectoryError(() => fs.moveNode(3, 1)); }); it("throws because of an already exising child under destination with the same name", () => { expectAlreadyExistsError(() => fs.moveNode(1, 2, "world.txt")); }); it("throws because it tried to move a node to a descendant of itself", () => { expectDirectoryLoopError(() => fs.moveNode(2, 4)); }); it("throws because it tried to move root", () => { expectIllegalMutationError(() => fs.moveNode(0, 1)); }); it("successfully moves a node to another directory", () => { const newFS = fs.moveNode(1, 2); const nodeSour = newFS.getNode(1) as FileNode; const nodeDest = newFS.getNode(2) as DirectoryNode; expect(nodeSour.parent).toBe(2); expect(nodeDest.children[nodeSour.name]).not.toBeUndefined(); expect((newFS.getNode(0) as DirectoryNode).children[nodeSour.name]).toBeUndefined(); }); it("successfully renames a node", () => { const newFS = fs.moveNode(3, 2, "foo.foo"); const nodeSour = newFS.getNode(3) as FileNode; const nodeDest = newFS.getNode(2) as DirectoryNode; expect(nodeSour.parent).toBe(2); expect(nodeSour.name).toBe("foo.foo"); expect((fs.getNode(3) as FileNode).parent).toEqual(nodeSour.parent); expect(nodeDest.children["world.txt"]).toBeUndefined(); expect(nodeDest.children["foo.foo"]).not.toBeUndefined(); }); });