Skip to content

Commit ca68148

Browse files
committed
Refactor tests to utilize Uint8Array for data validation
- Updated comprehensive API, lifecycle, cross-platform, and basic tests to replace Buffer checks with Uint8Array validations, enhancing consistency in data type handling. - Introduced a new utility function, `uint8ArrayEquals`, for comparing Uint8Array instances, improving code clarity and maintainability. - Adjusted assertions across multiple test files to ensure proper validation of cryptographic keys and encrypted data, aligning with recent API changes. - Enhanced overall test coverage and robustness by ensuring all relevant tests reflect the updated data handling practices.
1 parent ecf7b85 commit ca68148

File tree

8 files changed

+139
-121
lines changed

8 files changed

+139
-121
lines changed

runar-nodejs-api/tests/comprehensive_api_test.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
createTempDir,
55
cleanupTempDir,
66
withTimeout,
7-
createFreshKeys
7+
createFreshKeys,
8+
uint8ArrayEquals
89
} from './test_utils';
910

1011
const mod = loadAddon();
@@ -114,7 +115,7 @@ describe('Comprehensive API Tests', () => {
114115
keys.initAsMobile();
115116
await withTimeout(keys.mobileInitializeUserRootKey(), 5000, 'mobileInitializeUserRootKey');
116117
const pk = keys.mobileGetUserPublicKey();
117-
expect(Buffer.isBuffer(pk)).toBe(true);
118+
expect(pk instanceof Uint8Array).toBe(true);
118119
expect(pk.length).toBeGreaterThan(0);
119120
}, 10000);
120121

@@ -123,7 +124,7 @@ describe('Comprehensive API Tests', () => {
123124
keys.initAsMobile();
124125
await withTimeout(keys.mobileInitializeUserRootKey(), 5000, 'mobileInitializeUserRootKey');
125126
const pk = keys.mobileDeriveUserProfileKey('personal');
126-
expect(Buffer.isBuffer(pk)).toBe(true);
127+
expect(pk instanceof Uint8Array).toBe(true);
127128
expect(pk.length).toBeGreaterThan(0);
128129
}, 10000);
129130

@@ -152,7 +153,7 @@ describe('Comprehensive API Tests', () => {
152153
keys.mobileInstallNetworkPublicKey(testPk);
153154
const networkId = keys.mobileGenerateNetworkDataKey();
154155
const pk = keys.mobileGetNetworkPublicKey(networkId);
155-
expect(Buffer.isBuffer(pk)).toBe(true);
156+
expect(pk instanceof Uint8Array).toBe(true);
156157
expect(pk.length).toBeGreaterThan(0);
157158
}, 10000);
158159

@@ -180,23 +181,23 @@ describe('Comprehensive API Tests', () => {
180181
const keys = createFreshKeys(tmpDir);
181182
keys.initAsNode();
182183
const pk = keys.nodeGetPublicKey();
183-
expect(Buffer.isBuffer(pk)).toBe(true);
184+
expect(pk instanceof Uint8Array).toBe(true);
184185
expect(pk.length).toBeGreaterThan(0);
185186
});
186187

187188
test('should get node agreement public key successfully', () => {
188189
const keys = createFreshKeys(tmpDir);
189190
keys.initAsNode();
190191
const pk = keys.nodeGetAgreementPublicKey();
191-
expect(Buffer.isBuffer(pk)).toBe(true);
192+
expect(pk instanceof Uint8Array).toBe(true);
192193
expect(pk.length).toBeGreaterThan(0);
193194
});
194195

195196
test('should generate CSR successfully', () => {
196197
const keys = createFreshKeys(tmpDir);
197198
keys.initAsNode();
198199
const csr = keys.nodeGenerateCsr();
199-
expect(Buffer.isBuffer(csr)).toBe(true);
200+
expect(csr instanceof Uint8Array).toBe(true);
200201
expect(csr.length).toBeGreaterThan(0);
201202
});
202203

@@ -217,8 +218,8 @@ describe('Comprehensive API Tests', () => {
217218
keys.initAsNode();
218219
const data = Buffer.from('test data');
219220
const encrypted = keys.encryptLocalData(data);
220-
expect(Buffer.isBuffer(encrypted)).toBe(true);
221-
expect(encrypted.equals(data)).toBe(false);
221+
expect(encrypted instanceof Uint8Array).toBe(true);
222+
expect(uint8ArrayEquals(encrypted, data)).toBe(false);
222223
});
223224

224225
test('should decrypt local data successfully', () => {
@@ -227,7 +228,7 @@ describe('Comprehensive API Tests', () => {
227228
const data = Buffer.from('test data');
228229
const encrypted = keys.encryptLocalData(data);
229230
const decrypted = keys.decryptLocalData(encrypted);
230-
expect(decrypted.equals(data)).toBe(true);
231+
expect(uint8ArrayEquals(decrypted, data)).toBe(true);
231232
});
232233

233234
// Note: Network encryption tests removed - covered in e2e tests
@@ -246,7 +247,7 @@ describe('Comprehensive API Tests', () => {
246247
const keys = createFreshKeys(tmpDir);
247248
keys.initAsNode();
248249
const key = keys.ensureSymmetricKey('test-service');
249-
expect(Buffer.isBuffer(key)).toBe(true);
250+
expect(key instanceof Uint8Array).toBe(true);
250251
expect(key.length).toBe(32); // 256-bit key
251252
});
252253

@@ -255,15 +256,15 @@ describe('Comprehensive API Tests', () => {
255256
keys.initAsNode();
256257
const key1 = keys.ensureSymmetricKey('test-service');
257258
const key2 = keys.ensureSymmetricKey('test-service');
258-
expect(key1.equals(key2)).toBe(true);
259+
expect(uint8ArrayEquals(key1, key2)).toBe(true);
259260
});
260261

261262
test('should return different keys for different service names', () => {
262263
const keys = createFreshKeys(tmpDir);
263264
keys.initAsNode();
264265
const key1 = keys.ensureSymmetricKey('service-1');
265266
const key2 = keys.ensureSymmetricKey('service-2');
266-
expect(key1.equals(key2)).toBe(false);
267+
expect(uint8ArrayEquals(key1, key2)).toBe(false);
267268
});
268269
});
269270

runar-nodejs-api/tests/comprehensive_lifecycle_test.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os from 'os';
22
import fs from 'fs';
33
import path from 'path';
4+
import { uint8ArrayEquals } from './test_utils';
45

56
function loadAddon(): any {
67
const filename = 'index.linux-x64-gnu.node';
@@ -53,7 +54,7 @@ describe('Comprehensive End-to-End Lifecycle Tests', () => {
5354

5455
// Get the user root public key (essential for encrypting setup tokens)
5556
const userPublicKey = mobileKeys.mobileGetUserPublicKey();
56-
expect(Buffer.isBuffer(userPublicKey)).toBe(true);
57+
expect(userPublicKey instanceof Uint8Array).toBe(true);
5758
expect(userPublicKey.length).toBeGreaterThan(0);
5859
console.log(` ✅ User public key generated: ${userPublicKey.length} bytes`);
5960

@@ -73,13 +74,13 @@ describe('Comprehensive End-to-End Lifecycle Tests', () => {
7374

7475
// Get the node public key (node ID) - keys are created in constructor
7576
const nodePublicKey = nodeKeys.nodeGetPublicKey();
76-
expect(Buffer.isBuffer(nodePublicKey)).toBe(true);
77+
expect(nodePublicKey instanceof Uint8Array).toBe(true);
7778
expect(nodePublicKey.length).toBeGreaterThan(0);
7879
console.log(` ✅ Node identity created: ${nodePublicKey.length} bytes`);
7980

8081
// Generate setup token (CSR)
8182
const setupTokenCbor = nodeKeys.nodeGenerateCsr();
82-
expect(Buffer.isBuffer(setupTokenCbor)).toBe(true);
83+
expect(setupTokenCbor instanceof Uint8Array).toBe(true);
8384
expect(setupTokenCbor.length).toBeGreaterThan(0);
8485
console.log(` ✅ Setup token (CSR) generated: ${setupTokenCbor.length} bytes`);
8586

@@ -108,7 +109,7 @@ describe('Comprehensive End-to-End Lifecycle Tests', () => {
108109

109110
// 3 - (mobile side) - received the token and sign the CSR
110111
const certMessageCbor = mobileKeys.mobileProcessSetupToken(setupTokenForMobile);
111-
expect(Buffer.isBuffer(certMessageCbor)).toBe(true);
112+
expect(certMessageCbor instanceof Uint8Array).toBe(true);
112113
expect(certMessageCbor.length).toBeGreaterThan(0);
113114
console.log(' ✅ Certificate issued successfully');
114115

@@ -151,11 +152,11 @@ describe('Comprehensive End-to-End Lifecycle Tests', () => {
151152
// 3.2 Mobile creates network key message
152153
// Get the node agreement public key for encryption
153154
const nodeAgreementPublicKey = nodeKeys.nodeGetAgreementPublicKey();
154-
expect(Buffer.isBuffer(nodeAgreementPublicKey)).toBe(true);
155+
expect(nodeAgreementPublicKey instanceof Uint8Array).toBe(true);
155156
expect(nodeAgreementPublicKey.length).toBeGreaterThan(0);
156157

157158
const networkKeyMessage = mobileKeys.mobileCreateNetworkKeyMessage(networkId, nodeAgreementPublicKey);
158-
expect(Buffer.isBuffer(networkKeyMessage)).toBe(true);
159+
expect(networkKeyMessage instanceof Uint8Array).toBe(true);
159160
expect(networkKeyMessage.length).toBeGreaterThan(0);
160161
console.log(` ✅ Network key message created: ${networkKeyMessage.length} bytes`);
161162

@@ -167,11 +168,11 @@ describe('Comprehensive End-to-End Lifecycle Tests', () => {
167168
console.log('\n👤 ENHANCED KEY MANAGEMENT TESTING');
168169

169170
const personalProfileKey = mobileKeys.mobileDeriveUserProfileKey('personal');
170-
expect(Buffer.isBuffer(personalProfileKey)).toBe(true);
171+
expect(personalProfileKey instanceof Uint8Array).toBe(true);
171172
expect(personalProfileKey.length).toBeGreaterThan(0);
172173

173174
const workProfileKey = mobileKeys.mobileDeriveUserProfileKey('work');
174-
expect(Buffer.isBuffer(workProfileKey)).toBe(true);
175+
expect(workProfileKey instanceof Uint8Array).toBe(true);
175176
expect(workProfileKey.length).toBeGreaterThan(0);
176177

177178
console.log(' ✅ Profile keys generated: personal, work');
@@ -187,19 +188,19 @@ describe('Comprehensive End-to-End Lifecycle Tests', () => {
187188
// 5.1 Mobile encrypts with envelope
188189
// Get network public key from network ID for envelope encryption
189190
const networkPublicKey = mobileKeys.mobileGetNetworkPublicKey(networkId);
190-
expect(Buffer.isBuffer(networkPublicKey)).toBe(true);
191+
expect(networkPublicKey instanceof Uint8Array).toBe(true);
191192
expect(networkPublicKey.length).toBeGreaterThan(0);
192193

193194
const encryptedData = mobileKeys.mobileEncryptWithEnvelope(testData, networkPublicKey, profilePks);
194-
expect(Buffer.isBuffer(encryptedData)).toBe(true);
195-
expect(encryptedData.equals(testData)).toBe(false);
195+
expect(encryptedData instanceof Uint8Array).toBe(true);
196+
expect(uint8ArrayEquals(encryptedData, testData)).toBe(false);
196197
console.log(` ✅ Data encrypted with envelope: ${encryptedData.length} bytes`);
197198
console.log(` Network: ${networkId} (${networkPublicKey.length} bytes)`);
198199
console.log(` Profile recipients: ${profilePks.length}`);
199200

200201
// 5.2 Node decrypts envelope
201202
const decryptedData = nodeKeys.nodeDecryptEnvelope(encryptedData);
202-
expect(decryptedData.equals(testData)).toBe(true);
203+
expect(uint8ArrayEquals(decryptedData, testData)).toBe(true);
203204
console.log(' ✅ Node successfully decrypted envelope data using network key');
204205

205206
// 10 - Test node local storage encryption
@@ -208,12 +209,12 @@ describe('Comprehensive End-to-End Lifecycle Tests', () => {
208209
const fileData1 = Buffer.from('This is some secret file content that should be encrypted on the node.');
209210

210211
const encryptedFile1 = nodeKeys.encryptLocalData(fileData1);
211-
expect(Buffer.isBuffer(encryptedFile1)).toBe(true);
212-
expect(encryptedFile1.equals(fileData1)).toBe(false);
212+
expect(encryptedFile1 instanceof Uint8Array).toBe(true);
213+
expect(uint8ArrayEquals(encryptedFile1, fileData1)).toBe(false);
213214
console.log(` ✅ Encrypted local data (hex): ${encryptedFile1.toString('hex').substring(0, 32)}...`);
214215

215216
const decryptedFile1 = nodeKeys.decryptLocalData(encryptedFile1);
216-
expect(decryptedFile1.equals(fileData1)).toBe(true);
217+
expect(uint8ArrayEquals(decryptedFile1, fileData1)).toBe(true);
217218
console.log(' ✅ Local data encryption/decryption successful');
218219

219220
// State serialization and restoration check for profile keys
@@ -233,11 +234,11 @@ describe('Comprehensive End-to-End Lifecycle Tests', () => {
233234
// Additional local storage test
234235
const fileData2 = Buffer.from('This is secret file content to test after hydration.');
235236
const encryptedFile2 = nodeKeys.encryptLocalData(fileData2);
236-
expect(Buffer.isBuffer(encryptedFile2)).toBe(true);
237-
expect(encryptedFile2.equals(fileData2)).toBe(false);
237+
expect(encryptedFile2 instanceof Uint8Array).toBe(true);
238+
expect(uint8ArrayEquals(encryptedFile2, fileData2)).toBe(false);
238239

239240
const decryptedFile2 = nodeKeys.decryptLocalData(encryptedFile2);
240-
expect(decryptedFile2.equals(fileData2)).toBe(true);
241+
expect(uint8ArrayEquals(decryptedFile2, fileData2)).toBe(true);
241242
console.log(' ✅ Local storage encryption/decryption working correctly');
242243

243244
// ==========================================

runar-nodejs-api/tests/cross_platform_tests.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os from 'os';
22
import fs from 'fs';
33
import path from 'path';
4+
import { uint8ArrayEquals } from './test_utils';
45

56
function loadAddon(): any {
67
const filename = 'index.linux-x64-gnu.node';
@@ -79,7 +80,7 @@ describe('Cross-Platform Tests', () => {
7980
await expect(withTimeout(keys.mobileInitializeUserRootKey(), 5000, 'mobileInitializeUserRootKey')).resolves.not.toThrow();
8081

8182
const pk = keys.mobileGetUserPublicKey();
82-
expect(Buffer.isBuffer(pk)).toBe(true);
83+
expect(pk instanceof Uint8Array).toBe(true);
8384
expect(pk.length).toBeGreaterThan(0);
8485
}, 10000);
8586

@@ -92,7 +93,7 @@ describe('Cross-Platform Tests', () => {
9293
expect(id.length).toBeGreaterThan(0);
9394

9495
const pk = keys.nodeGetPublicKey();
95-
expect(Buffer.isBuffer(pk)).toBe(true);
96+
expect(pk instanceof Uint8Array).toBe(true);
9697
expect(pk.length).toBeGreaterThan(0);
9798
});
9899

@@ -113,11 +114,11 @@ describe('Cross-Platform Tests', () => {
113114

114115
const data = Buffer.from('test data for encryption');
115116
const encrypted = keys.encryptLocalData(data);
116-
expect(Buffer.isBuffer(encrypted)).toBe(true);
117-
expect(encrypted.equals(data)).toBe(false);
117+
expect(encrypted instanceof Uint8Array).toBe(true);
118+
expect(uint8ArrayEquals(encrypted, data)).toBe(false);
118119

119120
const decrypted = keys.decryptLocalData(encrypted);
120-
expect(decrypted.equals(data)).toBe(true);
121+
expect(uint8ArrayEquals(decrypted, data)).toBe(true);
121122
});
122123

123124
test('should perform symmetric key operations', () => {
@@ -128,10 +129,10 @@ describe('Cross-Platform Tests', () => {
128129
const key2 = keys.ensureSymmetricKey('service2');
129130
const key1Again = keys.ensureSymmetricKey('service1');
130131

131-
expect(Buffer.isBuffer(key1)).toBe(true);
132+
expect(key1 instanceof Uint8Array).toBe(true);
132133
expect(key1.length).toBe(32);
133-
expect(key1.equals(key2)).toBe(false);
134-
expect(key1.equals(key1Again)).toBe(true);
134+
expect(uint8ArrayEquals(key1, key2)).toBe(false);
135+
expect(uint8ArrayEquals(key1, key1Again)).toBe(true);
135136
});
136137

137138
test('should perform envelope encryption with mobile manager', async () => {
@@ -143,8 +144,8 @@ describe('Cross-Platform Tests', () => {
143144
const profilePks = [Buffer.alloc(65, 1)];
144145
const encrypted = keys.mobileEncryptWithEnvelope(data, 'test-network', profilePks);
145146

146-
expect(Buffer.isBuffer(encrypted)).toBe(true);
147-
expect(encrypted.equals(data)).toBe(false);
147+
expect(encrypted instanceof Uint8Array).toBe(true);
148+
expect(uint8ArrayEquals(encrypted, data)).toBe(false);
148149
}, 10000);
149150

150151
test('should perform envelope encryption with node manager', () => {
@@ -155,8 +156,8 @@ describe('Cross-Platform Tests', () => {
155156
const profilePks = [Buffer.alloc(65, 1)];
156157
const encrypted = keys.nodeEncryptWithEnvelope(data, 'test-network', profilePks);
157158

158-
expect(Buffer.isBuffer(encrypted)).toBe(true);
159-
expect(encrypted.equals(data)).toBe(false);
159+
expect(encrypted instanceof Uint8Array).toBe(true);
160+
expect(uint8ArrayEquals(encrypted, data)).toBe(false);
160161
});
161162
});
162163

@@ -199,10 +200,10 @@ describe('Cross-Platform Tests', () => {
199200
// Test with large data
200201
const largeData = Buffer.alloc(1024 * 1024, 0x42); // 1MB
201202
const encrypted = keys.encryptLocalData(largeData);
202-
expect(Buffer.isBuffer(encrypted)).toBe(true);
203+
expect(encrypted instanceof Uint8Array).toBe(true);
203204

204205
const decrypted = keys.decryptLocalData(encrypted);
205-
expect(decrypted.equals(largeData)).toBe(true);
206+
expect(uint8ArrayEquals(decrypted, largeData)).toBe(true);
206207
});
207208
});
208209
});

runar-nodejs-api/tests/discovery_basic.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
cleanupTempDir,
55
withTimeout,
66
createMobileKeys,
7-
createNodeKeys
7+
createNodeKeys,
8+
uint8ArrayEquals
89
} from './test_utils';
910

1011
describe('Discovery Basic Tests', () => {
@@ -48,15 +49,15 @@ describe('Discovery Basic Tests', () => {
4849
const nodePk = nodeKeys.nodeGetPublicKey();
4950
const nodeAgreementPk = nodeKeys.nodeGetAgreementPublicKey();
5051

51-
expect(Buffer.isBuffer(mobilePk)).toBe(true);
52-
expect(Buffer.isBuffer(nodePk)).toBe(true);
53-
expect(Buffer.isBuffer(nodeAgreementPk)).toBe(true);
52+
expect(mobilePk instanceof Uint8Array).toBe(true);
53+
expect(nodePk instanceof Uint8Array).toBe(true);
54+
expect(nodeAgreementPk instanceof Uint8Array).toBe(true);
5455
expect(mobilePk.length).toBeGreaterThan(0);
5556
expect(nodePk.length).toBeGreaterThan(0);
5657
expect(nodeAgreementPk.length).toBeGreaterThan(0);
5758

5859
// Verify keys are different (as they should be)
59-
expect(mobilePk.equals(nodePk)).toBe(false);
60+
expect(uint8ArrayEquals(mobilePk, nodePk)).toBe(false);
6061

6162
console.log(' ✅ Basic key validation successful');
6263
}, 30000);
@@ -77,7 +78,7 @@ describe('Discovery Basic Tests', () => {
7778
expect(() => mobileKeys.mobileInstallNetworkPublicKey(testPk)).not.toThrow();
7879

7980
const networkPk = mobileKeys.mobileGetNetworkPublicKey(networkId);
80-
expect(Buffer.isBuffer(networkPk)).toBe(true);
81+
expect(networkPk instanceof Uint8Array).toBe(true);
8182
expect(networkPk.length).toBeGreaterThan(0);
8283

8384
console.log(' ✅ Network setup for discovery successful');

runar-nodejs-api/tests/keys_basic.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
cleanupTempDir,
55
withTimeout,
66
createMobileKeys,
7-
createNodeKeys
7+
createNodeKeys,
8+
uint8ArrayEquals
89
} from './test_utils';
910

1011
const mod = loadAddon();
@@ -43,14 +44,14 @@ describe('Keys Basic Tests', () => {
4344
keys.initAsNode();
4445

4546
const key1 = keys.ensureSymmetricKey('test-service');
46-
expect(Buffer.isBuffer(key1)).toBe(true);
47+
expect(key1 instanceof Uint8Array).toBe(true);
4748
expect(key1.length).toBe(32);
4849

4950
const key2 = keys.ensureSymmetricKey('test-service');
50-
expect(key1.equals(key2)).toBe(true);
51+
expect(uint8ArrayEquals(key1, key2)).toBe(true);
5152

5253
const key3 = keys.ensureSymmetricKey('different-service');
53-
expect(key1.equals(key3)).toBe(false);
54+
expect(uint8ArrayEquals(key1, key3)).toBe(false);
5455
});
5556

5657
test('should handle basic keystore capabilities', () => {

0 commit comments

Comments
 (0)