Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jmcagent): Move jfrprobes_schema.xsd to correct location, add ProbeValidatorTest (backport #452) #457

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 217 additions & 0 deletions src/main/resources/io/cryostat/core/jmcagent/jfrprobes_schema.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element type="jfrAgentType" name="jfragent" />

<xs:complexType name="jfrAgentType">
<xs:all>
<xs:element type="configType" name="config" minOccurs="0">
<xs:annotation>
<xs:documentation>Global configuration options</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element type="eventsType" name="events" minOccurs="0" />
</xs:all>
</xs:complexType>

<xs:complexType name="configType">
<xs:all>
<xs:element type="xs:string" name="classprefix" minOccurs="0">
<xs:annotation>
<xs:documentation>This is the prefix to use when generating event class names</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element type="xs:boolean" name="allowtostring" minOccurs="0">
<xs:annotation>
<xs:documentation>
Will allow the recording of arrays and object parameters as Strings. This will cause toString to
be called for array elements and objects other than strings, which in turn can cause trouble if
the toString method is badly implemented. Use with care.
</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element type="xs:boolean" name="allowconverter" minOccurs="0">
<xs:annotation>
<xs:documentation>
Allows converters to be used. If a converter is badly implemented, you are on your own.
</xs:documentation>
</xs:annotation>
</xs:element>
</xs:all>
</xs:complexType>

<xs:complexType name="eventsType">
<xs:sequence>
<xs:element type="eventType" name="event" maxOccurs="unbounded" minOccurs="0" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="eventType">
<xs:all>
<xs:element type="xs:string" name="label" />
<xs:element type="classType" name="class" />
<xs:element type="methodType" name="method" />
<xs:element type="xs:string" name="description" minOccurs="0" />
<xs:element type="pathType" name="path" minOccurs="0" />
<xs:element type="xs:boolean" name="stacktrace" minOccurs="0" />
<xs:element type="xs:boolean" name="rethrow" minOccurs="0" />
<xs:element type="locationType" name="location" minOccurs="0" />
<xs:element type="fieldsType" name="fields" minOccurs="0" />
</xs:all>
<xs:attribute type="xs:string" name="id" use="required" />
</xs:complexType>

<xs:simpleType name="classType">
<xs:annotation>
<xs:documentation>the fully qualified class name (FQCN) of the class to be transformed</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:normalizedString">
<xs:pattern value="([a-zA-Z_$][a-zA-Z0-9_$]*\.)*([a-zA-Z_$][a-zA-Z0-9_$]*)" />
<xs:whiteSpace value="collapse" />
</xs:restriction>
</xs:simpleType>

<xs:complexType name="methodType">
<xs:all>
<xs:element type="methodNameType" name="name" />
<xs:element type="descriptorType" name="descriptor" />
<xs:element type="parametersType" name="parameters" minOccurs="0" />
<xs:element type="returnValueType" name="returnvalue" minOccurs="0" />
</xs:all>
</xs:complexType>

<xs:simpleType name="methodNameType">
<xs:restriction base="xs:normalizedString">
<xs:pattern value="[a-zA-Z_$][a-zA-Z0-9_$]*" />
<xs:whiteSpace value="collapse" />
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="descriptorType">
<xs:annotation>
<xs:documentation>see §4.3.3 in Java Virtual Machine Specification</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:normalizedString">
<xs:pattern value="\((\[*([BCDFIJSZ]|L([a-zA-Z_$][a-zA-Z0-9_$]*/)*[a-zA-Z_$][a-zA-Z0-9_$]*;))*\)(V|\[*([BCDFIJSZ]|L([a-zA-Z_$][a-zA-Z0-9_$]*/)*[a-zA-Z_$][a-zA-Z0-9_$]*;))" />
<xs:whiteSpace value="collapse" />
</xs:restriction>
</xs:simpleType>

<xs:complexType name="parametersType">
<xs:annotation>
<xs:documentation>only if we allow toString</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element type="parameterType" name="parameter" maxOccurs="unbounded" minOccurs="0" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="parameterType">
<xs:all>
<xs:element type="xs:string" name="name" />
<xs:element type="xs:string" name="description" minOccurs="0" />
<xs:element type="contentTypeType" name="contenttype" minOccurs="0" />
<xs:element type="relationKeyType" name="relationkey" minOccurs="0" />
<xs:element type="converterType" name="converter" minOccurs="0" />
</xs:all>
<xs:attribute type="xs:nonNegativeInteger" name="index" use="required" />
</xs:complexType>

<xs:simpleType name="contentTypeType">
<xs:annotation>
<xs:documentation>see com.oracle.jrockit.jfr.ContentType</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:normalizedString">
<xs:enumeration value="None" />
<xs:enumeration value="Bytes" />
<xs:enumeration value="Timestamp" />
<xs:enumeration value="Millis" />
<xs:enumeration value="Nanos" />
<xs:enumeration value="Ticks" />
<xs:enumeration value="Address" />
<xs:enumeration value="OSThread" />
<xs:enumeration value="JavaThread" />
<xs:enumeration value="StackTrace" />
<xs:enumeration value="Class" />
<xs:enumeration value="Percentage" />
<xs:whiteSpace value="collapse" />
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="relationKeyType">
<xs:annotation>
<xs:documentation>
a unique URI signifying a relationship between different events based on the values of specific
fields
</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:anyURI" />
</xs:simpleType>

<xs:simpleType name="converterType">
<xs:annotation>
<xs:documentation>the fully qualified class name (FQCN) of the converter used</xs:documentation>
</xs:annotation>
<xs:restriction base="classType" />
</xs:simpleType>

<xs:complexType name="returnValueType">
<xs:annotation>
<xs:documentation>This will only work if we allow toString</xs:documentation>
</xs:annotation>
<xs:all>
<xs:element type="xs:string" name="description" minOccurs="0" />
<xs:element type="contentTypeType" name="contenttype" minOccurs="0" />
<xs:element type="relationKeyType" name="relationkey" minOccurs="0" />
<xs:element type="converterType" name="converter" minOccurs="0" />
</xs:all>
</xs:complexType>

<xs:simpleType name="pathType">
<xs:restriction base="xs:normalizedString">
<xs:pattern value="([^/]+/)*([^/]*)" />
<xs:whiteSpace value="collapse" />
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="locationType">
<xs:annotation>
<xs:documentation>location {ENTRY, EXIT, WRAP}</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:normalizedString">
<xs:enumeration value="ENTRY" />
<xs:enumeration value="EXIT" />
<xs:enumeration value="WRAP" />
<xs:whiteSpace value="collapse" />
</xs:restriction>
</xs:simpleType>

<xs:complexType name="fieldsType">
<xs:sequence>
<xs:element type="fieldType" name="field" maxOccurs="unbounded" minOccurs="0" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="fieldType">
<xs:all>
<xs:element type="xs:string" name="name" />
<xs:element type="expressionType" name="expression" />
<xs:element type="xs:string" name="description" minOccurs="0" />
<xs:element type="contentTypeType" name="contenttype" minOccurs="0" />
<xs:element type="relationKeyType" name="relationkey" minOccurs="0" />
<xs:element type="converterType" name="converter" minOccurs="0" />
</xs:all>
</xs:complexType>

<xs:simpleType name="expressionType">
<xs:annotation>
<xs:documentation>
an expression in a subset of primary expressions (see §15.8 in Java Language Specification) to be
evaluated
</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:normalizedString">
<xs:pattern value="([a-zA-Z_$][a-zA-Z0-9_$]*\.)*([a-zA-Z_$][a-zA-Z0-9_$]*)(\.[a-zA-Z_$][a-zA-Z_$]*)*" />
<xs:whiteSpace value="collapse" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Copyright The Cryostat 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 io.cryostat.core.jmcagent;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

import javax.xml.transform.stream.StreamSource;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

@ExtendWith(MockitoExtension.class)
public class ProbeValidatorTest {

final String VALID_XML =
"<jfragent>\n"
+ //
"\t<events>\n"
+ //
"\t\t<event id=\"demo.jfr.test1\">\n"
+ //
"\t\t\t<label>JFR Hello World Event 1 %TEST_NAME%</label>\n"
+ //
"\t\t\t<description>Defined in the xml file and added by the"
+ " agent.</description>\n"
+ //
"\t\t\t<path>demo/jfrhelloworldevent1</path>\n"
+ //
"\t\t\t<stacktrace>true</stacktrace>\n"
+ //
"\t\t\t<class>org.openjdk.jmc.agent.test.InstrumentMe</class>\n"
+ //
"\t\t\t<method>\n"
+ //
"\t\t\t\t<name>printHelloWorldJFR1</name>\n"
+ //
"\t\t\t\t<descriptor>()V</descriptor>\n"
+ //
"\t\t\t</method>\n"
+ //
"\t\t\t<!-- location {ENTRY, EXIT, WRAP}-->\n"
+ //
"\t\t\t<location>WRAP</location>\n"
+ //
"\t\t\t<fields>\n"
+ //
"\t\t\t\t<field>\n"
+ //
"\t\t\t\t\t<name>'InstrumentMe.STATIC_STRING_FIELD'</name>\n"
+ //
"\t\t\t\t\t<description>Capturing outer class field with class name prefixed"
+ " field name</description>\n"
+ //
"\t\t\t\t\t<expression>InstrumentMe.STATIC_STRING_FIELD</expression>\n"
+ //
"\t\t\t\t</field>\n"
+ //
"\t\t\t</fields>\n"
+ //
"\t\t</event>\n"
+ //
"\t</events>\n"
+ //
"</jfragent>";

final String INVALID_XML =
"<jfragent>\n"
+ //
"\t<foo>\n"
+ //
"\t<bar>\n"
+ //
"'This XML is not a valid probe template'\n"
+ //
"\t</bar>\n"
+ //
"\t</foo>\n"
+ //
"\t</jfragent>";

ProbeValidator validator;

@BeforeEach
void setUp() {
validator = new ProbeValidator();
}

@Test
void shouldThrowProbeValidationErrorOnFailure() {
JMCAgentXMLStream stream =
new JMCAgentXMLStream(
new ByteArrayInputStream(INVALID_XML.getBytes(StandardCharsets.UTF_8)));
Assertions.assertThrows(
ProbeValidationException.class,
() -> {
validator.validate(new StreamSource(stream));
});
}

@Test
void shouldSuccessfullyValidateCorrectTemplate() {
JMCAgentXMLStream stream =
new JMCAgentXMLStream(
new ByteArrayInputStream(VALID_XML.getBytes(StandardCharsets.UTF_8)));
try {
validator.validate(new StreamSource(stream));
} catch (Exception e) {
System.out.println(e.toString());
Assertions.fail();
}
}

@Test
void shouldNotAllowOverridingErrorHandler() {
Assertions.assertThrows(
UnsupportedOperationException.class,
() -> {
validator.setErrorHandler(new TestErrorHandler());
});
}

private static class TestErrorHandler implements ErrorHandler {

@Override
public void warning(SAXParseException exception) throws SAXException {
throw new SAXException("We shouldn't have reached here");
}

@Override
public void error(SAXParseException exception) throws SAXException {
throw new SAXException("We shouldn't have reached here");
}

@Override
public void fatalError(SAXParseException exception) throws SAXException {
throw new SAXException("We shouldn't have reached here");
}
}
}
Loading