Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame^] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // https://developers.google.com/protocol-buffers/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | /** |
| 32 | * @fileoverview Test cases for jspb's binary protocol buffer writer. In |
| 33 | * practice BinaryWriter is used to drive the Decoder and Reader test cases, |
| 34 | * so only writer-specific tests are here. |
| 35 | * |
| 36 | * Test suite is written using Jasmine -- see http://jasmine.github.io/ |
| 37 | * |
| 38 | * @author aappleby@google.com (Austin Appleby) |
| 39 | */ |
| 40 | |
| 41 | goog.require('goog.crypt'); |
| 42 | goog.require('goog.testing.asserts'); |
| 43 | goog.require('jspb.BinaryWriter'); |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * @param {function()} func This function should throw an error when run. |
| 48 | */ |
| 49 | function assertFails(func) { |
| 50 | var e = assertThrows(func); |
| 51 | console.log(e); |
| 52 | //assertNotNull(e.toString().match(/Error/)); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | describe('binaryWriterTest', function() { |
| 57 | /** |
| 58 | * Verifies that misuse of the writer class triggers assertions. |
| 59 | */ |
| 60 | it('testWriteErrors', function() { |
| 61 | // Submessages with invalid field indices should assert. |
| 62 | var writer = new jspb.BinaryWriter(); |
| 63 | var dummyMessage = /** @type {!jspb.BinaryMessage} */({}); |
| 64 | |
| 65 | assertFails(function() { |
| 66 | writer.writeMessage(-1, dummyMessage, goog.nullFunction); |
| 67 | }); |
| 68 | |
| 69 | // Writing invalid field indices should assert. |
| 70 | writer = new jspb.BinaryWriter(); |
| 71 | assertFails(function() {writer.writeUint64(-1, 1);}); |
| 72 | |
| 73 | // Writing out-of-range field values should assert. |
| 74 | writer = new jspb.BinaryWriter(); |
| 75 | |
| 76 | assertFails(function() {writer.writeInt32(1, -Infinity);}); |
| 77 | assertFails(function() {writer.writeInt32(1, Infinity);}); |
| 78 | |
| 79 | assertFails(function() {writer.writeInt64(1, -Infinity);}); |
| 80 | assertFails(function() {writer.writeInt64(1, Infinity);}); |
| 81 | |
| 82 | assertFails(function() {writer.writeUint32(1, -1);}); |
| 83 | assertFails(function() {writer.writeUint32(1, Infinity);}); |
| 84 | |
| 85 | assertFails(function() {writer.writeUint64(1, -1);}); |
| 86 | assertFails(function() {writer.writeUint64(1, Infinity);}); |
| 87 | |
| 88 | assertFails(function() {writer.writeSint32(1, -Infinity);}); |
| 89 | assertFails(function() {writer.writeSint32(1, Infinity);}); |
| 90 | |
| 91 | assertFails(function() {writer.writeSint64(1, -Infinity);}); |
| 92 | assertFails(function() {writer.writeSint64(1, Infinity);}); |
| 93 | |
| 94 | assertFails(function() {writer.writeFixed32(1, -1);}); |
| 95 | assertFails(function() {writer.writeFixed32(1, Infinity);}); |
| 96 | |
| 97 | assertFails(function() {writer.writeFixed64(1, -1);}); |
| 98 | assertFails(function() {writer.writeFixed64(1, Infinity);}); |
| 99 | |
| 100 | assertFails(function() {writer.writeSfixed32(1, -Infinity);}); |
| 101 | assertFails(function() {writer.writeSfixed32(1, Infinity);}); |
| 102 | |
| 103 | assertFails(function() {writer.writeSfixed64(1, -Infinity);}); |
| 104 | assertFails(function() {writer.writeSfixed64(1, Infinity);}); |
| 105 | }); |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Basic test of retrieving the result as a Uint8Array buffer |
| 110 | */ |
| 111 | it('testGetResultBuffer', function() { |
| 112 | var expected = '0864120b48656c6c6f20776f726c641a0301020320c801'; |
| 113 | |
| 114 | var writer = new jspb.BinaryWriter(); |
| 115 | writer.writeUint32(1, 100); |
| 116 | writer.writeString(2, 'Hello world'); |
| 117 | writer.writeBytes(3, new Uint8Array([1, 2, 3])); |
| 118 | writer.writeUint32(4, 200); |
| 119 | |
| 120 | var buffer = writer.getResultBuffer(); |
| 121 | assertEquals(expected, goog.crypt.byteArrayToHex(buffer)); |
| 122 | }); |
| 123 | }); |