def test_athena(self): """ L{RemoteStatsCollector._emit} sends a I{StatUpdate} command with Athena transport data for Athena message send and receive events. """ self.receiver._emit({'athena_send_messages': True, 'count': 17}) self.receiver._emit({'athena_received_messages': True, 'count': 12}) self.assertEqual(len(self.sender.boxes), 2) send = set([(d['key'], d['value']) for d in parseString(self.sender.boxes[0]['data'])]) self.assertEqual( send, set([('count', '17'), ('athena_send_messages', 'True')])) received = set([(d['key'], d['value']) for d in parseString(self.sender.boxes[1]['data'])]) self.assertEqual( received, set([('count', '12'), ('athena_received_messages', 'True')]))
def fromString(self, inString): """ Convert the given string to an L{Identifier}. """ box = parseString(inString)[0] return Identifier(shareID=box['shareID'].decode('utf-8'), localpart=box['localpart'].decode('utf-8'), domain=box['domain'].decode('utf-8'))
def test_unparse(self): """ L{sharing.IdentifierArgument} should be able to unserialize an L{Identifier} from a serialized box. """ argDict = CommandWithIdentifier.parseArguments( parseString(self.expectedData)[0], None) self.assertEquals(argDict, dict(shareIdentTest=self.identifier))
def assert_roundtrips(self, command, **kwargs): """ ``kwargs`` supplied to ``command`` can be serialized and unserialized. """ amp_protocol = None argument_box = command.makeArguments(kwargs, amp_protocol) [roundtripped] = parseString(argument_box.serialize()) parsed_objects = command.parseArguments(roundtripped, amp_protocol) self.assertEqual(kwargs, parsed_objects)
def callRemote(self, command, **kwargs): """ Call the corresponding responder on the configured locator. @param commandType: a subclass of L{AMP_MODULE.Command}. @param kwargs: Keyword arguments taken by the command, a C{dict}. @return: A C{Deferred} that fires with the result of the responder. """ # Get a Box for the supplied arguments. E.g. # command = ClusterStatusUpdate # kwargs = {"configuration": Deployment(nodes={Node(...)})} # The Box contains the Deployment object converted to nested dict. E.g. # Box({"configuration": {"$__class__$": "Deployment", ...}}) argument_box = command.makeArguments(kwargs, self._locator) # Serialize the arguments to prove that we can. For example, if an # argument would serialize to more than 64kB then we can't actually # serialize it so we want a test attempting this to fail. # Wire format will contain bytes. E.g. # b"\x12\x32configuration..." wire_format = argument_box.serialize() # Now decode the bytes back to a Box [decoded_argument_box] = parseString(wire_format) # And supply that to the responder which internally reverses # makeArguments -> back to kwargs responder = self._locator.locateResponder(command.commandName) d = responder(decoded_argument_box) def serialize_response(response_box): # As above, prove we can serialize the response. wire_format = response_box.serialize() [decoded_response_box] = parseString(wire_format) return decoded_response_box d.addCallback(serialize_response) d.addCallback(command.parseResponse, self._locator) def massage_error(error): if error.check(RemoteAmpError): rje = error.value errorType = command.reverseErrors.get(rje.errorCode, UnknownRemoteError) return Failure(errorType(rje.description)) # In this case the actual AMP implementation closes the connection. # Weakly simulate that here by failing how things fail if the # connection closes and commands are outstanding. This is sort of # terrible behavior but oh well. https://tm.tl/7055 return Failure(ConnectionLost(str(error))) d.addErrback(massage_error) return d
def callRemote(self, command, **kwargs): """ Call the corresponding responder on the configured locator. @param commandType: a subclass of L{AMP_MODULE.Command}. @param kwargs: Keyword arguments taken by the command, a C{dict}. @return: A C{Deferred} that fires with the result of the responder. """ # Get a Box for the supplied arguments. E.g. # command = ClusterStatusUpdate # kwargs = {"configuration": Deployment(nodes={Node(...)})} # The Box contains the Deployment object converted to nested dict. E.g. # Box({"configuration": {"$__class__$": "Deployment", ...}}) argument_box = command.makeArguments(kwargs, self._locator) # Serialize the arguments to prove that we can. For example, if an # argument would serialize to more than 64kB then we can't actually # serialize it so we want a test attempting this to fail. # Wire format will contain bytes. E.g. # b"\x12\x32configuration..." wire_format = argument_box.serialize() # Now decode the bytes back to a Box [decoded_argument_box] = parseString(wire_format) # And supply that to the responder which internally reverses # makeArguments -> back to kwargs responder = self._locator.locateResponder(command.commandName) d = responder(decoded_argument_box) def serialize_response(response_box): # As above, prove we can serialize the response. wire_format = response_box.serialize() [decoded_response_box] = parseString(wire_format) return decoded_response_box d.addCallback(serialize_response) d.addCallback(command.parseResponse, self._locator) def massage_error(error): if error.check(RemoteAmpError): rje = error.value errorType = command.reverseErrors.get( rje.errorCode, UnknownRemoteError) return Failure(errorType(rje.description)) # In this case the actual AMP implementation closes the connection. # Weakly simulate that here by failing how things fail if the # connection closes and commands are outstanding. This is sort of # terrible behavior but oh well. https://tm.tl/7055 return Failure(ConnectionLost(str(error))) d.addErrback(massage_error) return d
def test_timestamp(self): """ L{RemoteStatsCollector._emit} sends a I{StatUpdate} command with a timestamp taken from the log event passed to it. """ self.receiver._emit({'time': 123456789.0, 'interface': IStatEvent}) self.assertEqual(len(self.sender.boxes), 1) timestamp = [ d['value'] for d in parseString(self.sender.boxes[0]['data']) if d['key'] == 'time' ] self.assertEqual(timestamp, ['123456789.0'])
def test_athena(self): """ L{RemoteStatsCollector._emit} sends a I{StatUpdate} command with Athena transport data for Athena message send and receive events. """ self.receiver._emit({ 'athena_send_messages': True, 'count': 17}) self.receiver._emit({ 'athena_received_messages': True, 'count': 12}) self.assertEqual(len(self.sender.boxes), 2) send = set([(d['key'], d['value']) for d in parseString(self.sender.boxes[0]['data'])]) self.assertEqual( send, set([('count', '17'), ('athena_send_messages', 'True')])) received = set([(d['key'], d['value']) for d in parseString(self.sender.boxes[1]['data'])]) self.assertEqual( received, set([('count', '12'), ('athena_received_messages', 'True')]))
def test_timestamp(self): """ L{RemoteStatsCollector._emit} sends a I{StatUpdate} command with a timestamp taken from the log event passed to it. """ self.receiver._emit({ 'time': 123456789.0, 'interface': IStatEvent}) self.assertEqual(len(self.sender.boxes), 1) timestamp = [ d['value'] for d in parseString(self.sender.boxes[0]['data']) if d['key'] == 'time'] self.assertEqual(timestamp, ['123456789.0'])
def _boxFromData(self, messageData): """ A box. @param messageData: a serialized AMP box representing either a message or an error. @type messageData: L{str} @raise MalformedMessage: if the C{messageData} parameter does not parse to exactly one AMP box. """ inputBoxes = parseString(messageData) if not len(inputBoxes) == 1: raise MalformedMessage() [inputBox] = inputBoxes return inputBox
def test_deliverStatEvents(self): """ When a L{RemoteStatsCollector} is active, it sends AMP boxes to its client when L{IStatEvent}s are logged. """ log.msg(interface=IStatEvent, foo="bar", baz=12, quux=u'\N{SNOWMAN}') self.assertEqual(len(self.sender.boxes), 1) stat = self.sender.boxes[0] self.assertNotIn(ASK, stat) self.assertEqual(stat[COMMAND], 'StatUpdate') # Skip testing the timestamp, another test which can control its value # will do that. data = set([(d['key'], d['value']) for d in parseString(stat['data']) if d['key'] != 'time']) self.assertIn(('foo', 'bar'), data) self.assertIn(('baz', '12'), data) self.assertIn(('quux', u'\N{SNOWMAN}'.encode('utf-8')), data)
def test_deliverStatEvents(self): """ When a L{RemoteStatsCollector} is active, it sends AMP boxes to its client when L{IStatEvent}s are logged. """ log.msg(interface=IStatEvent, foo="bar", baz=12, quux=u'\N{SNOWMAN}') self.assertEqual(len(self.sender.boxes), 1) stat = self.sender.boxes[0] self.assertNotIn(ASK, stat) self.assertEqual(stat[COMMAND], 'StatUpdate') # Skip testing the timestamp, another test which can control its value # will do that. data = set([ (d['key'], d['value']) for d in parseString(stat['data']) if d['key'] != 'time']) self.assertIn(('foo', 'bar'), data) self.assertIn(('baz', '12'), data) self.assertIn(('quux', u'\N{SNOWMAN}'.encode('utf-8')), data)
def key(self, domain, username): """ Retrieve the derived key for user with this name, in this domain. @param domain: This user's domain. @type domain: L{str} @param username: This user's name. @type username: L{str} @return: The user's key if they exist; otherwise L{None}. @rtype: L{str} or L{None} """ userpath = self.path.child(domain).child(username + ".info") if userpath.exists(): with userpath.open() as f: data = parseString(f.read())[0] return data['key']
def serialize_response(response_box): # As above, prove we can serialize the response. wire_format = response_box.serialize() [decoded_response_box] = parseString(wire_format) return decoded_response_box
def fromStringProto(self, inString, proto): boxes = amp.parseString(inString) return amp._stringsToObjects(boxes[0], self.subargs, proto)
if not os.path.exists(domainpath): os.makedirs(domainpath) userpath = os.path.join(domainpath, username+".info") if os.path.exists(userpath): raise NotAllowed() f = open(userpath, 'w') f.write(Box(username=username, password=password.encode('hex')).serialize()) f.close() def get(self, (domain, username)): domainpath = os.path.join(self.path, domain) if os.path.exists(domainpath): filepath = os.path.join(domainpath, username+".info") if os.path.exists(filepath): data = parseString(open(filepath).read())[0] return data['password'].decode('hex') class DirectoryCertificateAndUserStore(q2q.DirectoryCertificateStore): def __init__(self, filepath): q2q.DirectoryCertificateStore.__init__(self, filepath) self.users = _usermap(os.path.join(filepath, "users")) def getPrivateCertificate(self, domain): try: return q2q.DirectoryCertificateStore.getPrivateCertificate(self, domain) except KeyError: if len(self.localStore.keys()) > 10: # avoid DoS; nobody is going to need autocreated certs for more # than 10 domains raise