|
| 1 | +/* |
| 2 | + * GNU AGPL-3.0 License |
| 3 | + * |
| 4 | + * Copyright (c) 2021 - present core.ai . All rights reserved. |
| 5 | + * Original work Copyright (c) 2013 - 2021 Adobe Systems Incorporated. All rights reserved. |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify it |
| 8 | + * under the terms of the GNU Affero General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License |
| 15 | + * for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see https://opensource.org/licenses/AGPL-3.0. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +/*global describe, it, expect, beforeEach, afterEach, fs, path, jasmine, expectAsync*/ |
| 23 | + |
| 24 | +define(function (require, exports, module) { |
| 25 | + |
| 26 | + const SpecRunnerUtils = require("spec/SpecRunnerUtils"); |
| 27 | + |
| 28 | + describe("unit: Trust Ring Tests", function () { |
| 29 | + const TrustRing = window.specRunnerTestKernalModeTrust; |
| 30 | + |
| 31 | + beforeEach(async function () { |
| 32 | + |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(async function () { |
| 36 | + |
| 37 | + }); |
| 38 | + |
| 39 | + describe("generateDataSignature", function () { |
| 40 | + it("should generate a consistent signature for the same data and salt", async function () { |
| 41 | + const data = "test data"; |
| 42 | + const salt = "test salt"; |
| 43 | + |
| 44 | + const signature1 = await TrustRing.generateDataSignature(data, salt); |
| 45 | + const signature2 = await TrustRing.generateDataSignature(data, salt); |
| 46 | + |
| 47 | + expect(signature1).toBe(signature2); |
| 48 | + expect(signature1).toBeDefined(); |
| 49 | + expect(typeof signature1).toBe('string'); |
| 50 | + expect(signature1.length).toBeGreaterThan(0); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should generate different signatures for different data", async function () { |
| 54 | + const salt = "test salt"; |
| 55 | + |
| 56 | + const signature1 = await TrustRing.generateDataSignature("data1", salt); |
| 57 | + const signature2 = await TrustRing.generateDataSignature("data2", salt); |
| 58 | + |
| 59 | + expect(signature1).not.toBe(signature2); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should generate different signatures for different salts", async function () { |
| 63 | + const data = "test data"; |
| 64 | + |
| 65 | + const signature1 = await TrustRing.generateDataSignature(data, "salt1"); |
| 66 | + const signature2 = await TrustRing.generateDataSignature(data, "salt2"); |
| 67 | + |
| 68 | + expect(signature1).not.toBe(signature2); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should handle empty strings", async function () { |
| 72 | + const signature1 = await TrustRing.generateDataSignature("", ""); |
| 73 | + const signature2 = await TrustRing.generateDataSignature("", "salt"); |
| 74 | + const signature3 = await TrustRing.generateDataSignature("data", ""); |
| 75 | + |
| 76 | + expect(signature1).toBeDefined(); |
| 77 | + expect(signature2).toBeDefined(); |
| 78 | + expect(signature3).toBeDefined(); |
| 79 | + expect(signature1).not.toBe(signature2); |
| 80 | + expect(signature2).not.toBe(signature3); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should generate hexadecimal string output", async function () { |
| 84 | + const signature = await TrustRing.generateDataSignature("test", "salt"); |
| 85 | + |
| 86 | + expect(signature).toMatch(/^[a-f0-9]+$/); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe("validateDataSignature", function () { |
| 91 | + it("should validate correct signature", async function () { |
| 92 | + const data = "test data"; |
| 93 | + const salt = "test salt"; |
| 94 | + |
| 95 | + const signature = await TrustRing.generateDataSignature(data, salt); |
| 96 | + const isValid = await TrustRing.validateDataSignature(data, signature, salt); |
| 97 | + |
| 98 | + expect(isValid).toBe(true); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should reject invalid signature", async function () { |
| 102 | + const data = "test data"; |
| 103 | + const salt = "test salt"; |
| 104 | + const invalidSignature = "invalid signature"; |
| 105 | + |
| 106 | + const isValid = await TrustRing.validateDataSignature(data, invalidSignature, salt); |
| 107 | + |
| 108 | + expect(isValid).toBe(false); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should reject signature with wrong data", async function () { |
| 112 | + const salt = "test salt"; |
| 113 | + |
| 114 | + const signature = await TrustRing.generateDataSignature("original data", salt); |
| 115 | + const isValid = await TrustRing.validateDataSignature("different data", signature, salt); |
| 116 | + |
| 117 | + expect(isValid).toBe(false); |
| 118 | + }); |
| 119 | + |
| 120 | + it("should reject signature with wrong salt", async function () { |
| 121 | + const data = "test data"; |
| 122 | + |
| 123 | + const signature = await TrustRing.generateDataSignature(data, "original salt"); |
| 124 | + const isValid = await TrustRing.validateDataSignature(data, signature, "different salt"); |
| 125 | + |
| 126 | + expect(isValid).toBe(false); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should return false for null or undefined data", async function () { |
| 130 | + const signature = "some signature"; |
| 131 | + const salt = "test salt"; |
| 132 | + |
| 133 | + const isValid1 = await TrustRing.validateDataSignature(null, signature, salt); |
| 134 | + const isValid2 = await TrustRing.validateDataSignature(undefined, signature, salt); |
| 135 | + |
| 136 | + expect(isValid1).toBe(false); |
| 137 | + expect(isValid2).toBe(false); |
| 138 | + }); |
| 139 | + |
| 140 | + it("should return false for null or undefined signature", async function () { |
| 141 | + const data = "test data"; |
| 142 | + const salt = "test salt"; |
| 143 | + |
| 144 | + const isValid1 = await TrustRing.validateDataSignature(data, null, salt); |
| 145 | + const isValid2 = await TrustRing.validateDataSignature(data, undefined, salt); |
| 146 | + |
| 147 | + expect(isValid1).toBe(false); |
| 148 | + expect(isValid2).toBe(false); |
| 149 | + }); |
| 150 | + |
| 151 | + it("should handle empty strings correctly", async function () { |
| 152 | + const signature = await TrustRing.generateDataSignature("", ""); |
| 153 | + const isValid = await TrustRing.validateDataSignature("", signature, ""); |
| 154 | + |
| 155 | + expect(isValid).toBe(true); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + describe("generateDataSignature and validateDataSignature integration", function () { |
| 160 | + it("should work with JSON data like entitlements", async function () { |
| 161 | + const entitlements = { |
| 162 | + premium: true, |
| 163 | + features: ["feature1", "feature2"], |
| 164 | + expiry: "2024-12-31" |
| 165 | + }; |
| 166 | + const jsonData = JSON.stringify(entitlements); |
| 167 | + const salt = "random-salt-123"; |
| 168 | + |
| 169 | + const signature = await TrustRing.generateDataSignature(jsonData, salt); |
| 170 | + const isValid = await TrustRing.validateDataSignature(jsonData, signature, salt); |
| 171 | + |
| 172 | + expect(isValid).toBe(true); |
| 173 | + }); |
| 174 | + |
| 175 | + it("should work with unicode characters", async function () { |
| 176 | + const data = "测试数据 with émojis 🔒"; |
| 177 | + const salt = "unicode-salt-🔑"; |
| 178 | + |
| 179 | + const signature = await TrustRing.generateDataSignature(data, salt); |
| 180 | + const isValid = await TrustRing.validateDataSignature(data, signature, salt); |
| 181 | + |
| 182 | + expect(isValid).toBe(true); |
| 183 | + }); |
| 184 | + |
| 185 | + it("should work with large data strings", async function () { |
| 186 | + const largeData = "x".repeat(10000); |
| 187 | + const salt = "large-data-salt"; |
| 188 | + |
| 189 | + const signature = await TrustRing.generateDataSignature(largeData, salt); |
| 190 | + const isValid = await TrustRing.validateDataSignature(largeData, signature, salt); |
| 191 | + |
| 192 | + expect(isValid).toBe(true); |
| 193 | + }); |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments