Example #1
0
        private async Task SendMessageAsync(Mocks.CloudBlobStorageProviderMock cloudStorage, Mocks.InboxHttpHandlerMock inboxMock, CryptoSettings senderCrypto, OwnEndpoint senderEndpoint, Endpoint receiverEndpoint)
        {
            Requires.NotNull(cloudStorage, "cloudStorage");
            Requires.NotNull(senderCrypto, "senderCrypto");
            Requires.NotNull(senderEndpoint, "senderEndpoint");
            Requires.NotNull(receiverEndpoint, "receiverEndpoint");

            var httpHandler = new Mocks.HttpMessageHandlerMock();

            cloudStorage.AddHttpHandler(httpHandler);

            inboxMock.Register(httpHandler);

            var sentMessage = Valid.Message;

            var channel = new Channel()
            {
                HttpClient = new HttpClient(httpHandler),
                CloudBlobStorage = cloudStorage,
                CryptoServices = senderCrypto,
                Endpoint = senderEndpoint,
                Logger = this.logger,
            };

            await channel.PostAsync(sentMessage, new[] { receiverEndpoint }, Valid.ExpirationUtc);
        }
Example #2
0
        private async Task ReceiveMessageAsync(Mocks.CloudBlobStorageProviderMock cloudStorage, Mocks.InboxHttpHandlerMock inboxMock, CryptoSettings receiverCrypto, OwnEndpoint receiverEndpoint)
        {
            Requires.NotNull(cloudStorage, "cloudStorage");
            Requires.NotNull(receiverCrypto, "receiverCrypto");
            Requires.NotNull(receiverEndpoint, "receiverEndpoint");

            var httpHandler = new Mocks.HttpMessageHandlerMock();

            cloudStorage.AddHttpHandler(httpHandler);
            inboxMock.Register(httpHandler);

            var channel = new Channel
            {
                HttpClient = new HttpClient(httpHandler),
                HttpClientLongPoll = new HttpClient(httpHandler),
                CloudBlobStorage = cloudStorage,
                CryptoServices = receiverCrypto,
                Endpoint = receiverEndpoint,
                Logger = this.logger,
            };

            var messages = await channel.ReceiveAsync();
            Assert.Equal(1, messages.Count);
            Assert.Equal(Valid.Message, messages[0].Payload);
        }
Example #3
0
 public void Ctor()
 {
     var ownContact = new OwnEndpoint(Valid.ReceivingEndpoint.SigningKey, Valid.ReceivingEndpoint.EncryptionKey);
     ownContact.PublicEndpoint.MessageReceivingEndpoint = Valid.ReceivingEndpoint.PublicEndpoint.MessageReceivingEndpoint;
     Assert.Equal(Valid.ReceivingEndpoint.PublicEndpoint, ownContact.PublicEndpoint);
     Assert.Equal(Valid.ReceivingEndpoint.EncryptionKeyPrivateMaterial, ownContact.EncryptionKeyPrivateMaterial);
     Assert.Equal(Valid.ReceivingEndpoint.SigningKeyPrivateMaterial, ownContact.SigningKeyPrivateMaterial);
 }
		public void ExtractEndpoint() {
			var ownContact = new OwnEndpoint(Valid.ReceivingEndpoint.PublicEndpoint, Valid.ReceivingEndpoint.SigningKeyPrivateMaterial, Valid.ReceivingEndpoint.EncryptionKeyPrivateMaterial);
			var cryptoServices = new Mocks.MockCryptoProvider();
			var entry = ownContact.CreateAddressBookEntry(cryptoServices);

			var endpoint = entry.ExtractEndpoint(cryptoServices);
			Assert.That(endpoint, Is.EqualTo(ownContact.PublicEndpoint));
		}
        public void ExtractEndpoint()
        {
            var ownContact = new OwnEndpoint(Valid.ReceivingEndpoint.SigningKey, Valid.ReceivingEndpoint.EncryptionKey);
            var cryptoServices = new CryptoSettings(SecurityLevel.Minimum);
            var entry = ownContact.CreateAddressBookEntry(cryptoServices);

            var endpoint = entry.ExtractEndpoint();
            Assert.Equal(ownContact.PublicEndpoint, endpoint);
        }
Example #6
0
        private async Task TestSendAndReceiveAsync(
            CryptoSettings senderCrypto, OwnEndpoint senderEndpoint, CryptoSettings receiverCrypto, OwnEndpoint receiverEndpoint)
        {
            var inboxMock = new Mocks.InboxHttpHandlerMock(new[] { receiverEndpoint.PublicEndpoint });
            var cloudStorage = new Mocks.CloudBlobStorageProviderMock();

            await this.SendMessageAsync(cloudStorage, inboxMock, senderCrypto, senderEndpoint, receiverEndpoint.PublicEndpoint);
            await this.ReceiveMessageAsync(cloudStorage, inboxMock, receiverCrypto, receiverEndpoint);
        }
Example #7
0
 private async void OpenOwnEndpoint_OnClick(object sender, RoutedEventArgs e)
 {
     this.OpenOwnEndpoint.IsEnabled = false;
     this.OpenOwnEndpoint.Cursor    = Cursors.AppStarting;
     try {
         var  dialog = new OpenFileDialog();
         bool?result = dialog.ShowDialog(this);
         if (result.HasValue && result.Value)
         {
             using (var fileStream = dialog.OpenFile()) {
                 var reader           = new BinaryReader(fileStream, Encoding.UTF8);
                 var addressBookEntry = reader.DeserializeDataContract <Uri>();
                 await this.SetEndpointAsync(await OwnEndpoint.OpenAsync(fileStream), addressBookEntry);
             }
         }
     } finally {
         this.OpenOwnEndpoint.Cursor    = Cursors.Arrow;
         this.OpenOwnEndpoint.IsEnabled = true;
     }
 }
Example #8
0
        /// <summary>
        /// Creates a new local endpoint to identify the user, or opens a previously created one.
        /// </summary>
        /// <returns>A task whose result is the local user's own endpoint.</returns>
        private async Task <OwnEndpoint> CreateOrOpenEndpointAsync()
        {
            OwnEndpoint result;

            switch (MessageBox.Show("Do you have an existing endpoint you want to open?", "Endpoint selection", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
            {
            case DialogResult.Yes:
                var openFile = new OpenFileDialog();
                if (openFile.ShowDialog() == DialogResult.Cancel)
                {
                    result = null;
                    break;
                }

                using (var fileStream = openFile.OpenFile())
                {
                    result = await OwnEndpoint.OpenAsync(fileStream);
                }

                break;

            case DialogResult.No:
                result = await this.OwnEndpointServices.CreateAsync();

                string privateFilePath = Path.GetTempFileName();
                using (var stream = File.OpenWrite(privateFilePath))
                {
                    await result.SaveAsync(stream);
                }

                Console.WriteLine("Private receiving endpoint: \"{0}\"", privateFilePath);
                break;

            default:
                result = null;
                break;
            }

            return(result);
        }
Example #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Message" /> class.
 /// </summary>
 /// <param name="author">The author.</param>
 /// <param name="recipients">The recipients.</param>
 /// <param name="subject">The subject.</param>
 /// <param name="body">The body.</param>
 public Message(OwnEndpoint author, ReadOnlyListOfEndpoint recipients, string subject, string body)
     : this(author.PublicEndpoint, recipients, subject, body)
 {
 }
Example #10
0
		private async Task ReceiveMessageAsync(Mocks.CloudBlobStorageProviderMock cloudStorage, Mocks.InboxHttpHandlerMock inboxMock, ICryptoProvider receiverCrypto, OwnEndpoint receiverEndpoint) {
			Requires.NotNull(cloudStorage, "cloudStorage");
			Requires.NotNull(receiverCrypto, "receiverCrypto");
			Requires.NotNull(receiverEndpoint, "receiverEndpoint");

			var httpHandler = new Mocks.HttpMessageHandlerMock();

			cloudStorage.AddHttpHandler(httpHandler);
			inboxMock.Register(httpHandler);

			var channel = new Channel {
				HttpClient = new HttpClient(httpHandler),
				HttpClientLongPoll = new HttpClient(httpHandler),
				CloudBlobStorage = cloudStorage,
				CryptoServices = receiverCrypto,
				Endpoint = receiverEndpoint,
				Logger = this.logger,
			};

			var messages = await channel.ReceiveAsync();
			Assert.Equal(1, messages.Count);
			Assert.Equal(Valid.Message, messages[0].Payload);
		}
Example #11
0
		private async Task TestSendAndReceiveAsync(
			ICryptoProvider senderCrypto, OwnEndpoint senderEndpoint, ICryptoProvider receiverCrypto, OwnEndpoint receiverEndpoint) {
			var inboxMock = new Mocks.InboxHttpHandlerMock(new[] { receiverEndpoint.PublicEndpoint });
			var cloudStorage = new Mocks.CloudBlobStorageProviderMock();

			await this.SendMessageAsync(cloudStorage, inboxMock, senderCrypto, senderEndpoint, receiverEndpoint.PublicEndpoint);
			await this.ReceiveMessageAsync(cloudStorage, inboxMock, receiverCrypto, receiverEndpoint);
		}
Example #12
0
 public void CreateAddressBookEntryNullInput()
 {
     var ownContact = new OwnEndpoint(Valid.ReceivingEndpoint.SigningKey, Valid.ReceivingEndpoint.EncryptionKey);
     Assert.Throws<ArgumentNullException>(() => ownContact.CreateAddressBookEntry(null));
 }
Example #13
0
		private async Task<IReadOnlyCollection<Payload>> ReceiveMessageAsync(Mocks.CloudBlobStorageProviderMock cloudBlobStorage, Mocks.InboxHttpHandlerMock inboxMock, ICryptoProvider cryptoProvider, OwnEndpoint receiver) {
			Requires.NotNull(cloudBlobStorage, "cloudBlobStorage");
			Requires.NotNull(receiver, "receiver");

			var httpHandler = new Mocks.HttpMessageHandlerMock();

			cloudBlobStorage.AddHttpHandler(httpHandler);
			inboxMock.Register(httpHandler);

			var channel = new Channel {
				HttpClient = new HttpClient(httpHandler),
				HttpClientLongPoll = new HttpClient(httpHandler),
				CloudBlobStorage = cloudBlobStorage,
				CryptoServices = cryptoProvider,
				Endpoint = receiver,
				Logger = this.logger,
			};

			var progressMessage = new TaskCompletionSource<Payload>();
			var progress = new Progress<Payload>(m => progressMessage.SetResult(m));

			var messages = await channel.ReceiveAsync(progress: progress);
			Assert.That(messages.Count, Is.EqualTo(1));
			await progressMessage.Task;
			Assert.That(progressMessage.Task.Result, Is.SameAs(messages.Single()));
			return messages;
		}
Example #14
0
        private async Task <IReadOnlyCollection <Channel.PayloadReceipt> > ReceiveMessageAsync(Mocks.CloudBlobStorageProviderMock cloudBlobStorage, Mocks.InboxHttpHandlerMock inboxMock, ICryptoProvider cryptoProvider, OwnEndpoint receiver, bool expectMessage = true)
        {
            Requires.NotNull(cloudBlobStorage, "cloudBlobStorage");
            Requires.NotNull(receiver, "receiver");

            var httpHandler = new Mocks.HttpMessageHandlerMock();

            cloudBlobStorage.AddHttpHandler(httpHandler);
            inboxMock.Register(httpHandler);

            var channel = new Channel {
                HttpClient         = new HttpClient(httpHandler),
                HttpClientLongPoll = new HttpClient(httpHandler),
                CloudBlobStorage   = cloudBlobStorage,
                CryptoServices     = cryptoProvider,
                Endpoint           = receiver,
                Logger             = this.logger,
            };

            var progressMessage = new TaskCompletionSource <Payload>();
            var progress        = new Progress <Channel.PayloadReceipt>(m => progressMessage.SetResult(m.Payload));

            var messages = await channel.ReceiveAsync(progress : progress);

            if (expectMessage)
            {
                Assert.That(messages.Count, Is.EqualTo(1));
                await progressMessage.Task;
                Assert.That(progressMessage.Task.Result, Is.SameAs(messages.Single().Payload));
            }
            else
            {
                Assert.That(messages.Count, Is.EqualTo(0));
            }

            return(messages);
        }
Example #15
0
        private async Task<IReadOnlyCollection<Channel.PayloadReceipt>> ReceiveMessageAsync(Mocks.CloudBlobStorageProviderMock cloudBlobStorage, Mocks.InboxHttpHandlerMock inboxMock, CryptoSettings cryptoProvider, OwnEndpoint receiver, bool expectMessage = true)
        {
            Requires.NotNull(cloudBlobStorage, "cloudBlobStorage");
            Requires.NotNull(receiver, "receiver");

            var httpHandler = new Mocks.HttpMessageHandlerMock();

            cloudBlobStorage.AddHttpHandler(httpHandler);
            inboxMock.Register(httpHandler);

            var channel = new Channel
            {
                HttpClient = new HttpClient(httpHandler),
                HttpClientLongPoll = new HttpClient(httpHandler),
                CloudBlobStorage = cloudBlobStorage,
                CryptoServices = cryptoProvider,
                Endpoint = receiver,
                Logger = this.logger,
            };

            var progressMessage = new TaskCompletionSource<Payload>();
            var progress = new Progress<Channel.PayloadReceipt>(m => progressMessage.SetResult(m.Payload));

            var messages = await channel.ReceiveAsync(progress: progress);
            if (expectMessage)
            {
                Assert.Equal(1, messages.Count);
                await progressMessage.Task;
                Assert.Same(progressMessage.Task.Result, messages.Single().Payload);
            }
            else
            {
                Assert.Equal(0, messages.Count);
            }

            return messages;
        }
Example #16
0
		public void Ctor() {
			var ownContact = new OwnEndpoint(Valid.ReceivingEndpoint.PublicEndpoint, Valid.ReceivingEndpoint.SigningKeyPrivateMaterial, Valid.ReceivingEndpoint.EncryptionKeyPrivateMaterial);
			Assert.That(ownContact.PublicEndpoint, Is.SameAs(Valid.ReceivingEndpoint.PublicEndpoint));
			Assert.That(ownContact.EncryptionKeyPrivateMaterial, Is.SameAs(Valid.ReceivingEndpoint.EncryptionKeyPrivateMaterial));
			Assert.That(ownContact.SigningKeyPrivateMaterial, Is.SameAs(Valid.ReceivingEndpoint.SigningKeyPrivateMaterial));
		}
Example #17
0
        private async Task SendMessageAsync(Mocks.CloudBlobStorageProviderMock cloudStorage, Mocks.InboxHttpHandlerMock inboxMock, CryptoSettings senderCrypto, OwnEndpoint senderEndpoint, Endpoint receiverEndpoint)
        {
            Requires.NotNull(cloudStorage, "cloudStorage");
            Requires.NotNull(senderCrypto, "senderCrypto");
            Requires.NotNull(senderEndpoint, "senderEndpoint");
            Requires.NotNull(receiverEndpoint, "receiverEndpoint");

            var httpHandler = new Mocks.HttpMessageHandlerMock();

            cloudStorage.AddHttpHandler(httpHandler);

            inboxMock.Register(httpHandler);

            var sentMessage = Valid.Message;

            var channel = new Channel()
            {
                HttpClient       = new HttpClient(httpHandler),
                CloudBlobStorage = cloudStorage,
                CryptoServices   = senderCrypto,
                Endpoint         = senderEndpoint,
                Logger           = this.logger,
            };

            await channel.PostAsync(sentMessage, new[] { receiverEndpoint }, Valid.ExpirationUtc);
        }
Example #18
0
        private async Task SendMessageAsync(Mocks.CloudBlobStorageProviderMock cloudBlobStorage, Mocks.InboxHttpHandlerMock inboxMock, ICryptoProvider cryptoProvider, OwnEndpoint sender, Endpoint receiver, Payload message)
        {
            Requires.NotNull(cloudBlobStorage, "cloudBlobStorage");
            Requires.NotNull(sender, "sender");
            Requires.NotNull(message, "message");

            var httpHandler = new Mocks.HttpMessageHandlerMock();

            cloudBlobStorage.AddHttpHandler(httpHandler);
            inboxMock.Register(httpHandler);

            var channel = new Channel()
            {
                HttpClient       = new HttpClient(httpHandler),
                CloudBlobStorage = cloudBlobStorage,
                CryptoServices   = cryptoProvider,
                Endpoint         = sender,
                Logger           = this.logger,
            };

            await channel.PostAsync(Valid.Message, new[] { receiver }, Valid.ExpirationUtc);
        }
Example #19
0
 private async Task SetEndpointAsync(OwnEndpoint endpoint, Uri addressBookEntry, CancellationToken cancellationToken = default(CancellationToken))
 {
     this.Channel.Endpoint = endpoint;
     this.PublicEndpointUrlTextBlock.Text = addressBookEntry.AbsoluteUri;
     this.OpenChatroom.IsEnabled          = true;
 }
Example #20
0
        public void CreateAddressBookEntryNullInput()
        {
            var ownContact = new OwnEndpoint(Valid.ReceivingEndpoint.PublicEndpoint, Valid.ReceivingEndpoint.SigningKeyPrivateMaterial, Valid.ReceivingEndpoint.EncryptionKeyPrivateMaterial);

            Assert.Throws <ArgumentNullException>(() => ownContact.CreateAddressBookEntry(null));
        }
Example #21
0
		private async Task SendMessageAsync(Mocks.CloudBlobStorageProviderMock cloudBlobStorage, Mocks.InboxHttpHandlerMock inboxMock, ICryptoProvider cryptoProvider, OwnEndpoint sender, Endpoint receiver, Payload message) {
			Requires.NotNull(cloudBlobStorage, "cloudBlobStorage");
			Requires.NotNull(sender, "sender");
			Requires.NotNull(message, "message");

			var httpHandler = new Mocks.HttpMessageHandlerMock();

			cloudBlobStorage.AddHttpHandler(httpHandler);
			inboxMock.Register(httpHandler);

			var channel = new Channel() {
				HttpClient = new HttpClient(httpHandler),
				CloudBlobStorage = cloudBlobStorage,
				CryptoServices = cryptoProvider,
				Endpoint = sender,
				Logger = this.logger,
			};

			await channel.PostAsync(Valid.Message, new[] { receiver }, Valid.ExpirationUtc);
		}