1717
1818package org .apache .shenyu .plugin .agent .gateway .protocol ;
1919
20- import com .fasterxml .jackson .databind .JsonNode ;
2120import com .fasterxml .jackson .databind .ObjectMapper ;
2221import com .fasterxml .jackson .databind .node .ObjectNode ;
2322import com .google .gson .JsonParser ;
24- import com .networknt .schema .InvalidSchemaException ;
25- import com .networknt .schema .SchemaLocation ;
26- import com .sun .net .httpserver .HttpServer ;
2723import org .apache .shenyu .common .dto .AgentGatewayResultFilterConfig ;
2824import org .junit .jupiter .api .Test ;
29- import org .junit .jupiter .params .ParameterizedTest ;
30- import org .junit .jupiter .params .provider .ValueSource ;
3125
32- import java .net .InetSocketAddress ;
3326import java .util .Set ;
34- import java .util .concurrent .atomic .AtomicInteger ;
3527
3628import static org .junit .jupiter .api .Assertions .assertEquals ;
3729import static org .junit .jupiter .api .Assertions .assertFalse ;
3830import static org .junit .jupiter .api .Assertions .assertThrows ;
3931import static org .junit .jupiter .api .Assertions .assertTrue ;
4032
41- class AgentMcpOutputSchemaValidatorTest {
33+ /**
34+ * Behavior tests of the MCP result filter against the shared bounded schema
35+ * validator: deletion keeps the schema unchanged and validation stays fail-closed.
36+ */
37+ class AgentMcpResultFilterTest {
4238
4339 private final ObjectMapper mapper = new ObjectMapper ();
4440
@@ -76,15 +72,6 @@ void defaultsCannotRestoreADeletedFieldOrSatisfyARequiredField() throws Exceptio
7672 assertThrows (RuntimeException .class , () -> required .apply (result ));
7773 }
7874
79- @ Test
80- void schemaSnapshotIsIndependentOfMutableDefinition () throws Exception {
81- ObjectNode declaration = json ("{\" type\" :\" object\" ,\" required\" :[\" keep\" ]}" );
82- AgentMcpOutputSchemaValidator validator = new AgentMcpOutputSchemaValidator (declaration );
83- declaration .removeAll ();
84- assertThrows (RuntimeException .class , () -> validator .validate (json ("{\" secret\" :\" private\" }" )));
85- validator .validate (json ("{\" keep\" :1}" ));
86- }
87-
8875 @ Test
8976 void ignoresUnsupportedSchemasWhenNoPlanIsConfigured () throws Exception {
9077 ObjectNode source = json ("{\" structuredContent\" :{\" secret\" :\" private\" }}" );
@@ -93,76 +80,13 @@ void ignoresUnsupportedSchemasWhenNoPlanIsConfigured() throws Exception {
9380 assertTrue (source == filter .apply (source ));
9481 }
9582
96- @ ParameterizedTest
97- @ ValueSource (strings = {"{\" $ref\" :\" #\" }" , "{\" $ref\" :\" https://untrusted.invalid/schema\" }" ,
98- "{\" $dynamicRef\" :\" #anchor\" }" , "{\" pattern\" :\" (a+)+$\" }" , "{\" patternProperties\" :{}}" ,
99- "{\" $schema\" :\" https://untrusted.invalid/meta\" }" , "{\" type\" :\" invalid-type\" }" ,
100- "{\" properties\" :true}" , "{\" allOf\" :[true,true,true,true,true]}" , "{\" unknownAssertion\" :true}" })
101- void rejectsUnsupportedOrMalformedSchemas (final String declaration ) throws Exception {
102- assertThrows (RuntimeException .class , () -> new AgentMcpOutputSchemaValidator (json (declaration )));
103- }
104-
105- @ Test
106- void annotationDataDoesNotBecomeAReference () throws Exception {
107- AgentMcpOutputSchemaValidator validator = new AgentMcpOutputSchemaValidator (json (
108- "{\" type\" :\" object\" ,\" properties\" :{\" keep\" :{\" const\" :{\" $ref\" :\" not-a-control-plane\" }}}}" ));
109- validator .validate (json ("{\" keep\" :{\" $ref\" :\" not-a-control-plane\" }}" ));
110- }
111-
112- @ Test
113- void actualReferenceResolverCannotContactALocalHttpEndpoint () throws Exception {
114- AtomicInteger requests = new AtomicInteger ();
115- HttpServer server = HttpServer .create (new InetSocketAddress ("127.0.0.1" , 0 ), 0 );
116- server .createContext ("/schema" , exchange -> {
117- requests .incrementAndGet ();
118- exchange .sendResponseHeaders (500 , -1 );
119- exchange .close ();
120- });
121- server .start ();
122- try {
123- String uri = "http://127.0.0.1:" + server .getAddress ().getPort () + "/schema" ;
124- JsonNode declaration = json ("{\" $ref\" :\" " + uri + "\" }" );
125- InvalidSchemaException failure = assertThrows (InvalidSchemaException .class ,
126- () -> AgentMcpOutputSchemaValidator .localRegistry ().getSchema (declaration ).validate (json ("{}" )));
127- assertTrue (failure .getMessage ().contains ("not allowed" ));
128- assertEquals (0 , requests .get ());
129- } finally {
130- server .stop (0 );
131- }
132- }
133-
134- @ ParameterizedTest
135- @ ValueSource (strings = {"file:///does-not-exist/schema.json" , "classpath:META-INF/shenyu/agent-gateway-version.properties" ,
136- "https://untrusted.invalid/schema" })
137- void actualLoaderRejectsNonBuiltinResources (final String uri ) {
138- InvalidSchemaException failure = assertThrows (InvalidSchemaException .class ,
139- () -> AgentMcpOutputSchemaValidator .localRegistry ().getSchema (SchemaLocation .of (uri )));
140- assertTrue (failure .getMessage ().contains ("not allowed" ));
141- }
142-
14383 @ Test
14484 void activeCopyPolicyRequiresStructuredContentForBusinessErrorsToo () throws Exception {
14585 AgentMcpResultFilter filter = new AgentMcpResultFilter (config (), "crm.profile" , json ("{\" type\" :\" object\" }" ));
14686 assertThrows (RuntimeException .class , () -> filter .apply (json ("{\" isError\" :false}" )));
14787 assertThrows (RuntimeException .class , () -> filter .apply (json ("{\" isError\" :true}" )));
14888 }
14989
150- @ Test
151- void limitsInstanceAndSchemaCostBeforeValidation () throws Exception {
152- AgentMcpOutputSchemaValidator validator = new AgentMcpOutputSchemaValidator (json ("{\" type\" :\" object\" }" ));
153- ObjectNode large = json ("{}" );
154- var array = large .putArray ("values" );
155- for (int index = 0 ; index < 129 ; index ++) {
156- array .add (index );
157- }
158- assertThrows (RuntimeException .class , () -> validator .validate (large ));
159- ObjectNode longString = json ("{}" );
160- longString .put ("value" , "x" .repeat (4097 ));
161- assertThrows (RuntimeException .class , () -> validator .validate (longString ));
162- assertThrows (RuntimeException .class , () -> new AgentMcpOutputSchemaValidator (json (
163- "{\" allOf\" :[{\" allOf\" :[{\" allOf\" :[true]}]}]}" )));
164- }
165-
16690 private AgentGatewayResultFilterConfig config () {
16791 return AgentGatewayResultFilterConfig .parse (
16892 JsonParser .parseString ("{\" crm.profile\" :[\" /secret\" ]}" ), Set .of ("crm.profile" ));
0 commit comments