text: /**
   * Copyright (c) 2016 - 2018 Sqreen. All Rights Reserved.
   * Please refer to our terms for more information: https://www.sqreen.io/terms.html
   */
  'use strict';
  const Code = require('code');
  const Lab = require('lab');
  const lab = exports.lab = Lab.script();
  
  const describe = lab.describe;
  const it = lab.it;
  const after = lab.after;
  const expect = Code.expect;
  const EventEmitter = require('events').EventEmitter;
  const Util = require('util');
  
  const Tracing = require('../../../../lib/instrumentation/hooks/tracingHook');
  const Proxyquire = require('proxyquire');
  
  after((done) => {
  
      try {
          require('continuation-local-storage').destroyNamespace('sqreen_session');
      }
      catch (_) {}
      done();
  });
  
  describe('Instrumentation', () => {
  
      describe('isEmitter', () => {
  
          it('should do the mdgsuhlhjgfadlusygdfa coverage', { plan: 4 }, (done) => {
  
              expect(Tracing._isEmitter({})).to.be.false();
              expect(Tracing._isEmitter({ on: 1 })).to.be.false();
              expect(Tracing._isEmitter({ on: 1, addListener: 1 })).to.be.false();
              expect(Tracing._isEmitter({ on: 1, addListener: 1, emit: 1 })).to.be.true();
              done();
          });
      });
  
      describe('Tracing', () => {
  
          it('should not work since req/res are not EventEmitters ', { plan: 2 }, (done) => {
  
              require('../../../../lib/instrumentation/sqreenDirector')._init();
              const MockerTracing = require('../../../../lib/instrumentation/hooks/tracingHook');
  
              const Server = function Server() {
  
                  EventEmitter.apply(this, arguments);
              };
              Util.inherits(Server, EventEmitter);
  
              const module = { Server };
              MockerTracing.enable(module, {});
              const server = new module.Server();
              server.addListener('request', (req, res) => {
  
                  expect(req).to.equal('req');
                  expect(res).to.equal('res');
                  done();
              });
  
              server.emit('request', 'req', 'res');
          });
  
          it('should trace calls', { plan: 6 }, (done) => {
  
              const Server = function Server() {
  
                  EventEmitter.apply(this, arguments);
              };
              Util.inherits(Server, EventEmitter);
  
              const module = { Server };
              Tracing.enable(module, {});
              const server = new module.Server();
  
              const _req = new EventEmitter();
              _req.name = 'req';
              const _res = new EventEmitter();
              _res.name = 'res';
  
              _res.write = function () {
  
                  done(new Error('should not have ran'));
              };
              _res.__sqreen_finisehd = true;
  
              server.addListener('request', (req, res) => {
  
                  expect(req.name).to.equal('req');
                  expect(res.name).to.equal('res');
  
                  const session = require('continuation-local-storage').getNamespace('sqreen_session');
  
                  expect(session.get('req').name).to.equal('req');
                  expect(session.get('req').__sqreen_uuid).to.exist();
                  expect(session.get('req').__sqreen_res.name).to.equal('res');
                  expect(session.get('res').name).to.equal('res');
  
                  res.write();
  
                  res.on('finish', () => {
  
                      done();
                  });
                  res.emit('finish');
  
              });
  
              server.emit('request', _req, _res);
          });
  
          it('should trace calls', { plan: 7 }, (done) => {
  
              const Server = function Server() {
  
                  EventEmitter.apply(this, arguments);
              };
              Util.inherits(Server, EventEmitter);
  
              const module = { Server };
              Tracing.enable(module, {});
              const server = new module.Server();
  
              const _req = new EventEmitter();
              _req.name = 'req';
              const _res = new EventEmitter();
              _res.name = 'res';
  
              _res.write = function (str) {
  
                  expect(str).to.equal('str');
              };
  
              server.addListener('request', (req, res) => {
  
                  expect(req.name).to.equal('req');
                  expect(res.name).to.equal('res');
  
                  const session = require('continuation-local-storage').getNamespace('sqreen_session');
  
                  expect(session.get('req').name).to.equal('req');
                  expect(session.get('req').__sqreen_uuid).to.exist();
                  expect(session.get('req').__sqreen_res.name).to.equal('res');
                  expect(session.get('res').name).to.equal('res');
  
                  res.write('str');
  
                  res.on('finish', () => {
  
                      done();
                  });
                  res.emit('finish');
  
              });
  
              server.emit('request', _req, _res);
          });
  
          it('should trace calls but block due to action', { plan: 1 }, (done) => {
  
              const Actions = require('../../../../lib/actions');
              Actions.enforceActionList([{ action_id: 1, action: 'block_ip', parameters: { ip_cidr: '192.168.0.1/24' } }]);
              const Server = function Server() {
  
                  EventEmitter.apply(this, arguments);
              };
              Util.inherits(Server, EventEmitter);
  
              const module = { Server };
              Tracing.enable(module, {});
              const server = new module.Server();
  
              const _req = new EventEmitter();
              _req.name = 'req';
              _req.headers = { 'x-forwarded-for': '192.168.0.252' };
              const _res = new EventEmitter();
              _res.name = 'res';
              _res.writeHead = function (code) {
  
                  expect(code).to.equal(500);
              };
              _res.end = function () {};
              _req.socket = { end: done };
  
              _res.write = function (str) {
  
                  expect(str).to.equal('str');
              };
  
              server.addListener('request', () => {
  
                  done(new Error('should not have been called'));
              });
  
              server.emit('request', _req, _res);
          });
  
          it('should trace calls and flag whitelisted ips', { plan: 10 }, (done) => {
  
              const TracingM = Proxyquire('../../../../lib/instrumentation/hooks/tracingHook', {
                  '../../util': {
                      getXFFOrRemoteAddress: function () {
  
                          return '127.0.0.1';
                      }
                  },
                  '../whitelist': {
                      ipIsWhiteListed: function () {
  
                          return '127.0.0.1';
                      }
                  },
                  '../../metric': {
                      addObservations: function (args, date) {
  
                          expect(date).to.exist();
                          expect(args).to.equal([['whitelisted', '127.0.0.1', 1]]);
                      }
                  }
              });
  
              const Server = function Server() {
  
                  EventEmitter.apply(this, arguments);
              };
              Util.inherits(Server, EventEmitter);
  
              const module = { Server };
              TracingM.enable(module, {});
              const server = new module.Server();
  
              const _req = new EventEmitter();
              _req.name = 'req';
              const _res = new EventEmitter();
              _res.name = 'res';
  
              _res.write = function (str) {
  
                  expect(str).to.equal('str');
              };
  
              server.addListener('request', (req, res) => {
  
                  expect(req.name).to.equal('req');
                  expect(res.name).to.equal('res');
  
                  const session = require('continuation-local-storage').getNamespace('sqreen_session');
  
                  expect(session.get('req').name).to.equal('req');
                  expect(session.get('req')._sqreen_ip_whitelist).to.equal(true);
                  expect(session.get('req').__sqreen_uuid).to.exist();
                  expect(session.get('req').__sqreen_res.name).to.equal('res');
                  expect(session.get('res').name).to.equal('res');
  
                  res.write('str');
  
                  res.on('finish', () => {
  
                      done();
                  });
                  res.emit('finish');
  
              });
  
              server.emit('request', _req, _res);
          });
  
          it('should trace calls and flag whitelisted path', { plan: 10 }, (done) => {
  
              const TracingM = Proxyquire('../../../../lib/instrumentation/hooks/tracingHook', {
                  '../../util': {
                      getXFFOrRemoteAddress: function () {
  
                          return '127.0.0.1';
                      }
                  },
                  '../whitelist': {
                      pathIsWhiteListed: function () {
  
                          return '/home';
                      }
                  },
                  '../../metric': {
                      addObservations: function (args, date) {
  
                          expect(date).to.exist();
                          expect(args).to.equal([['whitelisted', '/home', 1]]);
                      }
                  }
              });
  
              const Server = function Server() {
  
                  EventEmitter.apply(this, arguments);
              };
              Util.inherits(Server, EventEmitter);
  
              const module = { Server };
              TracingM.enable(module, {});
              const server = new module.Server();
  
              const _req = new EventEmitter();
              _req.name = 'req';
              const _res = new EventEmitter();
              _res.name = 'res';
  
              _res.write = function (str) {
  
                  expect(str).to.equal('str');
              };
  
              server.addListener('request', (req, res) => {
  
                  expect(req.name).to.equal('req');
                  expect(res.name).to.equal('res');
  
                  const session = require('continuation-local-storage').getNamespace('sqreen_session');
  
                  expect(session.get('req').name).to.equal('req');
                  expect(session.get('req')._sqreen_ip_whitelist).to.equal(true);
                  expect(session.get('req').__sqreen_uuid).to.exist();
                  expect(session.get('req').__sqreen_res.name).to.equal('res');
                  expect(session.get('res').name).to.equal('res');
  
                  res.write('str');
  
                  res.on('finish', () => {
  
                      done();
                  });
                  res.emit('finish');
  
              });
  
              server.emit('request', _req, _res);
          });
  
          it('should trace calls and flag whitelisted path but with no metric', { plan: 8 }, (done) => {
  
              const Feature = require('../../../../lib/command/features');
              Feature.change({ whitelisted_metric: false });
  
              const TracingM = Proxyquire('../../../../lib/instrumentation/hooks/tracingHook', {
                  '../../util': {
                      getXFFOrRemoteAddress: function () {
  
                          return '127.0.0.1';
                      }
                  },
                  '../whitelist': {
                      pathIsWhiteListed: function () {
  
                          return '/home';
                      }
                  },
                  '../../metric': {
                      addObservations: function () {
  
                          throw new Error('should have not been called');
                      }
                  }
              });
  
              const Server = function Server() {
  
                  EventEmitter.apply(this, arguments);
              };
              Util.inherits(Server, EventEmitter);
  
              const module = { Server };
              TracingM.enable(module, {});
              const server = new module.Server();
  
              const _req = new EventEmitter();
              _req.name = 'req';
              const _res = new EventEmitter();
              _res.name = 'res';
  
              _res.write = function (str) {
  
                  expect(str).to.equal('str');
              };
  
              server.addListener('request', (req, res) => {
  
                  expect(req.name).to.equal('req');
                  expect(res.name).to.equal('res');
  
                  const session = require('continuation-local-storage').getNamespace('sqreen_session');
  
                  expect(session.get('req').name).to.equal('req');
                  expect(session.get('req')._sqreen_ip_whitelist).to.equal(true);
                  expect(session.get('req').__sqreen_uuid).to.exist();
                  expect(session.get('req').__sqreen_res.name).to.equal('res');
                  expect(session.get('res').name).to.equal('res');
  
                  res.write('str');
  
                  res.on('finish', () => {
  
                      Feature.change({ whitelisted_metric: true });
                      session.set('req', null);
                      session.set('res', null);
                      done();
                  });
                  res.emit('finish');
  
              });
  
              server.emit('request', _req, _res);
          });
  
          it('should ignore non request events', { plan: 4 }, (done) => {
  
              const session = require('continuation-local-storage').getNamespace('sqreen_session');
              const Server = function Server() {
  
                  EventEmitter.apply(this, arguments);
              };
              Util.inherits(Server, EventEmitter);
  
              const module = { Server };
              Tracing.enable(module, {});
              const server = new module.Server();
  
              const _req = new EventEmitter();
              _req.name = 'req';
              const _res = new EventEmitter();
              _res.name = 'res';
  
              server.addListener('request0', (req, res) => {
  
                  expect(req.name).to.equal('req');
                  expect(res.name).to.equal('res');
  
                  expect(session.get('req')).to.not.exist();
                  expect(session.get('res')).to.not.exist();
                  done();
              });
  
              server.emit('request0', _req, _res);
          });
      });
  });
  
  