public void SyncConfiguration_LoggerFactory_Test(LogLevel logLevel)
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var logBuilder = new StringBuilder();

                SyncConfigurationBase.CustomLogger = (message, level) =>
                {
                    logBuilder.AppendLine($"[{level}] {message}");
                };
                SyncConfigurationBase.LogLevel = logLevel;

                var config = await SyncTestHelpers.GetIntegrationConfigAsync(Guid.NewGuid().ToString());
                using (var realm = await GetRealmAsync(config))
                {
                    realm.Write(() =>
                    {
                        realm.Add(new Person());
                    });

                    await SyncTestHelpers.WaitForUploadAsync(realm);
                }

                var log = logBuilder.ToString();

                Assert.That(log, Does.Contain($"[{logLevel}]"));
                Assert.That(log, Does.Not.Contain($"[{(logLevel - 1)}]"));
            });
        }
        private async Task <Realm> GetQueryBasedRealm(bool openAsync, [CallerMemberName] string realmPath = null)
        {
            var user = await SyncTestHelpers.GetUserAsync();

            var config = new QueryBasedSyncConfiguration(SyncTestHelpers.RealmUri($"~/{realmPath}_{openAsync}"), user, Guid.NewGuid().ToString())
            {
                ObjectClasses = new[] { typeof(ObjectA), typeof(ObjectB), typeof(ObjectC) }
            };

            using (var original = GetRealm(config))
            {
                original.Write(() =>
                {
                    for (var i = 0; i < 10; i++)
                    {
                        var a = original.Add(new ObjectA
                        {
                            StringValue = "A #" + i,
                            IntValue    = i,
                            B           = new ObjectB
                            {
                                StringValue = "B #" + i,
                                BoolValue   = i % 2 == 0,
                            }
                        });

                        original.Add(new ObjectC
                        {
                            IntValue = i,
                            B        = a.B
                        });
                    }
                });

                await SyncTestHelpers.WaitForUploadAsync(original);
            }

            try
            {
                Realm.DeleteRealm(config);
            }
            catch
            {
            }

            config = new QueryBasedSyncConfiguration(config.ServerUri, config.User, config.DatabasePath + "_partial")
            {
                ObjectClasses = config.ObjectClasses
            };

            // We test both `GetInstance` and `GetInstanceAsync` to guard against regressions:
            // https://github.com/realm/realm-dotnet/issues/1814
            var result = await GetRealmAsync(config, openAsync).Timeout(5000);

            Assert.That(result.All <ObjectB>().Count(), Is.EqualTo(0));
            Assert.That(result.All <ObjectA>().Count(), Is.EqualTo(0));

            return(result);
        }
Exemple #3
0
        public void WriteToReadOnlyRealm_ThrowsPermissionDenied()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var alice = await SyncTestHelpers.GetUserAsync();
                var bob   = await SyncTestHelpers.GetUserAsync();

                var realmPath = $"/{alice.Identity}/willBeReadonly";
                var realmUrl  = SyncTestHelpers.RealmUri(realmPath).AbsoluteUri;
                await EnsureRealmExists(alice, realmUrl);

                // Give Bob just read permissions
                await alice.ApplyPermissionsAsync(PermissionCondition.UserId(bob.Identity), realmUrl, AccessLevel.Read);

                var config = new FullSyncConfiguration(new Uri(realmUrl), bob, Guid.NewGuid().ToString());

                var sessionErrorTask = TestHelpers.EventToTask <ErrorEventArgs>(h => Session.Error += h, h => Session.Error -= h);

                using (var realm = GetRealm(config))
                {
                    realm.Write(() => realm.Add(new Person()));

                    try
                    {
                        // Sometimes PermissionDenied will be thrown too fast moving the session to an error state
                        await SyncTestHelpers.WaitForUploadAsync(realm).Timeout(1000);
                    }
                    catch
                    {
                    }
                }

                var sessionError = await sessionErrorTask.Timeout(1000);
                Assert.That(sessionError.Exception, Is.TypeOf <PermissionDeniedException>());

                var pde = (PermissionDeniedException)sessionError.Exception;

                Assert.That(pde.ErrorCode, Is.EqualTo(ErrorCode.PermissionDenied));
                Assert.That(File.Exists(config.DatabasePath), Is.True);

                var result = pde.DeleteRealmUserInfo();

                Assert.That(result, Is.True);
                Assert.That(File.Exists(config.DatabasePath), Is.False);
            });
        }
        public void WhenObjectHasPK_ShouldNotCreateDuplicates(Type objectType, object pkValue, Func <dynamic, bool> pkValueChecker)
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var pkProperty = objectType.GetProperties(BindingFlags.Instance | BindingFlags.Public)
                                 .Single(p => p.GetCustomAttribute <PrimaryKeyAttribute>() != null);

                for (var i = 0; i < 5; i++)
                {
                    var instance = (RealmObject)Activator.CreateInstance(objectType);

                    pkProperty.SetValue(instance, (dynamic)pkValue);

                    using (var realm = await GetSyncedRealm(objectType))
                    {
                        try
                        {
                            realm.Write(() => realm.Add(instance));
                        }
                        catch (RealmDuplicatePrimaryKeyValueException)
                        {
                            // Sync went through too quickly (that's why we do 5 attempts)
                        }

                        await SyncTestHelpers.WaitForUploadAsync(realm);
                    }
                }

                using (var realm = await GetSyncedRealm(objectType))
                {
                    await SyncTestHelpers.WaitForDownloadAsync(realm);
                    var allObjects = realm.All(objectType.Name).ToArray();

                    Assert.That(allObjects.Count(pkValueChecker), Is.EqualTo(1));
                }
            });
        }
        public void GetInstanceAsync_ShouldDownloadRealm(bool singleTransaction)
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var user = await SyncTestHelpers.GetUserAsync();

                var realmUri = SyncTestHelpers.RealmUri("~/GetInstanceAsync_ShouldDownloadRealm");

                var config      = new FullSyncConfiguration(realmUri, user, Guid.NewGuid().ToString());
                var asyncConfig = new FullSyncConfiguration(realmUri, user, config.DatabasePath + "_async");

                using (var realm = GetRealm(config))
                {
                    AddDummyData(realm, singleTransaction);

                    await SyncTestHelpers.WaitForUploadAsync(realm);
                }

                using (var asyncRealm = await GetRealmAsync(asyncConfig))
                {
                    Assert.That(asyncRealm.All <IntPrimaryKeyWithValueObject>().Count(), Is.EqualTo(500));
                }
            });
        }
        private void TestSSLCore(Action <FullSyncConfiguration> setupSecureConfig, bool openAsync)
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var user          = await SyncTestHelpers.GetUserAsync();
                const string path = "~/TestSSLCore";
                var realmUri      = SyncTestHelpers.RealmUri(path);
                var config        = new FullSyncConfiguration(realmUri, user);

                var secureRealmUri = SyncTestHelpers.SecureRealmUri(path);
                var secureConfig   = new FullSyncConfiguration(secureRealmUri, user, config.DatabasePath + "2");
                setupSecureConfig(secureConfig);

                using (var realm = GetRealm(config))
                {
                    realm.Write(() =>
                    {
                        realm.Add(new IntPrimaryKeyWithValueObject
                        {
                            Id          = 1,
                            StringValue = "some value"
                        });
                    });

                    await SyncTestHelpers.WaitForUploadAsync(realm);
                }

                using (var newRealm = await GetRealmAsync(secureConfig, openAsync))
                {
                    var items = newRealm.All <IntPrimaryKeyWithValueObject>();

                    Assert.That(items.Count(), Is.EqualTo(1));
                    Assert.That(items.Single().StringValue, Is.EqualTo("some value"));
                }
            });
        }