Skip to content

Commit

Permalink
Fixing tests/bin/sessions, removed nested describes and concurrent test
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes authored and CMCDragonkai committed Dec 7, 2021
1 parent 4ca847b commit 40af8eb
Showing 1 changed file with 70 additions and 114 deletions.
184 changes: 70 additions & 114 deletions tests/bin/sessions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Session Token Refreshing', () => {
path.join(os.tmpdir(), 'polykey-test-'),
);
nodePath = path.join(dataDir, 'keynode');
command = ['vaults', 'list', '-np', nodePath];
command = ['agent', 'status', '-np', nodePath];
passwordFile = path.join(dataDir, 'passwordFile');
sessionFile = path.join(nodePath, 'token');
await fs.promises.writeFile(passwordFile, 'password');
Expand All @@ -68,6 +68,11 @@ describe('Session Token Refreshing', () => {
await fs.promises.rmdir(dataDir, { recursive: true });
});

beforeEach(async () => {
// Invalidate sessions
await polykeyAgent.sessionManager.resetKey();
});

test('Process should store session token in session file', async () => {
// Agent has not been unlocked yet
const result = await testUtils.pkStdio(
Expand All @@ -76,7 +81,7 @@ describe('Session Token Refreshing', () => {
dataDir,
);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain(vaultName);
expect(result.stdout).toContain('LIVE');

const buff = await fs.promises.readFile(sessionFile);
const newToken = buff.toString() as SessionToken;
Expand All @@ -85,131 +90,82 @@ describe('Session Token Refreshing', () => {
).not.toThrow();
});

describe('After session has been unlocked', () => {
beforeAll(async () => {
// Authorize session
await testUtils.pkStdio(
['agent', 'unlock', '-np', nodePath, '--password-file', passwordFile],
{},
dataDir,
);
}, global.polykeyStartupTimeout);
test('Process should refresh the session token', async () => {
const prevToken = await polykeyAgent.sessionManager.createToken();
await fs.promises.writeFile(sessionFile, prevToken);

test('Process should refresh the session token', async () => {
tokenBuffer = await fs.promises.readFile(sessionFile);
token = tokenBuffer.toString() as SessionToken;
const prevToken = token;
// At least 1 second of delay
// Expiry time resolution is in seconds
await sleep(1100);
const result = await testUtils.pkStdio(command, {}, dataDir);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('LIVE');

// At least 1 second of delay
// Expiry time resolution is in seconds
await sleep(1100);
const result = await testUtils.pkStdio(command, {}, dataDir);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain(vaultName);
const buff = await fs.promises.readFile(sessionFile);
const newToken = buff.toString() as SessionToken;
expect(
async () => await polykeyAgent.sessionManager.verifyToken(newToken),
).not.toThrow();
// Make sure the first and second tokens are not the same
expect(newToken).not.toEqual(prevToken);
});

const buff = await fs.promises.readFile(sessionFile);
const newToken = buff.toString() as SessionToken;
expect(
async () => await polykeyAgent.sessionManager.verifyToken(newToken),
).not.toThrow();
// Make sure the first and second tokens are not the same
expect(newToken).not.toEqual(prevToken);
});
test('Serial processes should refresh the session token', async () => {
token = await polykeyAgent.sessionManager.createToken();
await fs.promises.writeFile(sessionFile, token);

test('Serial processes should refresh the session token', async () => {
let result = await testUtils.pkStdio(command, {}, dataDir);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain(vaultName);
let result = await testUtils.pkStdio(command, {}, dataDir);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('LIVE');

tokenBuffer = await fs.promises.readFile(sessionFile);
const token1 = tokenBuffer.toString() as SessionToken;
expect(
async () => await polykeyAgent.sessionManager.verifyToken(token1),
).not.toThrow();
tokenBuffer = await fs.promises.readFile(sessionFile);
const token1 = tokenBuffer.toString() as SessionToken;
expect(
async () => await polykeyAgent.sessionManager.verifyToken(token1),
).not.toThrow();

// At least 1 second of delay
// Expiry time resolution is in seconds
await sleep(1100);
result = await testUtils.pkStdio(command, {}, dataDir);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain(vaultName);
// At least 1 second of delay
// Expiry time resolution is in seconds
await sleep(1100);
result = await testUtils.pkStdio(command, {}, dataDir);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('LIVE');

tokenBuffer = await fs.promises.readFile(sessionFile);
const token2 = tokenBuffer.toString() as SessionToken;
expect(
async () => await polykeyAgent.sessionManager.verifyToken(token2),
).not.toThrow();
tokenBuffer = await fs.promises.readFile(sessionFile);
const token2 = tokenBuffer.toString() as SessionToken;
expect(
async () => await polykeyAgent.sessionManager.verifyToken(token2),
).not.toThrow();

// Make sure the first and second tokens are not the same
expect(token1).not.toEqual(token2);
});
// Make sure the first and second tokens are not the same
expect(token1).not.toEqual(token2);
});

test(
'Failing processes should unlock the session file',
async () => {
const result = await testUtils.pkStdio(
['vaults', 'delete', 'NotAVault', '-np', nodePath],
{},
dataDir,
);
expect(result.exitCode).not.toBe(0);

tokenBuffer = await fs.promises.readFile(sessionFile);
token = tokenBuffer.toString() as SessionToken;
expect(
async () => await polykeyAgent.sessionManager.verifyToken(token),
).not.toThrow();

// Try to lock the session file to ensure it's unlocked
const fd = fs.openSync(sessionFile, 'r');
expect(lock(fd)).toBeTruthy();
lock.unlock(fd);
fs.closeSync(fd);
},
global.failedConnectionTimeout,
);
test(
'Failing processes should unlock the session file',
async () => {
token = await polykeyAgent.sessionManager.createToken();
await fs.promises.writeFile(sessionFile, token);

test('Parallel processes should not refresh the session token', async () => {
let tokenP1 = 'token1' as SessionToken;
let tokenP2 = 'token2' as SessionToken;
const result = await testUtils.pkStdio(
['vaults', 'delete', 'NotAVault', '-np', nodePath],
{},
dataDir,
);
expect(result.exitCode).not.toBe(0);

tokenBuffer = await fs.promises.readFile(sessionFile);
const prevTokenParallel = tokenBuffer.toString() as SessionToken;

async function runListCommand(): Promise<SessionToken> {
// At least 1 second of delay
// Expiry time resolution is in seconds
await sleep(1000);
await testUtils.pkStdio(command, {}, dataDir);
const buffer = await fs.promises.readFile(sessionFile);
return buffer.toString() as SessionToken;
}

[tokenP1, tokenP2] = await Promise.all([
runListCommand(),
runListCommand(),
]);

// Verify both tokens
expect(
async () => await polykeyAgent.sessionManager.verifyToken(tokenP1),
).not.toThrow();
token = tokenBuffer.toString() as SessionToken;
expect(
async () => await polykeyAgent.sessionManager.verifyToken(tokenP2),
async () => await polykeyAgent.sessionManager.verifyToken(token),
).not.toThrow();

// Check that both commands were completed
expect(tokenP1).not.toEqual('token1');
expect(tokenP2).not.toEqual('token2');

// Check that the session token was refreshed exactly one time
if (tokenP1 === prevTokenParallel) {
expect(tokenP2).not.toEqual(prevTokenParallel);
} else if (tokenP2 === prevTokenParallel) {
expect(tokenP1).not.toEqual(prevTokenParallel);
} else {
expect(tokenP1).toEqual(tokenP2);
}
});
});
// Try to lock the session file to ensure it's unlocked
const fd = fs.openSync(sessionFile, 'r');
expect(lock(fd)).toBeTruthy();
lock.unlock(fd);
fs.closeSync(fd);
},
global.failedConnectionTimeout,
);
});

0 comments on commit 40af8eb

Please sign in to comment.