From 5b2a36f5d91ffcd0bdf3a3a21d9ef650526ada04 Mon Sep 17 00:00:00 2001 From: Postmodern Date: Mon, 9 Dec 2024 22:29:02 -0800 Subject: [PATCH] Added `Network::Host::REGEX` (closes #550). --- lib/ronin/support/network/host.rb | 5 +++++ spec/network/host_spec.rb | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/ronin/support/network/host.rb b/lib/ronin/support/network/host.rb index 17ec68bc..f2e6102c 100644 --- a/lib/ronin/support/network/host.rb +++ b/lib/ronin/support/network/host.rb @@ -141,6 +141,11 @@ module Network # class Host + # A regular expression for matching host names. + # + # @since 1.2.0 + REGEX = /\A(?:(?:[a-zA-Z\d](?:[-a-zA-Z\d]*[a-zA-Z\d])?)\.)*(?:[a-zA-Z](?:[-a-zA-Z\d]*[a-zA-Z\d])?)\.?\z/ + # The host name. # # @return [String] diff --git a/spec/network/host_spec.rb b/spec/network/host_spec.rb index 87b5efd4..2a0812d9 100644 --- a/spec/network/host_spec.rb +++ b/spec/network/host_spec.rb @@ -18,6 +18,22 @@ subject { described_class.new(hostname) } + describe "REGEX" do + subject { described_class::REGEX } + + it "must match a local hostname" do + expect(subject =~ 'localhost').to be_truthy + end + + it "must match a domain name" do + expect(subject =~ 'example.com').to be_truthy + end + + it "must match a sub-domain name" do + expect(subject =~ 'www.example.com').to be_truthy + end + end + describe "#initialize" do it "must set #name" do expect(subject.name).to eq(hostname)