예제 #1
0
        public void TestDesiredPropertyRecurringReporting()
        {
            // Expected values
            var expectedEndpoint = "Dummy Endpoint";

            // Mock Object setup
            var callback = new CallBackMockup();

            callback.SendMessageHook      = SendMessageHook;
            callback.ReportPropertiesHook = ReportPropertiesHook;


            var systemConfigurator = new ConfigurationProxyMockup();

            systemConfigurator.SendCommandHook = (IRequest request) =>
            {
                if (request.Tag == DMMessageKind.DeviceHealthAttestationVerifyHealth)
                {
                    var request2 = (DeviceHealthAttestationVerifyHealthRequest)request;
                    Assert.AreEqual(expectedEndpoint, request2.HealthAttestationServerEndpoint);
                    return(new StatusCodeResponse(ResponseStatus.Success, DMMessageKind.DeviceHealthAttestationVerifyHealth));
                }
                Assert.Fail($"Got unexpected command - {request.Tag}");
                return(null);
            };

            var dha = new DeviceHealthAttestationHandler(callback, systemConfigurator);

            // Test begins
            try
            {
                var desiredProperty = new DeviceHealthAttestationDataContract.DesiredProperty();
                desiredProperty.Endpoint = expectedEndpoint;
                desiredProperty.ReportIntervalInSeconds = 1;
                dha.OnDesiredPropertyChange(JObject.FromObject(desiredProperty));

                // Test validation
                for (int i = 0; i < 3; i++)
                {
                    Assert.IsTrue(sentMessageEvent.WaitOne(TimeSpan.FromSeconds(5)));
                    Assert.AreEqual(DeviceHealthAttestationDataContract.NonceRequestTag, sentMessageProperties["MessageType"]);
                }
            }
            finally
            {
                // Disable the reporting
                var desiredProperty = new DeviceHealthAttestationDataContract.DesiredProperty();
                desiredProperty.Endpoint = "";
                desiredProperty.ReportIntervalInSeconds = -1;
                dha.OnDesiredPropertyChange(JObject.FromObject(desiredProperty));
            }
        }
예제 #2
0
        public void TestDirectMethodGetCertificate()
        {
            // Expected values
            var expectedNonce         = "AFAFAFAFAF";
            var expectedHealthCert    = "Dummy health cert";
            var expectedCorrelationId = "Dummy correlation id";

            // Mock Object setup
            var callback = new CallBackMockup();

            callback.SendMessageHook      = SendMessageHook;
            callback.ReportPropertiesHook = ReportPropertiesHook;

            var systemConfigurator = new ConfigurationProxyMockup();

            systemConfigurator.SendCommandHook = (IRequest request) =>
            {
                if (request.Tag == DMMessageKind.DeviceHealthAttestationGetReport)
                {
                    var request2 = (DeviceHealthAttestationGetReportRequest)request;
                    Assert.AreEqual(expectedNonce, request2.Nonce);
                    return(new DeviceHealthAttestationGetReportResponse(expectedHealthCert, expectedCorrelationId));
                }
                Assert.Fail($"Got unexpected command - {request.Tag}");
                return(null);
            };

            var dha = new DeviceHealthAttestationHandler(callback, systemConfigurator);

            // Test begins
            var getCertificateMethod = dha.GetDirectMethodHandler()[DeviceHealthAttestationDataContract.GetReportMethodName];
            var param = new DeviceHealthAttestationDataContract.GetReportMethodParam()
            {
                Nonce = expectedNonce
            };

            var response = getCertificateMethod(JsonConvert.SerializeObject(param)).Result;

            // Test validation
            Assert.IsTrue(sentMessageEvent.WaitOne(TimeSpan.FromSeconds(5)));
            Assert.AreEqual(SuccessResponse, response);
            Assert.AreEqual(DeviceHealthAttestationDataContract.HealthReportTag, sentMessageProperties["MessageType"]);
            var message = JsonConvert.DeserializeObject <DeviceHealthAttestationDataContract.HealthReport>(sentMessaage);

            Assert.AreEqual(expectedCorrelationId, message.CorrelationId);
            Assert.AreEqual(expectedHealthCert, message.HealthCertificate);
            EnsureReportedPropertyStatus("Reported");
        }
예제 #3
0
        public void TestDesiredPropertyDisabledReporting()
        {
            // Mock Object setup
            var callback = new CallBackMockup();

            callback.ReportPropertiesHook = ReportPropertiesHook;
            var systemConfigurator = new ConfigurationProxyMockup();
            var dha = new DeviceHealthAttestationHandler(callback, systemConfigurator);

            // Test begins
            var desiredProperty = new DeviceHealthAttestationDataContract.DesiredProperty();

            desiredProperty.Endpoint = "";
            desiredProperty.ReportIntervalInSeconds = -1;
            dha.OnDesiredPropertyChange(JObject.FromObject(desiredProperty));

            // Test validation
            EnsureReportedPropertyStatus("Reporting Disabled");
        }
예제 #4
0
        public void TestDesiredPropertyInvalidJObject()
        {
            // Send invalid JObject as desired property.  Ensure error is reflected in the reported property.
            var callback = new CallBackMockup();

            callback.ReportPropertiesHook = ReportPropertiesHook;
            var systemConfigurator = new ConfigurationProxyMockup();
            var dha = new DeviceHealthAttestationHandler(callback, systemConfigurator);

            {
                // Test 1: Endpoint = null
                dha.OnDesiredPropertyChange(new JObject());
                EnsureReportedPropertyStatus("VerifyHealth failed: Value cannot be null.");
            }

            {
                // Test 2:
                var jobj = new JObject();
                jobj.Add("ReportIntervalInSeconds", "string in an integer field");
                dha.OnDesiredPropertyChange(jobj);
                EnsureReportedPropertyStatus("VerifyHealth failed: Could not convert string to integer: string in an integer field. Path 'ReportIntervalInSeconds'.");
            }
        }