public static void Main(string[] args)
        {
            CouchbaseLiteServiceListener listener = new CouchbaseLiteTcpListener(Manager.SharedInstance, port);
            listener.Start();

            Console.ReadKey(true);
            listener.Stop();

        }
        public static void Main(string[] args)
        {
            CouchbaseLiteServiceListener listener = new CouchbaseLiteTcpListener(Manager.SharedInstance, port);
            listener.SetPasswords(new Dictionary<string, string> { { "jim", "borden" } });
            listener.Start();

            Console.ReadKey(true);
            listener.Stop();

        }
        static void Main(string[] args)
        {
            CouchbaseLiteServiceListener listener = new CouchbaseLiteTcpListener(Manager.SharedInstance, listenerPort);
            listener.Start();

            Console.WriteLine("LISTENING...");

            // sync workaround
            var syncManager = new SyncManager("sync-config.json");
            syncManager.StartReplications();

            Console.ReadKey(true);
            listener.Stop();
        }
        public void TestReplicateWithListener()
        {
            var listener = new CouchbaseLiteTcpListener(manager, 59840);
            listener.Start();

            var dbCopy = EnsureEmptyDatabase("replicate_end");
  
            var push = database.CreatePushReplication(new Uri("http://localhost:59840/replicate_end"));
            CreateDocuments(database, 20);
            var attachDoc = CreateDocWithAttachment(database, "attachment.png", "image/png");
            var attachDocId = attachDoc.Id;
            RunReplication(push);

            Assert.IsNull(push.LastError);
            Assert.AreEqual(21, dbCopy.DocumentCount);

            attachDoc = dbCopy.GetExistingDocument(attachDocId);
            Assert.IsNotNull(attachDoc, "Failed to store doc with attachment");
            Assert.IsNotNull(attachDoc.CurrentRevision.Attachments, "Failed to store attachments on attachment doc");

            var name = database.Name;
            database.Close();
            database = EnsureEmptyDatabase(name);

            var pull = database.CreatePullReplication(new Uri("http://localhost:59840/replicate_end"));
            RunReplication(pull);
            Assert.IsNull(pull.LastError);
            Assert.AreEqual(21, database.DocumentCount);

            attachDoc = database.GetExistingDocument(attachDocId);
            Assert.IsNotNull(attachDoc, "Failed to retrieve doc with attachment");
            Assert.IsNotNull(attachDoc.CurrentRevision.Attachments, "Failed to retrieve attachments on attachment doc");

            // Now with auth
            // Digest
            listener.SetPasswords(new Dictionary<string, string> { { "jim", "borden" } });
            dbCopy = EnsureEmptyDatabase("replicate_end");
            push = database.CreatePushReplication(new Uri("http://localhost:59840/replicate_end"));
            push.Authenticator = new DigestAuthenticator("jim", "borden");
            RunReplication(push);

            Assert.IsNull(push.LastError);
            Assert.AreEqual(21, dbCopy.DocumentCount);

            attachDoc = dbCopy.GetExistingDocument(attachDocId);
            Assert.IsNotNull(attachDoc, "Failed to store doc with attachment");
            Assert.IsNotNull(attachDoc.CurrentRevision.Attachments, "Failed to store attachments on attachment doc");

            name = database.Name;
            database.Close();
            database = EnsureEmptyDatabase(name);

            pull = database.CreatePullReplication(new Uri("http://localhost:59840/replicate_end"));
            pull.Authenticator = push.Authenticator;
            RunReplication(pull);
            Assert.IsNull(pull.LastError);
            Assert.AreEqual(21, database.DocumentCount);

            attachDoc = database.GetExistingDocument(attachDocId);
            Assert.IsNotNull(attachDoc, "Failed to retrieve doc with attachment");
            Assert.IsNotNull(attachDoc.CurrentRevision.Attachments, "Failed to retrieve attachments on attachment doc");

            // and now Basic
            listener.Stop();
            listener = new CouchbaseLiteTcpListener(manager, 59840, CouchbaseLiteTcpOptions.AllowBasicAuth);
            listener.SetPasswords(new Dictionary<string, string> { { "jim", "borden" } });
            listener.Start();

            dbCopy = EnsureEmptyDatabase("replicate_end");
            push = database.CreatePushReplication(new Uri("http://localhost:59840/replicate_end"));
            push.Authenticator = new BasicAuthenticator("jim", "borden");
            RunReplication(push);

            Assert.IsNull(push.LastError);
            Assert.AreEqual(21, dbCopy.DocumentCount);

            attachDoc = dbCopy.GetExistingDocument(attachDocId);
            Assert.IsNotNull(attachDoc, "Failed to store doc with attachment");
            Assert.IsNotNull(attachDoc.CurrentRevision.Attachments, "Failed to store attachments on attachment doc");

            name = database.Name;
            database.Close();
            database = EnsureEmptyDatabase(name);

            pull = database.CreatePullReplication(new Uri("http://localhost:59840/replicate_end"));
            pull.Authenticator = push.Authenticator;
            RunReplication(pull);
            Assert.IsNull(pull.LastError);
            Assert.AreEqual(21, database.DocumentCount);

            attachDoc = database.GetExistingDocument(attachDocId);
            Assert.IsNotNull(attachDoc, "Failed to retrieve doc with attachment");
            Assert.IsNotNull(attachDoc.CurrentRevision.Attachments, "Failed to retrieve attachments on attachment doc");
        }
        public void TestReplicateWithListener()
        {
            var listener = new CouchbaseLiteTcpListener(manager, 59840);
            listener.Start();

            var dbCopy = EnsureEmptyDatabase("replicate_end");
  
            var push = database.CreatePushReplication(new Uri("http://localhost:59840/replicate_end"));
            CreateDocuments(database, 20);
            var attachDoc = CreateDocWithAttachment(database, "attachment.png", "image/png");
            var attachDocId = attachDoc.Id;
            RunReplication(push);

            Assert.IsNull(push.LastError);
            Assert.AreEqual(21, dbCopy.DocumentCount);

            attachDoc = dbCopy.GetExistingDocument(attachDocId);
            Assert.IsNotNull(attachDoc, "Failed to store doc with attachment");
            Assert.IsNotNull(attachDoc.CurrentRevision.Attachments, "Failed to store attachments on attachment doc");

            var name = database.Name;
            database.Close();
            database = EnsureEmptyDatabase(name);

            var pull = database.CreatePullReplication(new Uri("http://localhost:59840/replicate_end"));
            RunReplication(pull);
            Assert.IsNull(pull.LastError);
            Assert.AreEqual(21, database.DocumentCount);

            attachDoc = database.GetExistingDocument(attachDocId);
            Assert.IsNotNull(attachDoc, "Failed to retrieve doc with attachment");
            Assert.IsNotNull(attachDoc.CurrentRevision.Attachments, "Failed to retrieve attachments on attachment doc");

            listener.Stop();

            // TODO: Auth
        }
        public void TestSsl()
        {
            var cert = X509Manager.GetPersistentCertificate("127.0.0.1", "123abc", System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "unit_test.pfx"));
            var sslListener = new CouchbaseLiteTcpListener(manager, 59841, CouchbaseLiteTcpOptions.UseTLS, cert);
            sslListener.Start();

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            ServicePointManager.ServerCertificateValidationCallback = 
                (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) =>
            {
                // If the certificate is a valid, signed certificate, return true.
                if (sslPolicyErrors == SslPolicyErrors.None || sslPolicyErrors == SslPolicyErrors.RemoteCertificateNameMismatch)
                {
                    return true;
                }

                // If there are errors in the certificate chain, look at each error to determine the cause.
                if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors) != 0)
                {
                    if (chain != null && chain.ChainStatus != null)
                    {
                        foreach (X509ChainStatus status in chain.ChainStatus)
                        {
                            if ((certificate.Subject == certificate.Issuer) &&
                                (status.Status == X509ChainStatusFlags.UntrustedRoot))
                            {
                                // Self-signed certificates with an untrusted root are valid. 
                                continue;
                            }
                            else
                            {
                                if (status.Status != X509ChainStatusFlags.NoError)
                                {
                                    // If there are any other errors in the certificate chain, the certificate is invalid,
                                    // so the method returns false.
                                    return false;
                                }
                            }
                        }
                    }

                    // When processing reaches this line, the only errors in the certificate chain are 
                    // untrusted root errors for self-signed certificates. These certificates are valid
                    // for default Exchange server installations, so return true.
                    return true;
                }
                else
                {
                    // In all other cases, return false.
                    return false;
                }
            };

            try {
                var request = (HttpWebRequest)WebRequest.Create("https://127.0.0.1:59841/");
                var response = (HttpWebResponse)request.GetResponse();
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

                request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:59841/");
                Assert.Throws<WebException>(() => response = (HttpWebResponse)request.GetResponse());
            } finally {
                sslListener.Stop();
            }
        }