|
| 1 | +/* |
| 2 | + * Copyright 2024-2024 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.spec; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.ValueSource; |
| 10 | + |
| 11 | +import static org.assertj.core.api.Assertions.assertThat; |
| 12 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 13 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 14 | + |
| 15 | +/** |
| 16 | + * Tests for {@link ToolNameValidator}. |
| 17 | + */ |
| 18 | +class ToolNameValidatorTests { |
| 19 | + |
| 20 | + @ParameterizedTest |
| 21 | + @ValueSource(strings = { "getUser", "DATA_EXPORT_v2", "admin.tools.list", "my-tool", "Tool123", "a", "A", |
| 22 | + "_private", "tool_name", "tool-name", "tool.name", "UPPERCASE", "lowercase", "MixedCase123" }) |
| 23 | + void validToolNames_strictMode(String name) { |
| 24 | + assertThatCode(() -> ToolNameValidator.validate(name, true)).doesNotThrowAnyException(); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + void validToolName_maxLength() { |
| 29 | + String name = "a".repeat(128); |
| 30 | + assertThatCode(() -> ToolNameValidator.validate(name, true)).doesNotThrowAnyException(); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void invalidToolName_null_strictMode() { |
| 35 | + assertThatThrownBy(() -> ToolNameValidator.validate(null, true)).isInstanceOf(IllegalArgumentException.class) |
| 36 | + .hasMessageContaining("null or empty"); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void invalidToolName_empty_strictMode() { |
| 41 | + assertThatThrownBy(() -> ToolNameValidator.validate("", true)).isInstanceOf(IllegalArgumentException.class) |
| 42 | + .hasMessageContaining("null or empty"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void invalidToolName_tooLong_strictMode() { |
| 47 | + String name = "a".repeat(129); |
| 48 | + assertThatThrownBy(() -> ToolNameValidator.validate(name, true)).isInstanceOf(IllegalArgumentException.class) |
| 49 | + .hasMessageContaining("128 characters"); |
| 50 | + } |
| 51 | + |
| 52 | + @ParameterizedTest |
| 53 | + @ValueSource(strings = { "tool name", // space |
| 54 | + "tool,name", // comma |
| 55 | + "tool@name", // at sign |
| 56 | + "tool#name", // hash |
| 57 | + "tool$name", // dollar |
| 58 | + "tool%name", // percent |
| 59 | + "tool&name", // ampersand |
| 60 | + "tool*name", // asterisk |
| 61 | + "tool+name", // plus |
| 62 | + "tool=name", // equals |
| 63 | + "tool/name", // slash |
| 64 | + "tool\\name", // backslash |
| 65 | + "tool:name", // colon |
| 66 | + "tool;name", // semicolon |
| 67 | + "tool'name", // single quote |
| 68 | + "tool\"name", // double quote |
| 69 | + "tool<name", // less than |
| 70 | + "tool>name", // greater than |
| 71 | + "tool?name", // question mark |
| 72 | + "tool!name", // exclamation |
| 73 | + "tool(name)", // parentheses |
| 74 | + "tool[name]", // brackets |
| 75 | + "tool{name}", // braces |
| 76 | + "tool|name", // pipe |
| 77 | + "tool~name", // tilde |
| 78 | + "tool`name", // backtick |
| 79 | + "tool^name", // caret |
| 80 | + "tööl", // non-ASCII |
| 81 | + "工具" // unicode |
| 82 | + }) |
| 83 | + void invalidToolNames_specialCharacters_strictMode(String name) { |
| 84 | + assertThatThrownBy(() -> ToolNameValidator.validate(name, true)).isInstanceOf(IllegalArgumentException.class) |
| 85 | + .hasMessageContaining("invalid characters"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void invalidToolName_nonStrictMode_doesNotThrow() { |
| 90 | + // Non-strict mode should not throw, just warn |
| 91 | + assertThatCode(() -> ToolNameValidator.validate("invalid name", false)).doesNotThrowAnyException(); |
| 92 | + assertThatCode(() -> ToolNameValidator.validate(null, false)).doesNotThrowAnyException(); |
| 93 | + assertThatCode(() -> ToolNameValidator.validate("", false)).doesNotThrowAnyException(); |
| 94 | + assertThatCode(() -> ToolNameValidator.validate("a".repeat(129), false)).doesNotThrowAnyException(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void toolBuilder_validatesName_strictMode() { |
| 99 | + assertThatThrownBy(() -> McpSchema.Tool.builder().name("invalid name with space").build()) |
| 100 | + .isInstanceOf(IllegalArgumentException.class) |
| 101 | + .hasMessageContaining("invalid characters"); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void toolBuilder_validName() { |
| 106 | + McpSchema.Tool tool = McpSchema.Tool.builder().name("valid_tool-name.v1").build(); |
| 107 | + assertThat(tool.name()).isEqualTo("valid_tool-name.v1"); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments