Skip to content

Commit

Permalink
lint fix
Browse files Browse the repository at this point in the history
temp

temp

ready
  • Loading branch information
aditi-khare-mongoDB committed Oct 7, 2024
1 parent 8e8bfb2 commit 842b52a
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import { setTimeout } from 'timers';
import { TLSSocket } from 'tls';
import { promisify } from 'util';

import { MongoClient } from '../../mongodb';
import { getEncryptExtraOptions } from '../../tools/utils';
import { createTimerSandbox } from '../../unit/timer_sandbox';

describe('Auto Encryption (Integration)', function () {
describe.skip('CSOT', function () {
let client;
let clock;
let timerSandbox;
let sleep;

const getKmsProviders = () => {
const my_key = Buffer.from(
'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk',
'base64'
);
return { local: { key: my_key } };
};
const keyVaultNamespace = 'keyvault.datakeys';

afterEach(async function () {
await client?.close();
});

context('when client is provided timeoutContext', function () {
it('should time out command sent through after timeoutMS', async function () {
client = new MongoClient('mongodb://localhost:27017', {
autoEncryption: {
keyVaultNamespace,
kmsProviders: getKmsProviders(),
extraOptions: getEncryptExtraOptions()
},
timeoutMS: 10000
});
await client.connect();

const err$ = await client
.db('test')
.command({ ping: 1 })
.catch(e => e);
const err = err$;
console.log(err);
});
});

context('when client is not provided timeoutContext', function () {
beforeEach(async function () {
sinon.stub(TLSSocket.prototype, 'connect').callsFake(function (..._args) {
clock.tick(30000);
});
timerSandbox = createTimerSandbox();
clock = sinon.useFakeTimers();
sleep = promisify(setTimeout);
});

afterEach(async function () {
if (clock) {
timerSandbox.restore();
clock.restore();
clock = undefined;
}
sinon.restore();
});

it('should not timeout the command sent through autoEncryption after timeoutMS', async function () {
client = this.configuration.newClient(
{},
{
autoEncryption: {
keyVaultNamespace,
kmsProviders: getKmsProviders(),
extraOptions: getEncryptExtraOptions()
}
}
);

const sleepingFn = async () => {
await sleep(30000);
throw Error('Slept for 30s');
};

const err$ = Promise.all([
client.db('test').collection('test').insert({ a: 1 }),
sleepingFn()
]).catch(e => e);
clock.tick(30000);
const err = await err$;
expect(err.message).to.equal('Slept for 30s');
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import {
MongoOperationTimeoutError,
MongoServerSelectionError,
now,
squashError
ObjectId,
promiseWithResolvers
promiseWithResolvers,
squashError
} from '../../mongodb';
import { type FailPoint } from '../../tools/utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ describe('CSOT spec unit tests', function () {
});
});

context('when client is not provided timeoutMS`', function () {
context('when client is not provided timeoutMS', function () {
it('should pass timeoutMS into commands sent to mongocryptd', async function () {
client = this.configuration.newClient(
{},
Expand All @@ -241,9 +241,6 @@ describe('CSOT spec unit tests', function () {
});
});
});

// TODO(NODE-6390): Add timeoutMS support to Auto Encryption
it.skip('The remaining timeoutMS value should apply to commands sent to mongocryptd as part of automatic encryption.', () => {});
});

context.skip('Background Connection Pooling', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/tools/runner/hooks/leak_checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const leakCheckerAfterEach = async function () {
}
};

const TRACE_SOCKETS = process.env.TRACE_SOCKETS === 'true' ? true : false;
const TRACE_SOCKETS = true; // process.env.TRACE_SOCKETS === 'true' ? true : false;
const kSocketId = Symbol('socketId');
const originalCreateConnection = net.createConnection;
let socketCounter = 0n;
Expand Down
69 changes: 1 addition & 68 deletions test/unit/client-side-encryption/auto_encrypter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { MongocryptdManager } from '../../../src/client-side-encryption/mongocry
import { StateMachine } from '../../../src/client-side-encryption/state_machine';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { MongoClient } from '../../../src/mongo_client';
import { BSON, CSOTTimeoutContext, type DataKey } from '../../mongodb';
import { sleep } from '../../tools/utils';
import { BSON, type DataKey } from '../../mongodb';
import * as requirements from './requirements.helper';

const bson = BSON;
Expand Down Expand Up @@ -375,70 +374,4 @@ describe('AutoEncrypter', function () {
it('should provide the libmongocrypt version', function () {
expect(AutoEncrypter.libmongocryptVersion).to.be.a('string');
});

describe('CSOT', function () {
let autoEncrypter: AutoEncrypter;
let stateMachineSpy;
let client;

beforeEach(async function () {
client = new MockClient() as MongoClient;
autoEncrypter = new AutoEncrypter(client, {
keyVaultNamespace: 'admin.datakeys',
kmsProviders: {
aws: { accessKeyId: 'example', secretAccessKey: 'example' },
local: { key: Buffer.alloc(96) }
}
});
await autoEncrypter.init();
stateMachineSpy = sinon.spy(StateMachine.prototype, 'execute');
});

afterEach(async function () {
sinon.restore();
});

describe('#encrypt', function () {
context('when encrypt is provided a timeoutContext', function () {
it('should call stateMachine.execute with a timeoutMS', async function () {
const timeoutContext = new CSOTTimeoutContext({
timeoutMS: 500,
serverSelectionTimeoutMS: 30000
});
await sleep(300);
await autoEncrypter.encrypt('test.test', { hello: 1 }, { timeoutContext });
expect(stateMachineSpy.getCalls()[0].args[2]).to.not.be.undefined;
expect(stateMachineSpy.getCalls()[0].args[2].remainingTimeMS).to.be.lessThanOrEqual(200);
});
});
context('when encrypt is not provided a timeoutContext', function () {
it('should call stateMachine.execute without a timeoutMS', async function () {
await autoEncrypter.encrypt('test.test', { hello: 1 });
expect(stateMachineSpy.getCalls()[0].args[2]).to.be.undefined;
});
});
});

describe('#decrypt', function () {
context('when decrypt is provided a timeoutContext', function () {
it('should respect remainingTimeMS', async function () {
const timeoutContext = new CSOTTimeoutContext({
timeoutMS: 500,
serverSelectionTimeoutMS: 30000
});
await sleep(300);
await autoEncrypter.decrypt(BSON.serialize({ ok: 1 }), { timeoutContext });
expect(stateMachineSpy.getCalls()[0].args[2]).to.not.be.undefined;
expect(stateMachineSpy.getCalls()[0].args[2].remainingTimeMS).to.be.lessThanOrEqual(200);
});
});

context('when decrypt is not provided a timeoutContext', function () {
it('should call stateMachine.execute without a timeoutMS', async function () {
await autoEncrypter.decrypt(BSON.serialize({ ok: 1 }));
expect(stateMachineSpy.getCalls()[0].args[2]).to.be.undefined;
});
});
});
});
});

0 comments on commit 842b52a

Please sign in to comment.