public async Task TransparentGateway(
            [Values] TestAuthenticationType testAuth,
            [Values(Protocol.Mqtt, Protocol.Amqp)] Protocol protocol)
        {
            CancellationToken token = this.TestToken;

            string leafDeviceId = DeviceId.Current.Generate();

            Option <string> parentId = testAuth == TestAuthenticationType.SasOutOfScope
                ? Option.None <string>()
                : Option.Some(this.runtime.DeviceId);

            var leaf = await LeafDevice.CreateAsync(
                leafDeviceId,
                protocol,
                testAuth.ToAuthenticationType(),
                parentId,
                testAuth.UseSecondaryCertificate(),
                this.ca,
                this.iotHub,
                token);

            await TryFinally.DoAsync(
                async() =>
            {
                DateTime seekTime = DateTime.Now;
                await leaf.SendEventAsync(token);
                await leaf.WaitForEventsReceivedAsync(seekTime, token);
                await leaf.InvokeDirectMethodAsync(token);
            },
                async() =>
            {
                await leaf.DeleteIdentityAsync(token);
            });
        }
Esempio n. 2
0
        public async Task X509ManualProvision()
        {
            CancellationToken token = this.TestToken;

            await this.runtime.DeployConfigurationAsync(token, Context.Current.NestedEdge);

            string leafDeviceId = DeviceId.Current.Generate();

            var leaf = await LeafDevice.CreateAsync(
                leafDeviceId,
                Protocol.Amqp,
                AuthenticationType.Sas,
                Option.Some(this.runtime.DeviceId),
                false,
                this.ca,
                this.IotHub,
                Context.Current.Hostname.GetOrElse(Dns.GetHostName().ToLower()),
                token,
                Option.None <string>(),
                Context.Current.NestedEdge);

            await TryFinally.DoAsync(
                async() =>
            {
                DateTime seekTime = DateTime.Now;
                await leaf.SendEventAsync(token);
                await leaf.WaitForEventsReceivedAsync(seekTime, token);
                await leaf.InvokeDirectMethodAsync(token);
            },
                async() =>
            {
                await leaf.DeleteIdentityAsync(token);
            });
        }
Esempio n. 3
0
        public async Task QuickstartCerts()
        {
            CancellationToken token = this.TestToken;

            await this.runtime.DeployConfigurationAsync(token, this.device.NestedEdge.IsNestedEdge);

            string leafDeviceId = DeviceId.Current.Generate();

            var leaf = await LeafDevice.CreateAsync(
                leafDeviceId,
                Protocol.Amqp,
                AuthenticationType.Sas,
                Option.Some(this.runtime.DeviceId),
                false,
                this.ca,
                this.IotHub,
                this.device.NestedEdge.DeviceHostname,
                token,
                Option.None <string>(),
                this.device.NestedEdge.IsNestedEdge);

            await TryFinally.DoAsync(
                async() =>
            {
                DateTime seekTime = DateTime.Now;
                await leaf.SendEventAsync(token);
                await leaf.WaitForEventsReceivedAsync(seekTime, token);
                await leaf.InvokeDirectMethodAsync(token);
            },
                async() =>
            {
                await leaf.DeleteIdentityAsync(token);
            });
        }
Esempio n. 4
0
        public async Task QuickstartCerts()
        {
            CancellationToken token = this.cts.Token;

            await this.runtime.DeployConfigurationAsync(token);

            string leafDeviceId = $"{Context.Current.DeviceId}-quickstart-certs";

            var leaf = await LeafDevice.CreateAsync(
                leafDeviceId,
                Protocol.Amqp,
                AuthenticationType.Sas,
                Option.None <string>(),
                false,
                CertificateAuthority.GetQuickstart(),
                this.iotHub,
                token);

            await TryFinally.DoAsync(
                async() =>
            {
                DateTime seekTime = DateTime.Now;
                await leaf.SendEventAsync(token);
                await leaf.WaitForEventsReceivedAsync(seekTime, token);
                await leaf.InvokeDirectMethodAsync(token);
            },
                async() =>
            {
                await leaf.DeleteIdentityAsync(token);
            });
        }
Esempio n. 5
0
        public async Task X509ManualProvision()
        {
            CancellationToken token = this.TestToken;

            await this.runtime.DeployConfigurationAsync(token);

            string leafDeviceId =
                IdentityLimits.CheckLeafId($"{Context.Current.DeviceId}-x509-quickstart-certs");

            var leaf = await LeafDevice.CreateAsync(
                leafDeviceId,
                Protocol.Amqp,
                AuthenticationType.Sas,
                Option.Some(Context.Current.DeviceId + "-x509"),
                false,
                CertificateAuthority.GetQuickstart(),
                this.iotHub,
                token);

            await TryFinally.DoAsync(
                async() =>
            {
                DateTime seekTime = DateTime.Now;
                await leaf.SendEventAsync(token);
                await leaf.WaitForEventsReceivedAsync(seekTime, token);
                await leaf.InvokeDirectMethodAsync(token);
            },
                async() =>
            {
                await leaf.DeleteIdentityAsync(token);
            });
        }
        public async Task TransparentGateway(
            [Values] TestAuthenticationType testAuth,
            [Values(Protocol.Mqtt, Protocol.Amqp)] Protocol protocol)
        {
            CancellationToken token = this.TestToken;

            await this.runtime.DeployConfigurationAsync(token, Context.Current.NestedEdge);

            string leafDeviceId = DeviceId.Current.Generate();

            Option <string> parentId = testAuth == TestAuthenticationType.SasOutOfScope
                ? Option.None <string>()
                : Option.Some(this.runtime.DeviceId);

            LeafDevice leaf = null;

            try
            {
                leaf = await LeafDevice.CreateAsync(
                    leafDeviceId,
                    protocol,
                    testAuth.ToAuthenticationType(),
                    parentId,
                    testAuth.UseSecondaryCertificate(),
                    this.ca,
                    this.iotHub,
                    Context.Current.Hostname.GetOrElse(Dns.GetHostName().ToLower()),
                    token,
                    Option.None <string>(),
                    Context.Current.NestedEdge);
            }
            catch (Exception) when(!parentId.HasValue)
            {
                return;
            }

            if (!parentId.HasValue)
            {
                Assert.Fail("Expected to fail when not in scope.");
            }

            Assert.NotNull(leaf);

            await TryFinally.DoAsync(
                async() =>
            {
                DateTime seekTime = DateTime.Now;
                await leaf.SendEventAsync(token);
                await leaf.WaitForEventsReceivedAsync(seekTime, token);
                await leaf.InvokeDirectMethodAsync(token);
            },
                async() =>
            {
                await leaf.DeleteIdentityAsync(token);
            });
        }
        public async Task TransparentGateway(
            [Values] TestAuthenticationType testAuth,
            [Values(Protocol.Mqtt, Protocol.Amqp)] Protocol protocol)
        {
            if (!OsPlatform.IsWindows() && (protocol == Protocol.Amqp))
            {
                switch (testAuth)
                {
                case TestAuthenticationType.SelfSignedPrimary:
                case TestAuthenticationType.SelfSignedSecondary:
                case TestAuthenticationType.CertificateAuthority:
                    Assert.Ignore("Skipping the test case due to BUG 5234369");
                    break;

                default:
                    // Intentionally left blank
                    break;
                }
            }

            CancellationToken token = this.TestToken;

            string leafDeviceId = IdentityLimits.CheckLeafId(
                $"{Context.Current.DeviceId}-{protocol}-{testAuth}");

            Option <string> parentId = testAuth == TestAuthenticationType.SasOutOfScope
                ? Option.None <string>()
                : Option.Some(Context.Current.DeviceId.ToString());

            var leaf = await LeafDevice.CreateAsync(
                leafDeviceId,
                protocol,
                testAuth.ToAuthenticationType(),
                parentId,
                testAuth.UseSecondaryCertificate(),
                this.ca,
                this.iotHub,
                token);

            await TryFinally.DoAsync(
                async() =>
            {
                DateTime seekTime = DateTime.Now;
                await leaf.SendEventAsync(token);
                await leaf.WaitForEventsReceivedAsync(seekTime, token);
                await leaf.InvokeDirectMethodAsync(token);
            },
                async() =>
            {
                await leaf.DeleteIdentityAsync(token);
            });
        }
Esempio n. 8
0
        public async Task TransparentGateway(
            [Values] TestAuthenticationType testAuth,
            [Values(Protocol.Mqtt, Protocol.Amqp)] Protocol protocol)
        {
            // For CA and self-signed cert tests, temporarily disable AMQP
            var auth = testAuth.ToAuthenticationType();

            if (protocol == Protocol.Amqp &&
                (auth == AuthenticationType.CertificateAuthority || auth == AuthenticationType.SelfSigned))
            {
                Assert.Ignore("x509 cert + AMQP tests disabled until bug is resolved");
            }

            CancellationToken token = this.cts.Token;

            // Generate a leaf device ID--based on the (edge) device ID--that is at most
            // (deviceId.Length + 26 chars) long. This gives us a leaf device ID of <= 63
            // characters, and gives LeafDevice.CreateAsync (called below) some wiggle room to
            // create certs with unique CNs that don't exceed the 64-char limit.
            string leafDeviceId = $"{Context.Current.DeviceId}-{protocol.ToString()}-{testAuth.ToString()}";

            Option <string> parentId = testAuth == TestAuthenticationType.SasOutOfScope
                ? Option.None <string>()
                : Option.Some(Context.Current.DeviceId);

            var leaf = await LeafDevice.CreateAsync(
                leafDeviceId,
                protocol,
                auth,
                parentId,
                testAuth.UseSecondaryCertificate(),
                this.ca,
                this.iotHub,
                token);

            await TryFinally.DoAsync(
                async() =>
            {
                DateTime seekTime = DateTime.Now;
                await leaf.SendEventAsync(token);
                await leaf.WaitForEventsReceivedAsync(seekTime, token);
                await leaf.InvokeDirectMethodAsync(token);
            },
                async() =>
            {
                await leaf.DeleteIdentityAsync(token);
            });
        }
Esempio n. 9
0
        public async Task DisableReenableParentEdge()
        {
            CancellationToken token = this.TestToken;

            Log.Information("Deploying L3 Edge");
            await this.runtime.DeployConfigurationAsync(token, Context.Current.NestedEdge);

            // Disable the parent Edge device
            Log.Information("Disabling Edge device");
            await this.IotHub.UpdateEdgeEnableStatus(this.runtime.DeviceId, false);

            await Task.Delay(TimeSpan.FromSeconds(10));

            // Re-enable parent Edge
            Log.Information("Re-enabling Edge device");
            await this.IotHub.UpdateEdgeEnableStatus(this.runtime.DeviceId, true);

            await Task.Delay(TimeSpan.FromSeconds(10));

            // Try connecting
            string leafDeviceId = DeviceId.Current.Generate();
            var    leaf         = await LeafDevice.CreateAsync(
                leafDeviceId,
                Protocol.Amqp,
                AuthenticationType.Sas,
                Option.Some(this.runtime.DeviceId),
                false,
                this.ca,
                this.IotHub,
                Context.Current.Hostname.GetOrElse(Dns.GetHostName().ToLower()),
                token,
                Option.None <string>(),
                Context.Current.NestedEdge);

            await TryFinally.DoAsync(
                async() =>
            {
                DateTime seekTime = DateTime.Now;
                await leaf.SendEventAsync(token);
                await leaf.WaitForEventsReceivedAsync(seekTime, token);
                await leaf.InvokeDirectMethodAsync(token);
            },
                async() =>
            {
                await leaf.DeleteIdentityAsync(token);
            });
        }
Esempio n. 10
0
        public Task AfterAllAsync() => TryFinally.DoAsync(
            () => Profiler.Run(
                async() =>
        {
            using (var cts = new CancellationTokenSource(Context.Current.TeardownTimeout))
            {
                CancellationToken token = cts.Token;

                await this.daemon.StopAsync(token);

                Assert.IsNotNull(this.device);
                await this.device.MaybeDeleteIdentityAsync(token);
            }
        },
                "Completed end-to-end test teardown"),
            () =>
        {
            Log.CloseAndFlush();
        });
Esempio n. 11
0
        public async Task PlugAndPlayDeviceClient(Protocol protocol)
        {
            CancellationToken token        = this.TestToken;
            string            leafDeviceId = DeviceId.Current.Generate();

            Action <EdgeConfigBuilder> config     = this.BuildAddEdgeHubConfig(protocol);
            EdgeDeployment             deployment = await this.runtime.DeployConfigurationAsync(
                config,
                token,
                Context.Current.NestedEdge);

            var leaf = await LeafDevice.CreateAsync(
                leafDeviceId,
                protocol,
                AuthenticationType.Sas,
                Option.Some(this.runtime.DeviceId),
                false,
                this.ca,
                this.IotHub,
                Context.Current.Hostname.GetOrElse(Dns.GetHostName().ToLower()),
                token,
                Option.Some(TestModelId),
                Context.Current.NestedEdge);

            await TryFinally.DoAsync(
                async() =>
            {
                DateTime seekTime = DateTime.Now;
                await leaf.SendEventAsync(token);
                await leaf.WaitForEventsReceivedAsync(seekTime, token);
                await this.ValidateIdentity(leafDeviceId, Option.None <string>(), TestModelId, token);
            },
                async() =>
            {
                await leaf.DeleteIdentityAsync(token);
            });
        }
Esempio n. 12
0
        public async Task QuickstartChangeSasKey()
        {
            CancellationToken token = this.TestToken;

            await this.runtime.DeployConfigurationAsync(token, Context.Current.NestedEdge);

            string leafDeviceId = DeviceId.Current.Generate();

            // Create leaf and send message
            var leaf = await LeafDevice.CreateAsync(
                leafDeviceId,
                Protocol.Amqp,
                AuthenticationType.Sas,
                Option.Some(this.runtime.DeviceId),
                false,
                this.ca,
                this.IotHub,
                Context.Current.Hostname.GetOrElse(Dns.GetHostName().ToLower()),
                token,
                Option.None <string>(),
                Context.Current.NestedEdge);

            await TryFinally.DoAsync(
                async() =>
            {
                DateTime seekTime = DateTime.Now;
                await leaf.SendEventAsync(token);
                await leaf.WaitForEventsReceivedAsync(seekTime, token);
                await leaf.InvokeDirectMethodAsync(token);
            },
                async() =>
            {
                await leaf.Close();
                await leaf.DeleteIdentityAsync(token);
            });

            // Re-create the leaf with the same device ID, for our purposes this is
            // the equivalent of updating the SAS keys
            var leafUpdated = await LeafDevice.CreateAsync(
                leafDeviceId,
                Protocol.Amqp,
                AuthenticationType.Sas,
                Option.Some(this.runtime.DeviceId),
                false,
                this.ca,
                this.IotHub,
                Context.Current.Hostname.GetOrElse(Dns.GetHostName().ToLower()),
                token,
                Option.None <string>(),
                Context.Current.NestedEdge);

            await TryFinally.DoAsync(
                async() =>
            {
                DateTime seekTime = DateTime.Now;
                await leafUpdated.SendEventAsync(token);
                await leafUpdated.WaitForEventsReceivedAsync(seekTime, token);
                await leafUpdated.InvokeDirectMethodAsync(token);
            },
                async() =>
            {
                await leafUpdated.Close();
                await leafUpdated.DeleteIdentityAsync(token);
            });
        }
        public async Task GrandparentScopeDevice(
            [Values(
                 TestAuthenticationType.SasInScope,
                 TestAuthenticationType.SelfSignedPrimary,
                 TestAuthenticationType.SelfSignedSecondary)] TestAuthenticationType testAuth,
            [Values(Protocol.Mqtt, Protocol.Amqp)] Protocol protocol)
        {
            if (!this.device.NestedEdge.IsNestedEdge)
            {
                Assert.Ignore("The test can only be run in the nested edge topology");
            }

            Option <string> parentId = Option.Some(this.runtime.DeviceId);

            CancellationToken token = this.TestToken;

            await this.runtime.DeployConfigurationAsync(token, Context.Current.NestedEdge);

            string leafDeviceId = DeviceId.Current.Generate();

            LeafDevice leaf = null;

            try
            {
                leaf = await LeafDevice.CreateAsync(
                    leafDeviceId,
                    protocol,
                    testAuth.ToAuthenticationType(),
                    parentId,
                    testAuth.UseSecondaryCertificate(),
                    this.ca,
                    this.IotHub,
                    this.device.NestedEdge.ParentHostname,
                    token,
                    Option.None <string>(),
                    this.device.NestedEdge.IsNestedEdge);
            }
            catch (Exception) when(!parentId.HasValue)
            {
                return;
            }

            if (!parentId.HasValue)
            {
                Assert.Fail("Expected to fail when not in scope.");
            }

            Assert.NotNull(leaf);

            await TryFinally.DoAsync(
                async() =>
            {
                DateTime seekTime = DateTime.Now;
                await leaf.SendEventAsync(token);
                await leaf.WaitForEventsReceivedAsync(seekTime, token);
                await leaf.InvokeDirectMethodAsync(token);
            },
                async() =>
            {
                await leaf.DeleteIdentityAsync(token);
                await Task.CompletedTask;
            });
        }