public async Task PusherEventEmitterPrivateEncryptedChannelTestAsync()
        {
            // Arrange
            byte[] encryptionKey = GenerateEncryptionMasterKey();
            var    pusherServer  = new PusherServer.Pusher(Config.AppId, Config.AppKey, Config.AppSecret, new PusherServer.PusherOptions
            {
                Cluster             = Config.Cluster,
                EncryptionMasterKey = encryptionKey,
            });

            ChannelTypes   channelType          = ChannelTypes.PrivateEncrypted;
            Pusher         localPusher          = PusherFactory.GetPusher(channelType: ChannelTypes.Presence, saveTo: _clients, encryptionKey: encryptionKey);
            string         testEventName        = "private-encrypted-event-test";
            AutoResetEvent globalEventReceived  = new AutoResetEvent(false);
            AutoResetEvent channelEventReceived = new AutoResetEvent(false);
            PusherEvent    globalEvent          = null;
            PusherEvent    channelEvent         = null;
            PusherEvent    pusherEvent          = CreatePusherEvent(channelType, testEventName);

            await localPusher.ConnectAsync().ConfigureAwait(false);

            Channel localChannel = await localPusher.SubscribeAsync(pusherEvent.ChannelName).ConfigureAwait(false);

            void GeneralListener(string eventName, PusherEvent eventData)
            {
                if (eventName == testEventName)
                {
                    globalEvent = eventData;
                    globalEventReceived.Set();
                }
            }

            void Listener(PusherEvent eventData)
            {
                channelEvent = eventData;
                channelEventReceived.Set();
            }

            EventTestData data = new EventTestData
            {
                TextField    = ExpectedTextField,
                IntegerField = ExpectedIntegerField,
            };

            // Act
            localPusher.BindAll(GeneralListener);
            localChannel.Bind(testEventName, Listener);
            await pusherServer.TriggerAsync(pusherEvent.ChannelName, testEventName, data).ConfigureAwait(false);

            // Assert
            Assert.IsTrue(globalEventReceived.WaitOne(TimeSpan.FromSeconds(5)));
            Assert.IsTrue(channelEventReceived.WaitOne(TimeSpan.FromSeconds(5)));
            AssertPusherEventsAreEqual(channelType, pusherEvent, globalEvent);
            AssertPusherEventsAreEqual(channelType, pusherEvent, channelEvent);
        }
 public InMemoryAuthorizer(PusherServer.Pusher pusher, PresenceChannelData presenceData)
 {
     _pusher       = pusher;
     _presenceData = presenceData;
 }
 public InMemoryAuthorizer(PusherServer.Pusher pusher) :
     this(pusher, null)
 {
 }
Example #4
0
 public Client()
 {
     _pusher = new PusherServer.Pusher("177397", "7b4abaf489e799ceeceb", "4505221232e1e8341ec9");
 }
Example #5
0
 public Client()
 {
     _pusher = new PusherServer.Pusher("177397", "7b4abaf489e799ceeceb", "4505221232e1e8341ec9");
 }
        private async Task PusherMultipleEventEmitterTestAsync(ChannelTypes channelType)
        {
            // Arrange
            byte[] encryptionKey = GenerateEncryptionMasterKey();
            string channelName   = ChannelNameFactory.CreateUniqueChannelName(channelType: channelType);
            var    pusherServer  = new PusherServer.Pusher(Config.AppId, Config.AppKey, Config.AppSecret, new PusherServer.PusherOptions
            {
                Cluster             = Config.Cluster,
                EncryptionMasterKey = encryptionKey,
            });

            Pusher localPusher = PusherFactory.GetPusher(channelType: ChannelTypes.Presence, saveTo: _clients, encryptionKey: encryptionKey);

            string[] eventNames = new string[] { "my-event-1", "my-event-2", "my-event-3" };
            Dictionary <string, AutoResetEvent> channelEventReceived      = new Dictionary <string, AutoResetEvent>(eventNames.Length);
            Dictionary <string, EventTestData>  channelEvent              = new Dictionary <string, EventTestData>(eventNames.Length);
            Dictionary <string, int>            channelEventReceivedCount = new Dictionary <string, int>(eventNames.Length);
            List <EventTestData> data = new List <EventTestData>(eventNames.Length);

            int count = 1;

            foreach (string eventName in eventNames)
            {
                channelEventReceived[eventName]      = new AutoResetEvent(false);
                channelEvent[eventName]              = null;
                channelEventReceivedCount[eventName] = 0;
                data.Add(new EventTestData {
                    TextField = $"{count}", IntegerField = count++,
                });
            }

            await localPusher.ConnectAsync().ConfigureAwait(false);

            Channel localChannel = await localPusher.SubscribeAsync(channelName).ConfigureAwait(false);

            void Listener1(PusherEvent eventData)
            {
                string key = eventNames[0];

                channelEventReceivedCount[key]++;
                if (eventData.EventName == key && eventData.ChannelName == channelName)
                {
                    channelEvent[key] = JsonConvert.DeserializeObject <EventTestData>(eventData.Data);
                    channelEventReceived[key].Set();
                }
            }

            void Listener2(PusherEvent eventData)
            {
                string key = eventNames[1];

                channelEventReceivedCount[key]++;
                if (eventData.EventName == key && eventData.ChannelName == channelName)
                {
                    channelEvent[key] = JsonConvert.DeserializeObject <EventTestData>(eventData.Data);
                    channelEventReceived[key].Set();
                }
            }

            void Listener3(PusherEvent eventData)
            {
                string key = eventNames[2];

                channelEventReceivedCount[key]++;
                if (eventData.EventName == key && eventData.ChannelName == channelName)
                {
                    channelEvent[key] = JsonConvert.DeserializeObject <EventTestData>(eventData.Data);
                    channelEventReceived[key].Set();
                }
            }

            // Act
            localChannel.Bind(eventNames[0], Listener1);
            localChannel.Bind(eventNames[1], Listener2);
            localChannel.Bind(eventNames[2], Listener3);
            for (int i = 0; i < eventNames.Length; i++)
            {
                await pusherServer.TriggerAsync(localChannel.Name, eventNames[i], data[i]).ConfigureAwait(false);
            }

            // Assert
            foreach (var eventReceived in channelEventReceived)
            {
                Assert.IsTrue(eventReceived.Value.WaitOne(TimeSpan.FromSeconds(5)), $"Event not received for {eventReceived.Key}");
            }

            foreach (var eventReceivedCount in channelEventReceivedCount)
            {
                Assert.AreEqual(1, eventReceivedCount.Value, $"#Events received for {eventReceivedCount.Key}");
            }

            for (int i = 0; i < eventNames.Length; i++)
            {
                string key = eventNames[i];
                Assert.AreEqual(data[i].IntegerField, channelEvent[key].IntegerField, $"{key} IntegerField");
                Assert.AreEqual(data[i].TextField, channelEvent[key].TextField, $"{key} TextField");
            }
        }
        public async Task PusherEventEmitterPrivateEncryptedChannelDecryptionFailureTestAsync()
        {
            // Arrange
            var pusherServer = new PusherServer.Pusher(Config.AppId, Config.AppKey, Config.AppSecret, new PusherServer.PusherOptions
            {
                Cluster             = Config.Cluster,
                EncryptionMasterKey = GenerateEncryptionMasterKey(),
            });

            ChannelDecryptionException decryptionException = null;
            ChannelTypes channelType = ChannelTypes.PrivateEncrypted;

            // Use a different encryption master key - decryption should fail
            Pusher         localPusher          = PusherFactory.GetPusher(channelType: ChannelTypes.Presence, saveTo: _clients, encryptionKey: GenerateEncryptionMasterKey());
            string         testEventName        = "private-encrypted-event-test";
            AutoResetEvent globalEventReceived  = new AutoResetEvent(false);
            AutoResetEvent channelEventReceived = new AutoResetEvent(false);
            AutoResetEvent errorReceived        = new AutoResetEvent(false);
            PusherEvent    pusherEvent          = CreatePusherEvent(channelType, testEventName);

            await localPusher.ConnectAsync().ConfigureAwait(false);

            Channel localChannel = await localPusher.SubscribeAsync(pusherEvent.ChannelName).ConfigureAwait(false);

            void GeneralListener(string eventName, PusherEvent eventData)
            {
                if (eventName == testEventName)
                {
                    globalEventReceived.Set();
                }
            }

            void Listener(PusherEvent eventData)
            {
                channelEventReceived.Set();
            }

            EventTestData data = new EventTestData
            {
                TextField    = ExpectedTextField,
                IntegerField = ExpectedIntegerField,
            };

            void ErrorHandler(object sender, PusherException error)
            {
                decryptionException = error as ChannelDecryptionException;
                if (decryptionException != null)
                {
                    errorReceived.Set();
                }
            }

            // Act
            localPusher.Error += ErrorHandler;
            localPusher.BindAll(GeneralListener);
            localChannel.Bind(testEventName, Listener);
            await pusherServer.TriggerAsync(pusherEvent.ChannelName, testEventName, data).ConfigureAwait(false);

            // Assert
            Assert.IsTrue(errorReceived.WaitOne(TimeSpan.FromSeconds(5)), "Expected to handle an error.");
            Assert.IsNotNull(decryptionException.SocketID, nameof(decryptionException.SocketID));
            Assert.AreEqual(testEventName, decryptionException.EventName, nameof(decryptionException.EventName));
            Assert.AreEqual(localChannel.Name, decryptionException.ChannelName, nameof(decryptionException.ChannelName));
            Assert.IsFalse(globalEventReceived.WaitOne(TimeSpan.FromSeconds(0.1)), "Did not expect to get a global event raised.");
            Assert.IsFalse(channelEventReceived.WaitOne(TimeSpan.FromSeconds(0.1)), "Did not expect to get a channel event raised.");
        }
 public InMemoryAuthorizer(PusherServer.Pusher pusher, PresenceChannelData presenceData)
 {
     _pusher = pusher;
     _presenceData = presenceData;
 }