This is an automated email from the ASF dual-hosted git repository.
gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new 5c2950d838 feat[common]: add protocl entity check (#4055)
5c2950d838 is described below
commit 5c2950d838e72a53c158db0e905536922c27d787
Author: shown <[email protected]>
AuthorDate: Sun Mar 8 19:31:18 2026 +0800
feat[common]: add protocl entity check (#4055)
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/DnsSdProtocolTest.java | 2 +-
.../entity/job/protocol/HttpSdProtocolTest.java | 2 +-
.../entity/job/protocol/IcmpProtocolTest.java | 118 +++++++++++
...rotocolTest.java => MemcachedProtocolTest.java} | 69 ++++---
.../entity/job/protocol/ModbusProtocolTest.java | 2 +-
.../entity/job/protocol/PlcProtocolTest.java | 2 +-
.../job/protocol/PrometheusProtocolTest.java | 230 +++++++++++++++++++++
.../common/entity/job/protocol/S7ProtocolTest.java | 2 +-
.../entity/job/protocol/TelnetProtocolTest.java | 177 ++++++++++++++++
.../entity/job/protocol/UdpProtocolTest.java | 207 +++++++++++++++++++
15 files changed, 846 insertions(+), 44 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/DnsSdProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/DnsSdProtocolTest.java
index 2194178018..d3e9097cf2 100644
---
a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/DnsSdProtocolTest.java
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/DnsSdProtocolTest.java
@@ -136,4 +136,4 @@ class DnsSdProtocolTest {
.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/HttpSdProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocolTest.java
index ec72018404..0a917594a9 100644
---
a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocolTest.java
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocolTest.java
@@ -87,4 +87,4 @@ class HttpSdProtocolTest {
.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/IcmpProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/IcmpProtocolTest.java
new file mode 100644
index 0000000000..0d9d330ad9
--- /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());
+ }
+}
diff --git
a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocolTest.java
similarity index 51%
copy from
hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocolTest.java
copy to
hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocolTest.java
index ec72018404..59b1ff0893 100644
---
a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocolTest.java
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocolTest.java
@@ -22,69 +22,86 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-class HttpSdProtocolTest {
+class MemcachedProtocolTest {
@Test
- void isInvalidValidHttpUrl() {
- HttpSdProtocol protocol = HttpSdProtocol.builder()
- .url("http://example.com/api/discovery")
+ void isInvalidValidProtocol() {
+ MemcachedProtocol protocol = MemcachedProtocol.builder()
+ .host("192.168.1.1")
+ .port("11211")
.build();
assertFalse(protocol.isInvalid());
}
@Test
- void isInvalidValidHttpsUrl() {
- HttpSdProtocol protocol = HttpSdProtocol.builder()
- .url("https://example.com/api/discovery")
+ void isInvalidValidProtocolWithDomain() {
+ MemcachedProtocol protocol = MemcachedProtocol.builder()
+ .host("memcached.example.com")
+ .port("11211")
.build();
assertFalse(protocol.isInvalid());
}
@Test
- void isInvalidValidUrlWithPort() {
- HttpSdProtocol protocol = HttpSdProtocol.builder()
- .url("http://example.com:8080/api/discovery")
+ void isInvalidValidProtocolWithLocalhost() {
+ MemcachedProtocol protocol = MemcachedProtocol.builder()
+ .host("localhost")
+ .port("11211")
.build();
assertFalse(protocol.isInvalid());
}
@Test
- void isInvalidBlankUrl() {
- HttpSdProtocol protocol = HttpSdProtocol.builder()
- .url("")
+ 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 isInvalidNullUrl() {
- HttpSdProtocol protocol = HttpSdProtocol.builder()
- .url(null)
+ void isInvalidNullHost() {
+ MemcachedProtocol protocol = MemcachedProtocol.builder()
+ .host(null)
+ .port("11211")
.build();
assertTrue(protocol.isInvalid());
}
@Test
- void isInvalidInvalidUrlNoProtocol() {
- HttpSdProtocol protocol = HttpSdProtocol.builder()
- .url("example.com/api/discovery")
+ void isInvalidInvalidPort() {
+ MemcachedProtocol protocol = MemcachedProtocol.builder()
+ .host("192.168.1.1")
+ .port("99999")
.build();
assertTrue(protocol.isInvalid());
}
@Test
- void isInvalidInvalidUrlWrongProtocol() {
- HttpSdProtocol protocol = HttpSdProtocol.builder()
- .url("ftp://example.com/api/discovery")
+ void isInvalidBlankPort() {
+ MemcachedProtocol protocol = MemcachedProtocol.builder()
+ .host("192.168.1.1")
+ .port("")
.build();
assertTrue(protocol.isInvalid());
}
@Test
- void isInvalidInvalidUrlOnlyProtocol() {
- HttpSdProtocol protocol = HttpSdProtocol.builder()
- .url("http://")
+ 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/ModbusProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/ModbusProtocolTest.java
index 645e6afdf7..e5a93697eb 100644
---
a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/ModbusProtocolTest.java
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/ModbusProtocolTest.java
@@ -158,4 +158,4 @@ class ModbusProtocolTest {
.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/PlcProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/PlcProtocolTest.java
index dbd76fe8bf..0eb4393119 100644
---
a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/PlcProtocolTest.java
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/PlcProtocolTest.java
@@ -158,4 +158,4 @@ class PlcProtocolTest {
.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..9390dd0a09
--- /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());
+ }
+}
diff --git
a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/S7ProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/S7ProtocolTest.java
index 205a3ead9a..9d82e843f7 100644
---
a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/S7ProtocolTest.java
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/S7ProtocolTest.java
@@ -208,4 +208,4 @@ class S7ProtocolTest {
.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/TelnetProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/TelnetProtocolTest.java
new file mode 100644
index 0000000000..8892746adb
--- /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());
+ }
+}
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..b96f0cfeee
--- /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());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]