createNode method implemented, canRead,
canWrite, canExecute methods implemented common FileSystem error types implemented, unit tests written and passing for filesystem initialization, createNode cases and permission methods
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
import { FileSystem } from "@core/filesystem/filesystem";
|
||||
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/treenode";
|
||||
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
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();
|
||||
|
||||
describe("FileSystem.init", () => {
|
||||
it("initializes correctly", () => {
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
expect(fs).toBeInstanceOf(FileSystem);
|
||||
expect(fs.INodeIndex).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createNode", () => {
|
||||
it("creates a file and updates parent", () => {
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||
|
||||
expect(newFS).toBeInstanceOf(FileSystem);
|
||||
const newNode = newFS.getNode(1) 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(1);
|
||||
|
||||
expect(newFS.INodeIndex).toBe(1);
|
||||
expect(newFS.Overlay.size).toBe(2);
|
||||
|
||||
expect(fs.getNode(1)).toBeUndefined();
|
||||
|
||||
const oldRoot = fs.getNode(0) as DirectoryNode;
|
||||
expect(oldRoot.children).toEqual({});
|
||||
});
|
||||
|
||||
it("fails to create a file as it alread exists", () => {
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||
|
||||
expect(() => newFS.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content)).toThrow();
|
||||
});
|
||||
|
||||
it("fails to create a file because the parent is not a directory", () => {
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||
|
||||
expect(() => newFS.createNode(1, "test", TreeNodeType.File, 0o644, 0, 0, content)).toThrow();
|
||||
});
|
||||
|
||||
it("fails to create a file because the specified parent doesnt exist", () => {
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||
|
||||
expect(() => fs.createNode(999, "readme", TreeNodeType.File, 0o644, 0, 0, content)).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("permission method group", () => {
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||
|
||||
it("returns true for user read permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o400, 1000, 1000, content);
|
||||
const perms = newFS.canRead(1, { uid: 1000, gid: 1000 });
|
||||
|
||||
expect(perms).toBe(true);
|
||||
});
|
||||
it("returns false for user read permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o200, 1000, 1000, content);
|
||||
const perms = newFS.canRead(1, { uid: 1000, gid: 1000 });
|
||||
|
||||
expect(perms).toBe(false);
|
||||
});
|
||||
it("returns true for group read permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o040, 1000, 2000, content);
|
||||
const perms = newFS.canRead(1, { uid: 999, gid: 2000 });
|
||||
|
||||
expect(perms).toBe(true);
|
||||
});
|
||||
it("returns false for group read permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o020, 1000, 2000, content);
|
||||
const perms = newFS.canRead(1, { uid: 999, gid: 2000 });
|
||||
|
||||
expect(perms).toBe(false);
|
||||
});
|
||||
it("returns true for other read permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o004, 1000, 2000, content);
|
||||
const perms = newFS.canRead(1, { uid: 999, gid: 3000 });
|
||||
|
||||
expect(perms).toBe(true);
|
||||
});
|
||||
it("returns false for other read permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o002, 1000, 2000, content);
|
||||
const perms = newFS.canRead(1, { uid: 999, gid: 3000 });
|
||||
|
||||
expect(perms).toBe(false);
|
||||
});
|
||||
it("returns true for user execute permissions", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o100, 1000, 2000, content);
|
||||
const perms = newFS.canExecute(1, { uid: 1000, gid: 2000 });
|
||||
|
||||
expect(perms).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user