Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++] Add padding characters to base64 encoded protobuf native schema #11492

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pulsar-client-cpp/lib/ProtobufNativeSchema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ SchemaInfo createProtobufNativeSchema(const google::protobuf::Descriptor* descri
std::vector<char> bytes(fileDescriptorSet.ByteSizeLong());
fileDescriptorSet.SerializeToArray(bytes.data(), bytes.size());

std::string base64String{base64(bytes.data()), base64(bytes.data() + bytes.size())};
// Pulsar broker only supports decoding Base64 with padding so we need to add padding '=' here
const size_t numPadding = 4 - base64String.size() % 4;
for (size_t i = 0; i < numPadding; i++) {
base64String.push_back('=');
}

const std::string schemaJson =
R"({"fileDescriptorSet":")" + std::string(base64(bytes.data()), base64(bytes.data() + bytes.size())) +
R"({"fileDescriptorSet":")" + base64String +
R"(","rootMessageTypeName":")" + rootMessageTypeName +
R"(","rootFileDescriptorName":")" + rootFileDescriptorName + R"("})";

Expand Down
9 changes: 9 additions & 0 deletions pulsar-client-cpp/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ set(PROTO_SOURCES ${LIB_AUTOGEN_DIR}/Test.pb.cc ${LIB_AUTOGEN_DIR}/ExternalTest.
add_custom_command(
OUTPUT ${PROTO_SOURCES}
COMMAND ${PROTOC_PATH} -I ${PROTO_DIR} ${PROTO_DIR}/Test.proto ${PROTO_DIR}/ExternalTest.proto --cpp_out=${LIB_AUTOGEN_DIR})

set(PROTO_SOURCE_PADDING ${LIB_AUTOGEN_DIR}/PaddingDemo.pb.cc)
add_custom_command(
OUTPUT ${PROTO_SOURCE_PADDING}
COMMAND ${PROTOC_PATH} -I . ./PaddingDemo.proto --cpp_out=${LIB_AUTOGEN_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

set(PROTO_SOURCES ${PROTO_SOURCES} ${PROTO_SOURCE_PADDING})

include_directories(${LIB_AUTOGEN_DIR})

find_library(GMOCK_LIBRARY_PATH gmock)
Expand Down
26 changes: 26 additions & 0 deletions pulsar-client-cpp/tests/PaddingDemo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
syntax = "proto3";

package padding.demo;

message Person {
string name = 1;
optional int32 id = 2;
}
19 changes: 19 additions & 0 deletions pulsar-client-cpp/tests/ProtobufNativeSchemaTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <pulsar/Client.h>
#include <pulsar/ProtobufNativeSchema.h>
#include <stdexcept>
#include "PaddingDemo.pb.h"
#include "Test.pb.h" // generated from "pulsar-client/src/test/proto/Test.proto"

using namespace pulsar;
Expand Down Expand Up @@ -122,3 +123,21 @@ TEST(ProtobufNativeSchemaTest, testEndToEnd) {

client.close();
}

TEST(ProtobufNativeSchemaTest, testBase64WithPadding) {
const auto schemaInfo = createProtobufNativeSchema(::padding::demo::Person::GetDescriptor());
const auto schemaJson = schemaInfo.getSchema();
size_t pos = schemaJson.find(R"(","rootMessageTypeName":)");
ASSERT_NE(pos, std::string::npos);
ASSERT_TRUE(pos > 0);
ASSERT_EQ(schemaJson[pos - 1], '='); // the tail of fileDescriptorSet is a padding character

Client client(lookupUrl);

const std::string topic = "ProtobufSchemaTest-testBase64WithPadding";
Producer producer;
ASSERT_EQ(ResultOk,
client.createProducer(topic, ProducerConfiguration().setSchema(schemaInfo), producer));

client.close();
}