<commit_before>var tcpsock = require('../providers/core.tcpsocket');
// White-box test for the Chrome APIs implementation of
// Freedom's TCP socket provider.
// Modeled on Freedom's social.loopback.unit.spec.js.
describe("tcpsocket", function() {
  var provider;
  // Supplied as an argument to the mock chrome.socket.create callback.
  var createResult;
  // Supplied as an argument to the mock chrome.socket.connect callback.
  var connResult;
  // Supplied as an argument to the mock chrome.socket.send callback.
  var sendResult;
  // Supplied as an argument to the mock chrome.socket.getInfo callback.
  var getInfoResult;
  var continuation = jasmine.createSpy('continuation');

  beforeEach(function() {
    chrome = {
      sockets: {
        tcp: {
          create: function(args, callback) {
            callback(createResult);
          },
          connect: function(socketId, address, port, callback) {
            callback(connResult);
          },
          send: function(socketId, data, callback) {
            callback(sendResult);
          },
          getInfo: function(socketId, callback) {
            callback(getInfoResult);
          },
          onReceive: {
            removeListener: function() {}
          },
          onReceiveError: {
            removeListener: function() {}
          }
        }
      }
    };

    spyOn(chrome.sockets.tcp, 'create').and.callThrough();
    spyOn(chrome.sockets.tcp, 'connect').and.callThrough();
    spyOn(chrome.sockets.tcp, 'send').and.callThrough();
    spyOn(chrome.sockets.tcp, 'getInfo').and.callThrough();

    provider = new tcpsock.provider(
        jasmine.createSpy('channel'),
        jasmine.createSpy('dispatchEvent'));
});

  it('connects', function() {
    createResult = { socketId: 1025 };
    connResult = -1, // failure! don't want an infinite loop.
    provider.connect('localhost', 5000, continuation);
    expect(chrome.sockets.tcp.create).toHaveBeenCalledWith(
        jasmine.any(Object),
        jasmine.any(Function));
    expect(chrome.sockets.tcp.connect).toHaveBeenCalledWith(
        createResult.socketId,
        'localhost',
        5000,
        jasmine.any(Function));
    expect(continuation).toHaveBeenCalled();
  });

  it('getInfo', function() {
    createResult = { socketId: 1025 };
    connResult = -1, // failure! don't want an infinite loop.
    getInfoResult = {
      localAddress: 'localhost',
      localPort: '9999'
    };
    provider.connect('localhost', 5000, continuation);
    provider.getInfo(continuation);
    expect(chrome.sockets.tcp.getInfo).toHaveBeenCalledWith(
        createResult.socketId,
        jasmine.any(Function));
    expect(continuation).toHaveBeenCalledWith(getInfoResult);
  });

  it('writes', function() {
    createResult = { socketId: 1025 };
    connResult = -1, // failure! don't want an infinite loop.
    sendResult = {
      bytesSent: 4
    };
    provider.connect('localhost', 5000, continuation);
    provider.write(new ArrayBuffer(), continuation);
    expect(chrome.sockets.tcp.send).toHaveBeenCalledWith(
        createResult.socketId,
        new ArrayBuffer(),
        jasmine.any(Function));
    expect(continuation).toHaveBeenCalledWith(undefined, jasmine.any(Object));
  });
});<commit_msg>add test for listen<commit_after>var tcpsock = require('../providers/core.tcpsocket');
// White-box test for the Chrome APIs implementation of
// Freedom's TCP socket provider.
// Modeled on Freedom's social.loopback.unit.spec.js.
describe("tcpsocket", function() {
  var provider;
  // Supplied as an argument to the mock chrome.sockets.tcp.create callback.
  var createClientResult;
  // Supplied as an argument to the mock chrome.sockets.tcp.connect callback.
  var connResult;
  // Supplied as an argument to the mock chrome.sockets.tcp.send callback.
  var sendResult;
  // Supplied as an argument to the mock chrome.sockets.tcp.getInfo callback.
  var getClientInfoResult;

  // Supplied as an argument to the mock chrome.sockets.tcpServer.create callback.
  var createServerResult;
  // Supplied as an argument to the mock chrome.sockets.tcpServer.listen callback.
  var listenResult;

  var continuation = jasmine.createSpy('continuation');

  beforeEach(function() {
    chrome = {
      sockets: {
        tcp: {
          create: function(args, callback) {
            callback(createClientResult);
          },
          connect: function(socketId, address, port, callback) {
            callback(connResult);
          },
          send: function(socketId, data, callback) {
            callback(sendResult);
          },
          getInfo: function(socketId, callback) {
            callback(getClientInfoResult);
          },
          onReceive: {
            removeListener: function() {}
          },
          onReceiveError: {
            removeListener: function() {}
          }
        },
        tcpServer: {
          create: function(args, callback) {
            callback(createServerResult);
          },
          listen: function(socketId, address, port, backlog, callback) {
            callback(listenResult);
          },
          onAccept: {
            removeListener: function() {}
          },
          onAcceptError: {
            removeListener: function() {}
          }
        }
      }
    };

    spyOn(chrome.sockets.tcp, 'create').and.callThrough();
    spyOn(chrome.sockets.tcp, 'connect').and.callThrough();
    spyOn(chrome.sockets.tcp, 'send').and.callThrough();
    spyOn(chrome.sockets.tcp, 'getInfo').and.callThrough();

    spyOn(chrome.sockets.tcpServer, 'create').and.callThrough();
    spyOn(chrome.sockets.tcpServer, 'listen').and.callThrough();

    provider = new tcpsock.provider(
        jasmine.createSpy('channel'),
        jasmine.createSpy('dispatchEvent'));
});

  it('connects', function() {
    createClientResult = { socketId: 1025 };
    connResult = -1, // failure! don't want an infinite loop.
    provider.connect('localhost', 5000, continuation);
    expect(chrome.sockets.tcp.create).toHaveBeenCalledWith(
        jasmine.any(Object),
        jasmine.any(Function));
    expect(chrome.sockets.tcp.connect).toHaveBeenCalledWith(
        createClientResult.socketId,
        'localhost',
        5000,
        jasmine.any(Function));
    expect(continuation).toHaveBeenCalled();
  });

  it('getInfo', function() {
    createClientResult = { socketId: 1025 };
    connResult = -1, // failure! don't want an infinite loop.
    getClientInfoResult = {
      localAddress: 'localhost',
      localPort: '9999'
    };
    provider.connect('localhost', 5000, continuation);
    provider.getInfo(continuation);
    expect(chrome.sockets.tcp.getInfo).toHaveBeenCalledWith(
        createClientResult.socketId,
        jasmine.any(Function));
    expect(continuation).toHaveBeenCalledWith(getClientInfoResult);
  });

  it('writes', function() {
    createClientResult = { socketId: 1025 };
    connResult = -1, // failure! don't want an infinite loop.
    sendResult = {
      bytesSent: 4
    };
    provider.connect('localhost', 5000, continuation);
    provider.write(new ArrayBuffer(), continuation);
    expect(chrome.sockets.tcp.send).toHaveBeenCalledWith(
        createClientResult.socketId,
        new ArrayBuffer(),
        jasmine.any(Function));
    expect(continuation).toHaveBeenCalledWith(undefined, jasmine.any(Object));
  });

  it('listens', function() {
    createServerResult = { socketId: 2048 };
    provider.listen('127.0.0.1', 5000, continuation);
    expect(chrome.sockets.tcpServer.create).toHaveBeenCalledWith(
        jasmine.any(Object),
        jasmine.any(Function));
    expect(chrome.sockets.tcpServer.listen).toHaveBeenCalledWith(
        createServerResult.socketId,
        '127.0.0.1',
        5000,
        100,
        jasmine.any(Function));
    expect(continuation).toHaveBeenCalled();
  });
});
{'fraction_non_alphanumeric': 0.1263505783653235, 'fraction_numerical': 0.01258421253336723, 'mean_word_length': 2.7808745795290726, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3367596', 'n_tokens_mistral': 2359, 'n_tokens_neox': 2229, 'n_words': 449}