Code coverage report for node/v2/checksum.js

Statements: 91.38% (53 / 58)      Branches: 66.67% (16 / 24)      Functions: 100% (5 / 5)      Lines: 91.07% (51 / 56)      Ignored: none     

All files » node/v2/ » checksum.js
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                                            1 1 1 1   1               1     1 16544 8272   8272 8272 8272     1 1834     1834     1834       1834           1 1 1 1       1 1 1 1   1           1 3667 3667 3667 3667 3667       2 2 6   2   3665 9199   3665       3667     1 1834 1834     1 1835 1835 2   1833 1833 1831   2              
// Copyright (c) 2015 Uber Technologies, Inc.
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
'use strict';
 
var farm32 = require('farmhash').fingerprint32;
var crc32 = require('crc').crc32;
var bufrw = require('bufrw');
var TypedError = require('error/typed');
 
var ChecksumError = TypedError({
    type: 'tchannel.checksum',
    message: 'invalid checksum',
    checksumType: null,
    expectedValue: null,
    actualValue: null
});
 
module.exports = Checksum;
 
// csumtype:1 (csum:4){0,1}
function Checksum(type, val) {
    if (!(this instanceof Checksum)) {
        return new Checksum(type, val);
    }
    var self = this;
    self.type = type;
    self.val = val || 0;
}
 
Checksum.objOrType = function objOrType(arg) {
    Iif (arg instanceof Checksum) {
        return arg;
    }
    Iif (typeof arg !== 'number') {
        throw new Error('expected a Checksum object or a valid checksum type');
    }
    switch (arg) {
        case 0x00:
        case 0x01:
        case 0x02:
            return Checksum(arg);
        default:
            throw new Error('invalid checsum type');
    }
};
 
Checksum.Types = Object.create(null);
Checksum.Types.None = 0x00;
Checksum.Types.CRC32 = 0x01;
Checksum.Types.Farm32 = 0x02;
 
// csumtype:1 (csum:4){0,1}
 
var rwCases = Object.create(null);
rwCases[Checksum.Types.None] = bufrw.Null;
rwCases[Checksum.Types.CRC32] = bufrw.UInt32BE;
rwCases[Checksum.Types.Farm32] = bufrw.UInt32BE;
 
Checksum.RW = bufrw.Switch(bufrw.UInt8, rwCases, {
    cons: Checksum,
    valKey: 'type',
    dataKey: 'val'
});
 
Checksum.prototype.compute = function compute(args, prior) {
    if (typeof prior !== 'number') prior = 0;
    var self = this;
    var csum = prior;
    var i;
    switch (self.type) {
        case 0x00:
            break;
        case 0x01:
            Eif (csum === 0) csum = undefined;
            for (i = 0; i < args.length; i++) {
                csum = crc32(args[i], csum);
            }
            break;
        case 0x02:
            for (i = 0; i < args.length; i++) {
                csum = farm32(args[i], csum);
            }
            break;
        default:
            throw new Error('invalid checksum type ' + self.type);
    }
    return csum;
};
 
Checksum.prototype.update = function update(args, prior) {
    var self = this;
    self.val = self.compute(args, prior);
};
 
Checksum.prototype.verify = function verify(args, prior) {
    var self = this;
    if (self.type === Checksum.Types.None) {
        return null;
    }
    var val = self.compute(args, prior);
    if (val === self.val) {
        return null;
    } else {
        return ChecksumError({
            checksumType: self.type,
            expectedValue: self.val,
            actualValue: val
        });
    }
};