This file is indexed.

/usr/lib/python3/dist-packages/protorpc/generate_python_test.py is in python3-protorpc-standalone 0.9.1-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env python
#
# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Tests for protorpc.generate_python_test."""

__author__ = 'rafek@google.com (Rafe Kaplan)'


import os
import shutil
import sys
import tempfile
import unittest

from protorpc import descriptor
from protorpc import generate_python
from protorpc import test_util
from protorpc import util


class ModuleInterfaceTest(test_util.ModuleInterfaceTest,
                          test_util.TestCase):

  MODULE = generate_python


class FormatPythonFileTest(test_util.TestCase):

  def setUp(self):
    self.original_path = list(sys.path)
    self.original_modules = dict(sys.modules)
    sys.path = list(sys.path)
    self.file_descriptor = descriptor.FileDescriptor()

    # Create temporary directory and add to Python path so that generated
    # Python code can be easily parsed, imported and executed.
    self.temp_dir = tempfile.mkdtemp()
    sys.path.append(self.temp_dir)

  def tearDown(self):
    # Reset path.
    sys.path[:] = []
    sys.path.extend(self.original_path)

    # Reset modules.
    sys.modules.clear()
    sys.modules.update(self.original_modules)

    # Remove temporary directory.
    try:
      shutil.rmtree(self.temp_dir)
    except IOError:
      pass

  def DoPythonTest(self, file_descriptor):
    """Execute python test based on a FileDescriptor object.

    The full test of the Python code generation is to generate a Python source
    code file, import the module and regenerate the FileDescriptor from it.
    If the generated FileDescriptor is the same as the original, it means that
    the generated source code correctly implements the actual FileDescriptor.
    """
    file_name = os.path.join(self.temp_dir,
                             '%s.py' % (file_descriptor.package or 'blank',))
    source_file = open(file_name, 'wt')
    try:
      generate_python.format_python_file(file_descriptor, source_file)
    finally:
      source_file.close()

    module_to_import = file_descriptor.package or 'blank'
    module = __import__(module_to_import)

    if not file_descriptor.package:
      self.assertFalse(hasattr(module, 'package'))
      module.package = ''  # Create package name so that comparison will work.

    reloaded_descriptor = descriptor.describe_file(module)

    # Need to sort both message_types fields because document order is never
    # Ensured.
    # TODO(rafek): Ensure document order.
    if reloaded_descriptor.message_types:
      reloaded_descriptor.message_types = sorted(
        reloaded_descriptor.message_types, key=lambda v: v.name)

    if file_descriptor.message_types:
      file_descriptor.message_types = sorted(
        file_descriptor.message_types, key=lambda v: v.name)

    self.assertEquals(file_descriptor, reloaded_descriptor)

  @util.positional(2)
  def DoMessageTest(self,
                    field_descriptors,
                    message_types=None,
                    enum_types=None):
    """Execute message generation test based on FieldDescriptor objects.

    Args:
      field_descriptor: List of FieldDescriptor object to generate and test.
      message_types: List of other MessageDescriptor objects that the new
        Message class depends on.
      enum_types: List of EnumDescriptor objects that the new Message class
        depends on.
    """
    file_descriptor = descriptor.FileDescriptor()
    file_descriptor.package = 'my_package'

    message_descriptor = descriptor.MessageDescriptor()
    message_descriptor.name = 'MyMessage'

    message_descriptor.fields = list(field_descriptors)

    file_descriptor.message_types = message_types or []
    file_descriptor.message_types.append(message_descriptor)

    if enum_types:
      file_descriptor.enum_types = list(enum_types)

    self.DoPythonTest(file_descriptor)

  def testBlankPackage(self):
    self.DoPythonTest(descriptor.FileDescriptor())

  def testEmptyPackage(self):
    file_descriptor = descriptor.FileDescriptor()
    file_descriptor.package = 'mypackage'
    self.DoPythonTest(file_descriptor)

  def testSingleField(self):
    field = descriptor.FieldDescriptor()
    field.name = 'integer_field'
    field.number = 1
    field.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field.variant = descriptor.FieldDescriptor.Variant.INT64

    self.DoMessageTest([field])

  def testMessageField_InternalReference(self):
    other_message = descriptor.MessageDescriptor()
    other_message.name = 'OtherMessage'

    field = descriptor.FieldDescriptor()
    field.name = 'message_field'
    field.number = 1
    field.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field.variant = descriptor.FieldDescriptor.Variant.MESSAGE
    field.type_name = 'my_package.OtherMessage'

    self.DoMessageTest([field], message_types=[other_message])

  def testMessageField_ExternalReference(self):
    field = descriptor.FieldDescriptor()
    field.name = 'message_field'
    field.number = 1
    field.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field.variant = descriptor.FieldDescriptor.Variant.MESSAGE
    field.type_name = 'protorpc.registry.GetFileSetResponse'

    self.DoMessageTest([field])

  def testEnumField_InternalReference(self):
    enum = descriptor.EnumDescriptor()
    enum.name = 'Color'

    field = descriptor.FieldDescriptor()
    field.name = 'color'
    field.number = 1
    field.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field.variant = descriptor.FieldDescriptor.Variant.ENUM
    field.type_name = 'my_package.Color'

    self.DoMessageTest([field], enum_types=[enum])

  def testEnumField_ExternalReference(self):
    field = descriptor.FieldDescriptor()
    field.name = 'color'
    field.number = 1
    field.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field.variant = descriptor.FieldDescriptor.Variant.ENUM
    field.type_name = 'protorpc.descriptor.FieldDescriptor.Label'

    self.DoMessageTest([field])

  def testDateTimeField(self):
    field = descriptor.FieldDescriptor()
    field.name = 'timestamp'
    field.number = 1
    field.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field.variant = descriptor.FieldDescriptor.Variant.MESSAGE
    field.type_name = 'protorpc.message_types.DateTimeMessage'

    self.DoMessageTest([field])

  def testNonDefaultVariant(self):
    field = descriptor.FieldDescriptor()
    field.name = 'integer_field'
    field.number = 1
    field.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field.variant = descriptor.FieldDescriptor.Variant.UINT64

    self.DoMessageTest([field])

  def testRequiredField(self):
    field = descriptor.FieldDescriptor()
    field.name = 'integer_field'
    field.number = 1
    field.label = descriptor.FieldDescriptor.Label.REQUIRED
    field.variant = descriptor.FieldDescriptor.Variant.INT64

    self.DoMessageTest([field])

  def testRepeatedField(self):
    field = descriptor.FieldDescriptor()
    field.name = 'integer_field'
    field.number = 1
    field.label = descriptor.FieldDescriptor.Label.REPEATED
    field.variant = descriptor.FieldDescriptor.Variant.INT64

    self.DoMessageTest([field])

  def testIntegerDefaultValue(self):
    field = descriptor.FieldDescriptor()
    field.name = 'integer_field'
    field.number = 1
    field.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field.variant = descriptor.FieldDescriptor.Variant.INT64
    field.default_value = '10'

    self.DoMessageTest([field])

  def testFloatDefaultValue(self):
    field = descriptor.FieldDescriptor()
    field.name = 'float_field'
    field.number = 1
    field.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field.variant = descriptor.FieldDescriptor.Variant.DOUBLE
    field.default_value = '10.1'

    self.DoMessageTest([field])

  def testStringDefaultValue(self):
    field = descriptor.FieldDescriptor()
    field.name = 'string_field'
    field.number = 1
    field.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field.variant = descriptor.FieldDescriptor.Variant.STRING
    field.default_value = u'a nice lovely string\'s "string"'

    self.DoMessageTest([field])

  def testEnumDefaultValue(self):
    field = descriptor.FieldDescriptor()
    field.name = 'label'
    field.number = 1
    field.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field.variant = descriptor.FieldDescriptor.Variant.ENUM
    field.type_name = 'protorpc.descriptor.FieldDescriptor.Label'
    field.default_value = '2'

    self.DoMessageTest([field])

  def testMultiFields(self):
    field1 = descriptor.FieldDescriptor()
    field1.name = 'integer_field'
    field1.number = 1
    field1.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field1.variant = descriptor.FieldDescriptor.Variant.INT64

    field2 = descriptor.FieldDescriptor()
    field2.name = 'string_field'
    field2.number = 2
    field2.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field2.variant = descriptor.FieldDescriptor.Variant.STRING

    field3 = descriptor.FieldDescriptor()
    field3.name = 'unsigned_integer_field'
    field3.number = 3
    field3.label = descriptor.FieldDescriptor.Label.OPTIONAL
    field3.variant = descriptor.FieldDescriptor.Variant.UINT64

    self.DoMessageTest([field1, field2, field3])

  def testNestedMessage(self):
    message = descriptor.MessageDescriptor()
    message.name = 'OuterMessage'

    inner_message = descriptor.MessageDescriptor()
    inner_message.name = 'InnerMessage'

    inner_inner_message = descriptor.MessageDescriptor()
    inner_inner_message.name = 'InnerInnerMessage'

    inner_message.message_types = [inner_inner_message]

    message.message_types = [inner_message]

    file_descriptor = descriptor.FileDescriptor()
    file_descriptor.message_types = [message]

    self.DoPythonTest(file_descriptor)

  def testNestedEnum(self):
    message = descriptor.MessageDescriptor()
    message.name = 'OuterMessage'

    inner_enum = descriptor.EnumDescriptor()
    inner_enum.name = 'InnerEnum'

    message.enum_types = [inner_enum]

    file_descriptor = descriptor.FileDescriptor()
    file_descriptor.message_types = [message]

    self.DoPythonTest(file_descriptor)

  def testService(self):
    service = descriptor.ServiceDescriptor()
    service.name = 'TheService'

    method1 = descriptor.MethodDescriptor()
    method1.name = 'method1'
    method1.request_type = 'protorpc.descriptor.FileDescriptor'
    method1.response_type = 'protorpc.descriptor.MethodDescriptor'

    service.methods = [method1]

    file_descriptor = descriptor.FileDescriptor()
    file_descriptor.service_types = [service]

    self.DoPythonTest(file_descriptor)

    # Test to make sure that implementation methods raise an exception.
    import blank
    service_instance = blank.TheService()
    self.assertRaisesWithRegexpMatch(NotImplementedError,
                                     'Method method1 is not implemented',
                                     service_instance.method1,
                                     descriptor.FileDescriptor())
  

def main():
  unittest.main()


if __name__ == '__main__':
  main()