def testParseFileToProtobufWithGoodAnalysisFile(self): expected_descriptor = wheelbarrow_pb2.AnalysisDescriptor() expected_descriptor.name = 'check_permissions' expected_descriptor.description = ( 'Check which permissions are changed by ' 'the package.') expected_descriptor.module = ('analyzers.permission_checker' '.PermissionChecker') expected_descriptor.category = 'file_system' arg = expected_descriptor.arguments.add() arg.string_args.append('/bin') arg = expected_descriptor.arguments.add() arg.string_args.append('/sbin') diff_pair = expected_descriptor.diff_pairs.add() diff_pair.before = wheelbarrow_pb2.EXTRACT diff_pair.after = wheelbarrow_pb2.INSTALL diff_pair = expected_descriptor.diff_pairs.add() diff_pair.before = wheelbarrow_pb2.EXTRACT diff_pair.after = wheelbarrow_pb2.REMOVE descriptor = wheelbarrow_pb2.AnalysisDescriptor() result = utils.ParseFileToProtobuf(self.good_analysis_file, descriptor, -1, True) self.assertTrue(result) str1 = text_format.MessageToString(expected_descriptor, as_one_line=True) str2 = text_format.MessageToString(descriptor, as_one_line=True) self.assertEqual(str1, str2)
def testParseFileToProtobufWithFileLoadingFailure(self): protobuf = wheelbarrow_pb2.AnalysisDescriptor self.mox.StubOutWithMock(utils, 'LoadFileToString') utils.LoadFileToString(self.good_analysis_file, -1).AndReturn(None) logging.error( 'Unable to read file %s while trying to parse it as protobuf ' 'message %s.', self.good_analysis_file, type(protobuf)) self.mox.ReplayAll() result = utils.ParseFileToProtobuf(self.good_analysis_file, protobuf) self.assertFalse(result) self.mox.VerifyAll()
def testParseFileToProtobufWithBadFieldName(self): bad_analysis_file = os.path.join(self.base_dir, 'analysis_bad') logging.error('Error while parsing file %s to protobuf message %s: %s', bad_analysis_file, type(wheelbarrow_pb2.AnalysisDescriptor()), mox.IgnoreArg()) self.mox.ReplayAll() descriptor = wheelbarrow_pb2.AnalysisDescriptor() result = utils.ParseFileToProtobuf(bad_analysis_file, descriptor, -1, True) self.assertFalse(result) self.mox.VerifyAll()
def testParseFileToProtobufWithBinaryFile(self): binary_file_name = os.path.join(self.base_dir, 'binary_package_descriptor.dat') expected_descriptor = wheelbarrow_pb2.Package() expected_descriptor.name = 'emacspeak-ss' expected_descriptor.version = '1.12.1-1' expected_descriptor.architecture = 'i386' expected_descriptor.status = wheelbarrow_pb2.Package.PROCESSING expected_descriptor.analysis_attempts = 1 descriptor = wheelbarrow_pb2.Package() result = utils.ParseFileToProtobuf(binary_file_name, descriptor) self.assertTrue(result) str1 = text_format.MessageToString(expected_descriptor, as_one_line=True) str2 = text_format.MessageToString(descriptor, as_one_line=True) self.assertEqual(str1, str2)
def _LoadBatchDescriptorFromFile(batch_descriptor_path): """Load a batch package descriptor from a file. Args: batch_descriptor_path: The path to a file containing a batch package protobuf in text format. Returns: A batch package descriptor is all goes well, None otherwise. """ batch_package_descriptor = wheelbarrow_pb2.BatchPackageDescriptor() if utils.ParseFileToProtobuf(batch_descriptor_path, batch_package_descriptor, -1, True): return batch_package_descriptor else: return None