public void TestInvalidDatabaseNames([Values("Foo", "1database", "", "foo;", TooLongName)] String testName)
        {
            // Arrange.
            // Act.
            var result = Manager.IsValidDatabaseName(testName);

            // Assert.
            Assert.IsFalse(result);
        }
Пример #2
0
        public void TestValidDatabaseNames([Values("foo", "try1", "foo-bar", "goofball99", TooLongName)] String testName)
        {
            // Arrange.
            // Act.
            if (testName.Length == 240) {
                testName = testName.Trim('0');
            }
            var result = Manager.IsValidDatabaseName(testName);

            // Assert.
            Assert.IsTrue(result);
        }
Пример #3
0
        private Status ParseReplicationProperties(IDictionary <string, object> properties, out bool isPush, out bool createTarget, IDictionary <string, object> results)
        {
            // http://wiki.apache.org/couchdb/Replication
            isPush       = false;
            createTarget = false;
            var sourceDict = ParseSourceOrTarget(properties, "source");
            var targetDict = ParseSourceOrTarget(properties, "target");
            var source     = sourceDict.GetCast <string>("url");
            var target     = targetDict.GetCast <string>("url");

            if (source == null || target == null)
            {
                return(new Status(StatusCode.BadRequest));
            }

            createTarget = properties.GetCast <bool>("create_target", false);
            IDictionary <string, object> remoteDict = null;
            bool targetIsLocal = Manager.IsValidDatabaseName(target);

            if (Manager.IsValidDatabaseName(source))
            {
                //Push replication
                if (targetIsLocal)
                {
                    // This is a local-to-local replication. It is not supported on .NET.
                    return(new Status(StatusCode.NotImplemented));
                }

                remoteDict = targetDict;
                if (results.ContainsKey("database"))
                {
                    results["database"] = GetExistingDatabase(source);
                }

                isPush = true;
            }
            else if (targetIsLocal)
            {
                //Pull replication
                remoteDict = sourceDict;
                if (results.ContainsKey("database"))
                {
                    Database db;
                    if (createTarget)
                    {
                        db = GetDatabase(target);
                        if (db == null)
                        {
                            return(new Status(StatusCode.DbError));
                        }
                    }
                    else
                    {
                        db = GetExistingDatabase(target);
                    }
                    results["database"] = db;
                }
            }
            else
            {
                return(new Status(StatusCode.BadId));
            }

            Uri remote;

            if (!Uri.TryCreate(remoteDict.GetCast <string>("url"), UriKind.Absolute, out remote))
            {
                Log.To.Router.W(TAG, "Unparseable replication URL <{0}> received", remoteDict.GetCast <string>("url"));
                return(new Status(StatusCode.BadRequest));
            }

            if (!remote.Scheme.Equals("http") && !remote.Scheme.Equals("https") && !remote.Scheme.Equals("ws") && !remote.Scheme.Equals("wss"))
            {
                Log.To.Router.W(TAG, "Replication URL <{0}> has unsupported scheme", remote);
                return(new Status(StatusCode.BadRequest));
            }

            var split = remote.PathAndQuery.Split('?');

            if (split.Length != 1)
            {
                Log.To.Router.W(TAG, "Replication URL <{0}> must not contain a query", remote);
                return(new Status(StatusCode.BadRequest));
            }

            var path = split[0];

            if (String.IsNullOrEmpty(path) || path == "/")
            {
                Log.To.Router.W(TAG, "Replication URL <{0}> missing database name", remote);
                return(new Status(StatusCode.BadRequest));
            }

            var database = results.Get("database");

            if (database == null)
            {
                return(new Status(StatusCode.NotFound));
            }

            if (results.ContainsKey("remote"))
            {
                results["remote"] = remote;
            }

            if (results.ContainsKey("headers"))
            {
                results["headers"] = remoteDict.Get("headers") ?? new Dictionary <string, string>();
            }

            if (results.ContainsKey("authorizer"))
            {
                var auth = remoteDict.Get("auth") as IDictionary <string, object>;
                if (auth != null)
                {
                    var persona  = auth.Get("persona") as IDictionary <string, object>;
                    var facebook = auth.Get("facebook") as IDictionary <string, object>;
                    if (persona != null)
                    {
                        string email = persona.Get("email") as string;
                        results["authorizer"] = new PersonaAuthorizer(email);
                    }
                    else if (facebook != null)
                    {
                        string email = facebook.Get("email") as string;
                        results["authorizer"] = new FacebookAuthorizer(email);
                    }
                    else
                    {
                        Log.To.Sync.W(TAG, "Invalid authorizer settings {0}",
                                      new SecureLogJsonString(auth, LogMessageSensitivity.Insecure));
                    }
                }
            }

            // Can't specify both a filter and doc IDs
            if (properties.ContainsKey("filter") && properties.ContainsKey("doc_ids"))
            {
                return(new Status(StatusCode.BadRequest));
            }

            return(new Status(StatusCode.Ok));
        }
Пример #4
0
        private Status ParseReplicationProperties(IDictionary <string, object> properties, out bool isPush, out bool createTarget,
                                                  IDictionary <string, object> results)
        {
            // http://wiki.apache.org/couchdb/Replication
            isPush       = false;
            createTarget = false;

            var sourceDict = ParseSourceOrTarget(properties, "source");
            var targetDict = ParseSourceOrTarget(properties, "target");
            var source     = sourceDict.Get("url") as string;
            var target     = targetDict.Get("url") as string;

            if (source == null || target == null)
            {
                return(new Status(StatusCode.BadRequest));
            }

            createTarget = properties.Get("create_target") is bool && (bool)properties.Get("create_target");

            IDictionary <string, object> remoteDict = null;
            bool targetIsLocal = Manager.IsValidDatabaseName(target);

            if (Manager.IsValidDatabaseName(source))
            {
                //Push replication
                if (targetIsLocal)
                {
                    // This is a local-to-local replication. Turn the remote into a full URL to keep the
                    // replicator happy:
                    Database targetDb;
                    if (createTarget)
                    {
                        targetDb = Manager.SharedInstance.GetDatabase(target);
                    }
                    else
                    {
                        targetDb = Manager.SharedInstance.GetExistingDatabase(target);
                    }

                    if (targetDb == null)
                    {
                        return(new Status(StatusCode.BadRequest));
                    }

                    targetDict["url"] = "http://localhost:20000" + targetDb.Path;
                }

                remoteDict = targetDict;
                if (results.ContainsKey("database"))
                {
                    results["database"] = GetExistingDatabase(source);
                }

                isPush = true;
            }
            else if (targetIsLocal)
            {
                //Pull replication
                remoteDict = sourceDict;
                if (results.ContainsKey("database"))
                {
                    Database db;
                    if (createTarget)
                    {
                        db = GetDatabase(target);
                        if (db == null)
                        {
                            return(new Status(StatusCode.DbError));
                        }
                    }
                    else
                    {
                        db = GetExistingDatabase(target);
                    }
                    results["database"] = db;
                }
            }
            else
            {
                return(new Status(StatusCode.BadId));
            }

            Uri remote = new Uri(remoteDict["url"] as string);

            if (!remote.Scheme.Equals("http") && !remote.Scheme.Equals("https") && !remote.Scheme.Equals("cbl"))
            {
                return(new Status(StatusCode.BadRequest));
            }

            var database = results.Get("database");

            if (database == null)
            {
                return(new Status(StatusCode.NotFound));
            }

            if (results.ContainsKey("remote"))
            {
                results["remote"] = remote;
            }

            if (results.ContainsKey("headers"))
            {
                results["headers"] = remoteDict.Get("headers");
            }

            if (results.ContainsKey("authorizer"))
            {
                var auth = remoteDict.Get("auth") as IDictionary <string, object>;
                if (auth != null)
                {
                    //var oauth = auth["oauth"] as IDictionary<string, object>;
                    var persona  = auth.Get("persona") as IDictionary <string, object>;
                    var facebook = auth.Get("facebook") as IDictionary <string, object>;
                    //TODO: OAuth

                    /*if (oauth != null) {
                     *  string consumerKey = oauth.Get("consumer_key") as string;
                     *  string consumerSec = oauth.Get("consumer_secret") as string;
                     *  string token = oauth.Get("token") as string;
                     *  string tokenSec = oauth.Get("token_secret") as string;
                     *  string sigMethod = oauth.Get("signature_method") as string;
                     *  results["authorizer"] =
                     * }*/
                    if (persona != null)
                    {
                        string email = persona.Get("email") as string;
                        results["authorizer"] = new PersonaAuthorizer(email);
                    }
                    else if (facebook != null)
                    {
                        string email = facebook.Get("email") as string;
                        results["authorizer"] = new FacebookAuthorizer(email);
                    }
                    else
                    {
                        Log.W(TAG, "Invalid authorizer settings {0}", auth);
                    }
                }
            }

            // Can't specify both a filter and doc IDs
            if (properties.ContainsKey("filter") && properties.ContainsKey("doc_ids"))
            {
                return(new Status(StatusCode.BadRequest));
            }

            return(new Status(StatusCode.Ok));
        }