def _get_client_by_transport(self, options, transport, socket=None): # Create the protocol and client if options.json: protocol = TJSONProtocol.TJSONProtocol(transport) elif options.compact: protocol = TCompactProtocol.TCompactProtocol(transport) # No explicit option about protocol is specified. Try to infer. elif options.framed or options.unframed: protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) elif socket is not None: # If json, compact, framed, and unframed are not specified, # THeaderProtocol is the default. Create a protocol using either # fuzzy or non-fuzzy transport depending on if options.fuzz is set. if options.fuzz is not None: transport = TFuzzyHeaderTransport(socket, fuzz_fields=options.fuzz, verbose=True) else: transport = THeaderTransport(socket) protocol = THeaderProtocol.THeaderProtocol(transport) else: self._exit(error_message=('No valid protocol ' 'specified for %s' % (type(self))), status=os.EX_USAGE) transport.open() self._transport = transport client = self.service_class.Client(protocol) return client
def _get_client_by_transport(self, options, transport, socket=None): # Create the protocol and client if options.json: protocol = TJSONProtocol.TJSONProtocol(transport) elif options.compact: protocol = TCompactProtocol.TCompactProtocol(transport) # No explicit option about protocol is specified. Try to infer. elif options.framed or options.unframed: protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) elif socket is not None: # If json, compact, framed, and unframed are not specified, # THeaderProtocol is the default. Create a protocol using either # fuzzy or non-fuzzy transport depending on if options.fuzz is set. if options.fuzz is not None: transport = TFuzzyHeaderTransport( socket, fuzz_fields=options.fuzz, verbose=True) else: transport = THeaderTransport(socket) protocol = THeaderProtocol.THeaderProtocol(transport) else: self._exit(error_message=('No valid protocol ' 'specified for %s' % (type(self))), status=os.EX_USAGE) transport.open() self._transport = transport client = self.service_class.Client(protocol) return client
def setUp(self): """Create two buffers, transports, and protocols. self._h_trans uses THeaderTransport self._f_trans uses TFuzzyHeaderTransport """ cls = self.__class__ # THeaderTransport attributes self._h_buf = TMemoryBuffer() self._h_trans = THeaderTransport(self._h_buf) self._h_prot = THeaderProtocol(self._h_trans) # TFuzzyHeaderTransport attributes self._f_buf = TMemoryBuffer() self._f_trans = TFuzzyHeaderTransport( self._f_buf, fuzz_fields=cls.fuzz_fields, fuzz_all_if_empty=False, verbose=False) self._f_prot = THeaderProtocol(self._f_trans)
def _get_client_by_transport(self, options, transport, socket=None): # Create the protocol and client if options.json: protocol = TJSONProtocol.TJSONProtocol(transport) elif options.compact: protocol = TCompactProtocol.TCompactProtocol(transport) # No explicit option about protocol is specified. Try to infer. elif options.framed or options.unframed: protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) elif socket is not None: # If json, compact, framed, and unframed are not specified, # THeaderProtocol is the default. Create a protocol using either # fuzzy or non-fuzzy transport depending on if options.fuzz is set. if options.fuzz is not None: transport = TFuzzyHeaderTransport(socket, fuzz_fields=options.fuzz, verbose=True) else: transport = THeaderTransport(socket) if options.headers is not None: try: parsed_headers = eval(options.headers) except Exception: self._exit( error_message='Request headers (--headers) argument' ' failed eval') if not isinstance(parsed_headers, dict): self._exit( error_message='Request headers (--headers) argument' ' must evaluate to a dict') for header_name, header_value in parsed_headers.items(): transport.set_header(header_name, header_value) protocol = THeaderProtocol.THeaderProtocol(transport) else: self._exit(error_message=('No valid protocol ' 'specified for %s' % (type(self))), status=os.EX_USAGE) transport.open() self._transport = transport client = self.service_class.Client(protocol) return client
def _get_client_by_transport(self, options, transport, socket=None): # Create the protocol and client if options.json: protocol = TJSONProtocol.TJSONProtocol(transport) elif options.compact: protocol = TCompactProtocol.TCompactProtocol(transport) # No explicit option about protocol is specified. Try to infer. elif options.framed or options.unframed: protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) elif socket is not None: # If json, compact, framed, and unframed are not specified, # THeaderProtocol is the default. Create a protocol using either # fuzzy or non-fuzzy transport depending on if options.fuzz is set. if options.fuzz is not None: transport = TFuzzyHeaderTransport( socket, fuzz_fields=options.fuzz, verbose=True) else: transport = THeaderTransport(socket) if options.headers is not None: try: parsed_headers = eval(options.headers) except Exception: self._exit( error_message='Request headers (--headers) argument' ' failed eval') if not isinstance(parsed_headers, dict): self._exit( error_message='Request headers (--headers) argument' ' must evaluate to a dict') for header_name, header_value in parsed_headers.items(): transport.set_header(header_name, header_value) protocol = THeaderProtocol.THeaderProtocol(transport) else: self._exit(error_message=('No valid protocol ' 'specified for %s' % (type(self))), status=os.EX_USAGE) transport.open() self._transport = transport client = self.service_class.Client(protocol) return client
class FuzzyTransportTest(object): """Test class that sets up a THeaderTransport and a TFuzzyHeaderTransport. Used for writing and comparing messages using both transports. """ fuzz_fields = [] # Sample TestService method arguments sampleListStruct = ttypes.ListStruct( a=[True, False], b=[1, 2, 3], c=[1.2, 3.4], d=["ab", "cd"], e=[list(sm.xrange(n)) for n in sm.xrange(20)], f=[{ 1: 2 }, { 3: 4, 5: 6 }], g=[{"a", "b"}, {"c"}, set()]) def setUp(self): """Create two buffers, transports, and protocols. self._h_trans uses THeaderTransport self._f_trans uses TFuzzyHeaderTransport """ cls = self.__class__ # THeaderTransport attributes self._h_buf = TMemoryBuffer() self._h_trans = THeaderTransport(self._h_buf) self._h_prot = THeaderProtocol(self._h_trans) # TFuzzyHeaderTransport attributes self._f_buf = TMemoryBuffer() self._f_trans = TFuzzyHeaderTransport(self._f_buf, fuzz_fields=cls.fuzz_fields, fuzz_all_if_empty=False, verbose=False) self._f_prot = THeaderProtocol(self._f_trans) def writeObject(self, obj=sampleListStruct): """Write an object to the test and reference protocols. Return the contents of both buffers. """ obj.write(self._h_prot) obj.write(self._f_prot) self._h_trans.flush() self._f_trans.flush() h_buf = self._h_buf.getvalue() f_buf = self._f_buf.getvalue() return h_buf, f_buf def differentIndices(self, header, fuzzy): """Return a list of byte positions at which two messages' bytes differ. Header should be the contents of self._h_buf Fuzzy should be the contents of self._f_buf """ indices = [] for i, (h, f) in enumerate(itertools.izip(header, fuzzy)): if h != f: indices.append(i) return indices def assertEqualsExceptIndices(self, header, fuzzy, indices): """Assert that the buffers `header` and `fuzzy` are equal, except possibly at the byte positions included in `indices`. This ensures that the message produced by TFuzzyHeaderProtocol (fuzzy) is equal to the message produced by THeaderProtocol (header), except at the byte positions that are expected to be fuzzed.""" self.assertEquals(len(header), len(fuzzy)) for diff in self.differentIndices(header, fuzzy): self.assertIn(diff, indices)
class FuzzyTransportTest(object): """Test class that sets up a THeaderTransport and a TFuzzyHeaderTransport. Used for writing and comparing messages using both transports. """ fuzz_fields = [] # Sample TestService method arguments sampleListStruct = ttypes.ListStruct( a=[True, False], b=[1, 2, 3], c=[1.2, 3.4], d=["ab", "cd"], e=[list(sm.xrange(n)) for n in sm.xrange(20)], f=[{1: 2}, {3: 4, 5: 6}], g=[{"a", "b"}, {"c"}, set()] ) def setUp(self): """Create two buffers, transports, and protocols. self._h_trans uses THeaderTransport self._f_trans uses TFuzzyHeaderTransport """ cls = self.__class__ # THeaderTransport attributes self._h_buf = TMemoryBuffer() self._h_trans = THeaderTransport(self._h_buf) self._h_prot = THeaderProtocol(self._h_trans) # TFuzzyHeaderTransport attributes self._f_buf = TMemoryBuffer() self._f_trans = TFuzzyHeaderTransport( self._f_buf, fuzz_fields=cls.fuzz_fields, fuzz_all_if_empty=False, verbose=False) self._f_prot = THeaderProtocol(self._f_trans) def writeObject(self, obj=sampleListStruct): """Write an object to the test and reference protocols. Return the contents of both buffers. """ obj.write(self._h_prot) obj.write(self._f_prot) self._h_trans.flush() self._f_trans.flush() h_buf = self._h_buf.getvalue() f_buf = self._f_buf.getvalue() return h_buf, f_buf def differentIndices(self, header, fuzzy): """Return a list of byte positions at which two messages' bytes differ. Header should be the contents of self._h_buf Fuzzy should be the contents of self._f_buf """ indices = [] for i, (h, f) in enumerate(itertools.izip(header, fuzzy)): if h != f: indices.append(i) return indices def assertEqualsExceptIndices(self, header, fuzzy, indices): """Assert that the buffers `header` and `fuzzy` are equal, except possibly at the byte positions included in `indices`. This ensures that the message produced by TFuzzyHeaderProtocol (fuzzy) is equal to the message produced by THeaderProtocol (header), except at the byte positions that are expected to be fuzzed.""" self.assertEquals(len(header), len(fuzzy)) for diff in self.differentIndices(header, fuzzy): self.assertIn(diff, indices)