20 changed files with 303 additions and 61 deletions
@ -0,0 +1,96 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed 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.thingsboard.server.service.script; |
||||
|
|
||||
|
import org.junit.jupiter.api.Test; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.test.context.TestPropertySource; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.controller.AbstractControllerTest; |
||||
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
||||
|
|
||||
|
import java.util.UUID; |
||||
|
import java.util.concurrent.ExecutionException; |
||||
|
|
||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
||||
|
|
||||
|
@DaoSqlTest |
||||
|
@TestPropertySource(properties = { |
||||
|
"js.max_script_body_size=50", |
||||
|
"js.max_total_args_size=50", |
||||
|
"js.max_result_size=50", |
||||
|
"js.local.max_errors=2" |
||||
|
}) |
||||
|
class JsInvokeServiceTest extends AbstractControllerTest { |
||||
|
|
||||
|
@Autowired |
||||
|
private NashornJsInvokeService jsInvokeService; |
||||
|
|
||||
|
@Value("${js.local.max_errors}") |
||||
|
private int maxJsErrors; |
||||
|
|
||||
|
@Test |
||||
|
void givenTooBigScriptForEval_thenReturnError() { |
||||
|
String hugeScript = "var a = 'qwertyqwertywertyqwabababer'; return {a: a};"; |
||||
|
|
||||
|
assertThatThrownBy(() -> { |
||||
|
evalScript(hugeScript); |
||||
|
}).hasMessageContaining("body exceeds maximum allowed size"); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void givenTooBigScriptInputArgs_thenReturnErrorAndReportScriptExecutionError() throws Exception { |
||||
|
String script = "return { msg: msg };"; |
||||
|
String hugeMsg = "{\"input\":\"123456781234349\"}"; |
||||
|
UUID scriptId = evalScript(script); |
||||
|
|
||||
|
for (int i = 0; i < maxJsErrors; i++) { |
||||
|
assertThatThrownBy(() -> { |
||||
|
invokeScript(scriptId, hugeMsg); |
||||
|
}).hasMessageContaining("input arguments exceed maximum"); |
||||
|
} |
||||
|
assertThatScriptIsBlocked(scriptId); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void whenScriptInvocationResultIsTooBig_thenReturnErrorAndReportScriptExecutionError() throws Exception { |
||||
|
String script = "var s = new Array(50).join('a'); return { s: s};"; |
||||
|
UUID scriptId = evalScript(script); |
||||
|
|
||||
|
for (int i = 0; i < maxJsErrors; i++) { |
||||
|
assertThatThrownBy(() -> { |
||||
|
invokeScript(scriptId, "{}"); |
||||
|
}).hasMessageContaining("result exceeds maximum allowed size"); |
||||
|
} |
||||
|
assertThatScriptIsBlocked(scriptId); |
||||
|
} |
||||
|
|
||||
|
private void assertThatScriptIsBlocked(UUID scriptId) { |
||||
|
assertThatThrownBy(() -> { |
||||
|
invokeScript(scriptId, "{}"); |
||||
|
}).hasMessageContaining("invocation is blocked due to maximum error"); |
||||
|
} |
||||
|
|
||||
|
private UUID evalScript(String script) throws ExecutionException, InterruptedException { |
||||
|
return jsInvokeService.eval(TenantId.SYS_TENANT_ID, JsScriptType.RULE_NODE_SCRIPT, script).get(); |
||||
|
} |
||||
|
|
||||
|
private String invokeScript(UUID scriptId, String msg) throws ExecutionException, InterruptedException { |
||||
|
return jsInvokeService.invokeFunction(TenantId.SYS_TENANT_ID, null, scriptId, msg, "{}", "POST_TELEMETRY_REQUEST").get(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue