Code coverage report for node/v2/checksum.js

Statements: 93.33% (70 / 75)      Branches: 85.71% (24 / 28)      Functions: 90% (9 / 10)      Lines: 93.15% (68 / 73)      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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152                                            1 1 1 1 1 1   1     1 8387 8387 8387 8387   1873 1873   15 15   11 11   6488 6488           1 3721 283   3438 1865   1573   1573         1573           1 1 1 1 1       1 1 1 1 1   1           1 1849 1849 1849     1849 1849 4897   1849       1 9     1 18 18     1 9773     1 18     1 4921 4921     1         1 1851 1851 2   1849 1849 1847   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 assert = require('assert');
var farm32 = require('farmhash').fingerprint32;
var crc32 = require('crc').crc32;
var crc32c = require('sse4_crc32').calculate;
var bufrw = require('bufrw');
var errors = require('../errors');
 
module.exports = Checksum;
 
// csumtype:1 (csum:4){0,1}
function Checksum(type, val) {
    var self = this;
    self.type = type;
    self.val = val || 0;
    switch (self.type) {
        case 0x00:
            self._compute = self._computeNone;
            break;
        case 0x01:
            self._compute = self._computeCrc32;
            break;
        case 0x02:
            self._compute = self._computeFarm32;
            break;
        case 0x03:
            self._compute = self._computeCrc32C;
            break;
        default:
            assert(false, 'invalid checksum type ' + self.type);
    }
}
 
Checksum.objOrType = function objOrType(arg) {
    if (arg instanceof Checksum) {
        return arg;
    }
    if (arg === undefined || arg === null) {
        return new Checksum(Checksum.Types.None);
    }
    assert(typeof arg === 'number',
           'expected a Checksum object or a valid checksum type');
    switch (arg) {
        case 0x00:
        case 0x01:
        case 0x02:
        case 0x03:
            return new Checksum(arg);
        default:
            assert(false, 'expected a Checksum object or a valid checksum type');
    }
};
 
Checksum.Types = Object.create(null);
Checksum.Types.None = 0x00;
Checksum.Types.CRC32 = 0x01;
Checksum.Types.Farm32 = 0x02;
Checksum.Types.CRC32C = 0x03;
 
// 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;
rwCases[Checksum.Types.CRC32C] = 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;
    Iif (self.type === Checksum.Types.None) {
        return 0;
    } else {
        var csum = prior;
        for (var i = 0; i < args.length; i++) {
            csum = self._compute(args[i], csum);
        }
        return csum;
    }
};
 
Checksum.prototype._computeNone = function _computeNone() {
    return 0;
};
 
Checksum.prototype._computeCrc32 = function _computeCrc32(arg, prior) {
    if (prior === 0) prior = undefined;
    return crc32(arg, prior);
};
 
Checksum.prototype._computeCrc32C = function _computeCrc32C(arg, prior) {
    return crc32c(arg, prior);
};
 
Checksum.prototype._computeFarm32 = function _computeFarm32(arg, prior) {
    return farm32(arg, prior);
};
 
Checksum.prototype.update1 = function update1(arg, prior) {
    var self = this;
    self.val = self._compute(arg, prior);
};
 
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 errors.ChecksumError({
            checksumType: self.type,
            expectedValue: self.val,
            actualValue: val
        });
    }
};