|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.expression.function.scalar.hash; |
| 9 | + |
| 10 | +import com.carrotsearch.randomizedtesting.annotations.Name; |
| 11 | +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
| 12 | + |
| 13 | +import org.apache.lucene.util.BytesRef; |
| 14 | +import org.elasticsearch.xpack.esql.core.expression.Expression; |
| 15 | +import org.elasticsearch.xpack.esql.core.tree.Source; |
| 16 | +import org.elasticsearch.xpack.esql.core.type.DataType; |
| 17 | +import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase; |
| 18 | +import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier; |
| 19 | + |
| 20 | +import java.security.MessageDigest; |
| 21 | +import java.security.NoSuchAlgorithmException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.HexFormat; |
| 24 | +import java.util.List; |
| 25 | +import java.util.function.Supplier; |
| 26 | + |
| 27 | +import static org.hamcrest.Matchers.equalTo; |
| 28 | + |
| 29 | +public class HashTests extends AbstractScalarFunctionTestCase { |
| 30 | + |
| 31 | + public HashTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) { |
| 32 | + this.testCase = testCaseSupplier.get(); |
| 33 | + } |
| 34 | + |
| 35 | + @ParametersFactory |
| 36 | + public static Iterable<Object[]> parameters() { |
| 37 | + List<TestCaseSupplier> cases = new ArrayList<>(); |
| 38 | + for (String alg : List.of("MD5", "SHA", "SHA-224", "SHA-256", "SHA-384", "SHA-512")) { |
| 39 | + cases.addAll(createTestCases(alg)); |
| 40 | + } |
| 41 | + return parameterSuppliersFromTypedData(cases); |
| 42 | + } |
| 43 | + |
| 44 | + private static List<TestCaseSupplier> createTestCases(String alg) { |
| 45 | + return List.of( |
| 46 | + createTestCase(alg, DataType.KEYWORD, DataType.KEYWORD), |
| 47 | + createTestCase(alg, DataType.KEYWORD, DataType.TEXT), |
| 48 | + createTestCase(alg, DataType.TEXT, DataType.KEYWORD), |
| 49 | + createTestCase(alg, DataType.TEXT, DataType.TEXT) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + private static TestCaseSupplier createTestCase(String alg, DataType algType, DataType inputType) { |
| 54 | + return new TestCaseSupplier(alg, List.of(algType, inputType), () -> { |
| 55 | + var input = randomAlphaOfLength(10); |
| 56 | + return new TestCaseSupplier.TestCase( |
| 57 | + List.of( |
| 58 | + new TestCaseSupplier.TypedData(new BytesRef(alg), algType, "alg"), |
| 59 | + new TestCaseSupplier.TypedData(input, inputType, "input") |
| 60 | + ), |
| 61 | + "HashEvaluator[alg=Attribute[channel=0], input=Attribute[channel=1]]", |
| 62 | + DataType.KEYWORD, |
| 63 | + equalTo(new BytesRef(hash(alg, input))) |
| 64 | + ); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + private static String hash(String alg, String input) { |
| 69 | + try { |
| 70 | + return HexFormat.of().formatHex(MessageDigest.getInstance(alg).digest(input.getBytes())); |
| 71 | + } catch (NoSuchAlgorithmException e) { |
| 72 | + throw new IllegalArgumentException("Unknown algorithm: " + alg); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected Expression build(Source source, List<Expression> args) { |
| 78 | + return new Hash(source, args.get(0), args.get(1)); |
| 79 | + } |
| 80 | +} |
0 commit comments