Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainMuller committed Jun 30, 2020
1 parent aa0b201 commit 0936a8f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/@jsii/kernel/lib/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class Kernel {
fs.mkdirpSync(packageDir);

// Force umask to have npm-install-like permissions
const umask = process.umask(0o022);
const originalUmask = process.umask(0o022);
try {
// untar the archive to its final location
tar.extract({
Expand All @@ -122,7 +122,7 @@ export class Kernel {
});
} finally {
// Reset umask to the initial value
process.umask(umask);
process.umask(originalUmask);
}

// read .jsii metadata from the root of the package
Expand Down
22 changes: 22 additions & 0 deletions packages/@jsii/kernel/test/kernel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ defineTest.skip = function (
return defineTest(name, method, test.skip);
};

test('load preserves file permissions', async () => {
// Changing the umask to 077 (which would neutralize group/other permissions)
const originalUmask = process.umask(0o077);

try {
const kernel = await createCalculatorSandbox(
'load_preserves_file_permissions',
);

const result = kernel.sinvoke({
fqn: 'jsii-calc.UmaskCheck',
method: 'mode',
});
expect(result.result).toBe(0o644);

return closeRecording(kernel);
} finally {
// Restore the original umask
process.umask(originalUmask);
}
});

defineTest('stats() return sandbox statistics', (sandbox) => {
const stats = sandbox.stats({});
expect(stats.objectCount).toBe(0);
Expand Down
17 changes: 17 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2502,3 +2502,20 @@ export abstract class Isomorphism {
return this;
}
}

/**
* Checks the current file permissions are cool (no funky UMASK down-scoping happened)
*
* @see https://github.com/aws/jsii/issues/1765
*/
export class UmaskCheck {
/**
* This should return 0o644 (-rw-r--r--)
*/
public static mode(): number {
// The bit-masking is to remove the file type information from .mode
return fs.statSync(__filename).mode & 0o0777;
}

private constructor() {}
}
36 changes: 35 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -11869,6 +11869,40 @@
}
]
},
"jsii-calc.UmaskCheck": {
"assembly": "jsii-calc",
"docs": {
"see": "https://github.com/aws/jsii/issues/1765",
"stability": "experimental",
"summary": "Checks the current file permissions are cool (no funky UMASK down-scoping happened)."
},
"fqn": "jsii-calc.UmaskCheck",
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2511
},
"methods": [
{
"docs": {
"stability": "experimental",
"summary": "This should return 0o644 (-rw-r--r--)."
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2515
},
"name": "mode",
"returns": {
"type": {
"primitive": "number"
}
},
"static": true
}
],
"name": "UmaskCheck"
},
"jsii-calc.UnaryOperation": {
"abstract": true,
"assembly": "jsii-calc",
Expand Down Expand Up @@ -13162,5 +13196,5 @@
}
},
"version": "0.0.0",
"fingerprint": "miVjqwWxNOLMY7fR23c/SVvfiGqkgqLgG+18qp4f8MM="
"fingerprint": "/0S7DMfFaE1kfja4CpY5k0Gl6j6opo50SUPCiBgy3Zg="
}

0 comments on commit 0936a8f

Please sign in to comment.