Skip to content
Open
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
34 changes: 28 additions & 6 deletions src/main/java/org/spdx/storage/simple/InMemSpdxStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,39 @@ public class InMemSpdxStore implements IModelStore {
public InMemSpdxStore() {

}

/**
* Key used to store an object URI.
* <p>
* An SPDX element id, the URI fragment that starts with {@code SPDXRef-}, is case-sensitive.
* {@code SPDXRef-Arcturus.ogg} and {@code SPDXRef-arcturus.ogg} are different elements.
* License identifiers and every other id stay case-insensitive.
* @param objectUri object URI
* @return map key
*/
static String storageKey(String objectUri) {
int fragmentStart = objectUri.lastIndexOf('#');
if (fragmentStart >= 0 && fragmentStart < objectUri.length() - 1) {
String fragment = objectUri.substring(fragmentStart + 1);
if (fragment.startsWith(SpdxConstantsCompatV2.SPDX_ELEMENT_REF_PRENUM)) {
return objectUri.substring(0, fragmentStart + 1).toLowerCase() + fragment;
}
} else if (objectUri.startsWith(SpdxConstantsCompatV2.SPDX_ELEMENT_REF_PRENUM)) {
return objectUri;
}
return objectUri.toLowerCase();
}

@Override
public boolean exists(String objectUri) {
return typedValueMap.containsKey(objectUri.toLowerCase());
return typedValueMap.containsKey(storageKey(objectUri));
}

@Override
public void create(TypedValue typedValue) throws InvalidSPDXAnalysisException {
StoredTypedItem value = new StoredTypedItem(typedValue.getObjectUri(), typedValue.getType(), typedValue.getSpecVersion());
updateNextIds(typedValue.getObjectUri());
if (Objects.nonNull(this.typedValueMap.putIfAbsent(typedValue.getObjectUri().toLowerCase(), value))) {
if (Objects.nonNull(this.typedValueMap.putIfAbsent(storageKey(typedValue.getObjectUri()), value))) {
throw new DuplicateSpdxIdException("Object URI "+typedValue.getObjectUri()+" already exists.");
}
}
Expand Down Expand Up @@ -197,7 +219,7 @@ private synchronized void checkUpdateNextLicenseId(Matcher licenseRefMatcher) {
* @throws InvalidSPDXAnalysisException on SPDX parsing errors
*/
protected StoredTypedItem getItem(String objectUri) throws InvalidSPDXAnalysisException {
StoredTypedItem item = this.typedValueMap.get(objectUri.toLowerCase());
StoredTypedItem item = this.typedValueMap.get(storageKey(objectUri));
if (item == null) {
throw new SpdxIdNotFoundException("Object URI "+objectUri+" was not found in the memory store. The ID must first be created before getting or setting property values.");
}
Expand Down Expand Up @@ -418,7 +440,7 @@ public Optional<String> getCaseSensitiveId(String nameSpace, String caseInsensit
Objects.requireNonNull(nameSpace, "Namespace can not be null");
Objects.requireNonNull(caseInsensitiveId, "CaseInsensitiveId can not be null");
String objectUri = nameSpace + "#" + caseInsensitiveId;
StoredTypedItem item = typedValueMap.get(objectUri.toLowerCase());
StoredTypedItem item = typedValueMap.get(storageKey(objectUri));
if (Objects.isNull(item)) {
return Optional.empty();
}
Expand All @@ -444,7 +466,7 @@ public void clear() {
@Override
public void delete(String objectUri) throws InvalidSPDXAnalysisException {
Objects.requireNonNull(objectUri, "Missing object URI");
if (!this.typedValueMap.containsKey(objectUri.toLowerCase())) {
if (!this.typedValueMap.containsKey(storageKey(objectUri))) {
return;
}
referenceCountLock.writeLock().lock();
Expand Down Expand Up @@ -473,7 +495,7 @@ public void delete(String objectUri) throws InvalidSPDXAnalysisException {
}
}
}
if (Objects.isNull(typedValueMap.remove(objectUri.toLowerCase()))) {
if (Objects.isNull(typedValueMap.remove(storageKey(objectUri)))) {
logger.error("Error deleting - object URI {} does not exist.", objectUri);
throw new SpdxIdNotFoundException("Error deleting - object URI "+objectUri+" does not exist.");
}
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/org/spdx/storage/simple/InMemSpdxStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -746,4 +746,49 @@ public void testReferenceCountsDelete() throws Exception {
store.delete(TEST_OBJECT_URI1);
}
}

public void testSpdxElementIdsAreCaseSensitive() throws Exception {
String namespace = "https://example.com/spdxdocs/android";
String upperId = SpdxConstantsCompatV2.SPDX_ELEMENT_REF_PRENUM + "Arcturus.ogg";
String lowerId = SpdxConstantsCompatV2.SPDX_ELEMENT_REF_PRENUM + "arcturus.ogg";
String upperUri = namespace + "#" + upperId;
String lowerUri = namespace + "#" + lowerId;
try (InMemSpdxStore store = new InMemSpdxStore()) {
store.create(new TypedValue(upperUri, TEST_TYPE1, "SPDX-2.3"));
assertTrue(store.exists(upperUri));
assertTrue(store.exists(namespace.toUpperCase() + "#" + upperId));
assertFalse(store.exists(lowerUri));
assertFalse(store.getCaseSensitiveId(namespace, lowerId).isPresent());
assertEquals(upperId, store.getCaseSensitiveId(namespace, upperId).get());
store.create(new TypedValue(lowerUri, TEST_TYPE1, "SPDX-2.3"));
assertTrue(store.exists(lowerUri));
assertEquals(lowerId, store.getCaseSensitiveId(namespace, lowerId).get());
store.delete(lowerUri);
assertFalse(store.exists(lowerUri));
assertTrue(store.exists(upperUri));
try {
store.create(new TypedValue(upperUri, TEST_TYPE1, "SPDX-2.3"));
fail("The same SPDX element id is a duplicate");
} catch (InvalidSPDXAnalysisException e) {
// expected
}
}
}

public void testLicenseRefIdsRemainCaseInsensitive() throws Exception {
String namespace = "https://example.com/spdxdocs/android";
String canonicalId = SpdxConstantsCompatV2.NON_STD_LICENSE_ID_PRENUM + "Name";
String otherCaseId = SpdxConstantsCompatV2.NON_STD_LICENSE_ID_PRENUM + "name";
try (InMemSpdxStore store = new InMemSpdxStore()) {
store.create(new TypedValue(namespace + "#" + canonicalId, TEST_TYPE1, "SPDX-2.3"));
try {
store.create(new TypedValue(namespace + "#" + otherCaseId, TEST_TYPE1, "SPDX-2.3"));
fail("LicenseRef identifiers that differ only by case are the same identifier");
} catch (InvalidSPDXAnalysisException e) {
// expected
}
assertTrue(store.exists(namespace + "#" + otherCaseId));
assertEquals(canonicalId, store.getCaseSensitiveId(namespace, otherCaseId.toLowerCase()).get());
}
}
}
Loading