public void ResolveIncomingNullAllTest()
        {
            var deviceOperations   = Substitute.For <IDeviceOperations>();
            var networkeOperations = Substitute.For <INetworkOperations>();
            var serviceOperations  = Substitute.For <IServiceOperations>();
            var companyOperations  = Substitute.For <ICompanyOperations>();
            var telemetryDataSinkMetadataRegistry = Substitute.For <ITelemetryDataSinkMetadataRegistry>();

            var deviceId = Identity.Next();

            var serviceId = Identity.Next();
            var networkId = Identity.Next();
            var companyId = Identity.Next();

            deviceOperations.Get(deviceId).Returns(TestDataCreator.Device(deviceId, null, networkId, serviceId, companyId, 0));
            serviceOperations.Get(serviceId)
            .Returns(TestDataCreator.Service(serviceId, null,
                                             companyId, new TelemetryDataSinkSettings {
                Incoming = null
            }));
            companyOperations.Get(companyId)
            .Returns(TestDataCreator.Company(companyId, new TelemetryDataSinkSettings {
                Incoming = null
            }));
            networkeOperations.Get(networkId)
            .Returns(TestDataCreator.Network(networkId, "key", null, serviceId, companyId, new TelemetryDataSinkSettings {
                Incoming = new List <TelemetryDataSinkParameters>()
            }));

            var telemetryDataSinkResolver = new TelemetryDataSinkResolver(deviceOperations, networkeOperations, serviceOperations, companyOperations,
                                                                          telemetryDataSinkMetadataRegistry, new DynamicConnectionStringResolver(null));

            Assert.AreEqual(0, telemetryDataSinkResolver.ResolveIncoming(deviceId).Count());
        }
Esempio n. 2
0
        public void ParentNetworkAuthOkTest()
        {
            var deviceOperations  = Substitute.For <IDeviceOperations>();
            var networkOperations = Substitute.For <INetworkOperations>();
            var serviceOperations = Substitute.For <IServiceOperations>();

            var deviceId  = Identity.Next();
            var deviceKey = Crypto.GenerateSafeRandomToken();

            var networkId  = Identity.Next();
            var networkKey = Crypto.GenerateSafeRandomToken();

            var parentNetworkId  = Identity.Next();
            var parentNetworkKey = Crypto.GenerateSafeRandomToken();

            var serviceId = Identity.Next();
            var apiKey    = Crypto.GenerateSafeRandomToken();

            deviceOperations.Get(deviceId).Returns(TestDataCreator.Device(deviceId, deviceKey, networkId, serviceId, null, 0));
            networkOperations.Get(networkId).Returns(TestDataCreator.Network(networkId, networkKey, parentNetworkId, serviceId, null, null));
            networkOperations.Get(parentNetworkId).Returns(TestDataCreator.Network(parentNetworkId, parentNetworkKey, null, serviceId, null, null));
            serviceOperations.Get(serviceId).Returns(TestDataCreator.Service(serviceId, apiKey, null, null));

            var deviceAuthenticator = new DeviceAuthenticator(deviceOperations, networkOperations, serviceOperations);

            Assert.IsTrue(deviceAuthenticator.Authenticate(new AuthenticationParameters(deviceId, parentNetworkKey)));
        }
Esempio n. 3
0
        public void SendToErrorTest()
        {
            var connection          = Substitute.For <IPersistentConnection>();
            var deviceAuthenticator = Substitute.For <IDeviceAuthenticator>();
            var deviceOperations    = Substitute.For <IDeviceOperations>();
            var dateTimeProvider    = Substitute.For <IDateTimeProvider>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            var deviceId = Identity.Next();

            connection.ConnectionId.Returns(Guid.NewGuid());
            deviceAuthenticator.Authenticate(null).ReturnsForAnyArgs(true);
            deviceOperations.Get(null).ReturnsForAnyArgs(TestDataCreator.Device(deviceId, "1234", "2345", "3456", "4567", 1));

            connectionRegistry.RegisterInitiatedConnection(connection);

            var commandExecutor = new CommandExecutor(pusherRegistry, connectionRegistry, null, deviceAuthenticator, deviceOperations, null, null);

            commandExecutor.Execute(connection, new LoginCommand(deviceId + " " + Identity.Next()));

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);

            commandExecutor.Execute(connection, new SendToCommand("{\"Temperature\": 24, \"Time\":" + DateTime.UtcNow.Ticks + "}"));
            connection.Received(1).Reply("sendto error");
        }
Esempio n. 4
0
        public void PeekTwiceOutgoingMessageTest()
        {
            var messagingOperations = Substitute.For <IMessagingOperations>();
            var pltDeviceOperations = Substitute.For <IDeviceOperations>();

            pltDeviceOperations.Get("1234").Returns(TestDataCreator.Device("1234", "1234", "12345", "123456", "1234567", 1));
            messagingOperations.Peek(1)
            .Returns(new OutgoingMessageToStoreWithState(new OutgoingMessageToStore(1, new byte[] { 48, 49, 50 }, 1, DateTime.UtcNow, "sender"), OutgoingState.Ok));

            var messagingService = new MessagingService(messagingOperations, pltDeviceOperations);

            var msg = messagingService.Peek("1234");

            Assert.IsNotNull(msg);
            Assert.AreEqual(OutgoingState.Ok, msg.State);
            Assert.AreEqual("1234", msg.Message.DeviceId);
            Assert.AreEqual("012", msg.Message.Payload);
            Assert.AreEqual("sender", msg.Message.SenderDeviceId);

            msg = messagingService.Peek("1234");

            Assert.IsNotNull(msg);
            Assert.AreEqual(OutgoingState.Ok, msg.State);
            Assert.AreEqual("1234", msg.Message.DeviceId);
            Assert.AreEqual("012", msg.Message.Payload);
            Assert.AreEqual("sender", msg.Message.SenderDeviceId);
        }
Esempio n. 5
0
        public void TelemetryDataLoginRequiredTest()
        {
            var connection                = Substitute.For <IPersistentConnection>();
            var deviceAuthenticator       = Substitute.For <IDeviceAuthenticator>();
            var deviceOperations          = Substitute.For <IDeviceOperations>();
            var dateTimeProvider          = Substitute.For <IDateTimeProvider>();
            var telemetryDataSinkResolver = Substitute.For <ITelemetryDataSinkResolver>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            var deviceId = Identity.Next();

            connection.ConnectionId.Returns(Guid.NewGuid());
            deviceAuthenticator.Authenticate(null).ReturnsForAnyArgs(true);
            deviceOperations.Get(null).ReturnsForAnyArgs(TestDataCreator.Device(deviceId, "1234", "2345", "3456", "4567", 1));

            connectionRegistry.RegisterInitiatedConnection(connection);

            telemetryDataSinkResolver.ResolveIncoming(null).ReturnsForAnyArgs(new List <ITelemetryDataSink>());
            var telemetryDataService = new DirectTelemetryDataService(telemetryDataSinkResolver);

            var commandExecutor = new CommandExecutor(pusherRegistry, connectionRegistry, null, deviceAuthenticator, deviceOperations, null, telemetryDataService);

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);

            commandExecutor.Execute(connection, new TelemetryDataCommand("{\"Temperature\": 24, \"Time\":" + DateTime.UtcNow.Ticks + "}"));
            connection.Received(1).Reply("telemetrydata unauthorized");
        }
Esempio n. 6
0
        public void CommitNotLoggedInTest()
        {
            var connection            = Substitute.For <IPersistentConnection>();
            var deviceAuthenticator   = Substitute.For <IDeviceAuthenticator>();
            var deviceOperations      = Substitute.For <IDeviceOperations>();
            var dateTimeProvider      = Substitute.For <IDateTimeProvider>();
            var outgoingMessageReader = Substitute.For <IMessagingOperations>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            var deviceId = Identity.Next();

            connection.ConnectionId.Returns(Guid.NewGuid());
            deviceAuthenticator.Authenticate(null).ReturnsForAnyArgs(true);
            deviceOperations.Get(null).ReturnsForAnyArgs(TestDataCreator.Device(deviceId, "1234", "2345", "3456", "4567", 1));

            connectionRegistry.RegisterInitiatedConnection(connection);

            var commandExecutor = new CommandExecutor(pusherRegistry, connectionRegistry, outgoingMessageReader, deviceAuthenticator, deviceOperations, null, null);

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
            pusherRegistry.SetAsCommitNeededConnections(new[] { connection });

            outgoingMessageReader.Commit((long)0).ReturnsForAnyArgs(OutgoingState.Ok);

            connection.ClearReceivedCalls();

            commandExecutor.Execute(connection, new CommitCommand());
            connection.DidNotReceive().Heartbeat();
        }
Esempio n. 7
0
        public void HeartbeatTest()
        {
            var connection          = Substitute.For <IPersistentConnection>();
            var deviceAuthenticator = Substitute.For <IDeviceAuthenticator>();
            var deviceOperations    = Substitute.For <IDeviceOperations>();

            var connectionRegistry = new ConnectionRegistry(new PusherRegistry(new DateTimeProvider()), null);

            connection.ConnectionId.Returns(Guid.NewGuid());
            deviceAuthenticator.Authenticate(null).ReturnsForAnyArgs(true);
            deviceOperations.Get(null).ReturnsForAnyArgs(TestDataCreator.Device("1234", "1234", "2345", "3456", "4567", 1));

            connectionRegistry.RegisterInitiatedConnection(connection);

            var commandExecutor = new CommandExecutor(null, connectionRegistry, null, deviceAuthenticator, deviceOperations, null, null);

            commandExecutor.Execute(connection, new LoginCommand(Identity.Next() + " " + Identity.Next()));

            commandExecutor.Execute(connection, new SubscribeCommand("receiveandforget"));

            commandExecutor.Execute(connection, new HeartbeatCommand());

            Assert.AreEqual(ConnectionState.LoggedIn | ConnectionState.Subscribed, connection.ConnectionState);
            connection.Received().Heartbeat();
        }
Esempio n. 8
0
        public void TryLoginSameDeviceTwiceTest()
        {
            var connection          = Substitute.For <IPersistentConnection>();
            var connection2         = Substitute.For <IPersistentConnection>();
            var deviceAuthenticator = Substitute.For <IDeviceAuthenticator>();
            var deviceOperations    = Substitute.For <IDeviceOperations>();

            var connectionRegistry = new ConnectionRegistry(new PusherRegistry(new DateTimeProvider()), null);

            connection.ConnectionId.Returns(Guid.NewGuid());
            connection2.ConnectionId.Returns(Guid.NewGuid());

            deviceAuthenticator.Authenticate(null).ReturnsForAnyArgs(true);
            var deviceId = Identity.Next();

            deviceOperations.Get(null).ReturnsForAnyArgs(TestDataCreator.Device(deviceId, "1234", "2345", "3456", "4567", 1));

            connectionRegistry.RegisterInitiatedConnection(connection);
            connectionRegistry.RegisterInitiatedConnection(connection2);

            var commandExecutor = new CommandExecutor(null, connectionRegistry, null, deviceAuthenticator, deviceOperations, null, null);

            commandExecutor.Execute(connection, new LoginCommand(deviceId + " " + Identity.Next()));
            commandExecutor.Execute(connection2, new LoginCommand(deviceId + " " + Identity.Next()));

            connection.Received().Reply("login ack");
            connection2.Received().Reply("login badcommand");
            Assert.AreEqual(ConnectionState.LoggedIn, connection.ConnectionState);
            Assert.AreEqual(1, connection.NumericDeviceId);
        }
        public void ResolveIncomingCompanyTest()
        {
            var deviceOperations   = Substitute.For <IDeviceOperations>();
            var networkeOperations = Substitute.For <INetworkOperations>();
            var serviceOperations  = Substitute.For <IServiceOperations>();
            var companyOperations  = Substitute.For <ICompanyOperations>();
            var telemetryDataSinkMetadataRegistry = Substitute.For <ITelemetryDataSinkMetadataRegistry>();

            var deviceId = Identity.Next();

            var serviceId = Identity.Next();
            var networkId = Identity.Next();
            var companyId = Identity.Next();

            telemetryDataSinkMetadataRegistry.Incoming.Returns(new List <TelemetryDataSinkMetadata>
            {
                new TelemetryDataSinkMetadata("azureData", "data", typeof(IncomingStubs.CurrentDataStub),
                                              new[] { "ConnectionString", "Table" }, new Dictionary <string, string>())
            });

            deviceOperations.Get(deviceId).Returns(TestDataCreator.Device(deviceId, null, networkId, serviceId, companyId, 0));
            serviceOperations.Get(serviceId)
            .Returns(TestDataCreator.Service(serviceId, null,
                                             companyId, new TelemetryDataSinkSettings {
                Incoming = new List <TelemetryDataSinkParameters>()
            }));

            companyOperations.Get(companyId)
            .Returns(TestDataCreator.Company(companyId, new TelemetryDataSinkSettings
            {
                Incoming =
                    new List <TelemetryDataSinkParameters>
                {
                    new TelemetryDataSinkParameters()
                    {
                        SinkName   = "azureData",
                        Parameters =
                            new Dictionary <string, string>()
                        {
                            { "ConnectionString", "that" },
                            { "Table", "t" }
                        }
                    }
                }
            }));
            networkeOperations.Get(networkId)
            .Returns(TestDataCreator.Network(networkId, "key", null, serviceId, companyId, new TelemetryDataSinkSettings {
                Incoming = new List <TelemetryDataSinkParameters>()
            }));

            var telemetryDataSinkResolver = new TelemetryDataSinkResolver(deviceOperations, networkeOperations, serviceOperations, companyOperations,
                                                                          telemetryDataSinkMetadataRegistry, new DynamicConnectionStringResolver(null));

            Assert.AreEqual(1, telemetryDataSinkResolver.ResolveIncoming(deviceId).Count());
            Assert.IsInstanceOfType(telemetryDataSinkResolver.ResolveIncoming(deviceId).First(), typeof(IncomingStubs.CurrentDataStub));
        }
Esempio n. 10
0
        public void RecordOutgoingMessagePayloadJustOkOtherNetworkTest()
        {
            var messagingOperations = Substitute.For <IMessagingOperations>();
            var pltDeviceOperations = Substitute.For <IDeviceOperations>();

            pltDeviceOperations.Get("1234").Returns(TestDataCreator.Device("1234", "1234", "12345", "123456", "1234567", 1));
            pltDeviceOperations.Get("1235").Returns(TestDataCreator.Device("1235", "1234", "54321", "123456", "1234567", 1));
            messagingOperations.Record(null).ReturnsForAnyArgs(OutgoingState.Ok);

            var messagingService = new MessagingService(messagingOperations, pltDeviceOperations);

            var success = messagingService.RecordOutgoingMessage("1234", "1235", new string('a', 512));
        }
Esempio n. 11
0
        public void CommitOutgoingMessageFailTset()
        {
            var messagingOperations = Substitute.For <IMessagingOperations>();
            var pltDeviceOperations = Substitute.For <IDeviceOperations>();

            pltDeviceOperations.Get("1234").Returns(TestDataCreator.Device("1234", "1234", "12345", "123456", "1234567", 1));
            messagingOperations.Commit(1).Returns(OutgoingState.Fail);

            var messagingService = new MessagingService(messagingOperations, pltDeviceOperations);

            var state = messagingService.Commit("1234");

            Assert.AreEqual(OutgoingState.Fail, state);
        }
Esempio n. 12
0
        public void RecordOutgoingMessageTest()
        {
            var messagingOperations = Substitute.For <IMessagingOperations>();
            var pltDeviceOperations = Substitute.For <IDeviceOperations>();

            pltDeviceOperations.Get("1234").Returns(TestDataCreator.Device("1234", "1234", "12345", "123456", "1234567", 1));
            messagingOperations.Record(null).ReturnsForAnyArgs(OutgoingState.Ok);

            var messagingService = new MessagingService(messagingOperations, pltDeviceOperations);

            var success = messagingService.RecordOutgoingMessage("1234", "1234", "32412341243");

            Assert.AreEqual(OutgoingState.Ok, success);
        }
Esempio n. 13
0
        public void PeekOutgoingMessageFailTset()
        {
            var messagingOperations = Substitute.For <IMessagingOperations>();
            var pltDeviceOperations = Substitute.For <IDeviceOperations>();

            pltDeviceOperations.Get("1234").Returns(TestDataCreator.Device("1234", "1234", "12345", "123456", "1234567", 1));
            messagingOperations.Peek(1).Returns(new OutgoingMessageToStoreWithState(null, OutgoingState.Fail));

            var messagingService = new MessagingService(messagingOperations, pltDeviceOperations);

            var msg = messagingService.Peek("1234");

            Assert.AreEqual(OutgoingState.Fail, msg.State);
            Assert.IsNull(msg.Message);
        }
Esempio n. 14
0
        public void CommitSuccessTest()
        {
            var connection            = Substitute.For <IPersistentConnection>();
            var deviceAuthenticator   = Substitute.For <IDeviceAuthenticator>();
            var deviceOperations      = Substitute.For <IDeviceOperations>();
            var dateTimeProvider      = Substitute.For <IDateTimeProvider>();
            var outgoingMessageReader = Substitute.For <IMessagingOperations>();

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            var deviceId = Identity.Next();

            connection.ConnectionId.Returns(Guid.NewGuid());
            deviceAuthenticator.Authenticate(null).ReturnsForAnyArgs(true);
            deviceOperations.Get(null).ReturnsForAnyArgs(TestDataCreator.Device(deviceId, "1234", "2345", "3456", "4567", 1));

            connectionRegistry.RegisterInitiatedConnection(connection);

            var commandExecutor = new CommandExecutor(pusherRegistry, connectionRegistry, outgoingMessageReader, deviceAuthenticator, deviceOperations, null, null);

            commandExecutor.Execute(connection, new LoginCommand(deviceId + " " + Identity.Next()));

            commandExecutor.Execute(connection, new SubscribeCommand("peekandcommit"));

            dateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
            pusherRegistry.SetAsCommitNeededConnections(new[] { connection });

            outgoingMessageReader.Commit((long)0).ReturnsForAnyArgs(OutgoingState.Ok);

            Assert.IsTrue(pusherRegistry.IsCommitable(deviceId));

            commandExecutor.Execute(connection, new CommitCommand());

            Assert.IsFalse(pusherRegistry.IsCommitable(deviceId));

            commandExecutor.Execute(connection, new CommitCommand());
        }
Esempio n. 15
0
        public void TryUnSubscribeNotLoggedinTest()
        {
            var connection          = Substitute.For <IPersistentConnection>();
            var deviceAuthenticator = Substitute.For <IDeviceAuthenticator>();
            var deviceOperations    = Substitute.For <IDeviceOperations>();

            var connectionRegistry = new ConnectionRegistry(new PusherRegistry(new DateTimeProvider()), null);

            connection.ConnectionId.Returns(Guid.NewGuid());
            deviceAuthenticator.Authenticate(null).ReturnsForAnyArgs(true);
            deviceOperations.Get(null).ReturnsForAnyArgs(TestDataCreator.Device("1234", "1234", "2345", "3456", "4567", 1));

            connectionRegistry.RegisterInitiatedConnection(connection);

            var commandExecutor = new CommandExecutor(null, connectionRegistry, null, deviceAuthenticator, deviceOperations, null, null);

            commandExecutor.Execute(connection, new UnsubscribeCommand());

            connection.Received().Reply("unsubscribe unauthorized");
            Assert.AreEqual(ConnectionState.Initiated, connection.ConnectionState);
            Assert.AreEqual(SubscriptionType.None, connection.SubscriptionType);
        }
Esempio n. 16
0
        public void DeviceOperationsTest()
        {
            var deviceOperations = Substitute.For <IPersistedDeviceOperations>();

            var cachingDeviceOperations = new DeviceOperations(deviceOperations);

            cachingDeviceOperations.Remove("1");

            deviceOperations.Get("1").Returns(TestDataCreator.Device("1", "2", "3", "4", "5", 1));

            var device = cachingDeviceOperations.Get("1");

            Assert.AreEqual("2", device.DeviceKey);

            deviceOperations.Received().Get("1");

            deviceOperations.ClearReceivedCalls();

            var device2 = cachingDeviceOperations.Get("1");

            Assert.AreEqual("2", device2.DeviceKey);

            deviceOperations.DidNotReceive().Get("1");
        }
Esempio n. 17
0
        public void CommitNotApplicableTest()
        {
            var connection          = Substitute.For <IPersistentConnection>();
            var deviceAuthenticator = Substitute.For <IDeviceAuthenticator>();
            var deviceOperations    = Substitute.For <IDeviceOperations>();
            var dateTimeProvider    = Substitute.For <IDateTimeProvider>();

            var pusherRegistry     = new PusherRegistry(dateTimeProvider);
            var connectionRegistry = new ConnectionRegistry(pusherRegistry, null);

            connection.ConnectionId.Returns(Guid.NewGuid());
            deviceAuthenticator.Authenticate(null).ReturnsForAnyArgs(true);
            deviceOperations.Get(null).ReturnsForAnyArgs(TestDataCreator.Device("1234", "1234", "2345", "3456", "4567", 1));

            connectionRegistry.RegisterInitiatedConnection(connection);

            var commandExecutor = new CommandExecutor(pusherRegistry, connectionRegistry, null, deviceAuthenticator, deviceOperations, null, null);

            commandExecutor.Execute(connection, new LoginCommand(Identity.Next() + " " + Identity.Next()));

            commandExecutor.Execute(connection, new SubscribeCommand("peekandcommit"));

            commandExecutor.Execute(connection, new CommitCommand());
        }