|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.calcite.standalone; |
| 7 | + |
| 8 | +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_STATE_COUNTRY; |
| 9 | +import static org.opensearch.sql.util.MatcherUtils.rows; |
| 10 | +import static org.opensearch.sql.util.MatcherUtils.schema; |
| 11 | +import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; |
| 12 | +import static org.opensearch.sql.util.MatcherUtils.verifyErrorMessageContains; |
| 13 | +import static org.opensearch.sql.util.MatcherUtils.verifySchema; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import org.json.JSONObject; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +public class CalcitePPLCryptographicFunctionIT extends CalcitePPLIntegTestCase { |
| 20 | + @Override |
| 21 | + public void init() throws IOException { |
| 22 | + super.init(); |
| 23 | + loadIndex(Index.STATE_COUNTRY); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void testMd5() { |
| 28 | + JSONObject actual = |
| 29 | + executeQuery( |
| 30 | + String.format( |
| 31 | + "source=%s | where name = 'Jake' | eval hello = MD5('hello'), california =" |
| 32 | + + " md5(state) | fields hello, california", |
| 33 | + TEST_INDEX_STATE_COUNTRY)); |
| 34 | + verifySchema(actual, schema("hello", "string"), schema("california", "string")); |
| 35 | + verifyDataRows( |
| 36 | + actual, rows("5d41402abc4b2a76b9719d911017c592", "356779a9a1696714480f57fa3fb66d4c")); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testSha1() { |
| 41 | + JSONObject actual = |
| 42 | + executeQuery( |
| 43 | + String.format( |
| 44 | + "source=%s | where name = 'John' | eval hello = SHA1('hello'), ontario =" |
| 45 | + + " SHA1(state) | fields hello, ontario", |
| 46 | + TEST_INDEX_STATE_COUNTRY)); |
| 47 | + verifySchema(actual, schema("hello", "string"), schema("ontario", "string")); |
| 48 | + verifyDataRows( |
| 49 | + actual, |
| 50 | + rows( |
| 51 | + "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d", |
| 52 | + "f9f742e1f653a74c4cd78d7ea283b5556539b96b")); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testSha2() { |
| 57 | + JSONObject actual = |
| 58 | + executeQuery( |
| 59 | + String.format( |
| 60 | + "source=%s | where name = 'Jane' | eval sha256 = SHA2('hello',256), sha512 =" |
| 61 | + + " SHA2('hello',512), sha224 = SHA2(country, 224), sha384 = SHA2(country, 384)" |
| 62 | + + " | fields sha256, sha512, sha224, sha384", |
| 63 | + TEST_INDEX_STATE_COUNTRY)); |
| 64 | + verifySchema( |
| 65 | + actual, |
| 66 | + schema("sha256", "string"), |
| 67 | + schema("sha512", "string"), |
| 68 | + schema("sha224", "string"), |
| 69 | + schema("sha384", "string")); |
| 70 | + verifyDataRows( |
| 71 | + actual, |
| 72 | + rows( |
| 73 | + "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", |
| 74 | + "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043", |
| 75 | + "c16f747ca3d2e267c76e7355429fb1583268d966887f237b8e1605c7", |
| 76 | + "de2abcb28b87d681830f3af25cd8dde7fdc2a4da9dcfde60b371fd2378a70ac39cef3e104bbe09aecda022aee7b4bf59")); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testSha2WrongAlgorithmShouldThrow() { |
| 81 | + Throwable e = |
| 82 | + assertThrows( |
| 83 | + IllegalArgumentException.class, |
| 84 | + () -> |
| 85 | + executeQuery( |
| 86 | + String.format( |
| 87 | + "source=%s | head 1 | eval sha100 = SHA2('hello', 100) | fields sha100", |
| 88 | + TEST_INDEX_STATE_COUNTRY))); |
| 89 | + verifyErrorMessageContains(e, "Unsupported SHA2 algorithm"); |
| 90 | + } |
| 91 | +} |
0 commit comments