test(ntfs): reuse resident attribute fixture (#15556)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-01 17:41:11 +00:00
1 parent 182cca1cbe
commit fa1970fd78
1 file changed
+16 -28
+16 -28
View File
@@ -1,25 +1,10 @@
import { expect, it } from "vitest";
import { residentMftAttribute } from "../../../tests/fixtures/ntfsMftExtension.js";
import {
readMftAttributes,
summarizeMftAttributes,
} from "./ntfsMftAttributes.js";
const resident = (type: number, value: Buffer, name = ""): Buffer => {
const nameBytes = Buffer.from(name, "utf16le");
const valueOffset = Math.ceil((24 + nameBytes.length) / 8) * 8;
const attribute = Buffer.alloc(
Math.ceil((valueOffset + value.length) / 8) * 8,
);
attribute.writeUInt32LE(type);
attribute.writeUInt32LE(attribute.length, 4);
attribute.writeUInt8(name.length, 9);
attribute.writeUInt16LE(24, 10);
nameBytes.copy(attribute, 24);
attribute.writeUInt32LE(value.length, 16);
attribute.writeUInt16LE(valueOffset, 20);
value.copy(attribute, valueOffset);
return attribute;
};
const scan = (...attributes: Buffer[]) => {
const marker = Buffer.alloc(8);
marker.writeUInt32LE(0xffffffff);
@@ -54,8 +39,8 @@ it.each([48, 72])(
value.writeBigInt64LE(-1n, 64);
}
const result = scan(
resident(0x10, value),
resident(0x80, Buffer.from("data"), " stream"),
residentMftAttribute(0x10, 0, value),
residentMftAttribute(0x80, 0, Buffer.from("data"), " stream"),
);
expect(result).toMatchObject({
attributeCoverage: "complete",
@@ -88,7 +73,7 @@ it.each([48, 72])(
it("rejects a FILE_NAME attribute with an undefined namespace", () => {
const value = Buffer.alloc(66);
value[65] = 4;
const result = scan(resident(0x30, value));
const result = scan(residentMftAttribute(0x30, 0, value));
expect(result).toMatchObject({
attributeCoverage: "incomplete",
attributeIssue: {
@@ -103,9 +88,9 @@ it("retains a prior FILE_NAME when the next name would read into attribute paddi
const value = Buffer.alloc(68);
value[64] = 1;
value.write("x", 66, "utf16le");
const valid = resident(0x30, value);
const valid = residentMftAttribute(0x30, 0, value);
value[64] = 2;
const malformed = resident(0x30, value);
const malformed = residentMftAttribute(0x30, 0, value);
expect(scan(valid, malformed)).toMatchObject({
attributes: [{ type: 0x30 }],
@@ -159,7 +144,10 @@ it.each([
])("isolates %s after a completed prefix", (_name, mutate) => {
const malformed = nonresident();
mutate(malformed);
const result = scan(resident(0x80, Buffer.from("ok")), malformed);
const result = scan(
residentMftAttribute(0x80, 0, Buffer.from("ok")),
malformed,
);
expect(result.attributes).toHaveLength(1);
expect(result.attributeCoverage).toBe("incomplete");
expect(result.attributeIssue).toMatchObject({
@@ -173,7 +161,7 @@ it("preserves self-referential attribute-list entries without traversing a cycle
list.writeUInt32LE(0x20);
list.writeUInt16LE(32, 4);
list.writeBigUInt64LE(0x0002_0000_0000_0004n, 16);
const result = scan(resident(0x20, list));
const result = scan(residentMftAttribute(0x20, 0, list));
expect(result).toMatchObject({
attributeCoverage: "complete",
fileNameCoverage: "incomplete",
@@ -212,7 +200,7 @@ it.each([
},
],
])("refuses %s at the attribute owner", (_name, mutate) => {
const attribute = resident(0x80, Buffer.from("test"));
const attribute = residentMftAttribute(0x80, 0, Buffer.from("test"));
mutate(attribute);
const result = scan(attribute);
expect(result).toMatchObject({
@@ -228,8 +216,8 @@ it("retains completed list entries and later bounded attributes after a corrupt
list.writeUInt16LE(32, 4);
list.writeUInt16LE(9, 24);
const result = scan(
resident(0x20, list),
resident(0x80, Buffer.from("tail")),
residentMftAttribute(0x20, 0, list),
residentMftAttribute(0x80, 0, Buffer.from("tail")),
);
expect(result).toMatchObject({
attributeCoverage: "incomplete",
@@ -268,8 +256,8 @@ it.each([48, 49, 60, 71, 72])(
const marker = Buffer.alloc(8);
marker.writeUInt32LE(0xffffffff);
const bytes = Buffer.concat([
resident(0x10, Buffer.alloc(length)),
resident(0x30, Buffer.alloc(66)),
residentMftAttribute(0x10, 0, Buffer.alloc(length)),
residentMftAttribute(0x30, 0, Buffer.alloc(66)),
marker,
]);
const detail = readMftAttributes(bytes, 0, bytes.length, 4096);