painor 5 роки тому
батько
коміт
77f2c77e34

+ 41 - 0
__tests__/AES.spec.js

@@ -0,0 +1,41 @@
+const AES = require('../gramjs/crypto/AES')
+const AESModeCTR = require('../gramjs/crypto/AESCTR')
+describe('IGE encrypt function', () => {
+    test('it should return 4a657a834edc2956ec95b2a42ec8c1f2d1f0a6028ac26fd830ed23855574b4e69dd1a2be2ba18a53a49b879b2' +
+        '45e1065e14b6e8ac5ba9b24befaff3209b77b5f', () => {
+        const plainText = Buffer.from('this should hold a 64 characters long string. only 10 more left.')
+        const iv = Buffer.from('the iv needs 32 characters long.')
+        const key = Buffer.from('the key needs 32 characters long')
+        const encrypted = Buffer.from('4a657a834edc2956ec95b2a42ec8c1f2d1f0a6028ac26fd830ed23855574b4e69dd1a2be' +
+            '2ba18a53a49b879b245e1065e14b6e8ac5ba9b24befaff3209b77b5f', 'hex')
+        expect(AES.encryptIge(plainText, key, iv)).toEqual(encrypted)
+    })
+})
+describe('IGE decrypt function', () => {
+    test('it should return "this should hold a 64 characters long string. only 10 more left."', () => {
+        const encrypted = Buffer.from('4a657a834edc2956ec95b2a42ec8c1f2d1f0a6028ac26fd830ed23855574b4e69dd1a2be' +
+            '2ba18a53a49b879b245e1065e14b6e8ac5ba9b24befaff3209b77b5f', 'hex')
+        const iv = Buffer.from('the iv needs 32 characters long.')
+        const key = Buffer.from('the key needs 32 characters long')
+        const plainText = Buffer.from('this should hold a 64 characters long string. only 10 more left.')
+        expect(AES.decryptIge(encrypted, key, iv)).toEqual(plainText)
+
+    })
+})
+describe('CTR encrypt function', () => {
+    test('it should return 5f40f14f8b70178f70e8045b44eff5f1b148714f23cd and' +
+        ' cd0779d148b466935cf573450212451692bc82fccd5b106e53', () => {
+        const encryptKey = Buffer.from('the key needs 32 characters long')
+        const encryptIv = Buffer.from('the iv does not.')
+        const encryptor = new AESModeCTR(encryptKey, encryptIv)
+        const firstData = Buffer.from('this can be any length')
+        const firstResult = encryptor.encrypt(firstData)
+        const secondData = Buffer.from('this also can be anything')
+        const secondResult = encryptor.encrypt(secondData)
+
+        const outputFirst = Buffer.from('5f40f14f8b70178f70e8045b44eff5f1b148714f23cd', 'hex')
+        const outputSecond = Buffer.from('cd0779d148b466935cf573450212451692bc82fccd5b106e53', 'hex')
+        expect([firstResult, secondResult]).toEqual([outputFirst, outputSecond])
+    })
+})
+

+ 19 - 0
__tests__/calcKey.spec.js

@@ -0,0 +1,19 @@
+const MTProtoState = require('../gramjs/network/MTProtoState')
+
+describe('calcKey function', () => {
+    test('it should return 0x93355e3f1f50529b6fb93eaf97f29b69c16345f53621e9d45cd9a11ddfbebac9 and' +
+        ' 11e94363ad7145222e2fbac4aaa27f01a6d832fb8115e89395bc43e23f868e47', () => {
+        const authKey = Buffer.from('bbf38532a79cd64363b490b3bc5e258adfc1d1a67ef3c6d322caac603f90a15215b609' +
+            '0ccb2226b477b24eb3412757d078d53c72b81864d1376ff20eb405a591781726495407628d8d611e37ecd6e23c605b57c5' +
+            '3b40270bac7e7de0312a5deb3a1a16e65808b944fcf700d3788da10074d5c088e9e6aca119320d7f07c16d7e3c9fd48e9d' +
+            '3f50ccc5276a30002d9919831bf783c368ce4b3e6f25f95875ec9315523cfcaa3ee50b1e40e5552cee2e16eec86b46308c' +
+            '97f808d58f249479bb0ee1b7b08cf7f0fc047fbe38df6083558494e732dbf26d16b1538c22d361bf31d3dc4f2b2cb115b3' +
+            'bfac1ec45c960e0854221cf484533025fa679a9b7a8ae11a00', 'hex')
+        const msgKey = Buffer.from('00f285b0bf254b5242e075bf87806c51', 'hex')
+        const aesKey = Buffer.from('93355e3f1f50529b6fb93eaf97f29b69c16345f53621e9d45cd9a11ddfbebac9', 'hex')
+        const aesIv = Buffer.from('11e94363ad7145222e2fbac4aaa27f01a6d832fb8115e89395bc43e23f868e47', 'hex')
+        const { key, iv } = new MTProtoState()._calcKey(authKey, msgKey, false)
+
+        expect([aesKey, aesIv]).toEqual([key, iv])
+    })
+})

+ 11 - 0
__tests__/factorizator.spec.js

@@ -0,0 +1,11 @@
+const Factorizator = require('../gramjs/crypto/Factorizator')
+
+describe('calcKey function', () => {
+    test('it should return 0x20a13b25e1726bfc', () => {
+        const input = BigInt('325672672642762197972197217945794795197912791579174576454600704764276407047277')
+        const { p, q } = Factorizator.factorize(input)
+        const outP = BigInt(19)
+        const outQ = BigInt('17140666981198010419589327260304989220942778504167082971294773934961916160383')
+        expect([p, q]).toEqual([outP, outQ])
+    })
+})

+ 70 - 0
__tests__/readBuffer.spec.js

@@ -0,0 +1,70 @@
+const Helpers = require('../gramjs/Helpers')
+
+describe('readBufferFromBigInt 8 bytes function', () => {
+    test('it should return 0x20a13b25e1726bfc', () => {
+        const input = BigInt('-257986242325798624')
+        const output = Buffer.from('20a13b25e1726bfc', 'hex')
+        expect(Helpers.readBufferFromBigInt(input, 8, true, true)).toEqual(output)
+    })
+    test('it should return 0xe05ec4da1e8d9403', () => {
+        const input = BigInt('257986242325798624')
+        const output = Buffer.from('e05ec4da1e8d9403', 'hex')
+        expect(Helpers.readBufferFromBigInt(input, 8, true, false)).toEqual(output)
+    })
+    test('it should return 0xfc6b72e1253ba120', () => {
+        const input = BigInt('-257986242325798624')
+        const output = Buffer.from('fc6b72e1253ba120', 'hex')
+        expect(Helpers.readBufferFromBigInt(input, 8, false, true)).toEqual(output)
+    })
+    test('it should return 0x03948d1edac45ee0', () => {
+        const input = BigInt('257986242325798624')
+        const output = Buffer.from('03948d1edac45ee0', 'hex')
+        expect(Helpers.readBufferFromBigInt(input, 8, false, false)).toEqual(output)
+    })
+})
+
+describe('readBufferFromBigInt 16 bytes function', () => {
+    test('it should return 0x8416c07962dac053b4346df39e5d97ec', () => {
+        const input = BigInt('-25798624232579862436622316998984984956')
+        const output = Buffer.from('8416c07962dac053b4346df39e5d97ec', 'hex')
+        expect(Helpers.readBufferFromBigInt(input, 16, true, true)).toEqual(output)
+    })
+    test('it should return 0x7ce93f869d253fac4bcb920c61a26813', () => {
+        const input = BigInt('25798624232579862436622316998984984956')
+        const output = Buffer.from('7ce93f869d253fac4bcb920c61a26813', 'hex')
+        expect(Helpers.readBufferFromBigInt(input, 16, true, false)).toEqual(output)
+    })
+    test('it should return 0xec975d9ef36d34b453c0da6279c01684', () => {
+        const input = BigInt('-25798624232579862436622316998984984956')
+        const output = Buffer.from('ec975d9ef36d34b453c0da6279c01684', 'hex')
+        expect(Helpers.readBufferFromBigInt(input, 16, false, true)).toEqual(output)
+    })
+    test('it should return 0x1368a2610c92cb4bac3f259d863fe97c', () => {
+        const input = BigInt('25798624232579862436622316998984984956')
+        const output = Buffer.from('1368a2610c92cb4bac3f259d863fe97c', 'hex')
+        expect(Helpers.readBufferFromBigInt(input, 16, false, false)).toEqual(output)
+    })
+})
+
+describe('readBufferFromBigInt 32 bytes function', () => {
+    test('it should return 0x7f113f5e2096936ec90cc4c73cc7bd3c96d20c115bf9ceb05c34232c037ff6c6', () => {
+        const input = BigInt('-25798624232579862436622316998984984912345482145214526587420145210501554564737')
+        const output = Buffer.from('7f113f5e2096936ec90cc4c73cc7bd3c96d20c115bf9ceb05c34232c037ff6c6', 'hex')
+        expect(Helpers.readBufferFromBigInt(input, 32, true, true)).toEqual(output)
+    })
+    test('it should return 0x81eec0a1df696c9136f33b38c33842c3692df3eea406314fa3cbdcd3fc800939', () => {
+        const input = BigInt('25798624232579862436622316998984984912345482145214526587420145210501554564737')
+        const output = Buffer.from('81eec0a1df696c9136f33b38c33842c3692df3eea406314fa3cbdcd3fc800939', 'hex')
+        expect(Helpers.readBufferFromBigInt(input, 32, true, false)).toEqual(output)
+    })
+    test('it should return 0xc6f67f032c23345cb0cef95b110cd2963cbdc73cc7c40cc96e9396205e3f117f', () => {
+        const input = BigInt('-25798624232579862436622316998984984912345482145214526587420145210501554564737')
+        const output = Buffer.from('c6f67f032c23345cb0cef95b110cd2963cbdc73cc7c40cc96e9396205e3f117f', 'hex')
+        expect(Helpers.readBufferFromBigInt(input, 32, false, true)).toEqual(output)
+    })
+    test('it should return 0x390980fcd3dccba34f3106a4eef32d69c34238c3383bf336916c69dfa1c0ee81', () => {
+        const input = BigInt('25798624232579862436622316998984984912345482145214526587420145210501554564737')
+        const output = Buffer.from('390980fcd3dccba34f3106a4eef32d69c34238c3383bf336916c69dfa1c0ee81', 'hex')
+        expect(Helpers.readBufferFromBigInt(input, 32, false, false)).toEqual(output)
+    })
+})

+ 19 - 0
babel.config.js

@@ -0,0 +1,19 @@
+// babel.config.js
+module.exports = {
+    presets: [
+        [
+            '@babel/preset-env',
+            {
+                targets: {
+                    node: 'current',
+                },
+            },
+        ],
+    ],
+    plugins: [
+        [
+            '@babel/plugin-proposal-class-properties',
+        ],
+    ],
+
+}

+ 188 - 0
jest.config.js

@@ -0,0 +1,188 @@
+// For a detailed explanation regarding each configuration property, visit:
+// https://jestjs.io/docs/en/configuration.html
+
+module.exports = {
+  // All imported modules in your tests should be mocked automatically
+  // automock: false,
+
+  // Stop running tests after `n` failures
+  // bail: 0,
+
+  // Respect "browser" field in package.json when resolving modules
+  // browser: false,
+
+  // The directory where Jest should store its cached dependency information
+  // cacheDirectory: "C:\\Users\\painor\\AppData\\Local\\Temp\\jest",
+
+  // Automatically clear mock calls and instances between every test
+  clearMocks: true,
+
+  // Indicates whether the coverage information should be collected while executing the test
+  // collectCoverage: false,
+
+  // An array of glob patterns indicating a set of files for which coverage information should be collected
+  // collectCoverageFrom: null,
+
+  // The directory where Jest should output its coverage files
+  coverageDirectory: "coverage",
+
+  // An array of regexp pattern strings used to skip coverage collection
+  // coveragePathIgnorePatterns: [
+  //   "\\\\node_modules\\\\"
+  // ],
+
+  // A list of reporter names that Jest uses when writing coverage reports
+  // coverageReporters: [
+  //   "json",
+  //   "text",
+  //   "lcov",
+  //   "clover"
+  // ],
+
+  // An object that configures minimum threshold enforcement for coverage results
+  // coverageThreshold: null,
+
+  // A path to a custom dependency extractor
+  // dependencyExtractor: null,
+
+  // Make calling deprecated APIs throw helpful error messages
+  // errorOnDeprecated: false,
+
+  // Force coverage collection from ignored files using an array of glob patterns
+  // forceCoverageMatch: [],
+
+  // A path to a module which exports an async function that is triggered once before all test suites
+  // globalSetup: null,
+
+  // A path to a module which exports an async function that is triggered once after all test suites
+  // globalTeardown: null,
+
+  // A set of global variables that need to be available in all test environments
+  // globals: {},
+
+  // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
+  // maxWorkers: "50%",
+
+  // An array of directory names to be searched recursively up from the requiring module's location
+  // moduleDirectories: [
+  //   "node_modules"
+  // ],
+
+  // An array of file extensions your modules use
+  // moduleFileExtensions: [
+  //   "js",
+  //   "json",
+  //   "jsx",
+  //   "ts",
+  //   "tsx",
+  //   "node"
+  // ],
+
+  // A map from regular expressions to module names that allow to stub out resources with a single module
+  // moduleNameMapper: {},
+
+  // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
+  // modulePathIgnorePatterns: [],
+
+  // Activates notifications for test results
+  // notify: false,
+
+  // An enum that specifies notification mode. Requires { notify: true }
+  // notifyMode: "failure-change",
+
+  // A preset that is used as a base for Jest's configuration
+  // preset: null,
+
+  // Run tests from one or more projects
+  // projects: null,
+
+  // Use this configuration option to add custom reporters to Jest
+  // reporters: undefined,
+
+  // Automatically reset mock state between every test
+  // resetMocks: false,
+
+  // Reset the module registry before running each individual test
+  // resetModules: false,
+
+  // A path to a custom resolver
+  // resolver: null,
+
+  // Automatically restore mock state between every test
+  // restoreMocks: false,
+
+  // The root directory that Jest should scan for tests and modules within
+  // rootDir: null,
+
+  // A list of paths to directories that Jest should use to search for files in
+  // roots: [
+  //   "<rootDir>"
+  // ],
+
+  // Allows you to use a custom runner instead of Jest's default test runner
+  // runner: "jest-runner",
+
+  // The paths to modules that run some code to configure or set up the testing environment before each test
+  // setupFiles: [],
+
+  // A list of paths to modules that run some code to configure or set up the testing framework before each test
+  // setupFilesAfterEnv: [],
+
+  // A list of paths to snapshot serializer modules Jest should use for snapshot testing
+  // snapshotSerializers: [],
+
+  // The test environment that will be used for testing
+  testEnvironment: "node",
+
+  // Options that will be passed to the testEnvironment
+  // testEnvironmentOptions: {},
+
+  // Adds a location field to test results
+  // testLocationInResults: false,
+
+  // The glob patterns Jest uses to detect test files
+  // testMatch: [
+  //   "**/__tests__/**/*.[jt]s?(x)",
+  //   "**/?(*.)+(spec|test).[tj]s?(x)"
+  // ],
+
+  // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
+  // testPathIgnorePatterns: [
+  //   "\\\\node_modules\\\\"
+  // ],
+
+  // The regexp pattern or array of patterns that Jest uses to detect test files
+  // testRegex: [],
+
+  // This option allows the use of a custom results processor
+  // testResultsProcessor: null,
+
+  // This option allows use of a custom test runner
+  // testRunner: "jasmine2",
+
+  // This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
+  // testURL: "http://localhost",
+
+  // Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
+  // timers: "real",
+
+  // A map from regular expressions to paths to transformers
+  // transform: null,
+
+  // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
+  // transformIgnorePatterns: [
+  //   "\\\\node_modules\\\\"
+  // ],
+
+  // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
+  // unmockedModulePathPatterns: undefined,
+
+  // Indicates whether each individual test should be reported during the run
+  // verbose: null,
+
+  // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
+  // watchPathIgnorePatterns: [],
+
+  // Whether to use watchman for file crawling
+  // watchman: true,
+};

Різницю між файлами не показано, бо вона завелика
+ 1367 - 11
package-lock.json


+ 5 - 2
package.json

@@ -4,6 +4,7 @@
   "description": "NodeJS MTProto API Telegram client library,",
   "main": "index.js",
   "scripts": {
+    "test": "jest",
     "lint": "eslint .",
     "postinstall": "node index.js gen tl errors"
   },
@@ -20,9 +21,9 @@
   },
   "homepage": "https://github.com/gram-js/gramjs#readme",
   "dependencies": {
-    "@babel/core": "^7.7.0",
+    "@babel/core": "^7.7.7",
     "@babel/plugin-proposal-class-properties": "^7.7.0",
-    "@babel/preset-env": "^7.7.0",
+    "@babel/preset-env": "^7.7.7",
     "aes-js": "^3.1.2",
     "babel-loader": "^8.0.6",
     "babel-register": "^6.26.0",
@@ -46,8 +47,10 @@
   "devDependencies": {
     "@babel/plugin-transform-async-to-generator": "^7.7.0",
     "babel-eslint": "^10.0.3",
+    "babel-jest": "^24.9.0",
     "eslint": "^6.5.1",
     "eslint-config-google": "^0.14.0",
+    "jest": "^24.9.0",
     "webpack": "^4.41.2",
     "webpack-cli": "^3.3.10",
     "websocket-as-promised": "^0.10.1"

Деякі файли не було показано, через те що забагато файлів було змінено