Example #1
0
    def testProtobufEncodeDateTimeMessage(self):
        """Test what happens when encoding a DateTimeField."""
        mine = HasDateTimeMessage(value=datetime.datetime(1970, 1, 1))
        nested = NestedDateTimeMessage()
        nested.value = message_types.DateTimeMessage(milliseconds=0)

        my_encoded = protobuf.encode_message(mine)
        encoded = protobuf.encode_message(nested)
        self.assertEquals(my_encoded, encoded)
  def testProtobufEncodeDateTimeMessage(self):
    """Test what happens when encoding a DateTimeField."""
    mine = HasDateTimeMessage(value=datetime.datetime(1970, 1, 1))
    nested = NestedDateTimeMessage()
    nested.value = message_types.DateTimeMessage(milliseconds=0)

    my_encoded = protobuf.encode_message(mine)
    encoded = protobuf.encode_message(nested)
    self.assertEquals(my_encoded, encoded)
Example #3
0
    def testProtobufEncodeDateTimeMessageWithTimeZone(self):
        """Test what happens when encoding a DateTimeField with a time zone."""
        for tz_offset in (30, -30, 8 * 60, 0):
            mine = HasDateTimeMessage(value=datetime.datetime(
                1970, 1, 1, tzinfo=util.TimeZoneOffset(tz_offset)))
            nested = NestedDateTimeMessage()
            nested.value = message_types.DateTimeMessage(
                milliseconds=0, time_zone_offset=tz_offset)

            my_encoded = protobuf.encode_message(mine)
            encoded = protobuf.encode_message(nested)
            self.assertEquals(my_encoded, encoded)
  def testProtobufEncodeDateTimeMessageWithTimeZone(self):
    """Test what happens when encoding a DateTimeField with a time zone."""
    for tz_offset in (30, -30, 8 * 60, 0):
      mine = HasDateTimeMessage(value=datetime.datetime(
          1970, 1, 1, tzinfo=util.TimeZoneOffset(tz_offset)))
      nested = NestedDateTimeMessage()
      nested.value = message_types.DateTimeMessage(
          milliseconds=0, time_zone_offset=tz_offset)

      my_encoded = protobuf.encode_message(mine)
      encoded = protobuf.encode_message(nested)
      self.assertEquals(my_encoded, encoded)
Example #5
0
  def __search_info(cls,
                    request,
                    info_class,
                    model_to_message,
                    customize_query=None):
    """Search over an Info subclass.

    Since all search request classes are very similar, it's possible to
    generalize how to do searches over them.

    Args:
      request: Search request received from client.
      info_class: The model.Info subclass to search.
      model_to_method: Function (model) -> message that transforms an instance
        of info_class in to the appropriate messages.Message subclass.
      customize_query: Function (request, query) -> None that adds additional
        filters to Datastore query based on specifics of that search message.

    Returns:
      Tuple (results, continuation):
        results: A list of messages satisfying the parameters of the request.
          None if there are no results.
        continuation: Continuation string for response if there are more
          results available.  None if there are no more results available.
    """
    # TODO(rafek): fetch_size from this request should take priority
    # over what is stored in continuation.
    if request.continuation:
      encoded_search, continuation = request.continuation.split(':', 1)
      decoded_search = base64.urlsafe_b64decode(encoded_search.encode('utf-8'))
      request = protobuf.decode_message(type(request), decoded_search)
    else:
      continuation = None
      encoded_search = unicode(base64.urlsafe_b64encode(
          protobuf.encode_message(request)))

    name_prefix = request.name_prefix

    query = info_class.search(name_prefix)
    query.order('name')
    if customize_query:
      customize_query(request, query)

    if continuation:
      # TODO(rafek): Pure query cursors are not safe for model with
      # query restrictions.  Would technically need to be encrypted.
      query.with_cursor(continuation)

    fetch_size = request.fetch_size

    model_instance = query.fetch(fetch_size)
    results = None
    continuation = None
    if model_instance:
      results = [model_to_message(i) for i in model_instance]
      if len(model_instance) == fetch_size:
        cursor = query.cursor()
        continuation = u'%s:%s' % (encoded_search, query.cursor())

    return results, continuation
Example #6
0
  def testImportFileSetFromFile(self):
    """Test importing a whole file set from a file."""
    file_set = descriptor.FileSet()
    file_set.files = [self.MakeFileDescriptor(u'standalone'),
                      self.MakeFileDescriptor(u'root.nested'),
                      self.MakeFileDescriptor(u'root.nested.nested'),
                     ]

    stream = StringIO.StringIO(protobuf.encode_message(file_set))

    self.mox = mox.Mox()
    opener = self.mox.CreateMockAnything()
    opener('my-file.dat', 'rb').AndReturn(stream)

    self.mox.ReplayAll()

    modules = {}
    definition.import_file_set('my-file.dat', modules=modules, _open=opener)

    self.assertEquals(file_set,
                      descriptor.describe_file_set(
                          [modules['standalone'],
                           modules['root.nested'],
                           modules['root.nested.nested'],
                          ]))
Example #7
0
  def __search_info(cls,
                    request,
                    info_class,
                    model_to_message,
                    customize_query=None):
    """Search over an Info subclass.

    Since all search request classes are very similar, it's possible to
    generalize how to do searches over them.

    Args:
      request: Search request received from client.
      info_class: The model.Info subclass to search.
      model_to_method: Function (model) -> message that transforms an instance
        of info_class in to the appropriate messages.Message subclass.
      customize_query: Function (request, query) -> None that adds additional
        filters to Datastore query based on specifics of that search message.

    Returns:
      Tuple (results, continuation):
        results: A list of messages satisfying the parameters of the request.
          None if there are no results.
        continuation: Continuation string for response if there are more
          results available.  None if there are no more results available.
    """
    # TODO(rafek): fetch_size from this request should take priority
    # over what is stored in continuation.
    if request.continuation:
      encoded_search, continuation = request.continuation.split(':', 1)
      decoded_search = base64.urlsafe_b64decode(encoded_search.encode('utf-8'))
      request = protobuf.decode_message(type(request), decoded_search)
    else:
      continuation = None
      encoded_search = unicode(base64.urlsafe_b64encode(
          protobuf.encode_message(request)))

    name_prefix = request.name_prefix

    query = info_class.search(name_prefix)
    query.order('name')
    if customize_query:
      customize_query(request, query)

    if continuation:
      # TODO(rafek): Pure query cursors are not safe for model with
      # query restrictions.  Would technically need to be encrypted.
      query.with_cursor(continuation)

    fetch_size = request.fetch_size

    model_instance = query.fetch(fetch_size)
    results = None
    continuation = None
    if model_instance:
      results = [model_to_message(i) for i in model_instance]
      if len(model_instance) == fetch_size:
        cursor = query.cursor()
        continuation = u'%s:%s' % (encoded_search, query.cursor())

    return results, continuation
Example #8
0
    def testImportFileSetFromFile(self):
        """Test importing a whole file set from a file."""
        file_set = descriptor.FileSet()
        file_set.files = [
            self.MakeFileDescriptor(u'standalone'),
            self.MakeFileDescriptor(u'root.nested'),
            self.MakeFileDescriptor(u'root.nested.nested'),
        ]

        stream = StringIO.StringIO(protobuf.encode_message(file_set))

        self.mox = mox.Mox()
        opener = self.mox.CreateMockAnything()
        opener('my-file.dat', 'rb').AndReturn(stream)

        self.mox.ReplayAll()

        modules = {}
        definition.import_file_set('my-file.dat',
                                   modules=modules,
                                   _open=opener)

        self.assertEquals(
            file_set,
            descriptor.describe_file_set([
                modules['standalone'],
                modules['root.nested'],
                modules['root.nested.nested'],
            ]))
  def testProtobufDecodeDateTimeMessage(self):
    """Test what happens when decoding a DateTimeMessage."""

    nested = NestedDateTimeMessage()
    nested.value = message_types.DateTimeMessage(milliseconds=2500)
    value = protobuf.decode_message(HasDateTimeMessage,
                                    protobuf.encode_message(nested)).value
    self.assertEqual(datetime.datetime(1970, 1, 1, 0, 0, 2, 500000), value)
Example #10
0
    def testProtobufDecodeDateTimeMessage(self):
        """Test what happens when decoding a DateTimeMessage."""

        nested = NestedDateTimeMessage()
        nested.value = message_types.DateTimeMessage(milliseconds=2500)
        value = protobuf.decode_message(HasDateTimeMessage,
                                        protobuf.encode_message(nested)).value
        self.assertEqual(datetime.datetime(1970, 1, 1, 0, 0, 2, 500000), value)
Example #11
0
 def _ValueToStr(self, value):
   """Serialize an application object so that it can be stored in memcache."""
   if not self.pb_class:
     return value
   elif self.pb_class == int:
     return str(value)
   else:
     return protobuf.encode_message(value)
Example #12
0
  def compute_deduplication_checksum(cls, request):
    """Computes the deduplication checksum for the given request.

    Args:
      request: The rpc_messages.LeaseRequest instance to deduplicate.

    Returns:
      The deduplication checksum.
    """
    return hashlib.sha1(protobuf.encode_message(request)).hexdigest()
Example #13
0
    def compute_deduplication_checksum(cls, request):
        """Computes the deduplication checksum for the given request.

    Args:
      request: The rpc_messages.LeaseRequest instance to deduplicate.

    Returns:
      The deduplication checksum.
    """
        return hashlib.sha1(protobuf.encode_message(request)).hexdigest()
 def testProtobufDecodeDateTimeMessageWithTimeZone(self):
   """Test what happens when decoding a DateTimeMessage with a time zone."""
   nested = NestedDateTimeMessage()
   nested.value = message_types.DateTimeMessage(milliseconds=12345678,
                                                time_zone_offset=60)
   value = protobuf.decode_message(HasDateTimeMessage,
                                   protobuf.encode_message(nested)).value
   self.assertEqual(datetime.datetime(1970, 1, 1, 3, 25, 45, 678000,
                                      tzinfo=util.TimeZoneOffset(60)),
                    value)
  def testUnrecognizedFieldWrongFormat(self):
    """Test that unrecognized fields in the wrong format are skipped."""

    class SimpleMessage(messages.Message):
      value = messages.IntegerField(1)

    message = SimpleMessage(value=3)
    message.set_unrecognized_field('from_json', 'test', messages.Variant.STRING)

    encoded = protobuf.encode_message(message)
    expected = (
        chr((1 << protobuf._WIRE_TYPE_BITS) | protobuf._Encoder.NUMERIC) +
        chr(3))
    self.assertEquals(encoded, expected)
Example #16
0
    def testUnrecognizedFieldWrongFormat(self):
        """Test that unrecognized fields in the wrong format are skipped."""
        class SimpleMessage(messages.Message):
            value = messages.IntegerField(1)

        message = SimpleMessage(value=3)
        message.set_unrecognized_field('from_json', 'test',
                                       messages.Variant.STRING)

        encoded = protobuf.encode_message(message)
        expected = (
            chr((1 << protobuf._WIRE_TYPE_BITS) | protobuf._Encoder.NUMERIC) +
            chr(3))
        self.assertEquals(encoded, expected)
Example #17
0
    def testDecodeWrongWireFormat(self):
        """Test what happens when wrong wire format found in protobuf."""
        class ExpectedProto(messages.Message):
            value = messages.StringField(1)

        class WrongVariant(messages.Message):
            value = messages.IntegerField(1)

        original = WrongVariant()
        original.value = 10
        self.assertErrorIs(messages.DecodeError,
                           'Expected wire type STRING but found NUMERIC',
                           protobuf.decode_message, ExpectedProto,
                           protobuf.encode_message(original))
  def testDecodeWrongWireFormat(self):
    """Test what happens when wrong wire format found in protobuf."""
    class ExpectedProto(messages.Message):
      value = messages.StringField(1)

    class WrongVariant(messages.Message):
      value = messages.IntegerField(1)

    original = WrongVariant()
    original.value = 10
    self.assertErrorIs(messages.DecodeError,
                       'Expected wire type STRING but found NUMERIC',
                       protobuf.decode_message,
                       ExpectedProto,
                       protobuf.encode_message(original))
Example #19
0
 def testProtobufDecodeDateTimeMessageWithTimeZone(self):
     """Test what happens when decoding a DateTimeMessage with a time zone."""
     nested = NestedDateTimeMessage()
     nested.value = message_types.DateTimeMessage(milliseconds=12345678,
                                                  time_zone_offset=60)
     value = protobuf.decode_message(HasDateTimeMessage,
                                     protobuf.encode_message(nested)).value
     self.assertEqual(
         datetime.datetime(1970,
                           1,
                           1,
                           3,
                           25,
                           45,
                           678000,
                           tzinfo=util.TimeZoneOffset(60)), value)
Example #20
0
def main(argv):
    options = parse_options(argv[1:])

    registry_url = 'http://%s%s' % (options.server, options.registry_path)

    http_transport = transport.HttpTransport(registry_url, protocol=protojson)
    remote_registry = registry.RegistryService.Stub(http_transport)

    # Get complete list of services.
    services = remote_registry.services()

    # Get file set for all services on server.
    get_file_set = registry.GetFileSetRequest()
    get_file_set.names = [service.name for service in services.services]
    file_set = remote_registry.get_file_set(get_file_set).file_set

    # Save file sets to disk.
    output = open(options.output, 'wb')
    try:
        output.write(protobuf.encode_message(file_set))
    finally:
        output.close()
Example #21
0
def main(argv):
  options = parse_options(argv[1:])

  registry_url =  'http://%s%s' % (options.server,
                                   options.registry_path)

  http_transport = transport.HttpTransport(registry_url, protocol=protojson)
  remote_registry = registry.RegistryService.Stub(http_transport)

  # Get complete list of services.
  services = remote_registry.services()

  # Get file set for all services on server.
  get_file_set = registry.GetFileSetRequest()
  get_file_set.names = [service.name for service in services.services]
  file_set = remote_registry.get_file_set(get_file_set).file_set

  # Save file sets to disk.
  output = open(options.output, 'wb')
  try:
    output.write(protobuf.encode_message(file_set))
  finally:
    output.close()
Example #22
0
def base64_encode_proto(proto_message):
  return base64_encode(protobuf.encode_message(proto_message))