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 35b79cf649 feat[common]: add hertzbeat common protocol entitiy check
(#4052)
35b79cf649 is described below
commit 35b79cf64935cdf22f6cd01fb7cd8cbe486bb2fd
Author: shown <[email protected]>
AuthorDate: Sat Mar 7 11:56:12 2026 +0800
feat[common]: add hertzbeat common protocol entitiy check (#4052)
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: Tomsun28 <[email protected]>
---
.../entity/job/protocol/CommonRequestProtocol.java | 2 +-
.../common/entity/job/protocol/DnsSdProtocol.java | 16 +-
.../common/entity/job/protocol/HttpSdProtocol.java | 8 +-
.../common/entity/job/protocol/ModbusProtocol.java | 18 +-
.../common/entity/job/protocol/PlcProtocol.java | 18 +-
.../common/entity/job/protocol/S7Protocol.java | 21 +-
.../entity/job/protocol/DnsSdProtocolTest.java | 139 ++++++++++++++
.../entity/job/protocol/HttpSdProtocolTest.java | 90 +++++++++
.../entity/job/protocol/ModbusProtocolTest.java | 161 ++++++++++++++++
.../entity/job/protocol/PlcProtocolTest.java | 161 ++++++++++++++++
.../common/entity/job/protocol/S7ProtocolTest.java | 211 +++++++++++++++++++++
11 files changed, 830 insertions(+), 15 deletions(-)
diff --git
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/CommonRequestProtocol.java
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/CommonRequestProtocol.java
index b5e0edd6ef..74cb93a1da 100644
---
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/CommonRequestProtocol.java
+++
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/CommonRequestProtocol.java
@@ -18,7 +18,7 @@
package org.apache.hertzbeat.common.entity.job.protocol;
/**
- * Define common field method for each protocol in {@link
org.apache.hertzbeat.common.entity.job.Metrics}
+ * Define common field method for each protocol in Metrics.
*/
public interface CommonRequestProtocol {
diff --git
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/DnsSdProtocol.java
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/DnsSdProtocol.java
index b2d00590ed..f2a5089552 100644
---
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/DnsSdProtocol.java
+++
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/DnsSdProtocol.java
@@ -21,6 +21,10 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
+import org.apache.commons.lang3.StringUtils;
+
+import static org.apache.hertzbeat.common.util.IpDomainUtil.validPort;
+import static org.apache.hertzbeat.common.util.IpDomainUtil.validateIpDomain;
/**
* Dns protocol
@@ -41,8 +45,16 @@ public class DnsSdProtocol implements Protocol {
@Override
public boolean isInvalid() {
+ if (!validateIpDomain(host) || !validPort(port)) {
+ return true;
+ }
+ if (StringUtils.isAnyBlank(recordType, recordName)) {
+ return true;
+ }
+ return !isValidRecordType(recordType);
+ }
- // todo: add
- return true;
+ private boolean isValidRecordType(String type) {
+ return type.matches("^(?i)(A|AAAA|CNAME|MX|TXT|NS|SRV|PTR|SOA|CAA)$");
}
}
diff --git
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocol.java
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocol.java
index a90751df81..e30a1f9fea 100644
---
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocol.java
+++
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocol.java
@@ -21,6 +21,7 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
+import org.apache.commons.lang3.StringUtils;
/**
* Dns protocol
@@ -35,8 +36,9 @@ public class HttpSdProtocol implements Protocol {
@Override
public boolean isInvalid() {
-
- // todo: add
- return true;
+ if (StringUtils.isBlank(url)) {
+ return true;
+ }
+ return !url.matches("^(?i)https?://.+");
}
}
diff --git
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ModbusProtocol.java
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ModbusProtocol.java
index b08840dce3..4d7a08a779 100644
---
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ModbusProtocol.java
+++
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ModbusProtocol.java
@@ -21,9 +21,14 @@ 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;
import java.util.List;
+import static org.apache.hertzbeat.common.util.IpDomainUtil.validPort;
+import static org.apache.hertzbeat.common.util.IpDomainUtil.validateIpDomain;
+
/**
* Modbus Protocol
@@ -55,8 +60,15 @@ public class ModbusProtocol implements Protocol {
@Override
public boolean isInvalid() {
-
- // todo: add
- return true;
+ if (!validateIpDomain(host) || !validPort(port)) {
+ return true;
+ }
+ if (StringUtils.isAnyBlank(driverName, addressSyntax, slaveId)) {
+ return true;
+ }
+ if (StringUtils.isNotBlank(timeout) && !CommonUtil.isNumeric(timeout))
{
+ return true;
+ }
+ return registerAddresses == null || registerAddresses.isEmpty();
}
}
diff --git
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PlcProtocol.java
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PlcProtocol.java
index bbcc01b1c9..51024b198e 100644
---
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PlcProtocol.java
+++
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PlcProtocol.java
@@ -22,9 +22,14 @@ 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;
import java.util.List;
+import static org.apache.hertzbeat.common.util.IpDomainUtil.validPort;
+import static org.apache.hertzbeat.common.util.IpDomainUtil.validateIpDomain;
+
/**
* Plc Protocol
*/
@@ -54,9 +59,16 @@ public class PlcProtocol implements Protocol {
@Override
public boolean isInvalid() {
-
- // todo: add
- return true;
+ if (!validateIpDomain(host) || !validPort(port)) {
+ return true;
+ }
+ if (StringUtils.isAnyBlank(driverName, addressSyntax, slaveId)) {
+ return true;
+ }
+ if (StringUtils.isNotBlank(timeout) && !CommonUtil.isNumeric(timeout))
{
+ return true;
+ }
+ return registerAddresses == null || registerAddresses.isEmpty();
}
}
diff --git
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/S7Protocol.java
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/S7Protocol.java
index bfcd18d0ab..d14b75be9c 100644
---
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/S7Protocol.java
+++
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/S7Protocol.java
@@ -21,9 +21,14 @@ 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;
import java.util.List;
+import static org.apache.hertzbeat.common.util.IpDomainUtil.validPort;
+import static org.apache.hertzbeat.common.util.IpDomainUtil.validateIpDomain;
+
/**
* Modbus Protocol
@@ -59,8 +64,18 @@ public class S7Protocol implements Protocol {
@Override
public boolean isInvalid() {
-
- // todo: add
- return true;
+ if (!validateIpDomain(host) || !validPort(port)) {
+ return true;
+ }
+ if (StringUtils.isAnyBlank(driverName, addressSyntax, controllerType))
{
+ return true;
+ }
+ if (!CommonUtil.isNumeric(rackId) || !CommonUtil.isNumeric(slotId)) {
+ return true;
+ }
+ if (StringUtils.isNotBlank(timeout) && !CommonUtil.isNumeric(timeout))
{
+ return true;
+ }
+ return registerAddresses == null || registerAddresses.isEmpty();
}
}
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
new file mode 100644
index 0000000000..2194178018
--- /dev/null
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/DnsSdProtocolTest.java
@@ -0,0 +1,139 @@
+/*
+ * 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 DnsSdProtocolTest {
+
+ @Test
+ void isInvalidValidProtocol() {
+ DnsSdProtocol protocol = DnsSdProtocol.builder()
+ .host("192.168.1.1")
+ .port("53")
+ .recordType("A")
+ .recordName("example.com")
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidValidProtocolWithIpv6() {
+ DnsSdProtocol protocol = DnsSdProtocol.builder()
+ .host("::1")
+ .port("53")
+ .recordType("AAAA")
+ .recordName("example.com")
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidValidProtocolWithLocalhost() {
+ DnsSdProtocol protocol = DnsSdProtocol.builder()
+ .host("localhost")
+ .port("53")
+ .recordType("CNAME")
+ .recordName("www.example.com")
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidHost() {
+ DnsSdProtocol protocol = DnsSdProtocol.builder()
+ .host("")
+ .port("53")
+ .recordType("A")
+ .recordName("example.com")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidPort() {
+ DnsSdProtocol protocol = DnsSdProtocol.builder()
+ .host("192.168.1.1")
+ .port("99999")
+ .recordType("A")
+ .recordName("example.com")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankRecordType() {
+ DnsSdProtocol protocol = DnsSdProtocol.builder()
+ .host("192.168.1.1")
+ .port("53")
+ .recordType("")
+ .recordName("example.com")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankRecordName() {
+ DnsSdProtocol protocol = DnsSdProtocol.builder()
+ .host("192.168.1.1")
+ .port("53")
+ .recordType("A")
+ .recordName("")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidRecordType() {
+ DnsSdProtocol protocol = DnsSdProtocol.builder()
+ .host("192.168.1.1")
+ .port("53")
+ .recordType("INVALID")
+ .recordName("example.com")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidValidRecordTypes() {
+ String[] validTypes = {"A", "AAAA", "CNAME", "MX", "TXT", "NS", "SRV",
"PTR", "SOA", "CAA"};
+ for (String type : validTypes) {
+ DnsSdProtocol protocol = DnsSdProtocol.builder()
+ .host("192.168.1.1")
+ .port("53")
+ .recordType(type)
+ .recordName("example.com")
+ .build();
+ assertFalse(protocol.isInvalid(), "Record type " + type + " should
be valid");
+ }
+ }
+
+ @Test
+ void isInvalidValidRecordTypesCaseInsensitive() {
+ DnsSdProtocol protocol = DnsSdProtocol.builder()
+ .host("192.168.1.1")
+ .port("53")
+ .recordType("a")
+ .recordName("example.com")
+ .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
new file mode 100644
index 0000000000..ec72018404
--- /dev/null
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocolTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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 HttpSdProtocolTest {
+
+ @Test
+ void isInvalidValidHttpUrl() {
+ HttpSdProtocol protocol = HttpSdProtocol.builder()
+ .url("http://example.com/api/discovery")
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidValidHttpsUrl() {
+ HttpSdProtocol protocol = HttpSdProtocol.builder()
+ .url("https://example.com/api/discovery")
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidValidUrlWithPort() {
+ HttpSdProtocol protocol = HttpSdProtocol.builder()
+ .url("http://example.com:8080/api/discovery")
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankUrl() {
+ HttpSdProtocol protocol = HttpSdProtocol.builder()
+ .url("")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidNullUrl() {
+ HttpSdProtocol protocol = HttpSdProtocol.builder()
+ .url(null)
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidUrlNoProtocol() {
+ HttpSdProtocol protocol = HttpSdProtocol.builder()
+ .url("example.com/api/discovery")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidUrlWrongProtocol() {
+ HttpSdProtocol protocol = HttpSdProtocol.builder()
+ .url("ftp://example.com/api/discovery")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidUrlOnlyProtocol() {
+ HttpSdProtocol protocol = HttpSdProtocol.builder()
+ .url("http://")
+ .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
new file mode 100644
index 0000000000..645e6afdf7
--- /dev/null
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/ModbusProtocolTest.java
@@ -0,0 +1,161 @@
+/*
+ * 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 java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class ModbusProtocolTest {
+
+ @Test
+ void isInvalidValidProtocol() {
+ ModbusProtocol protocol = ModbusProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .registerAddresses(Arrays.asList("40001", "40002"))
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidValidProtocolWithTimeout() {
+ ModbusProtocol protocol = ModbusProtocol.builder()
+ .host("localhost")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .timeout("5000")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidHost() {
+ ModbusProtocol protocol = ModbusProtocol.builder()
+ .host("")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidPort() {
+ ModbusProtocol protocol = ModbusProtocol.builder()
+ .host("192.168.1.1")
+ .port("99999")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankDriverName() {
+ ModbusProtocol protocol = ModbusProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankAddressSyntax() {
+ ModbusProtocol protocol = ModbusProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("")
+ .slaveId("1")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankSlaveId() {
+ ModbusProtocol protocol = ModbusProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidNonNumericTimeout() {
+ ModbusProtocol protocol = ModbusProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .timeout("abc")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidEmptyRegisterAddresses() {
+ ModbusProtocol protocol = ModbusProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .registerAddresses(Collections.emptyList())
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidNullRegisterAddresses() {
+ ModbusProtocol protocol = ModbusProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .registerAddresses(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/PlcProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/PlcProtocolTest.java
new file mode 100644
index 0000000000..dbd76fe8bf
--- /dev/null
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/PlcProtocolTest.java
@@ -0,0 +1,161 @@
+/*
+ * 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 java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class PlcProtocolTest {
+
+ @Test
+ void isInvalidValidProtocol() {
+ PlcProtocol protocol = PlcProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .registerAddresses(Arrays.asList("40001", "40002"))
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidValidProtocolWithTimeout() {
+ PlcProtocol protocol = PlcProtocol.builder()
+ .host("localhost")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .timeout("5000")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidHost() {
+ PlcProtocol protocol = PlcProtocol.builder()
+ .host("")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidPort() {
+ PlcProtocol protocol = PlcProtocol.builder()
+ .host("192.168.1.1")
+ .port("99999")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankDriverName() {
+ PlcProtocol protocol = PlcProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankAddressSyntax() {
+ PlcProtocol protocol = PlcProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("")
+ .slaveId("1")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankSlaveId() {
+ PlcProtocol protocol = PlcProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidNonNumericTimeout() {
+ PlcProtocol protocol = PlcProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .timeout("abc")
+ .registerAddresses(Arrays.asList("40001"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidEmptyRegisterAddresses() {
+ PlcProtocol protocol = PlcProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .registerAddresses(Collections.emptyList())
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidNullRegisterAddresses() {
+ PlcProtocol protocol = PlcProtocol.builder()
+ .host("192.168.1.1")
+ .port("502")
+ .driverName("modbus")
+ .addressSyntax("Modbus")
+ .slaveId("1")
+ .registerAddresses(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/S7ProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/S7ProtocolTest.java
new file mode 100644
index 0000000000..205a3ead9a
--- /dev/null
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/S7ProtocolTest.java
@@ -0,0 +1,211 @@
+/*
+ * 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 java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class S7ProtocolTest {
+
+ @Test
+ void isInvalidValidProtocol() {
+ S7Protocol protocol = S7Protocol.builder()
+ .host("192.168.1.1")
+ .port("102")
+ .driverName("s7")
+ .addressSyntax("S7")
+ .rackId("0")
+ .slotId("1")
+ .controllerType("S7_1200")
+ .registerAddresses(Arrays.asList("DB1.DBW0", "DB1.DBW2"))
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidValidProtocolWithTimeout() {
+ S7Protocol protocol = S7Protocol.builder()
+ .host("localhost")
+ .port("102")
+ .driverName("s7")
+ .addressSyntax("S7")
+ .rackId("0")
+ .slotId("1")
+ .controllerType("S7_1200")
+ .timeout("5000")
+ .registerAddresses(Arrays.asList("DB1.DBW0"))
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidHost() {
+ S7Protocol protocol = S7Protocol.builder()
+ .host("")
+ .port("102")
+ .driverName("s7")
+ .addressSyntax("S7")
+ .rackId("0")
+ .slotId("1")
+ .controllerType("S7_1200")
+ .registerAddresses(Arrays.asList("DB1.DBW0"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidPort() {
+ S7Protocol protocol = S7Protocol.builder()
+ .host("192.168.1.1")
+ .port("99999")
+ .driverName("s7")
+ .addressSyntax("S7")
+ .rackId("0")
+ .slotId("1")
+ .controllerType("S7_1200")
+ .registerAddresses(Arrays.asList("DB1.DBW0"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankDriverName() {
+ S7Protocol protocol = S7Protocol.builder()
+ .host("192.168.1.1")
+ .port("102")
+ .driverName("")
+ .addressSyntax("S7")
+ .rackId("0")
+ .slotId("1")
+ .controllerType("S7_1200")
+ .registerAddresses(Arrays.asList("DB1.DBW0"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankAddressSyntax() {
+ S7Protocol protocol = S7Protocol.builder()
+ .host("192.168.1.1")
+ .port("102")
+ .driverName("s7")
+ .addressSyntax("")
+ .rackId("0")
+ .slotId("1")
+ .controllerType("S7_1200")
+ .registerAddresses(Arrays.asList("DB1.DBW0"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankControllerType() {
+ S7Protocol protocol = S7Protocol.builder()
+ .host("192.168.1.1")
+ .port("102")
+ .driverName("s7")
+ .addressSyntax("S7")
+ .rackId("0")
+ .slotId("1")
+ .controllerType("")
+ .registerAddresses(Arrays.asList("DB1.DBW0"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidNonNumericRackId() {
+ S7Protocol protocol = S7Protocol.builder()
+ .host("192.168.1.1")
+ .port("102")
+ .driverName("s7")
+ .addressSyntax("S7")
+ .rackId("abc")
+ .slotId("1")
+ .controllerType("S7_1200")
+ .registerAddresses(Arrays.asList("DB1.DBW0"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidNonNumericSlotId() {
+ S7Protocol protocol = S7Protocol.builder()
+ .host("192.168.1.1")
+ .port("102")
+ .driverName("s7")
+ .addressSyntax("S7")
+ .rackId("0")
+ .slotId("xyz")
+ .controllerType("S7_1200")
+ .registerAddresses(Arrays.asList("DB1.DBW0"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidNonNumericTimeout() {
+ S7Protocol protocol = S7Protocol.builder()
+ .host("192.168.1.1")
+ .port("102")
+ .driverName("s7")
+ .addressSyntax("S7")
+ .rackId("0")
+ .slotId("1")
+ .controllerType("S7_1200")
+ .timeout("abc")
+ .registerAddresses(Arrays.asList("DB1.DBW0"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidEmptyRegisterAddresses() {
+ S7Protocol protocol = S7Protocol.builder()
+ .host("192.168.1.1")
+ .port("102")
+ .driverName("s7")
+ .addressSyntax("S7")
+ .rackId("0")
+ .slotId("1")
+ .controllerType("S7_1200")
+ .registerAddresses(Collections.emptyList())
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidNullRegisterAddresses() {
+ S7Protocol protocol = S7Protocol.builder()
+ .host("192.168.1.1")
+ .port("102")
+ .driverName("s7")
+ .addressSyntax("S7")
+ .rackId("0")
+ .slotId("1")
+ .controllerType("S7_1200")
+ .registerAddresses(null)
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]