From 39647f01b609875f8743c8789a680a8a57583317 Mon Sep 17 00:00:00 2001 From: Phillip Davis Date: Tue, 31 Mar 2026 15:13:08 -0400 Subject: [PATCH] exercise MMS rewriting for long msgs basically, add FakeFs and set MMS_{PATH, URL}, then fix things that those two broke --- Gemfile | 1 + sgx-bwmsgsv2.rb | 2 +- test/property/test_invisible_separator.rb | 4 +- test/test_component.rb | 28 +++++++------- test/test_helper.rb | 46 +++++++++++++++++------ 5 files changed, 53 insertions(+), 28 deletions(-) diff --git a/Gemfile b/Gemfile index a2d226e622437348ce074f8109fa343055bc55f1..0ca3d992d73396b72c7952350ca35b2f36432b60 100644 --- a/Gemfile +++ b/Gemfile @@ -46,4 +46,5 @@ group(:test) do # DSL wrapper around Rantly generators gem "jennifer", github: "phdavis1027/jennifer" gem "regexp-examples", "~> 1.6" + gem 'fakefs', require: false end diff --git a/sgx-bwmsgsv2.rb b/sgx-bwmsgsv2.rb index f81dd4bdad40c043d1720c9f7b88b470cdea5ff1..604ef04d364bf182a26dee4ac5fad145cf3e320a 100755 --- a/sgx-bwmsgsv2.rb +++ b/sgx-bwmsgsv2.rb @@ -469,7 +469,7 @@ module SGXbwmsgsv2 end segment_size = body.ascii_only? ? 160 : 70 - if !murl && ENV["MMS_PATH"] && body.length > segment_size*3 + if !murl && ENV["MMS_PATH"] && num_dest =~ /^\+?1/ && body.length > segment_size*3 file = Multibases.pack( 'base58btc', Multihashes.encode(Digest::SHA256.digest(body), "sha2-256") diff --git a/test/property/test_invisible_separator.rb b/test/property/test_invisible_separator.rb index 61df046a369d2b0607ffe49368f75809099b1ccf..a804e1ba3bd7b7ec1dd8ce5c98f288704d4d9177 100644 --- a/test/property/test_invisible_separator.rb +++ b/test/property/test_invisible_separator.rb @@ -67,7 +67,9 @@ class InvisibleSeparatorPropertyTest < Minitest::Test # mess when you can. bw_req = stub_request(:post, BW_MESSAGES_URL).with { |req| parsed = ::JSON.parse(req.body) - parsed["to"] == dest && parsed["text"] == m.body.to_s + parsed["to"] == dest && + (parsed["text"] == m.body.to_s || + (parsed["text"] == "" && parsed["media"])) }.to_return( status: 201, body: JSON.dump(id: "bw-msg-stub") diff --git a/test/test_component.rb b/test/test_component.rb index fa3e22073dad39049ce6d38e7ed46d2e2c6312b4..4ee2c838c9d7ceda3c8a14ec2637ffef4e511ff6 100644 --- a/test/test_component.rb +++ b/test/test_component.rb @@ -10,6 +10,7 @@ class ComponentTest < Minitest::Test def setup reset_stanzas! reset_redis! + FakeFS.clear! end def test_message_unregistered @@ -28,33 +29,32 @@ class ComponentTest < Minitest::Test em :test_message_unregistered def test_message_too_long + body = "a" * 4096 + file = Multibases.pack( + 'base58btc', + Multihashes.encode(Digest::SHA256.digest(body), "sha2-256") + ).to_s + expected_url = "https://mms.test.example.com/#{file}.txt" + req = stub_request( :post, "https://messaging.bandwidth.com/api/v2/users/account/messages" ).with(body: { from: "+15550000000", to: "+15551234567", - text: "a"*4096, + text: "", + media: expected_url, applicationId: nil, tag: " " - }).to_return(status: 400, body: JSON.dump( - description: "Bad text.", - fieldErrors: [{ description: "4096 not allowed" }] - )) + }).to_return(status: 201, body: JSON.dump(id: "bw-mms-123")) - m = Blather::Stanza::Message.new("+15551234567@component", "a"*4096) + m = Blather::Stanza::Message.new("+15551234567@component", body) m.from = "test@example.com" process_stanza(m) assert_requested req - assert_equal 1, written.length - - stanza = Blather::XMPPNode.parse(written.first.to_xml) - assert stanza.error? - error = stanza.find_first("error") - assert_equal "cancel", error["type"] - assert_equal "internal-server-error", xmpp_error_name(error) - assert_equal "Bad text. 4096 not allowed", xmpp_error_text(error) + assert_empty written + assert_equal body, File.read("/mms/#{file}") end em :test_message_too_long diff --git a/test/test_helper.rb b/test/test_helper.rb index 744476055ce105df2299f68d5d339205b4d30672..0bafe6ab55a538181533383d4e3f7f0f224476ec 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,6 +9,7 @@ end require "minitest/autorun" require "webmock/minitest" +require "fakefs/safe" MMS_PROXY = "https://proxy.test.example.com/" BW_MESSAGES_URL = "https://messaging.bandwidth.com/api/v2/users/account/messages" @@ -85,6 +86,17 @@ def xmpp_error_text(error) error.find_first("ns:text", ns: Blather::StanzaError::STANZA_ERR_NS)&.text end +def with_mms_env(path: "/mms", url: "https://mms.test.example.com") + old_path = ENV["MMS_PATH"] + old_url = ENV["MMS_URL"] + ENV["MMS_PATH"] = path + ENV["MMS_URL"] = url + yield +ensure + ENV["MMS_PATH"] = old_path + ENV["MMS_URL"] = old_url +end + begin require "pry-byebug" @@ -241,18 +253,28 @@ module Minitest define_method(m) do $panic = nil e = nil - EM.run do - Fiber.new { - ARGV[0] = "component" - ARGV[6] = MMS_PROXY - begin - send("raw_#{m}") - rescue - e = $! - ensure - EM.stop - end - }.resume + FakeFS do + FakeFS::FileSystem.clear + FileUtils.mkdir_p("/mms") + EM.run do + Fiber.new { + with_mms_env do + ARGV[0] = "component" + ARGV[6] = MMS_PROXY + begin + send("raw_#{m}") + rescue + e = $! + ensure + EM.stop + end + end # with_mms_env do + }.resume + end # EM.run do + end # FakeFs do + if e + LOG.error("Error in test: #{m}", e) + raise e end raise e if e end