From b4f521b3bf9477e05cf79015e631e135281f203a Mon Sep 17 00:00:00 2001 From: nick evans Date: Sun, 15 Dec 2024 15:52:25 -0500 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=85=20Add=20some=20test=20coverage=20?= =?UTF-8?q?for=20search=20charset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/net/imap/test_imap.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/net/imap/test_imap.rb b/test/net/imap/test_imap.rb index 1af03bbf..5fa9d643 100644 --- a/test/net/imap/test_imap.rb +++ b/test/net/imap/test_imap.rb @@ -1229,6 +1229,12 @@ def test_unselect imap.search(["subject", "hello", Set[1, 2, 3, 4, 5, 8, *(10..100)]]) assert_equal "subject hello 1:5,8,10:100", server.commands.pop.args + imap.search('SUBJECT "Hello world"', "UTF-8") + assert_equal 'CHARSET UTF-8 SUBJECT "Hello world"', server.commands.pop.args + + imap.search('CHARSET UTF-8 SUBJECT "Hello world"') + assert_equal 'CHARSET UTF-8 SUBJECT "Hello world"', server.commands.pop.args + imap.search([:*]) assert_equal "*", server.commands.pop.args From 3bdd1f9a9067e386852529a19597b215ce98aa2c Mon Sep 17 00:00:00 2001 From: nick evans Date: Sun, 15 Dec 2024 16:36:34 -0500 Subject: [PATCH 2/3] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Extract=20search=5Farg?= =?UTF-8?q?s=20from=20search=5Finternal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/net/imap.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/net/imap.rb b/lib/net/imap.rb index 48e34789..6bd2f107 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -3150,9 +3150,14 @@ def enforce_logindisabled? end end - def search_internal(cmd, keys, charset = nil) - keys = normalize_searching_criteria(keys) - args = charset ? ["CHARSET", charset, *keys] : keys + def search_args(keys, charset = nil) + args = normalize_searching_criteria(keys) + args.prepend("CHARSET", charset) if charset + args + end + + def search_internal(cmd, ...) + args = search_args(...) synchronize do send_command(cmd, *args) search_result = clear_responses("SEARCH").last @@ -3223,7 +3228,7 @@ def thread_internal(cmd, algorithm, search_keys, charset) end def normalize_searching_criteria(criteria) - return RawData.new(criteria) if criteria.is_a?(String) + return [RawData.new(criteria)] if criteria.is_a?(String) criteria.map {|i| if coerce_search_arg_to_seqset?(i) SequenceSet[i] From 8285049d8b90a865e59d1019b63dd8003a280935 Mon Sep 17 00:00:00 2001 From: nick evans Date: Sun, 15 Dec 2024 16:38:10 -0500 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=A5=85=20Raise=20ArgumentError=20on?= =?UTF-8?q?=20multiple=20search=20charset=20args?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/net/imap.rb | 4 ++++ test/net/imap/test_imap.rb | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/net/imap.rb b/lib/net/imap.rb index 6bd2f107..397f24b8 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -3151,6 +3151,10 @@ def enforce_logindisabled? end def search_args(keys, charset = nil) + # NOTE: not handling combined RETURN and CHARSET for raw strings + if charset && keys in /\ACHARSET\b/i | Array[/\ACHARSET\z/i, *] + raise ArgumentError, "multiple charset arguments" + end args = normalize_searching_criteria(keys) args.prepend("CHARSET", charset) if charset args diff --git a/test/net/imap/test_imap.rb b/test/net/imap/test_imap.rb index 5fa9d643..fc2ccdd5 100644 --- a/test/net/imap/test_imap.rb +++ b/test/net/imap/test_imap.rb @@ -1265,6 +1265,24 @@ def seqset_coercible.to_sequence_set end end + test("#search/#uid_search with invalid arguments") do + with_fake_server do |server, imap| + server.on "SEARCH" do |cmd| cmd.fail_no "should fail before this" end + server.on "UID SEARCH" do |cmd| cmd.fail_no "should fail before this" end + + assert_raise(ArgumentError) do + imap.search(["charset", "foo", "ALL"], "bar") + end + assert_raise(ArgumentError) do + imap.search("charset foo ALL", "bar") + end + # Parsing return opts is too complicated, for now. + # assert_raise(ArgumentError) do + # imap.search("return () charset foo ALL", "bar") + # end + end + end + test("missing server SEARCH response") do with_fake_server do |server, imap| server.on "SEARCH", &:done_ok