def test_binary_compatibility(repeat, test_data: TestData) -> None: plugin_module, reference_module, json_data = test_data for sample in json_data: reference_instance = Parse(sample.json, reference_module().Test()) reference_binary_output = reference_instance.SerializeToString() for _ in range(repeat): plugin_instance_from_json: betterproto.Message = ( plugin_module.Test().from_json(sample.json) ) plugin_instance_from_binary = plugin_module.Test.FromString( reference_binary_output ) # Generally this can't be relied on, but here we are aiming to match the # existing Python implementation and aren't doing anything tricky. # https://developers.google.com/protocol-buffers/docs/encoding#implications assert bytes(plugin_instance_from_json) == reference_binary_output assert bytes(plugin_instance_from_binary) == reference_binary_output assert plugin_instance_from_json == plugin_instance_from_binary assert dict_replace_nans( plugin_instance_from_json.to_dict() ) == dict_replace_nans(plugin_instance_from_binary.to_dict())
def create_policy(self, policy_json): policy = None # if base policy path specified, use that, otherwise default to empty if args.base_policy_path: with open(args.base_policy_path, 'rb') as f: base_policy_str = f.read() policy = Parse(base_policy_str, policy_pb2.policy()) else: policy = policy_pb2.policy() self.add_exe_name_policy(policy, 'fake') # redact_task_entry = policy.tasks.add() #redact_task_entry.match.magic = "banana" #raw_action = redact_task_entry.raw_actions.add() #raw_action.offset = 8 #raw_action.replace_bytes = "apple" if (policy_json == None): print("warning: no policy specified, using default (empty)") elif (policy_json['trigger_type'] == 'snort'): print("adding destination IP address policy, blocks: " + policy_json['dst_ip']) self.add_tcp_policy(policy, policy_json['dst_ip']) elif (policy_json['trigger_type'] == 'clamav'): print("adding exe_name policy, blocks: " + policy_json['path']) self.add_exe_name_policy(policy, policy_json['path']) else: raise Exception("invalid trigger type") self.policy_path = self.cur_workspace + "policy.img" print("writing to " + self.policy_path) with open(self.policy_path, 'wb') as f: f.write(policy.SerializeToString())
def produce(send_times: int, topic: str): for _ in tqdm.tqdm(range(send_times)): person_in_json_format = { "name": __Faker.name(), "birth_day": __Faker.date_of_birth(minimum_age=18).strftime("%Y-%m-%d"), "address": __Faker.address(), "job": __Faker.job() } person = Parse(json.dumps(person_in_json_format), Person()) try: __PRODUCER.send(topic, person.SerializeToString()) except Exception as e: print("failed to produce message into '{}', err: {}".format( topic, e)) sys.exit(-1)
def json_to_pb_message(module, str_response): ''' 获取模块描述信息 ''' try: pb_gen_file, pb_gen_path = manage_proto_repo(module) except Exception as ex: return False, '拉取代码仓库错误, %s' % ex sys.path.append(pb_gen_path) MODULE_NAME = pb_gen_file.replace('.py', '').replace('/', '.') MODULE_RESPONSE = module['pb_resp_name'] name = importlib.import_module(MODULE_NAME) target = getattr(name, MODULE_RESPONSE)() parsed_pb = Parse(str_response, target, ignore_unknown_fields=False) protobuf_str = parsed_pb.SerializeToString() return protobuf_str
) subprocess.run( f"protoc --plugin=protoc-gen-custom=../plugin.py --custom_out=. {os.path.basename(filename)}", shell=True, ) for filename in json_files: # Reset the internal symbol database so we can import the `Test` message # multiple times. Ugh. sym = symbol_database.Default() sym.pool = DescriptorPool() parts = get_base(filename).split("-") out = filename.replace(".json", ".bin") print(f"Using {parts[0]}_pb2 to generate {os.path.basename(out)}") imported = importlib.import_module(f"{parts[0]}_pb2") input_json = open(filename).read() parsed = Parse(input_json, imported.Test()) serialized = parsed.SerializeToString() preserve = "casing" not in filename serialized_json = MessageToJson(parsed, preserving_proto_field_name=preserve) s_loaded = json.loads(serialized_json) in_loaded = json.loads(input_json) if s_loaded != in_loaded: raise AssertionError("Expected JSON to be equal:", s_loaded, in_loaded) open(out, "wb").write(serialized)
import person_pb2 from google.protobuf.json_format import MessageToJson, Parse person = person_pb2.Person() person.name = "name" person.id = 1 person.email = "*****@*****.**" json_string = MessageToJson(person) print(json_string) person_parsed = Parse(json_string, person_pb2.Person()) print(person_parsed.SerializeToString())