common_test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. 'use strict';
  34. var assert = require('assert');
  35. var common = require('../src/common.js');
  36. var ProtoBuf = require('protobufjs');
  37. var messages_proto = ProtoBuf.loadProtoFile(
  38. __dirname + '/test_messages.proto').build();
  39. describe('Proto message long int serialize and deserialize', function() {
  40. var longSerialize = common.serializeCls(messages_proto.LongValues);
  41. var longDeserialize = common.deserializeCls(messages_proto.LongValues);
  42. var pos_value = '314159265358979';
  43. var neg_value = '-27182818284590';
  44. it('should preserve positive int64 values', function() {
  45. var serialized = longSerialize({int_64: pos_value});
  46. assert.strictEqual(longDeserialize(serialized).int_64.toString(),
  47. pos_value);
  48. });
  49. it('should preserve negative int64 values', function() {
  50. var serialized = longSerialize({int_64: neg_value});
  51. assert.strictEqual(longDeserialize(serialized).int_64.toString(),
  52. neg_value);
  53. });
  54. it('should preserve uint64 values', function() {
  55. var serialized = longSerialize({uint_64: pos_value});
  56. assert.strictEqual(longDeserialize(serialized).uint_64.toString(),
  57. pos_value);
  58. });
  59. it('should preserve positive sint64 values', function() {
  60. var serialized = longSerialize({sint_64: pos_value});
  61. assert.strictEqual(longDeserialize(serialized).sint_64.toString(),
  62. pos_value);
  63. });
  64. it('should preserve negative sint64 values', function() {
  65. var serialized = longSerialize({sint_64: neg_value});
  66. assert.strictEqual(longDeserialize(serialized).sint_64.toString(),
  67. neg_value);
  68. });
  69. it('should preserve fixed64 values', function() {
  70. var serialized = longSerialize({fixed_64: pos_value});
  71. assert.strictEqual(longDeserialize(serialized).fixed_64.toString(),
  72. pos_value);
  73. });
  74. it('should preserve positive sfixed64 values', function() {
  75. var serialized = longSerialize({sfixed_64: pos_value});
  76. assert.strictEqual(longDeserialize(serialized).sfixed_64.toString(),
  77. pos_value);
  78. });
  79. it('should preserve negative sfixed64 values', function() {
  80. var serialized = longSerialize({sfixed_64: neg_value});
  81. assert.strictEqual(longDeserialize(serialized).sfixed_64.toString(),
  82. neg_value);
  83. });
  84. it('should deserialize as a number with the right option set', function() {
  85. var longNumDeserialize = common.deserializeCls(messages_proto.LongValues,
  86. false, false);
  87. var serialized = longSerialize({int_64: pos_value});
  88. assert.strictEqual(typeof longDeserialize(serialized).int_64, 'string');
  89. /* With the longsAsStrings option disabled, long values are represented as
  90. * objects with 3 keys: low, high, and unsigned */
  91. assert.strictEqual(typeof longNumDeserialize(serialized).int_64, 'object');
  92. });
  93. });
  94. describe('Proto message bytes serialize and deserialize', function() {
  95. var sequenceSerialize = common.serializeCls(messages_proto.SequenceValues);
  96. var sequenceDeserialize = common.deserializeCls(
  97. messages_proto.SequenceValues);
  98. var sequenceBase64Deserialize = common.deserializeCls(
  99. messages_proto.SequenceValues, true);
  100. var buffer_val = new Buffer([0x69, 0xb7]);
  101. var base64_val = 'abc=';
  102. it('should preserve a buffer', function() {
  103. var serialized = sequenceSerialize({bytes_field: buffer_val});
  104. var deserialized = sequenceDeserialize(serialized);
  105. assert.strictEqual(deserialized.bytes_field.compare(buffer_val), 0);
  106. });
  107. it('should accept base64 encoded strings', function() {
  108. var serialized = sequenceSerialize({bytes_field: base64_val});
  109. var deserialized = sequenceDeserialize(serialized);
  110. assert.strictEqual(deserialized.bytes_field.compare(buffer_val), 0);
  111. });
  112. it('should output base64 encoded strings with an option set', function() {
  113. var serialized = sequenceSerialize({bytes_field: base64_val});
  114. var deserialized = sequenceBase64Deserialize(serialized);
  115. assert.strictEqual(deserialized.bytes_field, base64_val);
  116. });
  117. /* The next two tests are specific tests to verify that issue
  118. * https://github.com/grpc/grpc/issues/5174 has been fixed. They are skipped
  119. * because they will not pass until a protobuf.js release has been published
  120. * with a fix for https://github.com/dcodeIO/protobuf.js/issues/390 */
  121. it.skip('should serialize a repeated field as packed by default', function() {
  122. var expected_serialize = new Buffer([0x12, 0x01, 0x01, 0x0a]);
  123. var serialized = sequenceSerialize({repeated_field: [10]});
  124. assert.strictEqual(expected_serialize.compare(serialized), 0);
  125. });
  126. it.skip('should deserialize packed or unpacked repeated', function() {
  127. var serialized = new Buffer([0x12, 0x01, 0x01, 0x0a]);
  128. assert.doesNotThrow(function() {
  129. sequenceDeserialize(serialized);
  130. });
  131. });
  132. });