|
| 1 | +require "securerandom" |
| 2 | +require "json" |
| 3 | + |
| 4 | +require_relative "./index" |
| 5 | + |
| 6 | +module CipherStash |
| 7 | + # Generates test cases for orderable strings that can be used in automated tests in other |
| 8 | + # clients (e.g. StashRS). Test cases are written to JSON files. |
| 9 | + # |
| 10 | + # Example usage in StashRS: |
| 11 | + # https://github.com/cipherstash/cipherstash-rs/blob/00ff66d712e6f36a89acfa731b680a69789647cf/packages/cipherstash-client/src/indexer/mapping_indexer.rs#L537-L570 |
| 12 | + # |
| 13 | + # @private |
| 14 | + class OrderedStringTestGenerator |
| 15 | + # The number of test cases to generate per test file. This number is arbitrary. 100 seems good |
| 16 | + # because GitHub is willing to display the output files in diffs in PRs, but will require |
| 17 | + # pulling branches down for larger numbers of test cases. |
| 18 | + NUM_TEST_CASES = 100 |
| 19 | + |
| 20 | + # Max character code for ASCII characters in randomly generated strings in test data. |
| 21 | + ASCII_CHAR_CODE_MAX = 127 |
| 22 | + |
| 23 | + # The max length of randomly generated strings in test data. This number is also somewhat |
| 24 | + # arbitrary. The first 80 ASCII characters are considered for ordering, so we want some |
| 25 | + # strings with a length of at least 80. 200 seems good because this will also test lengths |
| 26 | + # beyond what actually gets indexed for ordering but doesn't bloat the test files too much. |
| 27 | + MAX_STRING_LENGTH = 200 |
| 28 | + |
| 29 | + def run |
| 30 | + create_orderise_string_test_cases |
| 31 | + create_string_comparison_test_cases |
| 32 | + end |
| 33 | + |
| 34 | + private |
| 35 | + |
| 36 | + def random_ascii_string |
| 37 | + (0..rand(MAX_STRING_LENGTH - 1)).map { rand(ASCII_CHAR_CODE_MAX + 1).chr }.join |
| 38 | + end |
| 39 | + |
| 40 | + def index |
| 41 | + id = SecureRandom.uuid |
| 42 | + settings = { |
| 43 | + "meta" => { |
| 44 | + "$indexId" => id, |
| 45 | + "$indexName" => "titleSort", |
| 46 | + "$prfKey" => SecureRandom.hex(16), |
| 47 | + "$prpKey" => SecureRandom.hex(16), |
| 48 | + }, |
| 49 | + "mapping" => { |
| 50 | + "kind" => "range", |
| 51 | + "field" => "title", |
| 52 | + "fieldType"=>"string", |
| 53 | + } |
| 54 | + } |
| 55 | + schema_versions = {:first=>0, :last=>0, :searchable=>true} |
| 56 | + |
| 57 | + CipherStash::Index.generate(id, settings, schema_versions) |
| 58 | + end |
| 59 | + |
| 60 | + def write_cases_to_file(filename, test_cases) |
| 61 | + File.write(filename, JSON.pretty_generate(test_cases)) |
| 62 | + puts "Created ./" + filename |
| 63 | + end |
| 64 | + |
| 65 | + def create_orderise_string_test_cases |
| 66 | + orderise_string_cases = (0..(NUM_TEST_CASES - 1)).map do |
| 67 | + str = random_ascii_string |
| 68 | + output = index.__send__ :orderise_string, str |
| 69 | + {input: str, output: output} |
| 70 | + end |
| 71 | + |
| 72 | + write_cases_to_file("orderise_string_test_cases.json", orderise_string_cases) |
| 73 | + end |
| 74 | + |
| 75 | + def create_string_comparison_test_cases |
| 76 | + string_comparison_cases = (0..(NUM_TEST_CASES - 1)).map do |
| 77 | + str_a = random_ascii_string |
| 78 | + terms_a = index.__send__ :orderise_string, str_a |
| 79 | + |
| 80 | + str_b = random_ascii_string |
| 81 | + terms_b = index.__send__ :orderise_string, str_b |
| 82 | + |
| 83 | + output = case terms_a <=> terms_b |
| 84 | + when -1 |
| 85 | + "<" |
| 86 | + when 0 |
| 87 | + "==" |
| 88 | + when 1 |
| 89 | + ">" |
| 90 | + end |
| 91 | + |
| 92 | + {input: [str_a, str_b], output: output} |
| 93 | + end |
| 94 | + |
| 95 | + write_cases_to_file("string_comparison_test_cases.json", string_comparison_cases) |
| 96 | + end |
| 97 | + end |
| 98 | +end |
0 commit comments