Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
267 lines
10 KiB
267 lines
10 KiB
5 years ago
|
/**
|
||
5 years ago
|
* @fileoverview Tests to make sure Kernel can read data in a backward
|
||
5 years ago
|
* compatible way even when protobuf schema changes according to the rules
|
||
|
* defined in
|
||
|
* https://developers.google.com/protocol-buffers/docs/proto#updating and
|
||
|
* https://developers.google.com/protocol-buffers/docs/proto3#updating.
|
||
|
*
|
||
|
* third_party/protobuf/conformance/binary_json_conformance_suite.cc already
|
||
|
* covers many compatibility tests, this file covers only the tests not covered
|
||
|
* by binary_json_conformance_suite. Ultimately all of the tests in this file
|
||
|
* should be moved to binary_json_conformance_suite.
|
||
|
*/
|
||
5 years ago
|
goog.module('protobuf.runtime.KernelCompatibilityTest');
|
||
5 years ago
|
|
||
|
goog.setTestOnly();
|
||
|
|
||
|
const ByteString = goog.require('protobuf.ByteString');
|
||
|
const Int64 = goog.require('protobuf.Int64');
|
||
5 years ago
|
const Kernel = goog.require('protobuf.runtime.Kernel');
|
||
5 years ago
|
const TestMessage = goog.require('protobuf.testing.binary.TestMessage');
|
||
|
const {CHECK_CRITICAL_STATE} = goog.require('protobuf.internal.checks');
|
||
|
|
||
|
/**
|
||
|
* @param {...number} bytes
|
||
|
* @return {!ArrayBuffer}
|
||
|
*/
|
||
|
function createArrayBuffer(...bytes) {
|
||
|
return new Uint8Array(bytes).buffer;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the Unicode character codes of a string.
|
||
|
* @param {string} str
|
||
|
* @return {!Array<number>}
|
||
|
*/
|
||
|
function getCharacterCodes(str) {
|
||
|
return Array.from(str, (c) => c.charCodeAt(0));
|
||
|
}
|
||
|
|
||
|
describe('optional -> repeated compatibility', () => {
|
||
|
it('is maintained for scalars', () => {
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.setInt32(1, 1);
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData).toEqual(createArrayBuffer(0x8, 0x1));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
expect(newAccessor.getRepeatedInt32Size(1)).toEqual(1);
|
||
|
expect(newAccessor.getRepeatedInt32Element(1, 0)).toEqual(1);
|
||
|
});
|
||
|
|
||
|
it('is maintained for messages', () => {
|
||
5 years ago
|
const message = new TestMessage(Kernel.createEmpty());
|
||
5 years ago
|
message.setInt32(1, 1);
|
||
|
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.setMessage(1, message);
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData).toEqual(createArrayBuffer(0xA, 0x2, 0x8, 0x1));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
expect(newAccessor.getRepeatedMessageSize(1, TestMessage.instanceCreator))
|
||
|
.toEqual(1);
|
||
|
expect(
|
||
|
newAccessor.getRepeatedMessageElement(1, TestMessage.instanceCreator, 0)
|
||
|
.serialize())
|
||
|
.toEqual(message.serialize());
|
||
|
});
|
||
|
|
||
|
it('is maintained for bytes', () => {
|
||
5 years ago
|
const message = new TestMessage(Kernel.createEmpty());
|
||
5 years ago
|
message.setInt32(1, 1);
|
||
|
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.setBytes(
|
||
|
1, ByteString.fromArrayBuffer(createArrayBuffer(0xA, 0xB)));
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData).toEqual(createArrayBuffer(0xA, 0x2, 0xA, 0xB));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
expect(newAccessor.getRepeatedBytesSize(1)).toEqual(1);
|
||
|
expect(newAccessor.getRepeatedBoolElement(1, 0))
|
||
|
.toEqual(ByteString.fromArrayBuffer(createArrayBuffer(0xA, 0xB)));
|
||
|
});
|
||
|
|
||
|
it('is maintained for strings', () => {
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.setString(1, 'hello');
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData)
|
||
|
.toEqual(createArrayBuffer(0xA, 0x5, 0x68, 0x65, 0x6C, 0x6C, 0x6F));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
expect(newAccessor.getRepeatedStringSize(1)).toEqual(1);
|
||
|
expect(newAccessor.getRepeatedStringElement(1, 0)).toEqual('hello');
|
||
|
});
|
||
|
});
|
||
|
|
||
5 years ago
|
describe('Kernel repeated -> optional compatibility', () => {
|
||
5 years ago
|
it('is maintained for unpacked scalars', () => {
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.addUnpackedInt32Element(1, 0);
|
||
|
oldAccessor.addUnpackedInt32Element(1, 1);
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData).toEqual(createArrayBuffer(0x8, 0x0, 0x8, 0x1));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
expect(newAccessor.getInt32WithDefault(1)).toEqual(1);
|
||
|
expect(newAccessor.serialize()).toEqual(serializedData);
|
||
|
});
|
||
|
|
||
|
// repeated -> optional transformation is not supported for packed fields yet:
|
||
|
// go/proto-schema-repeated
|
||
|
it('is not maintained for packed scalars', () => {
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.addPackedInt32Element(1, 0);
|
||
|
oldAccessor.addPackedInt32Element(1, 1);
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData).toEqual(createArrayBuffer(0xA, 0x2, 0x0, 0x1));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
if (CHECK_CRITICAL_STATE) {
|
||
|
expect(() => newAccessor.getInt32WithDefault(1)).toThrow();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
it('is maintained for messages', () => {
|
||
5 years ago
|
const message1 = new TestMessage(Kernel.createEmpty());
|
||
5 years ago
|
message1.setInt32(1, 1);
|
||
5 years ago
|
const message2 = new TestMessage(Kernel.createEmpty());
|
||
5 years ago
|
message2.setInt32(1, 2);
|
||
|
message2.setInt32(2, 3);
|
||
|
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.addRepeatedMessageElement(
|
||
|
1, message1, TestMessage.instanceCreator);
|
||
|
oldAccessor.addRepeatedMessageElement(
|
||
|
1, message2, TestMessage.instanceCreator);
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData)
|
||
|
.toEqual(createArrayBuffer(
|
||
|
0xA, 0x2, 0x8, 0x1, 0xA, 0x4, 0x8, 0x2, 0x10, 0x3));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
// Values from message1 and message2 have been merged
|
||
|
const newMessage = newAccessor.getMessage(1, TestMessage.instanceCreator);
|
||
|
expect(newMessage.getRepeatedInt32Size(1)).toEqual(2);
|
||
|
expect(newMessage.getRepeatedInt32Element(1, 0)).toEqual(1);
|
||
|
expect(newMessage.getRepeatedInt32Element(1, 1)).toEqual(2);
|
||
|
expect(newMessage.getInt32WithDefault(2)).toEqual(3);
|
||
|
expect(newMessage.serialize())
|
||
|
.toEqual(createArrayBuffer(0x8, 0x1, 0x8, 0x2, 0x10, 0x3));
|
||
|
});
|
||
|
|
||
|
it('is maintained for bytes', () => {
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.addRepeatedBytesElement(
|
||
|
1, ByteString.fromArrayBuffer(createArrayBuffer(0xA, 0xB)));
|
||
|
oldAccessor.addRepeatedBytesElement(
|
||
|
1, ByteString.fromArrayBuffer(createArrayBuffer(0xC, 0xD)));
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData)
|
||
|
.toEqual(createArrayBuffer(0xA, 0x2, 0xA, 0xB, 0xA, 0x2, 0xC, 0xD));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
expect(newAccessor.getBytesWithDefault(1))
|
||
|
.toEqual(ByteString.fromArrayBuffer(createArrayBuffer(0xC, 0xD)));
|
||
|
expect(newAccessor.serialize()).toEqual(serializedData);
|
||
|
});
|
||
|
|
||
|
it('is maintained for strings', () => {
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.addRepeatedStringElement(1, 'hello');
|
||
|
oldAccessor.addRepeatedStringElement(1, 'world');
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData)
|
||
|
.toEqual(createArrayBuffer(
|
||
|
0xA, 0x5, ...getCharacterCodes('hello'), 0xA, 0x5,
|
||
|
...getCharacterCodes('world')));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
expect(newAccessor.getStringWithDefault(1)).toEqual('world');
|
||
|
expect(newAccessor.serialize()).toEqual(serializedData);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
describe('Type change', () => {
|
||
|
it('is supported for fixed32 -> sfixed32', () => {
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.setFixed32(1, 4294967295);
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData)
|
||
|
.toEqual(createArrayBuffer(0xD, 0xFF, 0xFF, 0xFF, 0xFF));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
expect(newAccessor.getSfixed32WithDefault(1)).toEqual(-1);
|
||
|
expect(newAccessor.serialize()).toEqual(serializedData);
|
||
|
});
|
||
|
|
||
|
it('is supported for sfixed32 -> fixed32', () => {
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.setSfixed32(1, -1);
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData)
|
||
|
.toEqual(createArrayBuffer(0xD, 0xFF, 0xFF, 0xFF, 0xFF));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
expect(newAccessor.getFixed32WithDefault(1)).toEqual(4294967295);
|
||
|
expect(newAccessor.serialize()).toEqual(serializedData);
|
||
|
});
|
||
|
|
||
|
it('is supported for fixed64 -> sfixed64', () => {
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.setFixed64(1, Int64.fromHexString('0xFFFFFFFFFFFFFFFF'));
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData)
|
||
|
.toEqual(createArrayBuffer(
|
||
|
0x9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
expect(newAccessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(-1));
|
||
|
expect(newAccessor.serialize()).toEqual(serializedData);
|
||
|
});
|
||
|
|
||
|
it('is supported for sfixed64 -> fixed64', () => {
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.setSfixed64(1, Int64.fromInt(-1));
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData)
|
||
|
.toEqual(createArrayBuffer(
|
||
|
0x9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
expect(newAccessor.getFixed64WithDefault(1))
|
||
|
.toEqual(Int64.fromHexString('0xFFFFFFFFFFFFFFFF'));
|
||
|
expect(newAccessor.serialize()).toEqual(serializedData);
|
||
|
});
|
||
|
|
||
|
it('is supported for bytes -> message', () => {
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
5 years ago
|
oldAccessor.setBytes(
|
||
|
1, ByteString.fromArrayBuffer(createArrayBuffer(0x8, 0x1)));
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData).toEqual(createArrayBuffer(0xA, 0x2, 0x8, 0x1));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
const message = newAccessor.getMessage(1, TestMessage.instanceCreator);
|
||
|
expect(message.getInt32WithDefault(1)).toEqual(1);
|
||
|
expect(message.serialize()).toEqual(createArrayBuffer(0x8, 0x1));
|
||
|
expect(newAccessor.serialize()).toEqual(serializedData);
|
||
|
});
|
||
|
|
||
|
it('is supported for message -> bytes', () => {
|
||
5 years ago
|
const oldAccessor = Kernel.createEmpty();
|
||
|
const message = new TestMessage(Kernel.createEmpty());
|
||
5 years ago
|
message.setInt32(1, 1);
|
||
|
oldAccessor.setMessage(1, message);
|
||
|
const serializedData = oldAccessor.serialize();
|
||
|
expect(serializedData).toEqual(createArrayBuffer(0xA, 0x2, 0x8, 0x1));
|
||
|
|
||
5 years ago
|
const newAccessor = Kernel.fromArrayBuffer(serializedData);
|
||
5 years ago
|
expect(newAccessor.getBytesWithDefault(1))
|
||
|
.toEqual(ByteString.fromArrayBuffer(createArrayBuffer(0x8, 0x1)));
|
||
|
expect(newAccessor.serialize()).toEqual(serializedData);
|
||
|
});
|
||
|
});
|