Skip to content
This repository was archived by the owner on Jan 27, 2023. It is now read-only.

Commit 5096467

Browse files
committed
Move OrderedStringTestGenerator into CipherStash::Client
1 parent f0eb42c commit 5096467

3 files changed

Lines changed: 102 additions & 100 deletions

File tree

Rakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ task :release do
2828
sh "git release"
2929
end
3030

31-
require "./lib/cipherstash/ordered_string_test_generator"
31+
require "./lib/cipherstash/client/ordered_string_test_generator"
3232

3333
desc "Generate test cases for orderable strings to be used by other CipherStash clients"
3434
task :generate_ordered_string_test_cases do
35-
CipherStash::OrderedStringTestGenerator.new.run
35+
CipherStash::Client::OrderedStringTestGenerator.new.run
3636
end
3737

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

lib/cipherstash/ordered_string_test_generator.rb

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)