Skip to content

Commit

Permalink
modified KffDataObjectHandler.hash(..) method so it explicitly replac…
Browse files Browse the repository at this point in the history
…es IBDO params it generates. (#596)
  • Loading branch information
drivenflywheel authored Oct 11, 2023
1 parent bf98c76 commit 76a4659
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/main/java/emissary/core/BaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,13 @@ public void putParameters(final Map<? extends String, ? extends Object> m, final
}

final Object value = entry.getValue();

if ((policy == MergePolicy.DROP_EXISTING)) {
// store the provided value for this key, discarding any previously-stored value
setParameter(name, value);
continue;
}

if (value instanceof Iterable) {
for (final Object v : (Iterable<?>) value) {
if (policy == MergePolicy.KEEP_ALL || policy == MergePolicy.KEEP_EXISTING) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emissary/core/IBaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface IBaseDataObject {
* Define the merge policy values for parameter handling
*/
enum MergePolicy {
DISTINCT, KEEP_EXISTING, KEEP_ALL
DISTINCT, KEEP_EXISTING, KEEP_ALL, DROP_EXISTING
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/emissary/kff/KffDataObjectHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package emissary.kff;

import emissary.core.IBaseDataObject;
import emissary.core.IBaseDataObject.MergePolicy;
import emissary.core.channels.SeekableByteChannelFactory;

import org.slf4j.Logger;
Expand Down Expand Up @@ -193,9 +194,9 @@ public void hash(@Nullable final IBaseDataObject d, final boolean useSbc) throws

// Compute and add the hashes
if (useSbc && d.getChannelSize() > 0) {
d.putParameters(hashData(d.getChannelFactory(), d.shortName(), ""));
d.putParameters(hashData(d.getChannelFactory(), d.shortName(), ""), MergePolicy.DROP_EXISTING);
} else if (!useSbc && d.dataLength() > 0) {
d.putParameters(hashData(d.data(), d.shortName()));
d.putParameters(hashData(d.data(), d.shortName()), MergePolicy.DROP_EXISTING);
} else {
return;
}
Expand Down
25 changes: 23 additions & 2 deletions src/test/java/emissary/kff/KffDataObjectHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,38 @@ void testHashMethod() {
kff = new KffDataObjectHandler(true, true, true);
payload.setParameter(KffDataObjectHandler.KFF_PARAM_KNOWN_FILTER_NAME, "test.filter");
kff.hash(payload);
assertEquals("test.filter", payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_BASE + "FILTERED_BY"));
assertEquals("test.filter", payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_KNOWN_FILTER_NAME));
assertTrue(KffDataObjectHandler.hashPresent(payload));
assertEquals(DATA_MD5, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_MD5));
assertEquals(DATA_CRC32, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_BASE + "CRC32"));
assertEquals(DATA_CRC32, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_CRC32));
assertEquals(DATA_SSDEEP, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_SSDEEP));
assertEquals(DATA_SHA1, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_SHA1));
assertEquals(DATA_SHA256, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_SHA256));
assertEquals(KffDataObjectHandler.KFF_DUPE_CURRENT_FORM, payload.getFileType());
assertArrayEquals(new byte[0], payload.data());
}

@Test
void testHashMethodCalledTwice() {
// don't truncate known data or the second call will be made with an empty payload
kff = new KffDataObjectHandler(KffDataObjectHandler.KEEP_KNOWN_DATA, true, true);
payload.setParameter(KffDataObjectHandler.KFF_PARAM_KNOWN_FILTER_NAME, "test.filter");
kff.hash(payload);

// hash again, to see the effect on the hash-related params.
// none of the parameters should have a duplicated value

kff.hash(payload);
assertEquals("test.filter", payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_KNOWN_FILTER_NAME));
assertTrue(KffDataObjectHandler.hashPresent(payload));
assertEquals(DATA_MD5, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_MD5));
assertEquals(DATA_CRC32, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_CRC32));
assertEquals(DATA_SSDEEP, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_SSDEEP));
assertEquals(DATA_SHA1, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_SHA1));
assertEquals(DATA_SHA256, payload.getStringParameter(KffDataObjectHandler.KFF_PARAM_SHA256));
assertEquals(KffDataObjectHandler.KFF_DUPE_CURRENT_FORM, payload.getFileType());
}

@Test
void testParentToChildMethod() {
payload.setParameter(KffDataObjectHandler.KFF_PARAM_DUPE_HIT, payload);
Expand Down

0 comments on commit 76a4659

Please sign in to comment.