2023-05-28 23:13:24 +02:00
|
|
|
import { describe, expect, it } from 'vitest';
|
2024-05-06 00:14:13 +02:00
|
|
|
import { computeChmodOctalRepresentation, computeChmodSymbolicRepresentation, computePermissionsFromChmodOctalRepresentation, computePermissionsFromChmodSymbolicRepresentation } from './chmod-calculator.service';
|
2022-11-23 21:57:38 +01:00
|
|
|
|
|
|
|
describe('chmod-calculator', () => {
|
|
|
|
describe('computeChmodOctalRepresentation', () => {
|
|
|
|
it('get the octal representation from permissions', () => {
|
|
|
|
expect(
|
|
|
|
computeChmodOctalRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: true, write: true, execute: true },
|
|
|
|
group: { read: true, write: true, execute: true },
|
|
|
|
public: { read: true, write: true, execute: true },
|
2024-04-03 22:34:00 +02:00
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
2022-11-23 21:57:38 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('777');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodOctalRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: false, execute: false },
|
|
|
|
group: { read: false, write: false, execute: false },
|
|
|
|
public: { read: false, write: false, execute: false },
|
2024-04-03 22:34:00 +02:00
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
2022-11-23 21:57:38 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('000');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodOctalRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: true },
|
|
|
|
public: { read: true, write: false, execute: true },
|
2024-04-03 22:34:00 +02:00
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
2022-11-23 21:57:38 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('235');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodOctalRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: true, write: false, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: false, execute: true },
|
2024-04-03 22:34:00 +02:00
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
2022-11-23 21:57:38 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('421');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodOctalRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: false, execute: true },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: true, write: false, execute: false },
|
2024-04-03 22:34:00 +02:00
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
2022-11-23 21:57:38 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('124');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodOctalRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: false },
|
2024-04-03 22:34:00 +02:00
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
2022-11-23 21:57:38 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('222');
|
2023-06-18 05:01:54 -04:00
|
|
|
|
2024-04-03 22:34:00 +02:00
|
|
|
expect(
|
|
|
|
computeChmodOctalRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: false },
|
|
|
|
flags: { setuid: true, setgid: true, stickybit: true },
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('7222');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodOctalRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: false },
|
|
|
|
flags: { setuid: true, setgid: false, stickybit: false },
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('4222');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodOctalRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: false },
|
|
|
|
flags: { setuid: false, setgid: true, stickybit: false },
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('2222');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodOctalRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: false },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: true },
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('1222');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('computeChmodSymbolicRepresentation', () => {
|
2023-06-18 05:01:54 -04:00
|
|
|
it('get the symbolic representation from permissions', () => {
|
|
|
|
expect(
|
|
|
|
computeChmodSymbolicRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: true, write: true, execute: true },
|
|
|
|
group: { read: true, write: true, execute: true },
|
|
|
|
public: { read: true, write: true, execute: true },
|
2024-04-03 22:34:00 +02:00
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
2023-06-18 05:01:54 -04:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('rwxrwxrwx');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodSymbolicRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: false, execute: false },
|
|
|
|
group: { read: false, write: false, execute: false },
|
|
|
|
public: { read: false, write: false, execute: false },
|
2024-04-03 22:34:00 +02:00
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
2023-06-18 05:01:54 -04:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('---------');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodSymbolicRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: true },
|
|
|
|
public: { read: true, write: false, execute: true },
|
2024-04-03 22:34:00 +02:00
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
2023-06-18 05:01:54 -04:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('-w--wxr-x');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodSymbolicRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: true, write: false, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: false, execute: true },
|
2024-04-03 22:34:00 +02:00
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
2023-06-18 05:01:54 -04:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('r---w---x');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodSymbolicRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: false, execute: true },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: true, write: false, execute: false },
|
2024-04-03 22:34:00 +02:00
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
2023-06-18 05:01:54 -04:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('--x-w-r--');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodSymbolicRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: false },
|
2024-04-03 22:34:00 +02:00
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
2023-06-18 05:01:54 -04:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('-w--w--w-');
|
2024-04-03 22:34:00 +02:00
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodSymbolicRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: false, execute: true },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: true, write: false, execute: false },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: true },
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('--x-w-r-t');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodSymbolicRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: false, execute: true },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: true, write: false, execute: false },
|
|
|
|
flags: { setuid: false, setgid: true, stickybit: true },
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('--x-wsr-t');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodSymbolicRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: false, write: false, execute: true },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: true, write: false, execute: false },
|
|
|
|
flags: { setuid: true, setgid: true, stickybit: true },
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('--s-wsr-t');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodSymbolicRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: true, write: false, execute: true },
|
|
|
|
group: { read: true, write: true, execute: false },
|
|
|
|
public: { read: true, write: false, execute: false },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('r-xrw-r--');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computeChmodSymbolicRepresentation({
|
|
|
|
permissions: {
|
|
|
|
owner: { read: true, write: true, execute: true },
|
|
|
|
group: { read: true, write: true, execute: true },
|
|
|
|
public: { read: true, write: true, execute: true },
|
|
|
|
flags: { setuid: true, setgid: true, stickybit: true },
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
).to.eql('rwsrwsrwt');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('computePermissionsFromChmodOctalRepresentation', () => {
|
|
|
|
it('throws on invalid octal values', () => {
|
|
|
|
expect(() => computePermissionsFromChmodOctalRepresentation('12')).to.throw();
|
|
|
|
expect(() => computePermissionsFromChmodOctalRepresentation('12345')).to.throw();
|
|
|
|
expect(() => computePermissionsFromChmodOctalRepresentation('999')).to.throw();
|
|
|
|
expect(() => computePermissionsFromChmodOctalRepresentation('9999')).to.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('get permissions from octal representation', () => {
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodOctalRepresentation('777'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: true, write: true, execute: true },
|
|
|
|
group: { read: true, write: true, execute: true },
|
|
|
|
public: { read: true, write: true, execute: true },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodOctalRepresentation('000'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: false, execute: false },
|
|
|
|
group: { read: false, write: false, execute: false },
|
|
|
|
public: { read: false, write: false, execute: false },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodOctalRepresentation('235'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: true },
|
|
|
|
public: { read: true, write: false, execute: true },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodOctalRepresentation('421'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: true, write: false, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: false, execute: true },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodOctalRepresentation('124'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: false, execute: true },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: true, write: false, execute: false },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodOctalRepresentation('222'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: false },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodOctalRepresentation('7222'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: false },
|
|
|
|
flags: { setuid: true, setgid: true, stickybit: true },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodOctalRepresentation('4222'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: false },
|
|
|
|
flags: { setuid: true, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodOctalRepresentation('2222'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: false },
|
|
|
|
flags: { setuid: false, setgid: true, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodOctalRepresentation('1222'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: false },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: true },
|
|
|
|
});
|
2023-06-18 05:01:54 -04:00
|
|
|
});
|
2022-11-23 21:57:38 +01:00
|
|
|
});
|
2024-05-06 00:14:13 +02:00
|
|
|
describe('computePermissionsFromChmodSymbolicRepresentation', () => {
|
|
|
|
it('throws on invalid symbolic values', () => {
|
|
|
|
expect(() => computePermissionsFromChmodSymbolicRepresentation('rr---')).to.throw();
|
|
|
|
expect(() => computePermissionsFromChmodSymbolicRepresentation('rwxrwx--w')).to.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('get permissions from symbolic representation', () => {
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodSymbolicRepresentation('dr-xr-xr-x'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: true, write: false, execute: true },
|
|
|
|
group: { read: true, write: false, execute: true },
|
|
|
|
public: { read: true, write: false, execute: true },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodSymbolicRepresentation('-rw-r--r--'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: true, write: true, execute: false },
|
|
|
|
group: { read: true, write: false, execute: false },
|
|
|
|
public: { read: true, write: false, execute: false },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodSymbolicRepresentation('rwxrwxrwx'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: true, write: true, execute: true },
|
|
|
|
group: { read: true, write: true, execute: true },
|
|
|
|
public: { read: true, write: true, execute: true },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodSymbolicRepresentation('---------'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: false, execute: false },
|
|
|
|
group: { read: false, write: false, execute: false },
|
|
|
|
public: { read: false, write: false, execute: false },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodSymbolicRepresentation('r---wxr-x'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: true, write: false, execute: false },
|
|
|
|
group: { read: false, write: true, execute: true },
|
|
|
|
public: { read: true, write: false, execute: true },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodSymbolicRepresentation('r---w---x'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: true, write: false, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: false, execute: true },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodSymbolicRepresentation('--x-w-r--'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: false, execute: true },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: true, write: false, execute: false },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodSymbolicRepresentation('-w--w--w-'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: false },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodSymbolicRepresentation('-ws-ws-wt'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: true, execute: true },
|
|
|
|
group: { read: false, write: true, execute: true },
|
|
|
|
public: { read: false, write: true, execute: true },
|
|
|
|
flags: { setuid: true, setgid: true, stickybit: true },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodSymbolicRepresentation('-ws-w--w-'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: true, execute: true },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: false },
|
|
|
|
flags: { setuid: true, setgid: false, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodSymbolicRepresentation('-w--ws-w-'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: true },
|
|
|
|
public: { read: false, write: true, execute: false },
|
|
|
|
flags: { setuid: false, setgid: true, stickybit: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
computePermissionsFromChmodSymbolicRepresentation('-w--w--wt'),
|
|
|
|
).to.eql({
|
|
|
|
owner: { read: false, write: true, execute: false },
|
|
|
|
group: { read: false, write: true, execute: false },
|
|
|
|
public: { read: false, write: true, execute: true },
|
|
|
|
flags: { setuid: false, setgid: false, stickybit: true },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-11-23 21:57:38 +01:00
|
|
|
});
|