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

61 lines
2.2 KiB
JavaScript

////////////////////////////////////////////////////////////////////////////
//
// 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 BaseTest = require('./base-test');
var TestCase = require('./asserts');
var Schemas = require('./schemas');
module.exports = BaseTest.extend({
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)});
}, "Encryption Key must be 64 bytes");
},
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
var realm = new Realm({schema: [Schemas.TestObject], encryptionKey: key});
TestCase.assertEqual(realm.objects('TestObject').length, 1);
},
});