-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Countly/salt-checksum
Added salt
- Loading branch information
Showing
7 changed files
with
264 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* eslint-disable cypress/no-unnecessary-waiting */ | ||
/* eslint-disable require-jsdoc */ | ||
var Countly = require("../../Countly.js"); | ||
var Utils = require("../../modules/Utils.js"); | ||
// import * as Countly from "../../dist/countly_umd.js"; | ||
var hp = require("../support/helper.js"); | ||
const crypto = require('crypto'); | ||
|
||
function initMain(salt) { | ||
Countly.init({ | ||
app_key: "YOUR_APP_KEY", | ||
url: "https://your.domain.count.ly", | ||
debug: true, | ||
salt: salt | ||
}); | ||
} | ||
const salt = "salt"; | ||
|
||
/** | ||
* Tests for salt consists of: | ||
* 1. Init without salt | ||
* Create events and intercept the SDK requests. Request params should be normal and there should be no checksum | ||
* 2. Init with salt | ||
* Create events and intercept the SDK requests. Request params should be normal and there should be a checksum with length 64 | ||
* 3. Node and Web Crypto comparison | ||
* Compare the checksums calculated by node crypto api and SDK's web crypto api for the same data. Should be equal | ||
*/ | ||
describe("Salt Tests", () => { | ||
it("Init without salt", () => { | ||
hp.haltAndClearStorage(() => { | ||
initMain(null); | ||
var rqArray = []; | ||
hp.events(); | ||
cy.intercept("GET", "**/i?**", (req) => { | ||
const { url } = req; | ||
rqArray.push(url.split("?")[1]); // get the query string | ||
}); | ||
cy.wait(1000).then(() => { | ||
cy.log(rqArray).then(() => { | ||
for (const rq of rqArray) { | ||
const paramsObject = hp.turnSearchStringToObject(rq); | ||
hp.check_commons(paramsObject); | ||
expect(paramsObject.checksum256).to.be.not.ok; | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("Init with salt", () => { | ||
hp.haltAndClearStorage(() => { | ||
initMain(salt); | ||
var rqArray = []; | ||
hp.events(); | ||
cy.intercept("GET", "**/i?**", (req) => { | ||
const { url } = req; | ||
rqArray.push(url.split("?")[1]); | ||
}); | ||
cy.wait(1000).then(() => { | ||
cy.log(rqArray).then(() => { | ||
for (const rq of rqArray) { | ||
const paramsObject = hp.turnSearchStringToObject(rq); | ||
hp.check_commons(paramsObject); | ||
expect(paramsObject.checksum256).to.be.ok; | ||
expect(paramsObject.checksum256.length).to.equal(64); | ||
// TODO: directly check the checksum with the node crypto api. Will need some extra decoding logic | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('Node and Web Crypto comparison', () => { | ||
const hash = sha256("text" + salt); // node crypto api | ||
Utils.calculateChecksum("text", salt).then((hash2) => { // SDK uses web crypto api | ||
expect(hash2).to.equal(hash); | ||
}); | ||
}); | ||
}); | ||
|
||
/** | ||
* Calculate sha256 hash of given data | ||
* @param {*} data - data to hash | ||
* @returns {string} - sha256 hash | ||
*/ | ||
function sha256(data) { | ||
const hash = crypto.createHash('sha256'); | ||
hash.update(data); | ||
return hash.digest('hex'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ body { | |
a { | ||
text-decoration: none; | ||
color: #000; | ||
padding: 20px; | ||
} | ||
|
||
#header { | ||
|
Oops, something went wrong.