def test_fakesock_socket_real_sendall_when_http(POTENTIAL_HTTP_PORTS, old_socket): ("fakesock.socket#real_sendall should connect before sending data") # Background: the real socket will stop returning bytes after the # first call real_socket = old_socket.return_value real_socket.recv.side_effect = [b'response from foobar :)', b""] # And the potential http port is 4000 POTENTIAL_HTTP_PORTS.__contains__.side_effect = lambda other: int(other) == 4000 POTENTIAL_HTTP_PORTS.union.side_effect = lambda other: POTENTIAL_HTTP_PORTS # Given a fake socket socket = fakesock.socket() # When I call connect to a server in a port that is considered HTTP socket.connect(('foobar.com', 4000)) # And send some data socket.real_sendall(b"SOMEDATA") # Then connect should have been called real_socket.connect.assert_called_once_with(('foobar.com', 4000)) # And the socket was set to blocking real_socket.setblocking.assert_called_once_with(1) # And recv was called with the bufsize real_socket.recv.assert_has_calls([ call(socket._bufsize) ]) # And the buffer should contain the data from the server socket.fd.read().should.equal(b"response from foobar :)")
def test_fakesock_socket_real_sendall_when_http(old_socket): ("fakesock.socket#real_sendall sends data and buffers " "the response in the file descriptor") # Background: the real socket will stop returning bytes after the # first call real_socket = old_socket.return_value real_socket.recv.side_effect = [b'response from server', b""] # Given a fake socket socket = fakesock.socket() socket.is_http = True # When I call real_sendall with data, some args and kwargs socket.real_sendall(b"SOMEDATA", b'some extra args...', foo=b'bar') # Then it should have called sendall in the real socket real_socket.sendall.assert_called_once_with(b"SOMEDATA", b'some extra args...', foo=b'bar') # And the socket was set to blocking real_socket.setblocking.assert_called_once_with(1) # And recv was called with the bufsize real_socket.recv.assert_has_calls([ call(socket._bufsize) ]) # And the buffer should contain the data from the server socket.fd.read().should.equal(b"response from server") # And connect was called real_socket.connect.called.should.be.true
def test_fakesock_socket_real_sendall_socket_error_when_http(socket, old_socket): ("fakesock.socket#real_sendall should continue if the socket error was EAGAIN") socket.error = SocketErrorStub # Background: the real socket will stop returning bytes after the # first call real_socket = old_socket.return_value real_socket.recv.side_effect = [SocketErrorStub(42), b'after error', ""] # Given a fake socket socket = fakesock.socket() socket.is_http = True # When I call real_sendall with data, some args and kwargs socket.real_sendall(b"SOMEDATA", b'some extra args...', foo=b'bar') # Then it should have called sendall in the real socket real_socket.sendall.assert_called_once_with(b"SOMEDATA", b'some extra args...', foo=b'bar') # And the socket was set to blocking real_socket.setblocking.assert_called_once_with(1) # And recv was called with the bufsize real_socket.recv.assert_called_once_with(socket._bufsize) # And the buffer should contain the data from the server socket.fd.read().should.equal(b"") # And connect was called real_socket.connect.called.should.be.true
def test_fakesock_socket_getpeercert(dt): ("fakesock.socket#getpeercert should return a hardcoded fake certificate") # Background: dt.now.return_value = datetime(2013, 10, 4, 4, 20, 0) # Given a fake socket instance socket = fakesock.socket() # And that it's bound to some host and port socket.connect(('somewhere.com', 80)) # When I retrieve the peer certificate certificate = socket.getpeercert() # Then it should return a hardcoded value certificate.should.equal({ u'notAfter': 'Sep 29 04:20:00 GMT', u'subject': ( ((u'organizationName', u'*.somewhere.com'),), ((u'organizationalUnitName', u'Domain Control Validated'),), ((u'commonName', u'*.somewhere.com'),)), u'subjectAltName': ( (u'DNS', u'*somewhere.com'), (u'DNS', u'somewhere.com'), (u'DNS', u'*') ) })
def test_fakesock_socket_real_sendall(old_socket): ("fakesock.socket#real_sendall calls truesock#connect and bails " "out when not http") # Background: the real socket will stop returning bytes after the # first call real_socket = old_socket.return_value real_socket.recv.side_effect = [b'response from server', b""] # Given a fake socket socket = fakesock.socket() # When I call real_sendall with data, some args and kwargs socket.real_sendall(b"SOMEDATA", b'some extra args...', foo=b'bar') # Then it should have called sendall in the real socket real_socket.sendall.assert_called_once_with(b"SOMEDATA", b'some extra args...', foo=b'bar') # And setblocking was never called real_socket.setblocking.called.should.be.false # And recv was never called real_socket.recv.called.should.be.false # And the buffer is empty socket.fd.read().should.equal(b'') # And connect was never called real_socket.connect.called.should.be.false
def test_fakesock_socket_real_sendall(old_socket): ("fakesock.socket#real_sendall sends data and buffers " "the response in the file descriptor") # Background: the real socket will stop returning bytes after the # first call real_socket = old_socket.return_value real_socket.recv.side_effect = ['response from server', ""] # Given a fake socket socket = fakesock.socket() # When I call real_sendall with data, some args and kwargs socket.real_sendall("SOMEDATA", 'some extra args...', foo='bar') # Then it should have called sendall in the real socket real_socket.sendall.assert_called_once_with("SOMEDATA", 'some extra args...', foo='bar') # And the timeout was set to 0 real_socket.settimeout.assert_called_once_with(0) # And recv was called with the bufsize real_socket.recv.assert_has_calls([ call(16), call(16), ]) # And the buffer should contain the data from the server socket.fd.getvalue().should.equal("response from server") # And connect was never called real_socket.connect.called.should.be.false
def test_fakesock_socket_real_sendall_socket_error(socket, old_socket): ("fakesock.socket#real_sendall should continue if the socket error was EAGAIN" ) socket.error = SocketErrorStub # Background: the real socket will stop returning bytes after the # first call real_socket = old_socket.return_value real_socket.recv.side_effect = [SocketErrorStub(42), 'after error', ""] # Given a fake socket socket = fakesock.socket() # When I call real_sendall with data, some args and kwargs socket.real_sendall("SOMEDATA", 'some extra args...', foo='bar') # Then it should have called sendall in the real socket real_socket.sendall.assert_called_once_with("SOMEDATA", 'some extra args...', foo='bar') # And the timeout was set to 0 real_socket.settimeout.assert_called_once_with(0) # And recv was called with the bufsize real_socket.recv.assert_called_once_with(16) # And the buffer should contain the data from the server socket.fd.getvalue().should.equal("") # And connect was never called real_socket.connect.called.should.be.false
def test_fakesock_socket_real_sendall_socket_error(socket, old_socket): ("fakesock.socket#real_sendall should continue if the socket error was EAGAIN") socket.error = SocketErrorStub # Background: the real socket will stop returning bytes after the # first call real_socket = old_socket.return_value real_socket.recv.side_effect = [SocketErrorStub(42), 'after error', ""] # Given a fake socket socket = fakesock.socket() # When I call real_sendall with data, some args and kwargs socket.real_sendall("SOMEDATA", 'some extra args...', foo='bar') # Then it should have called sendall in the real socket real_socket.sendall.assert_called_once_with("SOMEDATA", 'some extra args...', foo='bar') # And the timeout was set to 0 real_socket.settimeout.assert_called_once_with(0) # And recv was called with the bufsize real_socket.recv.assert_called_once_with(16) # And the buffer should contain the data from the server socket.fd.getvalue().should.equal("") # And connect was never called real_socket.connect.called.should.be.false
def test_fakesock_socket_ssl(): ("fakesock.socket#ssl should take a socket instance and return itself") # Given a fake socket instance socket = fakesock.socket() # And a stubbed socket sentinel sentinel = Mock() # When I call `ssl` on that mock result = socket.ssl(sentinel) # Then it should have returned its first argument result.should.equal(sentinel)
def test_fakesock_socket_close(old_socket): ("fakesock.socket#close should close the actual socket in case " "it's not http and __truesock_is_connected__ is True") # Given a fake socket instance that is synthetically open socket = fakesock.socket() socket.__truesock_is_connected__ = True # When I close it socket.close() # Then its real socket should have been closed old_socket.return_value.close.assert_called_once_with() socket.__truesock_is_connected__.should.be.false
def test_fakesock_socket_connect_fallback(POTENTIAL_HTTP_PORTS, old_socket): ("fakesock.socket#connect should open a real connection if the " "given port is not a potential http port") # Background: the potential http ports are 80 and 443 POTENTIAL_HTTP_PORTS.__contains__.side_effect = lambda other: int(other) in (80, 443) # Given a fake socket instance socket = fakesock.socket() # When it is connected to a remote server in a port that isn't 80 nor 443 socket.connect(('somewhere.com', 42)) # Then it should have open a real connection in the background old_socket.return_value.connect.assert_called_once_with(('somewhere.com', 42))
def test_fakesock_socket_close(old_socket): ("fakesock.socket#close should close the actual socket in case " "it's not http and _closed is False") # Given a fake socket instance that is synthetically open socket = fakesock.socket() socket._closed = False # When I close it socket.close() # Then its real socket should have been closed old_socket.return_value.close.assert_called_once_with() # And _closed is set to True socket._closed.should.be.true
def test_fakesock_socket_connect_fallback(POTENTIAL_HTTP_PORTS, old_socket): ("fakesock.socket#connect should open a real connection if the " "given port is not a potential http port") # Background: the potential http ports are 80 and 443 POTENTIAL_HTTP_PORTS.__contains__.side_effect = lambda other: int(other) in (80, 443) # Given a fake socket instance socket = fakesock.socket() # When it is connected to a remote server in a port that isn't 80 nor 443 socket.connect(('somewhere.com', 42)) # Then it should have open a real connection in the background old_socket.return_value.connect.assert_called_once_with(('somewhere.com', 42)) # And _closed is set to False socket._closed.should.be.false
def test_fakesock_socket_makefile(old_socket): ("fakesock.socket#makefile should set the mode, " "bufsize and return its mocked file descriptor") # Given a fake socket that has a mocked Entry associated with it socket = fakesock.socket() socket._entry = Mock() # When I call makefile() fd = socket.makefile(mode='rw', bufsize=512) # Then it should have returned the socket's own filedescriptor expect(fd).to.equal(socket.fd) # And the mode should have been set in the socket instance socket._mode.should.equal('rw') # And the bufsize should have been set in the socket instance socket._bufsize.should.equal(512) # And the entry should have been filled with that filedescriptor socket._entry.fill_filekind.assert_called_once_with(fd)
def test_fakesock_socket_getpeercert(): ("fakesock.socket#getpeercert should return a hardcoded fake certificate") # Given a fake socket instance socket = fakesock.socket() # And that it's bound to some host socket._host = 'somewhere.com' # When I retrieve the peer certificate certificate = socket.getpeercert() # Then it should return a hardcoded value certificate.should.equal({ u'notAfter': 'Sep 29 04:20:00 GMT', u'subject': (((u'organizationName', u'*.somewhere.com'), ), ((u'organizationalUnitName', u'Domain Control Validated'), ), ((u'commonName', u'*.somewhere.com'), )), u'subjectAltName': ((u'DNS', u'*.somewhere.com'), (u'DNS', u'somewhere.com'), (u'DNS', u'*')) })