OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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 |
| 34 'use strict'; |
| 35 |
| 36 var assert = require('assert'); |
| 37 |
| 38 var common = require('../src/common.js'); |
| 39 |
| 40 var ProtoBuf = require('protobufjs'); |
| 41 |
| 42 var messages_proto = ProtoBuf.loadProtoFile( |
| 43 __dirname + '/test_messages.proto').build(); |
| 44 |
| 45 describe('Proto message long int serialize and deserialize', function() { |
| 46 var longSerialize = common.serializeCls(messages_proto.LongValues); |
| 47 var longDeserialize = common.deserializeCls(messages_proto.LongValues); |
| 48 var pos_value = '314159265358979'; |
| 49 var neg_value = '-27182818284590'; |
| 50 it('should preserve positive int64 values', function() { |
| 51 var serialized = longSerialize({int_64: pos_value}); |
| 52 assert.strictEqual(longDeserialize(serialized).int_64.toString(), |
| 53 pos_value); |
| 54 }); |
| 55 it('should preserve negative int64 values', function() { |
| 56 var serialized = longSerialize({int_64: neg_value}); |
| 57 assert.strictEqual(longDeserialize(serialized).int_64.toString(), |
| 58 neg_value); |
| 59 }); |
| 60 it('should preserve uint64 values', function() { |
| 61 var serialized = longSerialize({uint_64: pos_value}); |
| 62 assert.strictEqual(longDeserialize(serialized).uint_64.toString(), |
| 63 pos_value); |
| 64 }); |
| 65 it('should preserve positive sint64 values', function() { |
| 66 var serialized = longSerialize({sint_64: pos_value}); |
| 67 assert.strictEqual(longDeserialize(serialized).sint_64.toString(), |
| 68 pos_value); |
| 69 }); |
| 70 it('should preserve negative sint64 values', function() { |
| 71 var serialized = longSerialize({sint_64: neg_value}); |
| 72 assert.strictEqual(longDeserialize(serialized).sint_64.toString(), |
| 73 neg_value); |
| 74 }); |
| 75 it('should preserve fixed64 values', function() { |
| 76 var serialized = longSerialize({fixed_64: pos_value}); |
| 77 assert.strictEqual(longDeserialize(serialized).fixed_64.toString(), |
| 78 pos_value); |
| 79 }); |
| 80 it('should preserve positive sfixed64 values', function() { |
| 81 var serialized = longSerialize({sfixed_64: pos_value}); |
| 82 assert.strictEqual(longDeserialize(serialized).sfixed_64.toString(), |
| 83 pos_value); |
| 84 }); |
| 85 it('should preserve negative sfixed64 values', function() { |
| 86 var serialized = longSerialize({sfixed_64: neg_value}); |
| 87 assert.strictEqual(longDeserialize(serialized).sfixed_64.toString(), |
| 88 neg_value); |
| 89 }); |
| 90 it('should deserialize as a number with the right option set', function() { |
| 91 var longNumDeserialize = common.deserializeCls(messages_proto.LongValues, |
| 92 false, false); |
| 93 var serialized = longSerialize({int_64: pos_value}); |
| 94 assert.strictEqual(typeof longDeserialize(serialized).int_64, 'string'); |
| 95 /* With the longsAsStrings option disabled, long values are represented as |
| 96 * objects with 3 keys: low, high, and unsigned */ |
| 97 assert.strictEqual(typeof longNumDeserialize(serialized).int_64, 'object'); |
| 98 }); |
| 99 }); |
| 100 describe('Proto message bytes serialize and deserialize', function() { |
| 101 var sequenceSerialize = common.serializeCls(messages_proto.SequenceValues); |
| 102 var sequenceDeserialize = common.deserializeCls( |
| 103 messages_proto.SequenceValues); |
| 104 var sequenceBase64Deserialize = common.deserializeCls( |
| 105 messages_proto.SequenceValues, true); |
| 106 var buffer_val = new Buffer([0x69, 0xb7]); |
| 107 var base64_val = 'abc='; |
| 108 it('should preserve a buffer', function() { |
| 109 var serialized = sequenceSerialize({bytes_field: buffer_val}); |
| 110 var deserialized = sequenceDeserialize(serialized); |
| 111 assert.strictEqual(deserialized.bytes_field.compare(buffer_val), 0); |
| 112 }); |
| 113 it('should accept base64 encoded strings', function() { |
| 114 var serialized = sequenceSerialize({bytes_field: base64_val}); |
| 115 var deserialized = sequenceDeserialize(serialized); |
| 116 assert.strictEqual(deserialized.bytes_field.compare(buffer_val), 0); |
| 117 }); |
| 118 it('should output base64 encoded strings with an option set', function() { |
| 119 var serialized = sequenceSerialize({bytes_field: base64_val}); |
| 120 var deserialized = sequenceBase64Deserialize(serialized); |
| 121 assert.strictEqual(deserialized.bytes_field, base64_val); |
| 122 }); |
| 123 /* The next two tests are specific tests to verify that issue |
| 124 * https://github.com/grpc/grpc/issues/5174 has been fixed. They are skipped |
| 125 * because they will not pass until a protobuf.js release has been published |
| 126 * with a fix for https://github.com/dcodeIO/protobuf.js/issues/390 */ |
| 127 it.skip('should serialize a repeated field as packed by default', function() { |
| 128 var expected_serialize = new Buffer([0x12, 0x01, 0x01, 0x0a]); |
| 129 var serialized = sequenceSerialize({repeated_field: [10]}); |
| 130 assert.strictEqual(expected_serialize.compare(serialized), 0); |
| 131 }); |
| 132 it.skip('should deserialize packed or unpacked repeated', function() { |
| 133 var serialized = new Buffer([0x12, 0x01, 0x01, 0x0a]); |
| 134 assert.doesNotThrow(function() { |
| 135 sequenceDeserialize(serialized); |
| 136 }); |
| 137 }); |
| 138 }); |
OLD | NEW |