Skip to content

Commit

Permalink
GH-4248 check for invalid triple context value. added regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abrokenjester committed Nov 13, 2022
1 parent 214f49e commit 618a172
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,10 @@ private void add(Resource subj, IRI pred, Value obj, SailDataset dataset, SailSi
}
} else {
for (Resource ctx : contexts) {
if (ctx != null && ctx.isTriple()) {
throw new SailException("context argument can not be of type Triple: " + ctx.stringValue());
}

Resource[] contextsToCheck;
if (contexts.length == 1) {
contextsToCheck = contexts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ CloseableIteration<? extends Statement, SailException> createStatementIterator(R
for (Resource context : contexts) {
if (context == null) {
contextIDList.add(0);
} else {
} else if (!context.isTriple()) {
int contextID = valueStore.getID(context);

if (contextID != NativeValue.UNKNOWN_ID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -123,7 +124,7 @@ public void testAddRDFStarContext() {
try {
testCon.add(RDF.ALT, RDF.TYPE, RDF.ALT, rdfStarTriple);
Assertions.fail("RDF-star triple value should not be allowed by store as context identifier");
} catch (UnsupportedOperationException e) {
} catch (RepositoryException e) {
// fall through, expected behavior
testCon.rollback();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.rdf4j.testsuite.sail;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -27,9 +28,11 @@
import org.eclipse.rdf4j.model.Namespace;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Triple;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.base.CoreDatatype;
import org.eclipse.rdf4j.model.util.Values;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.RDFS;
import org.eclipse.rdf4j.model.vocabulary.XSD;
Expand Down Expand Up @@ -462,6 +465,33 @@ public void testAddData() throws Exception {
countQueryResults("select * where {?S ?P rdf:type}"));
}

/**
* @see https://github.com/eclipse/rdf4j/issues/4248
*/
@Test
public void testAddTripleContext() {

con.begin();
con.addStatement(painter, RDF.TYPE, RDFS.CLASS);
con.commit();

Triple tripleContext = Values.triple(guernica, RDF.TYPE, painting);

con.begin();
assertThatExceptionOfType(SailException.class)
.isThrownBy(() -> con.addStatement(picasso, paints, guernica, tripleContext))
.withMessageStartingWith("context argument can not be of type Triple: ");
con.commit();

con.begin();
con.addStatement(picasso, paints, guernica, context1);
con.commit();

assertThat(con.hasStatement(picasso, paints, guernica, true, tripleContext)).isFalse();
assertThat(con.hasStatement(painter, RDF.TYPE, RDFS.CLASS, true)).isTrue();
assertThat(con.hasStatement(picasso, paints, guernica, true, context1)).isTrue();
}

@Test
public void testAddWhileQuerying() {
// Add some data to the repository
Expand Down

0 comments on commit 618a172

Please sign in to comment.