This is an automated email from the ASF dual-hosted git repository. shown pushed a commit to branch 0308-yuluo/feat in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
commit d164a52ec6e4afda2c2f3c5d05bedeec74078dad Author: yuluo-yx <[email protected]> AuthorDate: Sun Mar 8 15:28:29 2026 +0800 feat[common]: add protocl entity check Signed-off-by: yuluo-yx <[email protected]> --- .../common/entity/job/protocol/IcmpProtocol.java | 11 +- .../entity/job/protocol/MemcachedProtocol.java | 7 +- .../entity/job/protocol/PrometheusProtocol.java | 29 ++- .../common/entity/job/protocol/TelnetProtocol.java | 12 +- .../common/entity/job/protocol/UdpProtocol.java | 20 +- .../entity/job/protocol/IcmpProtocolTest.java | 118 +++++++++++ .../entity/job/protocol/MemcachedProtocolTest.java | 107 ++++++++++ .../job/protocol/PrometheusProtocolTest.java | 230 +++++++++++++++++++++ .../entity/job/protocol/TelnetProtocolTest.java | 177 ++++++++++++++++ .../entity/job/protocol/UdpProtocolTest.java | 207 +++++++++++++++++++ 10 files changed, 905 insertions(+), 13 deletions(-) diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IcmpProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IcmpProtocol.java index 4133eb7480..d653396d6a 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IcmpProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IcmpProtocol.java @@ -17,10 +17,14 @@ package org.apache.hertzbeat.common.entity.job.protocol; +import static org.apache.hertzbeat.common.util.IpDomainUtil.validateIpDomain; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import org.apache.commons.lang3.StringUtils; +import org.apache.hertzbeat.common.util.CommonUtil; /** * ICMP (PING) PROTOCOL CONFIGURATION @@ -47,8 +51,9 @@ public class IcmpProtocol implements CommonRequestProtocol, Protocol { @Override public boolean isInvalid() { - - // todo: add - return true; + if (!validateIpDomain(host)) { + return true; + } + return StringUtils.isNotBlank(timeout) && !CommonUtil.isNumeric(timeout); } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocol.java index 6384600b94..0b76de7d5d 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocol.java @@ -17,6 +17,9 @@ package org.apache.hertzbeat.common.entity.job.protocol; +import static org.apache.hertzbeat.common.util.IpDomainUtil.validPort; +import static org.apache.hertzbeat.common.util.IpDomainUtil.validateIpDomain; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -43,9 +46,7 @@ public class MemcachedProtocol implements CommonRequestProtocol, Protocol { @Override public boolean isInvalid() { - - // todo: add - return true; + return !validateIpDomain(host) || !validPort(port); } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PrometheusProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PrometheusProtocol.java index 3411a2b56f..d460d5c4f1 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PrometheusProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PrometheusProtocol.java @@ -17,11 +17,16 @@ package org.apache.hertzbeat.common.entity.job.protocol; +import static org.apache.hertzbeat.common.util.IpDomainUtil.validPort; +import static org.apache.hertzbeat.common.util.IpDomainUtil.validateIpDomain; + import java.util.Map; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import org.apache.commons.lang3.StringUtils; +import org.apache.hertzbeat.common.util.CommonUtil; /** * Prometheus Protocol configuration @@ -107,8 +112,28 @@ public class PrometheusProtocol implements CommonRequestProtocol, Protocol { @Override public boolean isInvalid() { + if (!validateIpDomain(host) || !validPort(port)) { + return true; + } + if (StringUtils.isNotBlank(timeout) && !CommonUtil.isNumeric(timeout)) { + return true; + } + if (StringUtils.isNotBlank(ssl) && !"true".equalsIgnoreCase(ssl) && !"false".equalsIgnoreCase(ssl)) { + return true; + } + if (StringUtils.isNotBlank(method) && !isValidHttpMethod(method)) { + return true; + } + return false; + } - // todo: add - return true; + private boolean isValidHttpMethod(String method) { + return method.equalsIgnoreCase("get") + || method.equalsIgnoreCase("post") + || method.equalsIgnoreCase("put") + || method.equalsIgnoreCase("delete") + || method.equalsIgnoreCase("patch") + || method.equalsIgnoreCase("head") + || method.equalsIgnoreCase("options"); } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/TelnetProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/TelnetProtocol.java index a485c87b02..395db2ef84 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/TelnetProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/TelnetProtocol.java @@ -17,10 +17,15 @@ package org.apache.hertzbeat.common.entity.job.protocol; +import static org.apache.hertzbeat.common.util.IpDomainUtil.validPort; +import static org.apache.hertzbeat.common.util.IpDomainUtil.validateIpDomain; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import org.apache.commons.lang3.StringUtils; +import org.apache.hertzbeat.common.util.CommonUtil; /** * telnet Protocol configuration @@ -53,9 +58,10 @@ public class TelnetProtocol implements CommonRequestProtocol, Protocol { @Override public boolean isInvalid() { - - // todo: add - return true; + if (!validateIpDomain(host) || !validPort(port)) { + return true; + } + return StringUtils.isNotBlank(timeout) && !CommonUtil.isNumeric(timeout); } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/UdpProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/UdpProtocol.java index f062b39fb1..73e7d3e541 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/UdpProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/UdpProtocol.java @@ -17,10 +17,15 @@ package org.apache.hertzbeat.common.entity.job.protocol; +import static org.apache.hertzbeat.common.util.IpDomainUtil.validPort; +import static org.apache.hertzbeat.common.util.IpDomainUtil.validateIpDomain; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import org.apache.commons.lang3.StringUtils; +import org.apache.hertzbeat.common.util.CommonUtil; /** * Use udp implemented by socket for service port availability detection @@ -52,8 +57,19 @@ public class UdpProtocol implements CommonRequestProtocol, Protocol { @Override public boolean isInvalid() { + if (!validateIpDomain(host) || !validPort(port)) { + return true; + } + if (StringUtils.isNotBlank(timeout) && !CommonUtil.isNumeric(timeout)) { + return true; + } + if (StringUtils.isNotBlank(content) && !isValidHexString(content)) { + return true; + } + return false; + } - // todo: add - return true; + private boolean isValidHexString(String hex) { + return hex.matches("^[0-9a-fA-F]+$"); } } diff --git a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/IcmpProtocolTest.java b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/IcmpProtocolTest.java new file mode 100644 index 0000000000..96aae06a89 --- /dev/null +++ b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/IcmpProtocolTest.java @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.common.entity.job.protocol; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class IcmpProtocolTest { + + @Test + void isInvalidValidProtocol() { + IcmpProtocol protocol = IcmpProtocol.builder() + .host("192.168.1.1") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithDomain() { + IcmpProtocol protocol = IcmpProtocol.builder() + .host("example.com") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithLocalhost() { + IcmpProtocol protocol = IcmpProtocol.builder() + .host("localhost") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithIpv6() { + IcmpProtocol protocol = IcmpProtocol.builder() + .host("::1") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidHost() { + IcmpProtocol protocol = IcmpProtocol.builder() + .host("") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidNullHost() { + IcmpProtocol protocol = IcmpProtocol.builder() + .host(null) + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidDomain() { + IcmpProtocol protocol = IcmpProtocol.builder() + .host("invalid..domain") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidValidTimeout() { + IcmpProtocol protocol = IcmpProtocol.builder() + .host("192.168.1.1") + .timeout("5000") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidBlankTimeout() { + IcmpProtocol protocol = IcmpProtocol.builder() + .host("192.168.1.1") + .timeout("") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidNullTimeout() { + IcmpProtocol protocol = IcmpProtocol.builder() + .host("192.168.1.1") + .timeout(null) + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidTimeout() { + IcmpProtocol protocol = IcmpProtocol.builder() + .host("192.168.1.1") + .timeout("abc") + .build(); + assertTrue(protocol.isInvalid()); + } +} \ No newline at end of file diff --git a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocolTest.java b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocolTest.java new file mode 100644 index 0000000000..950a9f94ca --- /dev/null +++ b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocolTest.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.common.entity.job.protocol; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class MemcachedProtocolTest { + + @Test + void isInvalidValidProtocol() { + MemcachedProtocol protocol = MemcachedProtocol.builder() + .host("192.168.1.1") + .port("11211") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithDomain() { + MemcachedProtocol protocol = MemcachedProtocol.builder() + .host("memcached.example.com") + .port("11211") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithLocalhost() { + MemcachedProtocol protocol = MemcachedProtocol.builder() + .host("localhost") + .port("11211") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithIpv6() { + MemcachedProtocol protocol = MemcachedProtocol.builder() + .host("::1") + .port("11211") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidHost() { + MemcachedProtocol protocol = MemcachedProtocol.builder() + .host("") + .port("11211") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidNullHost() { + MemcachedProtocol protocol = MemcachedProtocol.builder() + .host(null) + .port("11211") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidPort() { + MemcachedProtocol protocol = MemcachedProtocol.builder() + .host("192.168.1.1") + .port("99999") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidBlankPort() { + MemcachedProtocol protocol = MemcachedProtocol.builder() + .host("192.168.1.1") + .port("") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidNullPort() { + MemcachedProtocol protocol = MemcachedProtocol.builder() + .host("192.168.1.1") + .port(null) + .build(); + assertTrue(protocol.isInvalid()); + } +} \ No newline at end of file diff --git a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/PrometheusProtocolTest.java b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/PrometheusProtocolTest.java new file mode 100644 index 0000000000..33c0edfde1 --- /dev/null +++ b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/PrometheusProtocolTest.java @@ -0,0 +1,230 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.common.entity.job.protocol; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class PrometheusProtocolTest { + + @Test + void isInvalidValidProtocol() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("9090") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithDomain() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("prometheus.example.com") + .port("9090") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithLocalhost() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("localhost") + .port("9090") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithIpv6() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("::1") + .port("9090") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidHost() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("") + .port("9090") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidNullHost() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host(null) + .port("9090") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidPort() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("99999") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidBlankPort() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidNullPort() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port(null) + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidValidTimeout() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("9090") + .timeout("5000") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidBlankTimeout() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("9090") + .timeout("") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidNullTimeout() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("9090") + .timeout(null) + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidTimeout() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("9090") + .timeout("abc") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidValidSsl() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("9090") + .ssl("true") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidSslFalse() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("9090") + .ssl("false") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidSsl() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("9090") + .ssl("invalid") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidBlankSsl() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("9090") + .ssl("") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidMethod() { + String[] validMethods = {"get", "GET", "post", "POST", "put", "PUT", "delete", "DELETE", "patch", "PATCH", "head", "HEAD", "options", "OPTIONS"}; + for (String method : validMethods) { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("9090") + .method(method) + .build(); + assertFalse(protocol.isInvalid(), "Method " + method + " should be valid"); + } + } + + @Test + void isInvalidInvalidMethod() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("9090") + .method("invalid") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidBlankMethod() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("9090") + .method("") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidPath() { + PrometheusProtocol protocol = PrometheusProtocol.builder() + .host("192.168.1.1") + .port("9090") + .path("/metrics") + .build(); + assertFalse(protocol.isInvalid()); + } +} \ No newline at end of file diff --git a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/TelnetProtocolTest.java b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/TelnetProtocolTest.java new file mode 100644 index 0000000000..774b84685b --- /dev/null +++ b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/TelnetProtocolTest.java @@ -0,0 +1,177 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.common.entity.job.protocol; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class TelnetProtocolTest { + + @Test + void isInvalidValidProtocol() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("192.168.1.1") + .port("23") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithDomain() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("telnet.example.com") + .port("23") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithLocalhost() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("localhost") + .port("23") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithIpv6() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("::1") + .port("23") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidHost() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("") + .port("23") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidNullHost() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host(null) + .port("23") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidPort() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("192.168.1.1") + .port("99999") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidBlankPort() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("192.168.1.1") + .port("") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidNullPort() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("192.168.1.1") + .port(null) + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidValidTimeout() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("192.168.1.1") + .port("23") + .timeout("5000") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidBlankTimeout() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("192.168.1.1") + .port("23") + .timeout("") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidNullTimeout() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("192.168.1.1") + .port("23") + .timeout(null) + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidTimeout() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("192.168.1.1") + .port("23") + .timeout("abc") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidValidCmd() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("192.168.1.1") + .port("23") + .cmd("ls -la") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidBlankCmd() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("192.168.1.1") + .port("23") + .cmd("") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidNullCmd() { + TelnetProtocol protocol = TelnetProtocol.builder() + .host("192.168.1.1") + .port("23") + .cmd(null) + .build(); + assertFalse(protocol.isInvalid()); + } +} \ No newline at end of file diff --git a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/UdpProtocolTest.java b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/UdpProtocolTest.java new file mode 100644 index 0000000000..96cfc194bf --- /dev/null +++ b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/UdpProtocolTest.java @@ -0,0 +1,207 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.common.entity.job.protocol; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class UdpProtocolTest { + + @Test + void isInvalidValidProtocol() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port("53") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithDomain() { + UdpProtocol protocol = UdpProtocol.builder() + .host("dns.example.com") + .port("53") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithLocalhost() { + UdpProtocol protocol = UdpProtocol.builder() + .host("localhost") + .port("53") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidProtocolWithIpv6() { + UdpProtocol protocol = UdpProtocol.builder() + .host("::1") + .port("53") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidHost() { + UdpProtocol protocol = UdpProtocol.builder() + .host("") + .port("53") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidNullHost() { + UdpProtocol protocol = UdpProtocol.builder() + .host(null) + .port("53") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidPort() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port("99999") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidBlankPort() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port("") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidNullPort() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port(null) + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidValidTimeout() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port("53") + .timeout("5000") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidBlankTimeout() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port("53") + .timeout("") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidNullTimeout() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port("53") + .timeout(null) + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidTimeout() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port("53") + .timeout("abc") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidValidContent() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port("53") + .content("48454C4C4F") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidValidContentLowercase() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port("53") + .content("48454c4c4f") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidBlankContent() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port("53") + .content("") + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidNullContent() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port("53") + .content(null) + .build(); + assertFalse(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidContent() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port("53") + .content("GGGG") + .build(); + assertTrue(protocol.isInvalid()); + } + + @Test + void isInvalidInvalidContentWithSpecialChar() { + UdpProtocol protocol = UdpProtocol.builder() + .host("192.168.1.1") + .port("53") + .content("48454C4C4F!") + .build(); + assertTrue(protocol.isInvalid()); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
