Skip to content

Commit

Permalink
issue #3446 param hash must flip ByteBuffer before reading
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Arnold <robin.arnold@ibm.com>
  • Loading branch information
punktilious committed Mar 14, 2022
1 parent 83ea787 commit 8bd65c5
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public void visit(TokenParmVal param) throws FHIRPersistenceException {
updateDigestWithParmValKey(param);
updateDigestWithString(param.getValueSystem());
updateDigestWithString(param.getValueCode());
updateDigestWithString(param.getVersion());
}

@Override
Expand Down Expand Up @@ -173,6 +172,7 @@ private void updateDigestWithBigDecimal(BigDecimal value) {
if (value != null) {
ByteBuffer bb = ByteBuffer.allocate(Double.SIZE);
bb.putDouble(value.doubleValue());
bb.flip();
digest.update(bb);
}
digest.update(DELIMITER);
Expand All @@ -186,6 +186,7 @@ private void updateDigestWithInteger(Integer value) {
if (value != null) {
ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE);
bb.putInt(value.intValue());
bb.flip();
digest.update(bb);
}
digest.update(DELIMITER);
Expand All @@ -199,6 +200,7 @@ private void updateDigestWithDouble(Double value) {
if (value != null) {
ByteBuffer bb = ByteBuffer.allocate(Double.SIZE);
bb.putDouble(value.doubleValue());
bb.flip();
digest.update(bb);
}
digest.update(DELIMITER);
Expand All @@ -212,6 +214,7 @@ private void updateDigestWithTimestamp(Timestamp value) {
if (value != null) {
ByteBuffer bb = ByteBuffer.allocate(Long.SIZE);
bb.putLong(value.getTime());
bb.flip();
digest.update(bb);
}
digest.update(DELIMITER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static org.testng.Assert.assertNotNull;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -32,6 +33,7 @@
import com.ibm.fhir.persistence.jdbc.util.ParameterHashVisitor;
import com.ibm.fhir.persistence.jdbc.util.type.NewNumberParmBehaviorUtil;
import com.ibm.fhir.search.date.DateTimeHandler;
import com.ibm.fhir.search.util.ReferenceUtil;
import com.ibm.fhir.search.util.ReferenceValue;
import com.ibm.fhir.search.util.ReferenceValue.ReferenceType;

Expand Down Expand Up @@ -69,23 +71,93 @@ public void testNoExtractedParameters() throws Exception {
}

@Test
public void testTokenParams() throws Exception {
TokenParmVal p1 = new TokenParmVal();
p1.setResourceType("Patient");
p1.setName("code2");
p1.setUrl("url2");
p1.setVersion("1");
p1.setValueSystem("valueSystem2");
p1.setValueCode("valueCode2");
public void testReferenceParams() throws Exception {
ReferenceValue rv1 = ReferenceUtil.createReferenceValueFrom("Patient/patient1/_history/1", "Patient", ReferenceUtil.getBaseUrl(null));
ReferenceParmVal p1 = new ReferenceParmVal();
p1.setName("subject");
p1.setRefValue(rv1);

ParameterHashVisitor visitor1 = new ParameterHashVisitor();
p1.accept(visitor1);

// Make sure that the hash changes when changing the reference version
ReferenceValue rv2 = ReferenceUtil.createReferenceValueFrom("Patient/patient1/_history/2", "Patient", ReferenceUtil.getBaseUrl(null));
p1.setRefValue(rv2);

ParameterHashVisitor visitor2 = new ParameterHashVisitor();
p1.accept(visitor2);
assertNotEquals(visitor1.getBase64Hash(), visitor2.getBase64Hash());
}

@Test
public void testDateParams() throws Exception {
DateParmVal p1 = new DateParmVal();
p1.setName("date");
p1.setValueDateStart(Timestamp.from(Instant.now()));
p1.setValueDateEnd(Timestamp.from(Instant.now()));
ParameterHashVisitor visitor1 = new ParameterHashVisitor();
p1.accept(visitor1);

// Make sure that the hash changes when changing the version
p1.setVersion("2");
// Modify the parameter with a different start date and check the hash changes
p1.setValueDateStart(Timestamp.from(Instant.now().plusMillis(1000)));
ParameterHashVisitor visitor2 = new ParameterHashVisitor();
p1.accept(visitor2);
assertNotEquals(visitor1.getBase64Hash(), visitor2.getBase64Hash());

// Now check it changes again when we change the date end value
p1.setValueDateEnd(Timestamp.from(Instant.now().plusMillis(1000)));
ParameterHashVisitor visitor3 = new ParameterHashVisitor();
p1.accept(visitor3);
assertNotEquals(visitor2.getBase64Hash(), visitor3.getBase64Hash());
}

@Test
public void testLocationParams() throws Exception {
LocationParmVal p1 = new LocationParmVal();
p1.setValueLatitude(0.1);
p1.setValueLongitude(1.1);
ParameterHashVisitor visitor1 = new ParameterHashVisitor();
p1.accept(visitor1);

// Modify latitude and check the signature changes
p1.setValueLatitude(0.2);
ParameterHashVisitor visitor2 = new ParameterHashVisitor();
p1.accept(visitor2);
assertNotEquals(visitor1.getBase64Hash(), visitor2.getBase64Hash());

// Modify longitude and check the signature changes
p1.setValueLongitude(1.2);
ParameterHashVisitor visitor3 = new ParameterHashVisitor();
p1.accept(visitor3);
assertNotEquals(visitor2.getBase64Hash(), visitor3.getBase64Hash());
}

@Test
public void testNumberParams() throws Exception {
NumberParmVal p1 = new NumberParmVal();
p1.setValueNumber(BigDecimal.valueOf(10));
p1.setValueNumberLow(BigDecimal.valueOf(5));
p1.setValueNumberHigh(BigDecimal.valueOf(100));
ParameterHashVisitor visitor1 = new ParameterHashVisitor();
p1.accept(visitor1);

// Modify number and check the signature changes
p1.setValueNumber(BigDecimal.valueOf(11));
ParameterHashVisitor visitor2 = new ParameterHashVisitor();
p1.accept(visitor2);
assertNotEquals(visitor1.getBase64Hash(), visitor2.getBase64Hash());

// Modify number low and check the signature changes
p1.setValueNumberLow(BigDecimal.valueOf(6));
ParameterHashVisitor visitor3 = new ParameterHashVisitor();
p1.accept(visitor3);
assertNotEquals(visitor2.getBase64Hash(), visitor3.getBase64Hash());

// Modify number high and check the signature changes
p1.setValueNumberHigh(BigDecimal.valueOf(101));
ParameterHashVisitor visitor4 = new ParameterHashVisitor();
p1.accept(visitor4);
assertNotEquals(visitor3.getBase64Hash(), visitor4.getBase64Hash());
}

@Test
Expand Down

0 comments on commit 8bd65c5

Please sign in to comment.