-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable, suggest, and demonstrate mimeType values
This patch adds a class hierarchy to distinguish between known IANA Media Types and Media Types known to not be registered with IANA. A unit test is added to demonstrate how mimeType being objects can also enable hierarchical searches, even between IANA and non-IANA types. A follow-on patch will generate validation result files. References: * #363 Signed-off-by: Alex Nelson <[email protected]>
- Loading branch information
1 parent
5c15b29
commit d75d307
Showing
7 changed files
with
360 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
{ | ||
"@context": { | ||
"core": "https://ontology.unifiedcyberontology.org/uco/core/", | ||
"dcterms": "http://purl.org/dc/terms/", | ||
"kb": "http://example.org/kb/", | ||
"observable": "https://ontology.unifiedcyberontology.org/uco/observable/", | ||
"rdfs": "http://www.w3.org/2000/01/rdf-schema#", | ||
"skos": "http://www.w3.org/2004/02/skos/core#", | ||
"types": "https://ontology.unifiedcyberontology.org/uco/types/" | ||
}, | ||
"@graph": [ | ||
{ | ||
"@id": "urn:example:mime:dcterms:application/gzip", | ||
"@type": "dcterms:FileFormat", | ||
"rdfs:comment": "This is a custom media type individual, designed for just this unit test. A larger taxonomy of individuals should be used outside of testing contexts.", | ||
"skos:notation": "application/gzip" | ||
}, | ||
{ | ||
"@id": "urn:example:mime:uco:application/gzip", | ||
"@type": "types:IANAMediaType", | ||
"rdfs:comment": "This is a custom media type individual, designed for just this unit test. A larger taxonomy of individuals should be used outside of testing contexts.", | ||
"skos:exactMatch": { | ||
"@id": "urn:example:mime:dcterms:application/gzip" | ||
}, | ||
"skos:notation": "application/gzip" | ||
}, | ||
{ | ||
"@id": "urn:example:mime:uco:application/tar", | ||
"@type": "types:NonIANAMediaType", | ||
"rdfs:comment": "This is a custom media type individual, designed for just this unit test. A larger taxonomy of individuals should be used outside of testing contexts.", | ||
"skos:notation": "application/tar" | ||
}, | ||
{ | ||
"@id": "urn:example:mime:uco:application/tar+gzip", | ||
"@type": "types:NonIANAMediaType", | ||
"rdfs:comment": "This is a custom media type individual, designed for just this unit test. A larger taxonomy of individuals should be used outside of testing contexts.", | ||
"skos:broader": [ | ||
{ | ||
"@id": "urn:example:mime:uco:application/gzip" | ||
}, | ||
{ | ||
"@id": "urn:example:mime:uco:application/tar" | ||
} | ||
], | ||
"skos:notation": "application/tar+gzip" | ||
}, | ||
{ | ||
"@id": "urn:example:mime:uco:image/example.image.type.without.notation", | ||
"@type": "types:NonIANAMediaType", | ||
"rdfs:comment": "This is a custom media type individual, designed for just this unit test. A larger taxonomy of individuals should be used outside of testing contexts." | ||
}, | ||
{ | ||
"@id": "kb:file-1", | ||
"@type": "observable:File", | ||
"core:hasFacet": [ | ||
{ | ||
"@type": "observable:ContentDataFacet", | ||
"observable:mimeType": { | ||
"@id": "urn:example:mime:dcterms:application/gzip" | ||
} | ||
}, | ||
{ | ||
"@type": "observable:FileFacet", | ||
"observable:fileName": "1.gz" | ||
} | ||
], | ||
"rdfs:comment": "This file node should trigger an sh:Info-severity result." | ||
}, | ||
{ | ||
"@id": "kb:file-2", | ||
"@type": "observable:File", | ||
"core:hasFacet": [ | ||
{ | ||
"@type": "observable:ContentDataFacet", | ||
"observable:mimeType": { | ||
"@id": "urn:example:mime:uco:application/gzip" | ||
} | ||
}, | ||
{ | ||
"@type": "observable:FileFacet", | ||
"observable:fileName": "2.gz" | ||
} | ||
] | ||
}, | ||
{ | ||
"@id": "kb:file-3", | ||
"@type": "observable:File", | ||
"core:hasFacet": [ | ||
{ | ||
"@type": "observable:ContentDataFacet", | ||
"observable:mimeType": { | ||
"@id": "urn:example:mime:uco:application/tar" | ||
} | ||
}, | ||
{ | ||
"@type": "observable:FileFacet", | ||
"observable:fileName": "3.tar" | ||
} | ||
] | ||
}, | ||
{ | ||
"@id": "kb:file-4", | ||
"@type": "observable:File", | ||
"core:hasFacet": [ | ||
{ | ||
"@type": "observable:ContentDataFacet", | ||
"observable:mimeType": { | ||
"@id": "urn:example:mime:uco:application/tar+gzip" | ||
} | ||
}, | ||
{ | ||
"@type": "observable:FileFacet", | ||
"observable:fileName": "4.tar.gz" | ||
} | ||
] | ||
}, | ||
{ | ||
"@id": "kb:file-5", | ||
"@type": "observable:File", | ||
"core:hasFacet": [ | ||
{ | ||
"@type": "observable:ContentDataFacet", | ||
"observable:mimeType": { | ||
"@id": "urn:example:mime:uco:image/example.image.type.without.notation" | ||
} | ||
}, | ||
{ | ||
"@type": "observable:FileFacet", | ||
"observable:fileName": "5.dat" | ||
} | ||
], | ||
"rdfs:comment": "This file node should trigger an sh:Warning-severity result." | ||
}, | ||
{ | ||
"@id": "kb:file-6", | ||
"@type": "observable:File", | ||
"core:hasFacet": [ | ||
{ | ||
"@type": "observable:ContentDataFacet", | ||
"observable:mimeType": { | ||
"@id": "urn:example:mime:uco:image/example.image.type.without.notation" | ||
} | ||
}, | ||
{ | ||
"@type": "observable:FileFacet", | ||
"observable:fileName": "6.dat" | ||
} | ||
], | ||
"rdfs:comment": "This file node should trigger an sh:Warning-severity result." | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"@context": { | ||
"core": "https://ontology.unifiedcyberontology.org/uco/core/", | ||
"dcterms": "http://purl.org/dc/terms/", | ||
"kb": "http://example.org/kb/", | ||
"observable": "https://ontology.unifiedcyberontology.org/uco/observable/", | ||
"rdfs": "http://www.w3.org/2000/01/rdf-schema#", | ||
"skos": "http://www.w3.org/2004/02/skos/core#", | ||
"types": "https://ontology.unifiedcyberontology.org/uco/types/" | ||
}, | ||
"@graph": [ | ||
{ | ||
"@id": "kb:file-7", | ||
"@type": "observable:File", | ||
"core:hasFacet": [ | ||
{ | ||
"@type": "observable:ContentDataFacet", | ||
"observable:mimeType": "application/gzip" | ||
}, | ||
{ | ||
"@type": "observable:FileFacet", | ||
"observable:fileName": "7.gz" | ||
} | ||
], | ||
"rdfs:comment": "This file node errantly uses a literal instead of object reference." | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# This software was developed at the National Institute of Standards | ||
# and Technology by employees of the Federal Government in the course | ||
# of their official duties. Pursuant to title 17 Section 105 of the | ||
# United States Code this software is not subject to copyright | ||
# protection and is in the public domain. NIST assumes no | ||
# responsibility whatsoever for its use by other parties, and makes | ||
# no guarantees, expressed or implied, about its quality, | ||
# reliability, or any other characteristic. | ||
# | ||
# We would appreciate acknowledgement if the software is used. | ||
|
||
import typing | ||
|
||
import pytest | ||
import rdflib | ||
|
||
NS_UCO_CORE = rdflib.Namespace("https://ontology.unifiedcyberontology.org/uco/core/") | ||
NS_UCO_OBSERVABLE = rdflib.Namespace("https://ontology.unifiedcyberontology.org/uco/observable/") | ||
|
||
NSDICT = { | ||
"core": NS_UCO_CORE, | ||
"observable": NS_UCO_OBSERVABLE, | ||
"skos": rdflib.SKOS | ||
} | ||
|
||
@pytest.fixture | ||
def mime_pass_graph() -> rdflib.Graph: | ||
graph = rdflib.Graph() | ||
graph.parse("mime_PASS.json", format="json-ld") | ||
return graph | ||
|
||
|
||
def test_mime_file_names(mime_pass_graph: rdflib.Graph) -> None: | ||
expected: typing.Set[str] = { | ||
"1.gz", | ||
"2.gz", | ||
"3.tar", | ||
"4.tar.gz", | ||
"5.dat", | ||
"6.dat", | ||
} | ||
computed: typing.Set[str] = set() | ||
|
||
query = rdflib.plugins.sparql.prepareQuery("""\ | ||
SELECT ?lFileName | ||
WHERE { | ||
?nFile core:hasFacet/observable:fileName ?lFileName . | ||
} | ||
""", initNs=NSDICT) | ||
for result in mime_pass_graph.query(query): | ||
computed.add(str(result[0])) | ||
assert expected == computed | ||
|
||
|
||
def test_mime_gzip_files(mime_pass_graph: rdflib.Graph) -> None: | ||
expected: typing.Set[str] = { | ||
"1.gz", | ||
"2.gz", | ||
"4.tar.gz", | ||
} | ||
computed: typing.Set[str] = set() | ||
|
||
query = rdflib.plugins.sparql.prepareQuery("""\ | ||
SELECT ?lFileName | ||
WHERE { | ||
?nFile core:hasFacet/observable:fileName ?lFileName . | ||
?nFile core:hasFacet/observable:mimeType ?nMimeType . | ||
?nMimeType skos:exactMatch*/skos:broader*/skos:notation "application/gzip" . | ||
} | ||
""", initNs=NSDICT) | ||
for result in mime_pass_graph.query(query): | ||
computed.add(str(result[0])) | ||
assert expected == computed | ||
|
||
|
||
|
||
def test_mime_tar_files(mime_pass_graph: rdflib.Graph) -> None: | ||
expected: typing.Set[str] = { | ||
"3.tar", | ||
"4.tar.gz", | ||
} | ||
computed: typing.Set[str] = set() | ||
|
||
query = rdflib.plugins.sparql.prepareQuery("""\ | ||
SELECT ?lFileName | ||
WHERE { | ||
?nFile core:hasFacet/observable:fileName ?lFileName . | ||
?nFile core:hasFacet/observable:mimeType ?nMimeType . | ||
?nMimeType skos:exactMatch*/skos:broader*/skos:notation "application/tar" . | ||
} | ||
""", initNs=NSDICT) | ||
for result in mime_pass_graph.query(query): | ||
computed.add(str(result[0])) | ||
assert expected == computed |
Oops, something went wrong.