This is an automated email from the ASF dual-hosted git repository.
shown pushed a commit to branch 0322-yuluo/inprove-add-check-1
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/0322-yuluo/inprove-add-check-1
by this push:
new e03579a143 [Improve] Add common protoctl entity check
e03579a143 is described below
commit e03579a143d333b54cd3480420133c851bb664e3
Author: yuluo-yx <[email protected]>
AuthorDate: Sun Mar 22 11:51:57 2026 +0800
[Improve] Add common protoctl entity check
Signed-off-by: yuluo-yx <[email protected]>
---
.../common/entity/job/protocol/IpmiProtocol.java | 22 ++-
.../common/entity/job/protocol/JmxProtocol.java | 28 +++-
.../common/entity/job/protocol/Protocol.java | 2 +-
.../common/entity/job/protocol/PushProtocol.java | 29 +++-
.../entity/job/protocol/RedfishProtocol.java | 33 +++-
.../entity/job/protocol/WebsocketProtocol.java | 17 +-
.../entity/job/protocol/IpmiProtocolTest.java | 135 ++++++++++++++++
.../entity/job/protocol/JmxProtocolTest.java | 128 +++++++++++++++
.../entity/job/protocol/PushProtocolTest.java | 138 +++++++++++++++++
.../entity/job/protocol/RedfishProtocolTest.java | 171 +++++++++++++++++++++
.../entity/job/protocol/WebsocketProtocolTest.java | 96 ++++++++++++
11 files changed, 783 insertions(+), 16 deletions(-)
diff --git
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IpmiProtocol.java
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IpmiProtocol.java
index 103626f541..6a5d6aea1b 100644
---
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IpmiProtocol.java
+++
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IpmiProtocol.java
@@ -17,10 +17,14 @@
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;
/**
* IPMI2 Protocol
@@ -72,8 +76,20 @@ public class IpmiProtocol implements CommonRequestProtocol,
Protocol {
@Override
public boolean isInvalid() {
-
- // todo: add
- return true;
+ if (!validateIpDomain(host) || !validPort(port)) {
+ return true;
+ }
+ if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
+ return true;
+ }
+ if (StringUtils.isBlank(type)) {
+ return true;
+ }
+ if (!"Chassis".equalsIgnoreCase(type)
+ && !"Sensor".equalsIgnoreCase(type)
+ && !"Raw".equalsIgnoreCase(type)) {
+ return true;
+ }
+ return "Raw".equalsIgnoreCase(type) && field == null;
}
}
diff --git
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/JmxProtocol.java
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/JmxProtocol.java
index 192d65e6c6..a5f5c8823b 100644
---
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/JmxProtocol.java
+++
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/JmxProtocol.java
@@ -17,10 +17,14 @@
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;
/**
* Jmx protocol
@@ -67,8 +71,26 @@ public class JmxProtocol implements CommonRequestProtocol,
Protocol {
@Override
public boolean isInvalid() {
-
- // todo: add
- return true;
+ if (StringUtils.isBlank(objectName)) {
+ return true;
+ }
+ if (StringUtils.isNotBlank(ssl)
+ && !"true".equalsIgnoreCase(ssl)
+ && !"false".equalsIgnoreCase(ssl)) {
+ return true;
+ }
+ if (StringUtils.isNotBlank(username) && StringUtils.isBlank(password))
{
+ return true;
+ }
+ if (StringUtils.isBlank(username) && StringUtils.isNotBlank(password))
{
+ return true;
+ }
+ if (StringUtils.isNotBlank(url)) {
+ return !url.startsWith("service:jmx:rmi:") ||
url.contains("/stub/");
+ }
+ if (!validateIpDomain(host) || !validPort(port)) {
+ return true;
+ }
+ return Integer.parseInt(port) <= 0;
}
}
diff --git
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/Protocol.java
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/Protocol.java
index 7d41fe9134..ad0becd6f9 100644
---
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/Protocol.java
+++
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/Protocol.java
@@ -23,7 +23,7 @@ package org.apache.hertzbeat.common.entity.job.protocol;
public interface Protocol {
/**
- * Check Protocol params vaild.
+ * Check Protocol params valid.
* @return True or False.
*/
boolean isInvalid();
diff --git
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PushProtocol.java
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PushProtocol.java
index 5cd8a46dc4..f032cc6faf 100644
---
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PushProtocol.java
+++
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PushProtocol.java
@@ -17,11 +17,16 @@
package org.apache.hertzbeat.common.entity.job.protocol;
+import static org.apache.hertzbeat.common.util.IpDomainUtil.isHasSchema;
+import static org.apache.hertzbeat.common.util.IpDomainUtil.validPort;
+import static org.apache.hertzbeat.common.util.IpDomainUtil.validateIpDomain;
+
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
+import org.apache.commons.lang3.StringUtils;
import org.apache.hertzbeat.common.entity.dto.Field;
/**
@@ -39,8 +44,26 @@ public class PushProtocol implements CommonRequestProtocol,
Protocol {
@Override
public boolean isInvalid() {
-
- // todo: add
- return true;
+ if ((!validateIpDomain(host) && !isHasSchema(host)) ||
!validPort(port)) {
+ return true;
+ }
+ if (Integer.parseInt(port) <= 0) {
+ return true;
+ }
+ if (StringUtils.isBlank(uri) || !uri.startsWith("/") ||
StringUtils.containsWhitespace(uri)) {
+ return true;
+ }
+ if (fields == null || fields.isEmpty()) {
+ return true;
+ }
+ for (Field field : fields) {
+ if (field == null
+ || StringUtils.isBlank(field.getName())
+ || field.getType() == null
+ || (field.getType() != 0 && field.getType() != 1)) {
+ return true;
+ }
+ }
+ return false;
}
}
diff --git
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RedfishProtocol.java
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RedfishProtocol.java
index 1d1764936a..0a8c709dbb 100644
---
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RedfishProtocol.java
+++
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RedfishProtocol.java
@@ -17,11 +17,16 @@
package org.apache.hertzbeat.common.entity.job.protocol;
+import static org.apache.hertzbeat.common.util.IpDomainUtil.isHasSchema;
+import static org.apache.hertzbeat.common.util.IpDomainUtil.validPort;
+import static org.apache.hertzbeat.common.util.IpDomainUtil.validateIpDomain;
+
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
+import org.apache.commons.lang3.StringUtils;
/**
* Redfish Protocol
@@ -65,8 +70,30 @@ public class RedfishProtocol implements
CommonRequestProtocol, Protocol {
@Override
public boolean isInvalid() {
-
- // todo: add
- return true;
+ if ((!validateIpDomain(host) && !isHasSchema(host)) ||
!validPort(port)) {
+ return true;
+ }
+ if (Integer.parseInt(port) <= 0) {
+ return true;
+ }
+ if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
+ return true;
+ }
+ if (!StringUtils.isNumeric(timeout)) {
+ return true;
+ }
+ if (StringUtils.isNotBlank(schema)
+ && (!schema.startsWith("/") ||
StringUtils.containsWhitespace(schema))) {
+ return true;
+ }
+ if (jsonPath == null || jsonPath.isEmpty()) {
+ return true;
+ }
+ for (String path : jsonPath) {
+ if (StringUtils.isBlank(path)) {
+ return true;
+ }
+ }
+ return false;
}
}
diff --git
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocol.java
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocol.java
index c4f5045fc1..58c91c2b08 100644
---
a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocol.java
+++
b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocol.java
@@ -17,10 +17,14 @@
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;
/**
* Websocket Protocol
@@ -47,8 +51,15 @@ public class WebsocketProtocol implements
CommonRequestProtocol, Protocol {
@Override
public boolean isInvalid() {
-
- // todo: add
- return true;
+ if (!validateIpDomain(host) || !validPort(port)) {
+ return true;
+ }
+ if (Integer.parseInt(port) <= 0) {
+ return true;
+ }
+ if (StringUtils.isBlank(path)) {
+ return false;
+ }
+ return !path.startsWith("/") || StringUtils.containsWhitespace(path);
}
}
diff --git
a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/IpmiProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/IpmiProtocolTest.java
new file mode 100644
index 0000000000..12f883c1d6
--- /dev/null
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/IpmiProtocolTest.java
@@ -0,0 +1,135 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+class IpmiProtocolTest {
+
+ @Test
+ void isInvalidValidProtocol() {
+ IpmiProtocol protocol = IpmiProtocol.builder()
+ .host("192.168.1.1")
+ .port("623")
+ .username("admin")
+ .password("password")
+ .type("Chassis")
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidValidRawProtocol() {
+ IpmiProtocol protocol = IpmiProtocol.builder()
+ .host("192.168.1.1")
+ .port("623")
+ .username("admin")
+ .password("password")
+ .type("Raw")
+ .field(new IpmiProtocol.Field())
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidHost() {
+ IpmiProtocol protocol = IpmiProtocol.builder()
+ .host("")
+ .port("623")
+ .username("admin")
+ .password("password")
+ .type("Chassis")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidPort() {
+ IpmiProtocol protocol = IpmiProtocol.builder()
+ .host("192.168.1.1")
+ .port("70000")
+ .username("admin")
+ .password("password")
+ .type("Chassis")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankUsername() {
+ IpmiProtocol protocol = IpmiProtocol.builder()
+ .host("192.168.1.1")
+ .port("623")
+ .username("")
+ .password("password")
+ .type("Chassis")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankPassword() {
+ IpmiProtocol protocol = IpmiProtocol.builder()
+ .host("192.168.1.1")
+ .port("623")
+ .username("admin")
+ .password("")
+ .type("Chassis")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankType() {
+ IpmiProtocol protocol = IpmiProtocol.builder()
+ .host("192.168.1.1")
+ .port("623")
+ .username("admin")
+ .password("password")
+ .type("")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidUnsupportedType() {
+ IpmiProtocol protocol = IpmiProtocol.builder()
+ .host("192.168.1.1")
+ .port("623")
+ .username("admin")
+ .password("password")
+ .type("Unknown")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidRawWithoutField() {
+ IpmiProtocol protocol = IpmiProtocol.builder()
+ .host("192.168.1.1")
+ .port("623")
+ .username("admin")
+ .password("password")
+ .type("Raw")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+}
diff --git
a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/JmxProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/JmxProtocolTest.java
new file mode 100644
index 0000000000..bfd2fb435d
--- /dev/null
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/JmxProtocolTest.java
@@ -0,0 +1,128 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+class JmxProtocolTest {
+
+ @Test
+ void isInvalidValidHostPortProtocol() {
+ JmxProtocol protocol = JmxProtocol.builder()
+ .host("192.168.1.1")
+ .port("9999")
+ .objectName("java.lang:type=Runtime")
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidValidUrlProtocol() {
+ JmxProtocol protocol = JmxProtocol.builder()
+ .url("service:jmx:rmi:///jndi/rmi://127.0.0.1:9999/jmxrmi")
+ .objectName("java.lang:type=Runtime")
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankObjectName() {
+ JmxProtocol protocol = JmxProtocol.builder()
+ .host("192.168.1.1")
+ .port("9999")
+ .objectName("")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidHost() {
+ JmxProtocol protocol = JmxProtocol.builder()
+ .host("")
+ .port("9999")
+ .objectName("java.lang:type=Runtime")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidZeroPort() {
+ JmxProtocol protocol = JmxProtocol.builder()
+ .host("192.168.1.1")
+ .port("0")
+ .objectName("java.lang:type=Runtime")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidUnsupportedSsl() {
+ JmxProtocol protocol = JmxProtocol.builder()
+ .host("192.168.1.1")
+ .port("9999")
+ .objectName("java.lang:type=Runtime")
+ .ssl("yes")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidOnlyUsername() {
+ JmxProtocol protocol = JmxProtocol.builder()
+ .host("192.168.1.1")
+ .port("9999")
+ .objectName("java.lang:type=Runtime")
+ .username("admin")
+ .password("")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidOnlyPassword() {
+ JmxProtocol protocol = JmxProtocol.builder()
+ .host("192.168.1.1")
+ .port("9999")
+ .objectName("java.lang:type=Runtime")
+ .username("")
+ .password("password")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidUrlProtocolMismatch() {
+ JmxProtocol protocol = JmxProtocol.builder()
+ .url("http://127.0.0.1:9999/jmxrmi")
+ .objectName("java.lang:type=Runtime")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidUrlContainsStub() {
+ JmxProtocol protocol = JmxProtocol.builder()
+ .url("service:jmx:rmi:///jndi/rmi://127.0.0.1:9999/stub/path")
+ .objectName("java.lang:type=Runtime")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+}
diff --git
a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/PushProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/PushProtocolTest.java
new file mode 100644
index 0000000000..eb139d8d33
--- /dev/null
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/PushProtocolTest.java
@@ -0,0 +1,138 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.List;
+import org.apache.hertzbeat.common.entity.dto.Field;
+import org.junit.jupiter.api.Test;
+
+class PushProtocolTest {
+
+ @Test
+ void isInvalidValidProtocol() {
+ PushProtocol protocol = PushProtocol.builder()
+ .host("127.0.0.1")
+ .port("1157")
+ .uri("/api/push")
+ .fields(List.of(Field.builder().name("cpuUsage").type((byte)
0).build()))
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidValidProtocolWithSchemaHost() {
+ PushProtocol protocol = PushProtocol.builder()
+ .host("http://127.0.0.1")
+ .port("1157")
+ .uri("/api/push")
+ .fields(List.of(Field.builder().name("status").type((byte)
1).build()))
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidHost() {
+ PushProtocol protocol = PushProtocol.builder()
+ .host("")
+ .port("1157")
+ .uri("/api/push")
+ .fields(List.of(Field.builder().name("status").type((byte)
1).build()))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidZeroPort() {
+ PushProtocol protocol = PushProtocol.builder()
+ .host("127.0.0.1")
+ .port("0")
+ .uri("/api/push")
+ .fields(List.of(Field.builder().name("status").type((byte)
1).build()))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankUri() {
+ PushProtocol protocol = PushProtocol.builder()
+ .host("127.0.0.1")
+ .port("1157")
+ .uri("")
+ .fields(List.of(Field.builder().name("status").type((byte)
1).build()))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidUriWithoutLeadingSlash() {
+ PushProtocol protocol = PushProtocol.builder()
+ .host("127.0.0.1")
+ .port("1157")
+ .uri("api/push")
+ .fields(List.of(Field.builder().name("status").type((byte)
1).build()))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankFields() {
+ PushProtocol protocol = PushProtocol.builder()
+ .host("127.0.0.1")
+ .port("1157")
+ .uri("/api/push")
+ .fields(List.of())
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidFieldWithoutName() {
+ PushProtocol protocol = PushProtocol.builder()
+ .host("127.0.0.1")
+ .port("1157")
+ .uri("/api/push")
+ .fields(List.of(Field.builder().name("").type((byte)
1).build()))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidFieldWithoutType() {
+ PushProtocol protocol = PushProtocol.builder()
+ .host("127.0.0.1")
+ .port("1157")
+ .uri("/api/push")
+
.fields(List.of(Field.builder().name("status").type(null).build()))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidFieldWithUnsupportedType() {
+ PushProtocol protocol = PushProtocol.builder()
+ .host("127.0.0.1")
+ .port("1157")
+ .uri("/api/push")
+ .fields(List.of(Field.builder().name("status").type((byte)
2).build()))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+}
diff --git
a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/RedfishProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/RedfishProtocolTest.java
new file mode 100644
index 0000000000..9a8bebed06
--- /dev/null
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/RedfishProtocolTest.java
@@ -0,0 +1,171 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.List;
+import org.junit.jupiter.api.Test;
+
+class RedfishProtocolTest {
+
+ @Test
+ void isInvalidValidProtocol() {
+ RedfishProtocol protocol = RedfishProtocol.builder()
+ .host("127.0.0.1")
+ .port("443")
+ .username("Administrator")
+ .password("Password")
+ .timeout("5000")
+ .jsonPath(List.of("$.Id"))
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidValidProtocolWithSchemaHost() {
+ RedfishProtocol protocol = RedfishProtocol.builder()
+ .host("https://127.0.0.1")
+ .port("443")
+ .username("Administrator")
+ .password("Password")
+ .timeout("5000")
+ .jsonPath(List.of("$.Id"))
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidHost() {
+ RedfishProtocol protocol = RedfishProtocol.builder()
+ .host("")
+ .port("443")
+ .username("Administrator")
+ .password("Password")
+ .timeout("5000")
+ .jsonPath(List.of("$.Id"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidZeroPort() {
+ RedfishProtocol protocol = RedfishProtocol.builder()
+ .host("127.0.0.1")
+ .port("0")
+ .username("Administrator")
+ .password("Password")
+ .timeout("5000")
+ .jsonPath(List.of("$.Id"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankUsername() {
+ RedfishProtocol protocol = RedfishProtocol.builder()
+ .host("127.0.0.1")
+ .port("443")
+ .username("")
+ .password("Password")
+ .timeout("5000")
+ .jsonPath(List.of("$.Id"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankPassword() {
+ RedfishProtocol protocol = RedfishProtocol.builder()
+ .host("127.0.0.1")
+ .port("443")
+ .username("Administrator")
+ .password("")
+ .timeout("5000")
+ .jsonPath(List.of("$.Id"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankTimeout() {
+ RedfishProtocol protocol = RedfishProtocol.builder()
+ .host("127.0.0.1")
+ .port("443")
+ .username("Administrator")
+ .password("Password")
+ .timeout("")
+ .jsonPath(List.of("$.Id"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidNonNumericTimeout() {
+ RedfishProtocol protocol = RedfishProtocol.builder()
+ .host("127.0.0.1")
+ .port("443")
+ .username("Administrator")
+ .password("Password")
+ .timeout("5s")
+ .jsonPath(List.of("$.Id"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidSchema() {
+ RedfishProtocol protocol = RedfishProtocol.builder()
+ .host("127.0.0.1")
+ .port("443")
+ .username("Administrator")
+ .password("Password")
+ .timeout("5000")
+ .schema("redfish/v1/Chassis")
+ .jsonPath(List.of("$.Id"))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidBlankJsonPath() {
+ RedfishProtocol protocol = RedfishProtocol.builder()
+ .host("127.0.0.1")
+ .port("443")
+ .username("Administrator")
+ .password("Password")
+ .timeout("5000")
+ .jsonPath(List.of())
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidJsonPathContainsBlankItem() {
+ RedfishProtocol protocol = RedfishProtocol.builder()
+ .host("127.0.0.1")
+ .port("443")
+ .username("Administrator")
+ .password("Password")
+ .timeout("5000")
+ .jsonPath(List.of("$.Id", ""))
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+}
diff --git
a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocolTest.java
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocolTest.java
new file mode 100644
index 0000000000..dad66502d9
--- /dev/null
+++
b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocolTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+class WebsocketProtocolTest {
+
+ @Test
+ void isInvalidValidProtocol() {
+ WebsocketProtocol protocol = WebsocketProtocol.builder()
+ .host("127.0.0.1")
+ .port("80")
+ .path("/")
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidValidProtocolWithoutPath() {
+ WebsocketProtocol protocol = WebsocketProtocol.builder()
+ .host("127.0.0.1")
+ .port("80")
+ .path("")
+ .build();
+ assertFalse(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidHost() {
+ WebsocketProtocol protocol = WebsocketProtocol.builder()
+ .host("")
+ .port("80")
+ .path("/")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidInvalidPort() {
+ WebsocketProtocol protocol = WebsocketProtocol.builder()
+ .host("127.0.0.1")
+ .port("99999")
+ .path("/")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidZeroPort() {
+ WebsocketProtocol protocol = WebsocketProtocol.builder()
+ .host("127.0.0.1")
+ .port("0")
+ .path("/")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidPathWithoutLeadingSlash() {
+ WebsocketProtocol protocol = WebsocketProtocol.builder()
+ .host("127.0.0.1")
+ .port("80")
+ .path("ws")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+
+ @Test
+ void isInvalidPathContainsWhitespace() {
+ WebsocketProtocol protocol = WebsocketProtocol.builder()
+ .host("127.0.0.1")
+ .port("80")
+ .path("/chat room")
+ .build();
+ assertTrue(protocol.isInvalid());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]