|
| 1 | +const crypto = require('crypto') |
| 2 | +const assert = require('assert') |
| 3 | +const http = require('http') |
| 4 | +const Socket = require('net').Socket |
| 5 | + |
| 6 | +const req = new http.IncomingMessage(new Socket()) |
| 7 | +const res = new http.ServerResponse(req) |
| 8 | + |
| 9 | +function getFunctionHash (fn) { |
| 10 | + const src = fn.toString().replace(/\s+/g, '') // normalize whitespace |
| 11 | + return crypto.createHash('sha256').update(src).digest('hex') |
| 12 | +} |
| 13 | + |
| 14 | +const knownWriteHeadHash = '281e0d02084a69893b8c3b8692e3c7c4de2ce22a626217fcf597fa6ddf6955a9' |
| 15 | +const knownSetHeaderHash = '2d4f95e92586d28bfd4d3137a8eaacb82b255967d8c26413015c6b56daf0afe7' |
| 16 | +const knownAppendHeaderHash = '0deb9f70c3bba63993321cca9281fb4607e2567bed1436b8574c5b86698125a8' |
| 17 | +const knownRemoveHeaderHash = '3ad5ccb0a858beb6268f281492bd8d42c9815f5316cc3c4f7f735e142fcd29d9' |
| 18 | + |
| 19 | +describe('function verification', function () { |
| 20 | + it('should match the known function hash of writeHead', function () { |
| 21 | + const currentHash = getFunctionHash(res.writeHead) |
| 22 | + assert.strictEqual(currentHash, knownWriteHeadHash, 'writeHead hash has changed') |
| 23 | + }) |
| 24 | + |
| 25 | + it('should match the known function hash of setHeader', function () { |
| 26 | + const currentHash = getFunctionHash(res.setHeader) |
| 27 | + assert.strictEqual(currentHash, knownSetHeaderHash, 'setHeader hash has changed') |
| 28 | + }) |
| 29 | + |
| 30 | + it('should match the known function hash of appendHeader', function () { |
| 31 | + const currentHash = getFunctionHash(res.appendHeader) |
| 32 | + assert.strictEqual(currentHash, knownAppendHeaderHash, 'appendHeader hash has changed') |
| 33 | + }) |
| 34 | + |
| 35 | + it('should match the known function hash of removeHeader', function () { |
| 36 | + const currentHash = getFunctionHash(res.removeHeader) |
| 37 | + assert.strictEqual(currentHash, knownRemoveHeaderHash, 'removeHeader hash has changed') |
| 38 | + }) |
| 39 | +}) |
0 commit comments