protofuzz icon indicating copy to clipboard operation
protofuzz copied to clipboard

Compiling proto-file and loading module which contains other proto-file imports

Open ghost opened this issue 5 years ago • 0 comments

While creating a dictionary of generators from file by calling protofuzz.from_file(file.proto), the file is compiled using _compile_proto(full_path, dest) in pbimport.py.

Assuming file.proto:

syntax = "proto2";
import "file2.proto";

message Test {
    required Comment c = 1; 
}

and file2.proto:

syntax = "proto2";

message Comment {
    required string s = 1; 
}

Issue: only file.proto is compiled to file_pb2.py while file2.proto is ignored and the module can't be loaded due to a missing import.

(Hacky) Workaround: I compiled file.proto and file2.proto manually,

protoc --python_out=. file.proto
protoc --python_out=. file2.proto

keeping the outputs file_pb2.py and file2_pb2.py in the same directory and then got the module and generator as follows:

    module = pbimport._load_module(os.path.abspath('file_pb2.py'))
    generator = protofuzz._module_to_generators(module)

Basically, the only thing missing is the compilation of all imported proto-files. Another potential issue could be if the import file is situated in a different folder (e.g. import dir/file2.proto), which would require the creation of a similar folder structure when compiling the files if I'm not mistaken, but I didn't look into it too much.

ghost avatar Oct 23 '19 09:10 ghost