摸鱼周报 20231124

这一周都在疯狂打码。。。

主要实现了一个dsl语言的解析。

步骤

  1. 使用ANTLR4将语言解析成结构化对象。
    @Test
            public void projectionsTest() {
                    final var xql = "select name,id,createdAt,updatedAt from policy where id = '1' and name exists and name contains 'abc' and createdAt before 2023-05-20 12:00:00";
                    final Query parse = XQL.parse(xql);
                    Assert.assertEquals(parse, Query.builder()
                                    .projections(List.of("name", "id", "createdAt", "updatedAt"))
                                    .type(DataType.POLICY)
                                    .filter(CompositeFilterOperator.and(
                                                    new PropertyFilter("id", Operator.EQ, "1"),
                                                    new PropertyFilter("name", Operator.EXISTS, ""),
                                                    new PropertyFilter("name", Operator.CONTAINS, "abc"),
                                                    new PropertyFilter("createdAt", Operator.BEFORE,
                                                                    "2023-05-20 12:00:00")))
                                    .build());
            }
    
            @Test
            public void emptyProjectionsTest() {
                    final var xql = "select from policy where id = '1' and name not exists and action like 'de' and src.ip x ('1.2.3.4','2.3.3.4')";
                    final Query parse = XQL.parse(xql);
                    Assert.assertEquals(parse, Query.builder()
                                    .type(DataType.POLICY)
                                    .filter(CompositeFilterOperator.and(
                                                    new PropertyFilter("id", Operator.EQ, "1"),
                                                    new PropertyFilter("name", Operator.NOT_EXISTS, ""),
                                                    new PropertyFilter("action", Operator.LIKE, "de"),
                                                    new PropertyFilter("src.ip", Operator.INTERSECT_WITH,
                                                                    List.of("1.2.3.4", "2.3.3.4"))))
                                    .build());
            }
    
  2. 编写算子。
        @Test
        public void policyOpTest() {
            Policy policy = createSamplePolicy();
            var xql = "select * from policy where enabled = true and src.ip in '192.168.0.0/16' and dst.ip ~ '3.3.3.5'";
            var query = XQL.parse(xql);
            var operator = new PolicyQueryOperator();
            Assert.assertTrue(operator.op(policy, query));
    
            xql = "select * from policy where src.user = ('fangcong1','gakkiyomi') or (src.ip = '192.168.0.0/16' and dst.ip ~ '3.3.3.5')";
            query = XQL.parse(xql);
            Assert.assertFalse(operator.op(policy, query));
    
            xql = "select * from policy where (src.ip in '192.168.1.2' or description regex '^this(.*)') or enabled = false";
            query = XQL.parse(xql);
            Assert.assertTrue(operator.op(policy, query));
        }
    
        // 添加更多的测试用例...
    
        private Policy createSamplePolicy() {
            Policy policy = new Policy();
            policy.setId(UUID.randomUUID());
            policy.setName("SamplePolicy");
            policy.setDescription("this is test");
            policy.setPolicyId("12345");
            policy.setTable("CP");
            policy.setEnabled(true);
            policy.setAction(ActionType.ALLOW);
            policy.setSection("Section1");
            policy.setLine(1);
            policy.setHitCount(100000L);
            policy.setSrcZone(Arrays.asList("zone1", "zone2"));
            policy.setSrcInf("eth0");
            policy.setDstInf("eth2");
            policy.setDstZone(Arrays.asList("zone3", "zone4"));
            policy.setSrcAddr(List.of("192.168.2.0/24", "192.168.1.0/24"));
            policy.setSrcUser(List.of("fangcong", "gakkiyomi"));
            policy.setDstAddr(List.of("range"));
            policy.setServices(List.of("any"));
            policy.setSourceNegate(true);
            policy.setSrcAddrObjects(List.of(
                    new Address.Builder()
                            .setType(ObjectType.object)
                            .setValues(List.of(
                                    new AddressValue.Builder()
                                            .setType(AddressType.host)
                                            .setValue(new WildcardValue("192.168.2.0/24"))
                                            .build()))
                            .build(),
                    new Address.Builder()
                            .setType(ObjectType.object)
                            .setValues(List.of(
                                    new AddressValue.Builder()
                                            .setType(AddressType.subnet)
                                            .setValue(new WildcardValue("192.168.1.0/24"))
                                            .build()))
                            .build()));
            policy.setDstAddrObjects(List.of(
                    new Address.Builder()
                            .setName("range")
                            .setType(ObjectType.object)
                            .setValues(List.of(
                                    new AddressValue.Builder()
                                            .setType(AddressType.range)
                                            .setValue(new RangeValue("3.3.3.3", "3.3.3.44"))
                                            .build()))
                            .build()));
            return policy;
        }
    

但是测试用例全是❤️️❤️️❤️️❤️️❤️️❤️️❤️️生成的,也让我周五可以摸一下鱼。

 
  • 摸鱼周报

    每周五的摸鱼派活动-摸鱼周报 发表你的每周总结

    290 引用