Files
termos/test/core/filesystem/filesystem.test.ts
T

401 lines
13 KiB
TypeScript

import { FileSystem } from "@core/filesystem/filesystem";
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/treenode";
import type { UserInfo } from "@core/session_manager";
import { createImmutableBuffer } from "@core/util/immutable_buffer";
import {
expectAlreadyExistsError,
expectDirectoryLoopError,
expectIllegalMutationError,
expectNodeNotFoundError,
expectNotADirectoryError,
} from "@tests/helpers/error_matchers";
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
import { describe, expect, it } from "vitest";
const testTree: FixtureNode = {
inode: 0,
name: "root",
type: "Dir",
children: [
{
inode: 1,
name: "hello.txt",
type: "File",
content: "Goodbye World!",
children: [],
},
{
inode: 2,
name: "sub",
type: "Dir",
children: [
{
inode: 3,
name: "world.txt",
type: "File",
content: "Goodbye!",
children: [],
},
{
inode: 4,
name: "bye",
type: "Dir",
children: [],
},
],
},
],
};
const fs = buildTestFS(testTree);
describe("FileSystem.init", () => {
const map: Map<number, TreeNode> = new Map();
const time = Date.now();
const root: DirectoryNode = {
type: TreeNodeType.Directory,
inode: 0,
name: "root",
parent: 0,
mode: 0o555,
owner: 0,
group: 0,
mtime: time,
atime: time,
ctime: time,
children: {},
};
map.set(0, root);
const base: ReadonlyMap<number, TreeNode> = map;
const overlay: ReadonlyMap<number, TreeNode | null> = new Map();
it("initializes correctly", () => {
const fs = FileSystem.init(base, overlay, 0);
expect(fs).toBeInstanceOf(FileSystem);
expect(fs.INodeIndex).toBe(0);
});
});
describe("createNode", () => {
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
it("creates a file and updates parent", () => {
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
expect(newFS).toBeInstanceOf(FileSystem);
const newNode = newFS.getNode(5) 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();
});
});