github-advanced-security[bot] commented on code in PR #785: URL: https://github.com/apache/datasketches-java/pull/785#discussion_r4433560712
########## src/test/java/org/apache/datasketches/tuple/TupleCustomSeedTest.java: ########## @@ -0,0 +1,258 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.datasketches.tuple; + +import static org.apache.datasketches.common.Util.DEFAULT_UPDATE_SEED; +import static org.apache.datasketches.common.Util.computeSeedHash; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.expectThrows; + +import java.lang.foreign.MemorySegment; + +import org.apache.datasketches.common.ByteArrayUtil; +import org.apache.datasketches.common.ResizeFactor; +import org.apache.datasketches.common.SketchesArgumentException; +import org.apache.datasketches.common.SketchesStateException; +import org.apache.datasketches.fdt.FdtSketch; +import org.apache.datasketches.theta.UpdatableThetaSketch; +import org.apache.datasketches.tuple.adouble.DoubleSummary; +import org.apache.datasketches.tuple.adouble.DoubleSummaryDeserializer; +import org.apache.datasketches.tuple.adouble.DoubleSummaryFactory; +import org.apache.datasketches.tuple.adouble.DoubleSummarySetOperations; +import org.apache.datasketches.tuple.adouble.DoubleTupleSketch; +import org.apache.datasketches.tuple.aninteger.IntegerSummary; +import org.apache.datasketches.tuple.aninteger.IntegerTupleSketch; +import org.apache.datasketches.tuple.strings.ArrayOfStringsTupleSketch; +import org.testng.annotations.Test; + +/** + * Checks TupleSketches and their set operations created with a custom update seed. + */ +@SuppressWarnings("deprecation") +public class TupleCustomSeedTest { + private static final long SEED = 123L; + private static final short SEED_HASH = computeSeedHash(SEED); + private static final short DEFAULT_SEED_HASH = computeSeedHash(DEFAULT_UPDATE_SEED); + private static final int SEED_HASH_SHORT = 6; + + private final DoubleSummaryFactory factory = new DoubleSummaryFactory(DoubleSummary.Mode.Sum); + private final DoubleSummarySetOperations dsso = new DoubleSummarySetOperations(); + private final DoubleSummaryDeserializer deser = new DoubleSummaryDeserializer(); + + private UpdatableTupleSketch<Double, DoubleSummary> tupleSketch(final long seed, final int start, final int n) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = + new UpdatableTupleSketchBuilder<>(factory).setSeed(seed).build(); + for (int i = start; i < (start + n); i++) { sk.update(i, 1.0); } + return sk; + } + + private static UpdatableThetaSketch thetaSketch(final long seed, final int start, final int n) { + final UpdatableThetaSketch sk = UpdatableThetaSketch.builder().setSeed(seed).build(); + for (int i = start; i < (start + n); i++) { sk.update(i); } + return sk; + } + + private static short seedHashOf(final byte[] bytes) { + return ByteArrayUtil.getShortLE(bytes, SEED_HASH_SHORT); + } + + @Test + public void checkSeedHashIsCarried() { + final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED, 0, 10); + assertEquals(sk.getSeedHash(), SEED_HASH); + assertEquals(sk.copy().getSeedHash(), SEED_HASH); + assertEquals(sk.compact().getSeedHash(), SEED_HASH); + assertEquals(seedHashOf(sk.compact().toByteArray()), SEED_HASH); + assertEquals(tupleSketch(DEFAULT_UPDATE_SEED, 0, 10).getSeedHash(), DEFAULT_SEED_HASH); + } + + @Test + public void checkDefaultSeedHashMatchesCpp() { + final byte[] bytes = tupleSketch(DEFAULT_UPDATE_SEED, 0, 10).compact().toByteArray(); + assertEquals(seedHashOf(bytes), (short) 37836); + } + + @Test + public void checkSeedChangesHashes() { + assertNotEquals(hashOf(tupleSketch(SEED, 0, 1)), hashOf(tupleSketch(DEFAULT_UPDATE_SEED, 0, 1))); + } + + @Test + public void checkHeapifyCompact() { + final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED, 0, 100); + final MemorySegment seg = MemorySegment.ofArray(sk.compact().toByteArray()); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser)); + final TupleSketch<DoubleSummary> sk2 = TupleSketch.heapifySketch(seg, deser, SEED); + assertEquals(sk2.getEstimate(), sk.getEstimate()); + assertEquals(sk2.getSeedHash(), SEED_HASH); + //default seed image read with a custom seed is rejected + final MemorySegment segDef = MemorySegment.ofArray(tupleSketch(DEFAULT_UPDATE_SEED, 0, 100).compact().toByteArray()); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(segDef, deser, SEED)); + } + + @Test + public void checkHeapifyEmptyCompact() { + final byte[] bytes = tupleSketch(SEED, 0, 0).compact().toByteArray(); + assertEquals(seedHashOf(bytes), (short) 0); //empty image carries zero, as in theta + final MemorySegment seg = MemorySegment.ofArray(bytes); + assertTrue(TupleSketch.heapifySketch(seg, deser).isEmpty()); + final TupleSketch<DoubleSummary> sk = TupleSketch.heapifySketch(seg, deser, SEED); + assertTrue(sk.isEmpty()); + assertEquals(sk.getSeedHash(), SEED_HASH); + } + + @Test + public void checkUpdatableSerialization() { + final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED, 0, 10); + expectThrows(SketchesStateException.class, () -> sk.toByteArray()); + //the updatable form has no seed hash, so it is read as the default seed + final byte[] bytes = tupleSketch(DEFAULT_UPDATE_SEED, 0, 10).toByteArray(); + final MemorySegment seg = MemorySegment.ofArray(bytes); + assertEquals(TupleSketch.heapifySketch(seg, deser).getRetainedEntries(), 10); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser, SEED)); + } + + @Test + public void checkUpdatableSerializationComparesFullSeed() { + //a custom seed whose 16-bit seed hash equals that of the default seed must still be refused + final short defaultSeedHash = computeSeedHash(DEFAULT_UPDATE_SEED); + long seed = 1; + while ((seed == DEFAULT_UPDATE_SEED) || (computeSeedHash(seed) != defaultSeedHash)) { seed++; } + final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(seed, 0, 10); + assertEquals(sk.getSeedHash(), defaultSeedHash); + expectThrows(SketchesStateException.class, () -> sk.toByteArray()); + expectThrows(SketchesStateException.class, () -> sk.copy().toByteArray()); + expectThrows(SketchesStateException.class, + () -> new DoubleTupleSketch(12, 3, 1f, DoubleSummary.Mode.Sum, SEED).toByteArray()); Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [UpdatableTupleSketch.toByteArray](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/984) ########## src/test/java/org/apache/datasketches/tuple/TupleCustomSeedTest.java: ########## @@ -0,0 +1,258 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.datasketches.tuple; + +import static org.apache.datasketches.common.Util.DEFAULT_UPDATE_SEED; +import static org.apache.datasketches.common.Util.computeSeedHash; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.expectThrows; + +import java.lang.foreign.MemorySegment; + +import org.apache.datasketches.common.ByteArrayUtil; +import org.apache.datasketches.common.ResizeFactor; +import org.apache.datasketches.common.SketchesArgumentException; +import org.apache.datasketches.common.SketchesStateException; +import org.apache.datasketches.fdt.FdtSketch; +import org.apache.datasketches.theta.UpdatableThetaSketch; +import org.apache.datasketches.tuple.adouble.DoubleSummary; +import org.apache.datasketches.tuple.adouble.DoubleSummaryDeserializer; +import org.apache.datasketches.tuple.adouble.DoubleSummaryFactory; +import org.apache.datasketches.tuple.adouble.DoubleSummarySetOperations; +import org.apache.datasketches.tuple.adouble.DoubleTupleSketch; +import org.apache.datasketches.tuple.aninteger.IntegerSummary; +import org.apache.datasketches.tuple.aninteger.IntegerTupleSketch; +import org.apache.datasketches.tuple.strings.ArrayOfStringsTupleSketch; +import org.testng.annotations.Test; + +/** + * Checks TupleSketches and their set operations created with a custom update seed. + */ +@SuppressWarnings("deprecation") +public class TupleCustomSeedTest { + private static final long SEED = 123L; + private static final short SEED_HASH = computeSeedHash(SEED); + private static final short DEFAULT_SEED_HASH = computeSeedHash(DEFAULT_UPDATE_SEED); + private static final int SEED_HASH_SHORT = 6; + + private final DoubleSummaryFactory factory = new DoubleSummaryFactory(DoubleSummary.Mode.Sum); + private final DoubleSummarySetOperations dsso = new DoubleSummarySetOperations(); + private final DoubleSummaryDeserializer deser = new DoubleSummaryDeserializer(); + + private UpdatableTupleSketch<Double, DoubleSummary> tupleSketch(final long seed, final int start, final int n) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = + new UpdatableTupleSketchBuilder<>(factory).setSeed(seed).build(); + for (int i = start; i < (start + n); i++) { sk.update(i, 1.0); } + return sk; + } + + private static UpdatableThetaSketch thetaSketch(final long seed, final int start, final int n) { + final UpdatableThetaSketch sk = UpdatableThetaSketch.builder().setSeed(seed).build(); + for (int i = start; i < (start + n); i++) { sk.update(i); } + return sk; + } + + private static short seedHashOf(final byte[] bytes) { + return ByteArrayUtil.getShortLE(bytes, SEED_HASH_SHORT); + } + + @Test + public void checkSeedHashIsCarried() { + final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED, 0, 10); + assertEquals(sk.getSeedHash(), SEED_HASH); + assertEquals(sk.copy().getSeedHash(), SEED_HASH); + assertEquals(sk.compact().getSeedHash(), SEED_HASH); + assertEquals(seedHashOf(sk.compact().toByteArray()), SEED_HASH); + assertEquals(tupleSketch(DEFAULT_UPDATE_SEED, 0, 10).getSeedHash(), DEFAULT_SEED_HASH); + } + + @Test + public void checkDefaultSeedHashMatchesCpp() { + final byte[] bytes = tupleSketch(DEFAULT_UPDATE_SEED, 0, 10).compact().toByteArray(); + assertEquals(seedHashOf(bytes), (short) 37836); + } + + @Test + public void checkSeedChangesHashes() { + assertNotEquals(hashOf(tupleSketch(SEED, 0, 1)), hashOf(tupleSketch(DEFAULT_UPDATE_SEED, 0, 1))); + } + + @Test + public void checkHeapifyCompact() { + final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED, 0, 100); + final MemorySegment seg = MemorySegment.ofArray(sk.compact().toByteArray()); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser)); + final TupleSketch<DoubleSummary> sk2 = TupleSketch.heapifySketch(seg, deser, SEED); + assertEquals(sk2.getEstimate(), sk.getEstimate()); + assertEquals(sk2.getSeedHash(), SEED_HASH); + //default seed image read with a custom seed is rejected + final MemorySegment segDef = MemorySegment.ofArray(tupleSketch(DEFAULT_UPDATE_SEED, 0, 100).compact().toByteArray()); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(segDef, deser, SEED)); + } + + @Test + public void checkHeapifyEmptyCompact() { + final byte[] bytes = tupleSketch(SEED, 0, 0).compact().toByteArray(); + assertEquals(seedHashOf(bytes), (short) 0); //empty image carries zero, as in theta + final MemorySegment seg = MemorySegment.ofArray(bytes); + assertTrue(TupleSketch.heapifySketch(seg, deser).isEmpty()); + final TupleSketch<DoubleSummary> sk = TupleSketch.heapifySketch(seg, deser, SEED); + assertTrue(sk.isEmpty()); + assertEquals(sk.getSeedHash(), SEED_HASH); + } + + @Test + public void checkUpdatableSerialization() { + final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED, 0, 10); + expectThrows(SketchesStateException.class, () -> sk.toByteArray()); + //the updatable form has no seed hash, so it is read as the default seed + final byte[] bytes = tupleSketch(DEFAULT_UPDATE_SEED, 0, 10).toByteArray(); + final MemorySegment seg = MemorySegment.ofArray(bytes); + assertEquals(TupleSketch.heapifySketch(seg, deser).getRetainedEntries(), 10); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser, SEED)); + } + + @Test + public void checkUpdatableSerializationComparesFullSeed() { + //a custom seed whose 16-bit seed hash equals that of the default seed must still be refused + final short defaultSeedHash = computeSeedHash(DEFAULT_UPDATE_SEED); + long seed = 1; + while ((seed == DEFAULT_UPDATE_SEED) || (computeSeedHash(seed) != defaultSeedHash)) { seed++; } + final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(seed, 0, 10); + assertEquals(sk.getSeedHash(), defaultSeedHash); + expectThrows(SketchesStateException.class, () -> sk.toByteArray()); Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [UpdatableTupleSketch.toByteArray](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/982) ########## src/test/java/org/apache/datasketches/tuple/TupleCustomSeedTest.java: ########## @@ -0,0 +1,258 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.datasketches.tuple; + +import static org.apache.datasketches.common.Util.DEFAULT_UPDATE_SEED; +import static org.apache.datasketches.common.Util.computeSeedHash; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.expectThrows; + +import java.lang.foreign.MemorySegment; + +import org.apache.datasketches.common.ByteArrayUtil; +import org.apache.datasketches.common.ResizeFactor; +import org.apache.datasketches.common.SketchesArgumentException; +import org.apache.datasketches.common.SketchesStateException; +import org.apache.datasketches.fdt.FdtSketch; +import org.apache.datasketches.theta.UpdatableThetaSketch; +import org.apache.datasketches.tuple.adouble.DoubleSummary; +import org.apache.datasketches.tuple.adouble.DoubleSummaryDeserializer; +import org.apache.datasketches.tuple.adouble.DoubleSummaryFactory; +import org.apache.datasketches.tuple.adouble.DoubleSummarySetOperations; +import org.apache.datasketches.tuple.adouble.DoubleTupleSketch; +import org.apache.datasketches.tuple.aninteger.IntegerSummary; +import org.apache.datasketches.tuple.aninteger.IntegerTupleSketch; +import org.apache.datasketches.tuple.strings.ArrayOfStringsTupleSketch; +import org.testng.annotations.Test; + +/** + * Checks TupleSketches and their set operations created with a custom update seed. + */ +@SuppressWarnings("deprecation") +public class TupleCustomSeedTest { + private static final long SEED = 123L; + private static final short SEED_HASH = computeSeedHash(SEED); + private static final short DEFAULT_SEED_HASH = computeSeedHash(DEFAULT_UPDATE_SEED); + private static final int SEED_HASH_SHORT = 6; + + private final DoubleSummaryFactory factory = new DoubleSummaryFactory(DoubleSummary.Mode.Sum); + private final DoubleSummarySetOperations dsso = new DoubleSummarySetOperations(); + private final DoubleSummaryDeserializer deser = new DoubleSummaryDeserializer(); + + private UpdatableTupleSketch<Double, DoubleSummary> tupleSketch(final long seed, final int start, final int n) { + final UpdatableTupleSketch<Double, DoubleSummary> sk = + new UpdatableTupleSketchBuilder<>(factory).setSeed(seed).build(); + for (int i = start; i < (start + n); i++) { sk.update(i, 1.0); } + return sk; + } + + private static UpdatableThetaSketch thetaSketch(final long seed, final int start, final int n) { + final UpdatableThetaSketch sk = UpdatableThetaSketch.builder().setSeed(seed).build(); + for (int i = start; i < (start + n); i++) { sk.update(i); } + return sk; + } + + private static short seedHashOf(final byte[] bytes) { + return ByteArrayUtil.getShortLE(bytes, SEED_HASH_SHORT); + } + + @Test + public void checkSeedHashIsCarried() { + final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED, 0, 10); + assertEquals(sk.getSeedHash(), SEED_HASH); + assertEquals(sk.copy().getSeedHash(), SEED_HASH); + assertEquals(sk.compact().getSeedHash(), SEED_HASH); + assertEquals(seedHashOf(sk.compact().toByteArray()), SEED_HASH); + assertEquals(tupleSketch(DEFAULT_UPDATE_SEED, 0, 10).getSeedHash(), DEFAULT_SEED_HASH); + } + + @Test + public void checkDefaultSeedHashMatchesCpp() { + final byte[] bytes = tupleSketch(DEFAULT_UPDATE_SEED, 0, 10).compact().toByteArray(); + assertEquals(seedHashOf(bytes), (short) 37836); + } + + @Test + public void checkSeedChangesHashes() { + assertNotEquals(hashOf(tupleSketch(SEED, 0, 1)), hashOf(tupleSketch(DEFAULT_UPDATE_SEED, 0, 1))); + } + + @Test + public void checkHeapifyCompact() { + final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED, 0, 100); + final MemorySegment seg = MemorySegment.ofArray(sk.compact().toByteArray()); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser)); + final TupleSketch<DoubleSummary> sk2 = TupleSketch.heapifySketch(seg, deser, SEED); + assertEquals(sk2.getEstimate(), sk.getEstimate()); + assertEquals(sk2.getSeedHash(), SEED_HASH); + //default seed image read with a custom seed is rejected + final MemorySegment segDef = MemorySegment.ofArray(tupleSketch(DEFAULT_UPDATE_SEED, 0, 100).compact().toByteArray()); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(segDef, deser, SEED)); + } + + @Test + public void checkHeapifyEmptyCompact() { + final byte[] bytes = tupleSketch(SEED, 0, 0).compact().toByteArray(); + assertEquals(seedHashOf(bytes), (short) 0); //empty image carries zero, as in theta + final MemorySegment seg = MemorySegment.ofArray(bytes); + assertTrue(TupleSketch.heapifySketch(seg, deser).isEmpty()); + final TupleSketch<DoubleSummary> sk = TupleSketch.heapifySketch(seg, deser, SEED); + assertTrue(sk.isEmpty()); + assertEquals(sk.getSeedHash(), SEED_HASH); + } + + @Test + public void checkUpdatableSerialization() { + final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED, 0, 10); + expectThrows(SketchesStateException.class, () -> sk.toByteArray()); + //the updatable form has no seed hash, so it is read as the default seed + final byte[] bytes = tupleSketch(DEFAULT_UPDATE_SEED, 0, 10).toByteArray(); + final MemorySegment seg = MemorySegment.ofArray(bytes); + assertEquals(TupleSketch.heapifySketch(seg, deser).getRetainedEntries(), 10); + expectThrows(SketchesArgumentException.class, () -> TupleSketch.heapifySketch(seg, deser, SEED)); + } + + @Test + public void checkUpdatableSerializationComparesFullSeed() { + //a custom seed whose 16-bit seed hash equals that of the default seed must still be refused + final short defaultSeedHash = computeSeedHash(DEFAULT_UPDATE_SEED); + long seed = 1; + while ((seed == DEFAULT_UPDATE_SEED) || (computeSeedHash(seed) != defaultSeedHash)) { seed++; } + final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(seed, 0, 10); + assertEquals(sk.getSeedHash(), defaultSeedHash); + expectThrows(SketchesStateException.class, () -> sk.toByteArray()); + expectThrows(SketchesStateException.class, () -> sk.copy().toByteArray()); Review Comment: ## CodeQL / Deprecated method or constructor invocation Invoking [UpdatableTupleSketch.toByteArray](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/983) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
