Merge pull request #718 from realm/kd/test-closed-realm-protection

Add tests for closed-realm access protection
This commit is contained in:
Ari Lazier 2016-12-07 16:44:35 +01:00 committed by GitHub
commit 87260a42c9
4 changed files with 55 additions and 2 deletions

View File

@ -37,7 +37,7 @@ LOCAL_SRC_FILES += src/object-store/src/impl/realm_coordinator.cpp
LOCAL_SRC_FILES += src/object-store/src/impl/results_notifier.cpp
LOCAL_SRC_FILES += src/object-store/src/impl/transact_log_handler.cpp
LOCAL_SRC_FILES += src/object-store/src/impl/weak_realm_notifier.cpp
LOCAL_SRC_FILES += src/object-store/src/impl/android/external_commit_helper.cpp
LOCAL_SRC_FILES += src/object-store/src/impl/epoll/external_commit_helper.cpp
LOCAL_SRC_FILES += src/object-store/src/parser/parser.cpp
LOCAL_SRC_FILES += src/object-store/src/parser/query_builder.cpp
LOCAL_SRC_FILES += src/object-store/src/util/format.cpp

View File

@ -638,6 +638,9 @@ void RealmClass<T>::add_listener(ContextType ctx, ObjectType this_object, size_t
auto callback = Value::validated_to_function(ctx, arguments[1]);
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
if (realm->is_closed()) {
throw ClosedRealmException();
}
get_delegate<T>(realm.get())->add_notification(callback);
}
@ -649,6 +652,9 @@ void RealmClass<T>::remove_listener(ContextType ctx, ObjectType this_object, siz
auto callback = Value::validated_to_function(ctx, arguments[1]);
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
if (realm->is_closed()) {
throw ClosedRealmException();
}
get_delegate<T>(realm.get())->remove_notification(callback);
}
@ -660,6 +666,9 @@ void RealmClass<T>::remove_all_listeners(ContextType ctx, ObjectType this_object
}
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
if (realm->is_closed()) {
throw ClosedRealmException();
}
get_delegate<T>(realm.get())->remove_all_notifications();
}

@ -1 +1 @@
Subproject commit 5904d3966d724d4b82b1f62f5cf8a53f6391db52
Subproject commit 5ba86e730071733997a9275d19dab801e1f3b851

View File

@ -654,6 +654,50 @@ module.exports = {
TestCase.assertThrows(function() {
realm.objects(InvalidPerson);
});
var person = realm.objects('PersonObject')[0];
var listenerCallback = () => {};
realm.addListener('change', listenerCallback);
// The tests below assert that everthing throws when
// operating on a closed realm
realm.close();
TestCase.assertThrows(function() {
console.log("Name: ", person.name);
});
TestCase.assertThrows(function() {
realm.objects('PersonObject');
});
TestCase.assertThrows(function() {
realm.addListener('change', () => {});
});
TestCase.assertThrows(function() {
realm.create('PersonObject', {name: 'Ari', age: 10});
});
TestCase.assertThrows(function() {
realm.delete(person);
});
TestCase.assertThrows(function() {
realm.deleteAll();
});
TestCase.assertThrows(function() {
realm.write(() => {});
});
TestCase.assertThrows(function() {
realm.removeListener('change', listenerCallback);
});
TestCase.assertThrows(function() {
realm.removeAllListeners();
});
},
testRealmObjectForPrimaryKey: function() {