コード例 #1
0
        public void ValidateBridgeConfig_ValidInput()
        {
            var validator = new BrokerPropertiesValidator();

            EdgeHubDesiredProperties properties = ConfigTestData.GetTestData();

            IList <string> errors = validator.ValidateBridgeConfig(properties.BrokerConfiguration.Bridges);

            Assert.Equal(0, errors.Count);
        }
コード例 #2
0
        public void ValidateAuthorizationConfig_ValidInput()
        {
            var validator = new BrokerPropertiesValidator();

            EdgeHubDesiredProperties_1_2 properties = ConfigTestData.GetTestData();

            var authzProperties = properties.BrokerConfiguration.Authorizations;

            IList <string> errors = validator.ValidateAuthorizationConfig(authzProperties);

            Assert.Equal(0, errors.Count);
        }
コード例 #3
0
        public void ValidateAuthorizationConfig_EmptyResourceAllowedForConnectOperation()
        {
            var validator = new BrokerPropertiesValidator();

            EdgeHubDesiredProperties properties = ConfigTestData.GetTestData();

            var authzProperties = properties.BrokerConfiguration.Authorizations;

            // arrange connect op with no resources.
            authzProperties[0].Deny[0].Operations.Clear();
            authzProperties[0].Deny[0].Operations.Insert(0, "mqtt:connect");
            authzProperties[0].Deny[0].Resources.Clear();

            IList <string> errors = validator.ValidateAuthorizationConfig(authzProperties);

            Assert.Equal(0, errors.Count);
        }
コード例 #4
0
        public void ValidateAuthorizationConfig_InvalidTopicFilters()
        {
            var validator = new BrokerPropertiesValidator();

            EdgeHubDesiredProperties properties = ConfigTestData.GetTestData();

            var authzProperties = properties.BrokerConfiguration.Authorizations;

            // arrange some errors
            authzProperties[0].Deny[0].Resources[0]  = "topic/#/";
            authzProperties[1].Allow[0].Resources[0] = "topic+";

            IList <string> errors = validator.ValidateAuthorizationConfig(authzProperties);

            Assert.Equal(2, errors.Count);
            Assert.Equal("Statement 0: Deny: Resource (topic filter) is invalid: topic/#/", errors[0]);
            Assert.Equal("Statement 1: Allow: Resource (topic filter) is invalid: topic+", errors[1]);
        }
コード例 #5
0
        public void ValidateAuthorizationConfig_InvalidVariableNames()
        {
            var validator = new BrokerPropertiesValidator();

            EdgeHubDesiredProperties properties = ConfigTestData.GetTestData();

            var authzProperties = properties.BrokerConfiguration.Authorizations;

            // arrange some errors
            authzProperties[0].Identities[0]         = "{{anywhat}}";
            authzProperties[1].Allow[0].Resources[0] = "topic/{{invalid}}/{{myothervar}}";

            IList <string> errors = validator.ValidateAuthorizationConfig(authzProperties);

            Assert.Equal(3, errors.Count);
            Assert.Equal("Statement 0: Invalid variable name: {{anywhat}}", errors[0]);
            Assert.Equal("Statement 1: Invalid variable name: {{invalid}}", errors[1]);
            Assert.Equal("Statement 1: Invalid variable name: {{myothervar}}", errors[2]);
        }
コード例 #6
0
        public void ValidateAuthorizationConfig_InvalidOperation()
        {
            var validator = new BrokerPropertiesValidator();

            EdgeHubDesiredProperties properties = ConfigTestData.GetTestData();

            var authzProperties = properties.BrokerConfiguration.Authorizations;

            // arrange some errors
            authzProperties[0].Deny[0].Operations[0] = "invalid";

            IList <string> errors = validator.ValidateAuthorizationConfig(authzProperties);

            Assert.Equal(1, errors.Count);
            Assert.Equal(
                "Statement 0: Deny: Unknown mqtt operation: invalid. "
                + "List of supported operations: mqtt:publish, mqtt:subscribe, mqtt:connect",
                errors[0]);
        }
コード例 #7
0
        public void ValidateAuthorizationConfig_EmptyElements()
        {
            var validator = new BrokerPropertiesValidator();

            EdgeHubDesiredProperties properties = ConfigTestData.GetTestData();

            var authzProperties = properties.BrokerConfiguration.Authorizations;

            // arrange some errors
            authzProperties[0].Identities[0] = string.Empty;
            authzProperties[1].Allow[0].Operations.RemoveAt(0);
            authzProperties[1].Allow[0].Operations.RemoveAt(0);

            IList <string> errors = validator.ValidateAuthorizationConfig(properties.BrokerConfiguration.Authorizations);

            Assert.Equal(2, errors.Count);
            Assert.Equal("Statement 0: Identity name is invalid: ", errors[0]);
            Assert.Equal("Statement 1: Allow: Operations list must not be empty", errors[1]);
        }
コード例 #8
0
        public void GetEdgeHubConfig_ValidInput_MappingIsCorrect()
        {
            var validator = new Mock <BrokerPropertiesValidator>();

            validator
            .Setup(v => v.ValidateAuthorizationConfig(It.IsAny <AuthorizationProperties>()))
            .Returns(new List <string>());

            var routeFactory = new EdgeRouteFactory(new Mock <IEndpointFactory>().Object);
            var configParser = new EdgeHubConfigParser(routeFactory, validator.Object);

            EdgeHubDesiredProperties properties = ConfigTestData.GetTestData();

            // act
            EdgeHubConfig result = configParser.GetEdgeHubConfig(properties);

            // assert
            validator.Verify(v => v.ValidateAuthorizationConfig(properties.BrokerConfiguration.Authorizations), Times.Once());

            Assert.Equal("1.2.0", result.SchemaVersion);
            AuthorizationConfig authzConfig = result
                                              .BrokerConfiguration
                                              .Expect(() => new InvalidOperationException("missing broker config"))
                                              .Authorizations
                                              .Expect(() => new InvalidOperationException("missing authorization config"));

            Assert.Equal(3, authzConfig.Statements.Count);

            var result0 = authzConfig.Statements[0];

            Assert.Equal(Effect.Deny, result0.Effect);
            Assert.Equal(2, result0.Identities.Count);
            Assert.Equal("device_1", result0.Identities[0]);
            Assert.Equal("device_3", result0.Identities[1]);
            Assert.Equal(1, result0.Operations.Count);
            Assert.Equal("mqtt:publish", result0.Operations[0]);
            Assert.Equal(2, result0.Resources.Count);
            Assert.Equal("system/alerts/+", result0.Resources[0]);
            Assert.Equal("core/#", result0.Resources[1]);

            var result1 = authzConfig.Statements[1];

            Assert.Equal(Effect.Allow, result1.Effect);
            Assert.Equal(2, result1.Identities.Count);
            Assert.Equal("device_1", result1.Identities[0]);
            Assert.Equal("device_3", result1.Identities[1]);
            Assert.Equal(2, result1.Operations.Count);
            Assert.Equal("mqtt:publish", result1.Operations[0]);
            Assert.Equal("mqtt:subscribe", result1.Operations[1]);
            Assert.Equal(2, result1.Resources.Count);
            Assert.Equal("topic/a", result1.Resources[0]);
            Assert.Equal("topic/b", result1.Resources[1]);

            var result2 = authzConfig.Statements[2];

            Assert.Equal(Effect.Allow, result2.Effect);
            Assert.Equal(1, result2.Identities.Count);
            Assert.Equal("device_2", result2.Identities[0]);
            Assert.Equal(2, result2.Operations.Count);
            Assert.Equal("mqtt:publish", result2.Operations[0]);
            Assert.Equal("mqtt:subscribe", result2.Operations[1]);
            Assert.Equal(2, result2.Resources.Count);
            Assert.Equal("topic1", result2.Resources[0]);
            Assert.Equal("topic2", result2.Resources[1]);
        }