smart-buffer.test.js 14.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
var SmartBuffer = require('../lib/smart-buffer.js');
var assert = require('chai').assert;


describe('Constructing a SmartBuffer', function () {
    describe('Constructing with an existing Buffer', function () {
        var buff = new Buffer([0xAA, 0xBB, 0xCC, 0xDD, 0xFF, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99]);
        var reader = new SmartBuffer(buff);

        it('should have the exact same internal Buffer when constructed with a Buffer', function () {
            assert.strictEqual(reader.buff, buff);
        });

        it('should return a buffer with the same content', function () {
            assert.deepEqual(reader.toBuffer(), buff);
        });
    });

    describe('Constructing with an existing Buffer and setting the encoding', function () {
        var buff = new Buffer([0xAA, 0xBB, 0xCC, 0xDD, 0xFF, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99]);
        var reader = new SmartBuffer(buff, 'ascii');

        it('should have the exact same internal Buffer', function () {
            assert.strictEqual(reader.buff, buff);
        });

        it('should have the same encoding that was set', function () {
            assert.strictEqual(reader.encoding, 'ascii');
        });
    });

    describe('Constructing with a specified size', function () {
        var size = 128;
        var reader = new SmartBuffer(size);

        it('should have an internal Buffer with the same length as the size defined in the constructor', function () {
            assert.strictEqual(reader.buff.length, size);
        });
    });

    describe('Constructing with a specified encoding', function () {
        var encoding = 'utf8';

        it('should have an internal encoding with the encoding given to the constructor (1st argument)', function () {
            var reader = new SmartBuffer(encoding);
            assert.strictEqual(reader.encoding, encoding);
        });

        it('should have an internal encoding with the encoding given to the constructor (2nd argument)', function () {
            var reader = new SmartBuffer(1024, encoding);
            assert.strictEqual(reader.encoding, encoding);
        });

    });

    describe('Constructing with invalid parameters', function () {
        it('should throw an exception when given an invalid number size', function () {
            assert.throws(function () {
                var reader = new SmartBuffer(-100);
            }, Error);
        });

        it('should throw an exception when give a invalid encoding', function () {
            assert.throws(function () {
                var reader = new SmartBuffer('invalid');
            }, Error);

            assert.throws(function () {
                var reader = new SmartBuffer(1024, 'invalid');
            }, Error);
        });

        it('should throw and exception when given an object that is not a Buffer', function () {
            assert.throws(function () {
                var reader = new SmartBuffer(null);
            }, TypeError);
        });
    });
});


describe('Reading/Writing To/From SmartBuffer', function () {
    /**
     * Technically, if one of these works, they all should. But they're all here anyways.
     */

    describe('Numeric Values', function () {
        var reader = new SmartBuffer();
        reader.writeInt8(0x44);
        reader.writeUInt8(0xFF);
        reader.writeInt16BE(0x6699);
        reader.writeInt16LE(0x6699);
        reader.writeUInt16BE(0xFFDD);
        reader.writeUInt16LE(0xFFDD);
        reader.writeInt32BE(0x77889900);
        reader.writeInt32LE(0x77889900);
        reader.writeUInt32BE(0xFFDDCCBB);
        reader.writeUInt32LE(0xFFDDCCBB);
        reader.writeFloatBE(1.234);
        reader.writeFloatLE(1.234);
        reader.writeDoubleBE(1.234567890);
        reader.writeDoubleLE(1.234567890);

        it('should equal the correct values that were written above', function () {
            assert.strictEqual(reader.readInt8(), 0x44);
            assert.strictEqual(reader.readUInt8(), 0xFF);
            assert.strictEqual(reader.readInt16BE(), 0x6699);
            assert.strictEqual(reader.readInt16LE(), 0x6699);
            assert.strictEqual(reader.readUInt16BE(), 0xFFDD);
            assert.strictEqual(reader.readUInt16LE(), 0xFFDD);
            assert.strictEqual(reader.readInt32BE(), 0x77889900);
            assert.strictEqual(reader.readInt32LE(), 0x77889900);
            assert.strictEqual(reader.readUInt32BE(), 0xFFDDCCBB);
            assert.strictEqual(reader.readUInt32LE(), 0xFFDDCCBB);
            assert.closeTo(reader.readFloatBE(), 1.234, 0.001);
            assert.closeTo(reader.readFloatLE(), 1.234, 0.001);
            assert.closeTo(reader.readDoubleBE(), 1.234567890, 0.001);
            assert.closeTo(reader.readDoubleLE(), 1.234567890, 0.001);
        });

    });

    describe('Basic String Values', function () {
        var reader = new SmartBuffer();
        reader.writeStringNT('hello');
        reader.writeString('world');
        reader.writeStringNT('✎✏✎✏✎✏');

        it('should equal the correct strings that were written above', function () {
            assert.strictEqual(reader.readStringNT(), 'hello');
            assert.strictEqual(reader.readString(5), 'world');
            assert.strictEqual(reader.readStringNT(), '✎✏✎✏✎✏');
        });
    });

    describe('Mixed Encoding Strings', function () {
        var reader = new SmartBuffer('ascii');
        reader.writeStringNT('some ascii text');
        reader.writeStringNT('ѕσмє υтƒ8 тєχт', 'utf8');

        it('should equal the correct strings that were written above', function () {
            assert.strictEqual(reader.readStringNT(), 'some ascii text');
            assert.strictEqual(reader.readStringNT('utf8'), 'ѕσмє υтƒ8 тєχт');
        });
    });

    describe('Null/non-null terminating strings', function () {
        var reader = new SmartBuffer();
        reader.writeString('hello\0test\0bleh');

        it('should equal hello', function () {
            assert.strictEqual(reader.readStringNT(), 'hello');
        });

        it('should equal: test', function () {
            assert.strictEqual(reader.readString(4), 'test');
        });

        it('should have a length of zero', function () {
            assert.strictEqual(reader.readStringNT().length, 0);
        });

        it('should equal: bleh', function () {
            assert.strictEqual(reader.readStringNT(), 'bleh');
        });


    });

    describe('Reading string without specifying length', function () {
        var str = 'hello123';
        var writer = new SmartBuffer();
        writer.writeString(str);

        var reader = new SmartBuffer(writer.toBuffer());

        assert.strictEqual(reader.readString(), str);
    });

    describe('Write string as specific position', function () {
        var str = 'hello123';
        var writer = new SmartBuffer();
        writer.writeString(str, 10);

        var reader = new SmartBuffer(writer.toBuffer());

        reader.skipTo(10);
        it('Should read the correct string from the original position it was written to.', function () {
            assert.strictEqual(reader.readString(), str);
        });


    });

    describe('Buffer Values', function () {
        describe('Writing buffer to position 0', function () {
            var buff = new SmartBuffer();
            var frontBuff = new Buffer([1, 2, 3, 4, 5, 6]);
            buff.writeStringNT('hello');
            buff.writeBuffer(frontBuff, 0);

            it('should write the buffer to the front of the smart buffer instance', function () {
                var readBuff = buff.readBuffer(frontBuff.length);
                assert.deepEqual(readBuff, frontBuff);
            });
        });

        describe('Writing null terminated buffer to position 0', function () {
            var buff = new SmartBuffer();
            var frontBuff = new Buffer([1, 2, 3, 4, 5, 6]);
            buff.writeStringNT('hello');
            buff.writeBufferNT(frontBuff, 0);

            console.log(buff);

            it('should write the buffer to the front of the smart buffer instance', function () {
                var readBuff = buff.readBufferNT();
                console.log(readBuff);
                assert.deepEqual(readBuff, frontBuff);
            });
        });

        describe('Explicit lengths', function () {
            var buff = new Buffer([0x01, 0x02, 0x04, 0x08, 0x16, 0x32, 0x64]);
            var reader = new SmartBuffer();
            reader.writeBuffer(buff);

            it('should equal the buffer that was written above.', function () {
                assert.deepEqual(reader.readBuffer(7), buff);
            });
        });

        describe('Implicit lengths', function () {
            var buff = new Buffer([0x01, 0x02, 0x04, 0x08, 0x16, 0x32, 0x64]);
            var reader = new SmartBuffer();
            reader.writeBuffer(buff);

            it('should equal the buffer that was written above.', function () {
                assert.deepEqual(reader.readBuffer(), buff);
            });
        });

        describe('Null Terminated Buffer Reading', function () {
            var buff = new SmartBuffer();
            buff.writeBuffer(new Buffer([0x01, 0x02, 0x03, 0x04, 0x00, 0x01, 0x02, 0x03]));

            var read1 = buff.readBufferNT();
            var read2 = buff.readBufferNT();

            it('Should return a length of 4 for the four bytes before the first null in the buffer.', function () {
                assert.equal(read1.length, 4);
            });

            it('Should return a length of 3 for the three bytes after the first null in the buffer after reading to end.', function () {
                assert.equal(read2.length, 3);
            });
        });

        describe('Null Terminated Buffer Writing', function () {
            var buff = new SmartBuffer();
            buff.writeBufferNT(new Buffer([0x01, 0x02, 0x03, 0x04]));

            var read1 = buff.readBufferNT();

            it('Should read the correct null terminated buffer data.', function () {
                assert.equal(read1.length, 4);
            });

        })

    });

    describe('Inserting values into specific positions', function () {
        var reader = new SmartBuffer();

        reader.writeUInt16LE(0x0060);
        reader.writeStringNT('something');
        reader.writeUInt32LE(8485934);
        reader.writeUInt16LE(6699);
        reader.writeStringNT('else');
        reader.writeUInt16LE(reader.length - 2, 2);


        it('should equal the size of the remaining data in the buffer', function () {
            reader.readUInt16LE();
            var size = reader.readUInt16LE();
            assert.strictEqual(reader.remaining(), size);
        });
    });

    describe('Adding more data to the buffer than the internal buffer currently allows.', function () {
        it('Should automatically adjust internal buffer size when needed', function () {
            var writer = new SmartBuffer();
            var largeBuff = new Buffer(10000);

            writer.writeBuffer(largeBuff);

            assert.strictEqual(writer.length, largeBuff.length);
        });
    });

});

describe('Skipping around data', function () {
    var writer = new SmartBuffer();
    writer.writeStringNT('hello');
    writer.writeUInt16LE(6699);
    writer.writeStringNT('world!');

    it('Should equal the UInt16 that was written above', function () {
        var reader = new SmartBuffer(writer.toBuffer());
        reader.skip(6);
        assert.strictEqual(reader.readUInt16LE(), 6699);
        reader.skipTo(0);
        assert.strictEqual(reader.readStringNT(), 'hello');
        reader.rewind(6);
        assert.strictEqual(reader.readStringNT(), 'hello');
    });

    it('Should throw an error when attempting to skip more bytes than actually exist.', function () {
        var reader = new SmartBuffer(writer.toBuffer());

        assert.throws(function () {
            reader.skip(10000);
        });
    });

    it('Should throw an error when attempting to skip to a position that does not exist.', function () {
        var reader = new SmartBuffer(writer.toBuffer());

        assert.throws(function () {
            reader.skipTo(10000);
        });
    });

    it('Should throw an error when attempting to rewind past the start of the buffer.', function () {
        var buff = new SmartBuffer();
        assert.throws(function () {
            buff.rewind(10000);
        });
    });
});

describe('Automatic internal buffer resizing', function () {
    var writer;

    it('Should not throw an error when adding data that is larger than current buffer size (internal resize algo fails)', function () {
        var str = 'String larger than one byte';
        writer = new SmartBuffer(1);
        writer.writeString(str);

        assert.strictEqual(writer.buff.length, str.length);

    });

    it('Should not throw an error when adding data that is larger than current buffer size (internal resize algo succeeds)', function () {
        writer = new SmartBuffer(100);
        var buff = new Buffer(105);

        writer.writeBuffer(buff);

        // Test internal array growth algo.
        assert.strictEqual(writer.buff.length, (100 * 3 / 2 + 1));
    });
});

describe('Clearing the buffer', function () {
    var writer = new SmartBuffer();
    writer.writeString('somedata');

    it('Should contain some data.', function () {
        assert.notStrictEqual(writer.length, 0);
    });

    it('Should contain zero data after being cleared.', function () {
        writer.clear();
        assert.strictEqual(writer.length, 0);
    });
});

describe('Displaying the buffer as a string', function () {
    var buff = new Buffer([1, 2, 3, 4]);
    var sbuff = new SmartBuffer(buff);

    var str = buff.toString();
    var str64 = buff.toString('base64');

    it('Should return a valid string representing the internal buffer', function () {
        assert.strictEqual(str, sbuff.toString());
    });

    it('Should return a valid base64 string representing the internal buffer', function () {
        assert.strictEqual(str64, sbuff.toString('base64'));
    });
});

describe('Destroying the buffer', function () {
    var writer = new SmartBuffer();
    writer.writeString('hello123');

    writer.destroy();

    it('Should have a length of zero when buffer is destroyed', function () {
        assert.strictEqual(0, writer.length);
    });

    it('Should have no internal buff property when buffer is destroyed', function () {
        assert.notProperty(writer, 'buff');
    });
});