realm-js/tests/js/encryption-tests.js

91 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-02-26 19:08:59 +00:00
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
'use strict';
var Realm = require('realm');
var TestCase = require('./asserts');
var Schemas = require('./schemas');
module.exports = {
2016-02-26 19:08:59 +00:00
testEncryptedInvalidKeys: function() {
// test failure with invalid keys
TestCase.assertThrows(function() {
new Realm({schema: [Schemas.TestObject], encryptionKey: " ".repeat(64)});
}, "Encryption Key must be an ArrayBuffer");
TestCase.assertThrows(function() {
new Realm({schema: [Schemas.TestObject], encryptionKey: new Int8Array(63)});
2016-03-17 22:07:15 +00:00
}, "Encryption Key must be 64 bytes");
2016-02-26 19:08:59 +00:00
},
testEncryptionValidKey: function() {
var key = new Int8Array(64);
key[0] = 1;
var realm = new Realm({schema: [Schemas.TestObject], encryptionKey: key});
realm.write(function() {
realm.create('TestObject', {doubleCol: 1});
TestCase.assertEqual(realm.objects('TestObject').length, 1);
});
// test failure with different or missing
realm.close();
TestCase.assertThrows(function() {
new Realm({schema: [Schemas.TestObject], encryptionKey: new Int8Array(64)});
});
TestCase.assertThrows(function() {
new Realm({schema: [Schemas.TestObject]});
});
// test can reopen with original key
2016-05-20 23:33:16 +00:00
realm = new Realm({schema: [Schemas.TestObject], encryptionKey: key});
2016-02-26 19:08:59 +00:00
TestCase.assertEqual(realm.objects('TestObject').length, 1);
},
testRealmSchemaVersion: function() {
var encryptionKey = new Int8Array(64);
var realm = new Realm({schema: [], schemaVersion: 3, path: 'encrypted.realm', encryptionKey: encryptionKey});
TestCase.assertEqual(realm.schemaVersion, 3);
TestCase.assertEqual(Realm.schemaVersion('encrypted.realm', encryptionKey), 3);
TestCase.assertThrows(function() {
Realm.schemaVersion('encrypted.realm', encryptionKey, 'extra');
});
TestCase.assertThrows(function() {
Realm.schemaVersion('encrypted.realm', 'asdf');
});
},
2017-10-16 18:47:35 +00:00
testEncryptionWithSync: function() {
if (!global.enableSyncTests) {
return Promise.resolve();
}
2017-10-13 16:12:30 +00:00
const credentials = Realm.Sync.Credentials.usernamePassword('realm-admin', '');
return Realm.Sync.User.login('http://localhost:9080', credentials).then(adminUser => {
2017-10-13 16:12:30 +00:00
new Realm({
encryptionKey: new Int8Array(64),
sync: {
2017-10-16 18:47:35 +00:00
user: adminUser,
2017-10-13 16:12:30 +00:00
url: 'realm://localhost:9080'
}
});
2017-10-16 18:47:35 +00:00
adminUser.logout(); // FIXME: clearTestState() doesn't clean up enough and Realm.Sync.User.current might not work
});
2017-10-13 16:12:30 +00:00
}
2017-10-16 18:47:35 +00:00
};