Пример #1
0
        private static NetworkAclConfig ParseAcl(IConfiguration config, string section)
        {
            var acl = new NetworkAclConfig();

            var aclSection = config?.GetSection(section);

            acl.Path = ParseString(aclSection, "path");
            acl.Type = ParseEnum(aclSection, "type", NetworkAclType.None);

            return(acl);
        }
Пример #2
0
        public void IsAllowed_Blacklist()
        {
            // Arrange
            var aclFactory = AutoMockContainer.Create <NetworkAclLoader>();
            var cfg        = new NetworkAclConfig();
            var tmpRules   = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.json");

            File.WriteAllText(tmpRules, @"[{'value':'192\\.168\\.8\\..*','regex':true},{'value':'192.168.7.1','regex':false}]");
            cfg.Path = tmpRules;
            cfg.Type = NetworkAclType.Blacklist;

            // Act
            var acl = aclFactory.Load(cfg);

            File.Delete(tmpRules);

            // Assert
            for (var i = 0; i < 256; ++i)
            {
                acl.IsAllowed("192.168.8." + i).Should().BeFalse();
                acl.IsAllowed("192.168." + i + ".1").Should().Be(i != 8 && i != 7);
                acl.IsAllowed("192.168.7." + i).Should().Be(i != 1);
            }
        }