valid_email2 7.0.15 → 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yaml +2 -7
- data/.gitignore +1 -0
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +30 -0
- data/Gemfile +5 -0
- data/README.md +46 -0
- data/config/disposable_email_domains.txt +15896 -7
- data/lib/valid_email2/address.rb +22 -11
- data/lib/valid_email2/dns.rb +26 -26
- data/lib/valid_email2/email_validator.rb +22 -12
- data/lib/valid_email2/sorted_domain_list.rb +36 -0
- data/lib/valid_email2/version.rb +1 -1
- data/lib/valid_email2.rb +27 -0
- data/pull_mailchecker_emails.rb +2 -0
- data/spec/address_spec.rb +63 -157
- data/spec/benchmark_spec.rb +11 -1
- data/spec/dns_spec.rb +44 -0
- data/spec/sorted_domain_list_spec.rb +17 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/valid_email2_spec.rb +114 -0
- metadata +215 -192
data/lib/valid_email2/address.rb
CHANGED
|
@@ -8,9 +8,12 @@ module ValidEmail2
|
|
|
8
8
|
class Address
|
|
9
9
|
attr_accessor :address
|
|
10
10
|
|
|
11
|
-
PROHIBITED_DOMAIN_CHARACTERS_REGEX = /[+!_\/\s'
|
|
11
|
+
PROHIBITED_DOMAIN_CHARACTERS_REGEX = /[+!_\/\s'#`|]/
|
|
12
12
|
DEFAULT_RECIPIENT_DELIMITER = '+'
|
|
13
13
|
DOT_DELIMITER = '.'
|
|
14
|
+
HYPHEN_DELIMITER = '-'
|
|
15
|
+
MAX_DOMAIN_LENGTH = 253
|
|
16
|
+
MAX_LABEL_LENGTH = 63
|
|
14
17
|
|
|
15
18
|
def self.prohibited_domain_characters_regex
|
|
16
19
|
@prohibited_domain_characters_regex ||= PROHIBITED_DOMAIN_CHARACTERS_REGEX
|
|
@@ -52,13 +55,13 @@ module ValidEmail2
|
|
|
52
55
|
def valid_domain?
|
|
53
56
|
domain = address.domain
|
|
54
57
|
return false if domain.nil?
|
|
58
|
+
return false if domain =~ self.class.prohibited_domain_characters_regex
|
|
59
|
+
return false if domain.length > MAX_DOMAIN_LENGTH
|
|
55
60
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
!domain.start_with?('-') &&
|
|
61
|
-
!domain.include?('-.')
|
|
61
|
+
labels = domain.split(DOT_DELIMITER, -1)
|
|
62
|
+
return false if labels.length < 2
|
|
63
|
+
|
|
64
|
+
labels.all? { |label| valid_domain_label?(label) }
|
|
62
65
|
end
|
|
63
66
|
|
|
64
67
|
def valid_address?
|
|
@@ -82,11 +85,11 @@ module ValidEmail2
|
|
|
82
85
|
end
|
|
83
86
|
|
|
84
87
|
def disposable_domain?
|
|
85
|
-
domain_is_in?(address.domain, ValidEmail2.
|
|
88
|
+
valid? && domain_is_in?(address.domain, ValidEmail2.disposable_domains)
|
|
86
89
|
end
|
|
87
90
|
|
|
88
91
|
def allow_listed?
|
|
89
|
-
domain_is_in?(address.domain, ValidEmail2.allow_list)
|
|
92
|
+
valid? && domain_is_in?(address.domain, ValidEmail2.allow_list)
|
|
90
93
|
end
|
|
91
94
|
|
|
92
95
|
def deny_listed?
|
|
@@ -109,8 +112,16 @@ module ValidEmail2
|
|
|
109
112
|
|
|
110
113
|
private
|
|
111
114
|
|
|
115
|
+
# An RFC 1035 label: 1-63 octets, and it may not begin or end with a hyphen.
|
|
116
|
+
def valid_domain_label?(label)
|
|
117
|
+
!label.empty? &&
|
|
118
|
+
label.length <= MAX_LABEL_LENGTH &&
|
|
119
|
+
!label.start_with?(HYPHEN_DELIMITER) &&
|
|
120
|
+
!label.end_with?(HYPHEN_DELIMITER)
|
|
121
|
+
end
|
|
122
|
+
|
|
112
123
|
def disposable_mx_server?
|
|
113
|
-
mx_server_is_in?(ValidEmail2.
|
|
124
|
+
mx_server_is_in?(ValidEmail2.disposable_domains)
|
|
114
125
|
end
|
|
115
126
|
|
|
116
127
|
# TODO: (PS) keep this for backward compatibility with the test setup described in the reamde
|
|
@@ -125,7 +136,7 @@ module ValidEmail2
|
|
|
125
136
|
return true if domain_list.include?(address_domain)
|
|
126
137
|
|
|
127
138
|
tokens = address_domain.split('.')
|
|
128
|
-
|
|
139
|
+
next false if tokens.length < 3
|
|
129
140
|
|
|
130
141
|
# check only 6 elements deep
|
|
131
142
|
2.upto(6).each do |depth|
|
data/lib/valid_email2/dns.rb
CHANGED
|
@@ -1,22 +1,28 @@
|
|
|
1
1
|
require "resolv"
|
|
2
|
+
require "logger" # Fix concurrent-ruby removing logger dependency which Rails itself does not have
|
|
3
|
+
require "active_support"
|
|
4
|
+
require "active_support/cache"
|
|
2
5
|
|
|
3
6
|
module ValidEmail2
|
|
4
7
|
class Dns
|
|
5
|
-
|
|
6
|
-
CACHE = {}
|
|
8
|
+
DEFAULT_CACHE_SIZE_BYTES = 1024 * 1024
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
class << self
|
|
11
|
+
attr_reader :cache, :cache_size
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
def cache_size=(bytes)
|
|
14
|
+
@cache_size = bytes
|
|
15
|
+
# Cached records are never mutated, so they don't need MemoryStore's defensive copies.
|
|
16
|
+
@cache = ActiveSupport::Cache::MemoryStore.new(size: bytes, coder: nil)
|
|
17
|
+
end
|
|
15
18
|
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
def clear_cache
|
|
20
|
+
cache.clear
|
|
21
|
+
end
|
|
18
22
|
end
|
|
19
23
|
|
|
24
|
+
self.cache_size = DEFAULT_CACHE_SIZE_BYTES
|
|
25
|
+
|
|
20
26
|
def initialize(dns_timeout = 5, dns_nameserver = nil)
|
|
21
27
|
@dns_timeout = dns_timeout
|
|
22
28
|
@dns_nameserver = dns_nameserver
|
|
@@ -32,22 +38,10 @@ module ValidEmail2
|
|
|
32
38
|
|
|
33
39
|
private
|
|
34
40
|
|
|
35
|
-
def prune_cache
|
|
36
|
-
self.class.prune_cache
|
|
37
|
-
end
|
|
38
|
-
|
|
39
41
|
def fetch(domain, type)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
cache_key = [domain, type]
|
|
44
|
-
cache_entry = CACHE[cache_key]
|
|
45
|
-
|
|
46
|
-
if cache_entry && Time.now - cache_entry.cached_at < cache_entry.ttl
|
|
47
|
-
return cache_entry.records
|
|
48
|
-
else
|
|
49
|
-
CACHE.delete(cache_key)
|
|
50
|
-
end
|
|
42
|
+
cache_key = "#{domain.downcase}/#{type}"
|
|
43
|
+
cached_records = self.class.cache.read(cache_key)
|
|
44
|
+
return cached_records if cached_records
|
|
51
45
|
|
|
52
46
|
records = Resolv::DNS.open(resolv_config) do |dns|
|
|
53
47
|
dns.timeouts = @dns_timeout
|
|
@@ -56,12 +50,18 @@ module ValidEmail2
|
|
|
56
50
|
|
|
57
51
|
if records.any?
|
|
58
52
|
ttl = records.map(&:ttl).min
|
|
59
|
-
|
|
53
|
+
cache_records(cache_key, records, ttl)
|
|
60
54
|
end
|
|
61
55
|
|
|
62
56
|
records
|
|
63
57
|
end
|
|
64
58
|
|
|
59
|
+
def cache_records(cache_key, records, ttl)
|
|
60
|
+
self.class.cache.write(cache_key, records, expires_in: ttl)
|
|
61
|
+
rescue TypeError
|
|
62
|
+
# MemoryStore sizes entries with Marshal, which can't dump test doubles
|
|
63
|
+
end
|
|
64
|
+
|
|
65
65
|
def resolv_config
|
|
66
66
|
config = Resolv::DNS::Config.default_config_hash
|
|
67
67
|
# REM: resolv gem 0.6.1 on linux can return frozen hash
|
|
@@ -6,7 +6,16 @@ require "active_model/validations"
|
|
|
6
6
|
module ValidEmail2
|
|
7
7
|
class EmailValidator < ActiveModel::EachValidator
|
|
8
8
|
def default_options
|
|
9
|
-
{
|
|
9
|
+
{
|
|
10
|
+
disposable: false,
|
|
11
|
+
mx: false,
|
|
12
|
+
strict_mx: false,
|
|
13
|
+
disallow_subaddressing: false,
|
|
14
|
+
multiple: false,
|
|
15
|
+
dns_timeout: 5,
|
|
16
|
+
dns_nameserver: nil,
|
|
17
|
+
specific_error_messages: false
|
|
18
|
+
}
|
|
10
19
|
end
|
|
11
20
|
|
|
12
21
|
def validate_each(record, attribute, value)
|
|
@@ -19,39 +28,39 @@ module ValidEmail2
|
|
|
19
28
|
error(record, attribute) && return unless addresses.all?(&:valid?)
|
|
20
29
|
|
|
21
30
|
if options[:disallow_dotted]
|
|
22
|
-
error(record, attribute) && return if addresses.any?(&:dotted?)
|
|
31
|
+
error(record, attribute, :disallow_dotted) && return if addresses.any?(&:dotted?)
|
|
23
32
|
end
|
|
24
33
|
|
|
25
34
|
if options[:disallow_subaddressing]
|
|
26
|
-
error(record, attribute) && return if addresses.any?(&:subaddressed?)
|
|
35
|
+
error(record, attribute, :disallow_subaddressing) && return if addresses.any?(&:subaddressed?)
|
|
27
36
|
end
|
|
28
37
|
|
|
29
38
|
if options[:disposable]
|
|
30
|
-
error(record, attribute) && return if addresses.any?(&:disposable?)
|
|
39
|
+
error(record, attribute, :disposable) && return if addresses.any?(&:disposable?)
|
|
31
40
|
end
|
|
32
41
|
|
|
33
42
|
if options[:disposable_domain]
|
|
34
|
-
error(record, attribute) && return if addresses.any?(&:disposable_domain?)
|
|
43
|
+
error(record, attribute, :disposable_domain) && return if addresses.any?(&:disposable_domain?)
|
|
35
44
|
end
|
|
36
45
|
|
|
37
46
|
if options[:disposable_with_allow_list]
|
|
38
|
-
error(record, attribute) && return if addresses.any? { |address| address.
|
|
47
|
+
error(record, attribute, :disposable_with_allow_list) && return if addresses.any? { |address| !address.allow_listed? && address.disposable? }
|
|
39
48
|
end
|
|
40
49
|
|
|
41
50
|
if options[:disposable_domain_with_allow_list]
|
|
42
|
-
error(record, attribute) && return if addresses.any? { |address| address.disposable_domain? && !address.allow_listed? }
|
|
51
|
+
error(record, attribute, :disposable_domain_with_allow_list) && return if addresses.any? { |address| address.disposable_domain? && !address.allow_listed? }
|
|
43
52
|
end
|
|
44
53
|
|
|
45
54
|
if options[:deny_list]
|
|
46
|
-
error(record, attribute) && return if addresses.any?(&:deny_listed?)
|
|
55
|
+
error(record, attribute, :deny_list) && return if addresses.any?(&:deny_listed?)
|
|
47
56
|
end
|
|
48
57
|
|
|
49
58
|
if options[:mx]
|
|
50
|
-
error(record, attribute) && return unless addresses.all?(&:valid_mx?)
|
|
59
|
+
error(record, attribute, :mx) && return unless addresses.all?(&:valid_mx?)
|
|
51
60
|
end
|
|
52
61
|
|
|
53
62
|
if options[:strict_mx]
|
|
54
|
-
error(record, attribute) && return unless addresses.all?(&:valid_strict_mx?)
|
|
63
|
+
error(record, attribute, :strict_mx) && return unless addresses.all?(&:valid_strict_mx?)
|
|
55
64
|
end
|
|
56
65
|
end
|
|
57
66
|
|
|
@@ -67,10 +76,11 @@ module ValidEmail2
|
|
|
67
76
|
email_list.reject(&:empty?)
|
|
68
77
|
end
|
|
69
78
|
|
|
70
|
-
def error(record, attribute)
|
|
79
|
+
def error(record, attribute, error_key = :invalid)
|
|
71
80
|
message = options[:message].respond_to?(:call) ? options[:message].call : options[:message]
|
|
81
|
+
message ||= options[:specific_error_messages] ? error_key : :invalid
|
|
72
82
|
|
|
73
|
-
record.errors.add(attribute, message
|
|
83
|
+
record.errors.add(attribute, message)
|
|
74
84
|
end
|
|
75
85
|
end
|
|
76
86
|
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ValidEmail2
|
|
4
|
+
# A list of domains read from a sorted file, one domain per line, and kept as a single String
|
|
5
|
+
# searched by bisection. A Set of the same domains takes several times the memory: each domain
|
|
6
|
+
# becomes a String object of its own, plus its hash table entry.
|
|
7
|
+
class SortedDomainList
|
|
8
|
+
NEWLINE = "\n".b.freeze
|
|
9
|
+
|
|
10
|
+
def initialize(path)
|
|
11
|
+
@list = File.binread(path).freeze
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def include?(domain)
|
|
15
|
+
domain = domain.to_s.b
|
|
16
|
+
low = 0
|
|
17
|
+
high = @list.bytesize
|
|
18
|
+
|
|
19
|
+
while low < high
|
|
20
|
+
middle = (low + high) / 2
|
|
21
|
+
start = (@list.rindex(NEWLINE, middle - 1) || -1) + 1
|
|
22
|
+
stop = @list.index(NEWLINE, start) || @list.bytesize
|
|
23
|
+
line = @list.byteslice(start, stop - start)
|
|
24
|
+
return true if line == domain
|
|
25
|
+
|
|
26
|
+
if line < domain
|
|
27
|
+
low = stop + 1
|
|
28
|
+
else
|
|
29
|
+
high = start
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
false
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
data/lib/valid_email2/version.rb
CHANGED
data/lib/valid_email2.rb
CHANGED
|
@@ -1,17 +1,44 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "valid_email2/email_validator"
|
|
4
|
+
require "valid_email2/sorted_domain_list"
|
|
4
5
|
|
|
5
6
|
module ValidEmail2
|
|
6
7
|
DENY_LIST_FILE = "config/deny_listed_email_domains.yml"
|
|
7
8
|
ALLOW_LIST_FILE = "config/allow_listed_email_domains.yml"
|
|
8
9
|
DISPOSABLE_FILE = File.expand_path('../config/disposable_email_domains.txt', __dir__)
|
|
10
|
+
DISPOSABLE_DOMAINS_LOOKUPS = %i[set sorted_list].freeze
|
|
9
11
|
|
|
10
12
|
class << self
|
|
13
|
+
# How disposable domains are looked up:
|
|
14
|
+
# - :set (default): the Set of disposable_emails, about 35 MB, with the fastest lookups;
|
|
15
|
+
# - :sorted_list: a SortedDomainList, about 3 MB, with lookups of a few microseconds.
|
|
16
|
+
def disposable_domains_lookup
|
|
17
|
+
@disposable_domains_lookup || :set
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def disposable_domains_lookup=(lookup)
|
|
21
|
+
unless DISPOSABLE_DOMAINS_LOOKUPS.include?(lookup)
|
|
22
|
+
raise ArgumentError, "disposable_domains_lookup must be one of #{DISPOSABLE_DOMAINS_LOOKUPS}, " \
|
|
23
|
+
"got #{lookup}"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
@disposable_domains_lookup = lookup
|
|
27
|
+
end
|
|
28
|
+
|
|
11
29
|
def disposable_emails
|
|
12
30
|
@disposable_emails ||= load_file(DISPOSABLE_FILE)
|
|
13
31
|
end
|
|
14
32
|
|
|
33
|
+
# The disposable domains lookups use, as chosen by disposable_domains_lookup.
|
|
34
|
+
def disposable_domains
|
|
35
|
+
if disposable_domains_lookup == :sorted_list
|
|
36
|
+
@sorted_disposable_domains ||= SortedDomainList.new(DISPOSABLE_FILE)
|
|
37
|
+
else
|
|
38
|
+
disposable_emails
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
15
42
|
def deny_list
|
|
16
43
|
@deny_list ||= load_if_exists(DENY_LIST_FILE) || Set.new
|
|
17
44
|
end
|
data/pull_mailchecker_emails.rb
CHANGED
|
@@ -16,6 +16,7 @@ allow_listed_emails = %w[
|
|
|
16
16
|
duck.com mozmail.com dralias.com 8alias.com 8shield.net
|
|
17
17
|
mailinblack.com anonaddy.com anonaddy.me addy.io privaterelay.appleid.com appleid.com
|
|
18
18
|
net.ua kommespaeter.de alpenjodel.de my.id web.id directbox.com embarqmail.com
|
|
19
|
+
unlimit.com reallyfast.info dreamhost.com ecmail.com google.com
|
|
19
20
|
]
|
|
20
21
|
|
|
21
22
|
existing_emails = File.open("config/disposable_email_domains.txt") { |f| f.read.split("\n") }
|
|
@@ -40,5 +41,6 @@ result_emails = result_emails.map do |line|
|
|
|
40
41
|
ts.last(2).join(".")
|
|
41
42
|
end
|
|
42
43
|
end.uniq.sort
|
|
44
|
+
result_emails -= allow_listed_emails
|
|
43
45
|
|
|
44
46
|
File.open("config/disposable_email_domains.txt", "w") { |f| f.puts(result_emails) }
|
data/spec/address_spec.rb
CHANGED
|
@@ -38,6 +38,11 @@ describe ValidEmail2::Address do
|
|
|
38
38
|
expect(address.valid?).to be false
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
+
it "is invalid if email contains a pipe" do
|
|
42
|
+
address = described_class.new("foo@gmai|.com")
|
|
43
|
+
expect(address.valid?).to be false
|
|
44
|
+
end
|
|
45
|
+
|
|
41
46
|
it "is invalid if it contains Japanese characters" do
|
|
42
47
|
address = described_class.new("あいうえお@example.com")
|
|
43
48
|
expect(address.valid?).to be false
|
|
@@ -83,6 +88,11 @@ describe ValidEmail2::Address do
|
|
|
83
88
|
address = described_class.new("foo@example.com")
|
|
84
89
|
expect(address.disposable_domain?).to eq false
|
|
85
90
|
end
|
|
91
|
+
|
|
92
|
+
it "is false if the email cannot be parsed" do
|
|
93
|
+
address = described_class.new('"><test>')
|
|
94
|
+
expect(address.disposable_domain?).to eq false
|
|
95
|
+
end
|
|
86
96
|
end
|
|
87
97
|
|
|
88
98
|
context "when the disposable domain has subdomains" do
|
|
@@ -111,7 +121,21 @@ describe ValidEmail2::Address do
|
|
|
111
121
|
let(:email_instance) { described_class.new(email_address, dns_instance) }
|
|
112
122
|
let(:ttl) { 1_000 }
|
|
113
123
|
let(:mock_resolv_dns) { instance_double(Resolv::DNS) }
|
|
114
|
-
let(:mock_mx_records) { [
|
|
124
|
+
let(:mock_mx_records) { [mx_record("mx.ymail.com")] }
|
|
125
|
+
|
|
126
|
+
def with_ttl(record)
|
|
127
|
+
record.instance_variable_set(:@ttl, ttl)
|
|
128
|
+
record
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def mx_record(exchange)
|
|
132
|
+
with_ttl(Resolv::DNS::Resource::IN::MX.new(10, Resolv::DNS::Name.create(exchange)))
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def travel(seconds)
|
|
136
|
+
now = Time.now
|
|
137
|
+
allow(Time).to receive(:now).and_return(now + seconds)
|
|
138
|
+
end
|
|
115
139
|
|
|
116
140
|
before do
|
|
117
141
|
allow(email_instance).to receive(:null_mx?).and_return(false)
|
|
@@ -158,12 +182,32 @@ describe ValidEmail2::Address do
|
|
|
158
182
|
|
|
159
183
|
expect(disposable_email_instance.send(:disposable_mx_server?)).to be_truthy
|
|
160
184
|
end
|
|
185
|
+
|
|
186
|
+
context "when the domain has several MX servers" do
|
|
187
|
+
let(:ordinary_mx_record) { double("MX", exchange: "ordinary.example", preference: 10, ttl:) }
|
|
188
|
+
let(:disposable_mx_record) { double("MX", exchange: "emailfake.com", preference: 20, ttl:) }
|
|
189
|
+
|
|
190
|
+
def address_with_mx_records(domain, mx_records)
|
|
191
|
+
allow(mock_resolv_dns).to receive(:getresources)
|
|
192
|
+
.with(domain, Resolv::DNS::Resource::IN::MX)
|
|
193
|
+
.and_return(mx_records)
|
|
194
|
+
|
|
195
|
+
described_class.new("example@#{domain}", dns_instance)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
it "is disposable when an ordinary MX host with one dot is listed before a disposable one" do
|
|
199
|
+
address = address_with_mx_records("ordinary-first.example", [ordinary_mx_record, disposable_mx_record])
|
|
200
|
+
expect(address.disposable?).to be_truthy
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
it "is disposable when a disposable MX host is listed before an ordinary one with one dot" do
|
|
204
|
+
address = address_with_mx_records("disposable-first.example", [disposable_mx_record, ordinary_mx_record])
|
|
205
|
+
expect(address.disposable?).to be_truthy
|
|
206
|
+
end
|
|
207
|
+
end
|
|
161
208
|
end
|
|
162
209
|
|
|
163
210
|
describe "#valid_strict_mx?" do
|
|
164
|
-
let(:cached_at) { Time.now }
|
|
165
|
-
let(:mock_cache_data) { { [email_instance.address.domain, Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_mx_records, cached_at, ttl) } }
|
|
166
|
-
|
|
167
211
|
before do
|
|
168
212
|
allow(mock_resolv_dns).to receive(:getresources)
|
|
169
213
|
.with(email_instance.address.domain, Resolv::DNS::Resource::IN::MX)
|
|
@@ -198,101 +242,31 @@ describe ValidEmail2::Address do
|
|
|
198
242
|
|
|
199
243
|
describe "ttl" do
|
|
200
244
|
before do
|
|
201
|
-
|
|
202
|
-
allow(ValidEmail2::Dns).to receive(:new).and_return(dns_instance)
|
|
203
|
-
allow(dns_instance).to receive(:fetch).with(email_instance.address.domain, Resolv::DNS::Resource::IN::MX).and_call_original
|
|
245
|
+
email_instance.valid_strict_mx?
|
|
204
246
|
end
|
|
205
247
|
|
|
206
248
|
context "when the time since last lookup is less than the cached ttl entry" do
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
it "does not call the MX servers lookup" do
|
|
210
|
-
email_instance.valid_strict_mx?
|
|
211
|
-
|
|
212
|
-
expect(Resolv::DNS).not_to have_received(:open)
|
|
213
|
-
end
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
context "when the time since last lookup is greater than the cached ttl entry" do
|
|
217
|
-
let(:cached_at) { Time.now - ttl }
|
|
218
|
-
|
|
219
|
-
it "calls the MX servers lookup" do
|
|
249
|
+
it "does not call the MX servers lookup again" do
|
|
250
|
+
travel(ttl - 1)
|
|
220
251
|
email_instance.valid_strict_mx?
|
|
221
252
|
|
|
222
253
|
expect(Resolv::DNS).to have_received(:open).once
|
|
223
254
|
end
|
|
224
255
|
end
|
|
225
|
-
end
|
|
226
|
-
|
|
227
|
-
describe "cache size" do
|
|
228
|
-
before do
|
|
229
|
-
stub_const("ValidEmail2::Dns::CACHE", mock_cache_data)
|
|
230
|
-
allow(ValidEmail2::Dns).to receive(:new).and_return(dns_instance)
|
|
231
|
-
allow(dns_instance).to receive(:fetch).with(email_instance.address.domain, Resolv::DNS::Resource::IN::MX).and_call_original
|
|
232
|
-
end
|
|
233
|
-
|
|
234
|
-
context "when the cache size is less than or equal to the max cache size" do
|
|
235
|
-
before do
|
|
236
|
-
stub_const("ValidEmail2::Dns::MAX_CACHE_SIZE", 1)
|
|
237
|
-
end
|
|
238
|
-
|
|
239
|
-
it "does not prune the cache" do
|
|
240
|
-
expect(dns_instance).not_to receive(:prune_cache)
|
|
241
|
-
|
|
242
|
-
email_instance.valid_strict_mx?
|
|
243
|
-
end
|
|
244
|
-
|
|
245
|
-
it "does not call the MX servers lookup" do
|
|
246
|
-
email_instance.valid_strict_mx?
|
|
247
|
-
|
|
248
|
-
expect(Resolv::DNS).not_to have_received(:open)
|
|
249
|
-
end
|
|
250
|
-
|
|
251
|
-
context "and there are older cached entries" do
|
|
252
|
-
let(:mock_cache_data) { { ["another_domain.com", Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_mx_records, cached_at - 100, ttl) } }
|
|
253
|
-
|
|
254
|
-
it "does not prune those entries" do
|
|
255
|
-
email_instance.valid_strict_mx?
|
|
256
|
-
|
|
257
|
-
expect(ValidEmail2::Dns::CACHE.keys).to match_array([[email_instance.address.domain, Resolv::DNS::Resource::IN::MX], ["another_domain.com", Resolv::DNS::Resource::IN::MX]])
|
|
258
|
-
end
|
|
259
|
-
end
|
|
260
|
-
end
|
|
261
|
-
|
|
262
|
-
context "when the cache size is greater than the max cache size" do
|
|
263
|
-
before do
|
|
264
|
-
stub_const("ValidEmail2::Dns::MAX_CACHE_SIZE", 0)
|
|
265
|
-
end
|
|
266
|
-
|
|
267
|
-
it "prunes the cache" do
|
|
268
|
-
expect(dns_instance).to receive(:prune_cache).once
|
|
269
256
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
it "calls the the MX servers lookup" do
|
|
257
|
+
context "when the time since last lookup is greater than the cached ttl entry" do
|
|
258
|
+
it "calls the MX servers lookup again" do
|
|
259
|
+
travel(ttl)
|
|
274
260
|
email_instance.valid_strict_mx?
|
|
275
261
|
|
|
276
|
-
expect(Resolv::DNS).to have_received(:open).
|
|
277
|
-
end
|
|
278
|
-
|
|
279
|
-
context "and there are older cached entries" do
|
|
280
|
-
let(:mock_cache_data) { { ["another_domain.com", Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_mx_records, cached_at - 100, ttl) } }
|
|
281
|
-
|
|
282
|
-
it "prunes those entries" do
|
|
283
|
-
email_instance.valid_strict_mx?
|
|
284
|
-
|
|
285
|
-
expect(ValidEmail2::Dns::CACHE.keys).to match_array([[email_instance.address.domain, Resolv::DNS::Resource::IN::MX]])
|
|
286
|
-
end
|
|
262
|
+
expect(Resolv::DNS).to have_received(:open).twice
|
|
287
263
|
end
|
|
288
264
|
end
|
|
289
265
|
end
|
|
290
266
|
end
|
|
291
267
|
|
|
292
268
|
describe "#valid_mx?" do
|
|
293
|
-
let(:
|
|
294
|
-
let(:mock_cache_data) { { [email_instance.address.domain, Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_a_records, cached_at, ttl) } }
|
|
295
|
-
let(:mock_a_records) { [double("A", address: "192.168.1.1", ttl:)] }
|
|
269
|
+
let(:mock_a_records) { [with_ttl(Resolv::DNS::Resource::IN::A.new("192.168.1.1"))] }
|
|
296
270
|
|
|
297
271
|
before do
|
|
298
272
|
allow(email_instance).to receive(:mx_servers).and_return(mock_mx_records)
|
|
@@ -329,92 +303,24 @@ describe ValidEmail2::Address do
|
|
|
329
303
|
|
|
330
304
|
describe "ttl" do
|
|
331
305
|
before do
|
|
332
|
-
|
|
333
|
-
allow(ValidEmail2::Dns).to receive(:new).and_return(dns_instance)
|
|
334
|
-
allow(dns_instance).to receive(:fetch).with(email_instance.address.domain, Resolv::DNS::Resource::IN::MX).and_call_original
|
|
306
|
+
email_instance.valid_mx?
|
|
335
307
|
end
|
|
336
308
|
|
|
337
309
|
context "when the time since last lookup is less than the cached ttl entry" do
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
it "does not call the MX or A servers lookup" do
|
|
341
|
-
email_instance.valid_mx?
|
|
342
|
-
|
|
343
|
-
expect(Resolv::DNS).not_to have_received(:open)
|
|
344
|
-
end
|
|
345
|
-
end
|
|
346
|
-
|
|
347
|
-
context "when the time since last lookup is greater than the cached ttl entry" do
|
|
348
|
-
let(:cached_at) { Time.now - ttl }
|
|
349
|
-
|
|
350
|
-
it "calls the MX or A servers lookup " do
|
|
310
|
+
it "does not call the MX or A servers lookup again" do
|
|
311
|
+
travel(ttl - 1)
|
|
351
312
|
email_instance.valid_mx?
|
|
352
313
|
|
|
353
314
|
expect(Resolv::DNS).to have_received(:open).once
|
|
354
315
|
end
|
|
355
316
|
end
|
|
356
|
-
end
|
|
357
|
-
|
|
358
|
-
describe "cache size" do
|
|
359
|
-
before do
|
|
360
|
-
stub_const("ValidEmail2::Dns::CACHE", mock_cache_data)
|
|
361
|
-
allow(ValidEmail2::Dns).to receive(:new).and_return(dns_instance)
|
|
362
|
-
allow(dns_instance).to receive(:fetch).with(email_instance.address.domain, Resolv::DNS::Resource::IN::MX).and_call_original
|
|
363
|
-
end
|
|
364
|
-
|
|
365
|
-
context "when the cache size is less than or equal to the max cache size" do
|
|
366
|
-
before do
|
|
367
|
-
stub_const("ValidEmail2::Dns::MAX_CACHE_SIZE", 1)
|
|
368
|
-
end
|
|
369
|
-
|
|
370
|
-
it "does not prune the cache" do
|
|
371
|
-
expect(email_instance).not_to receive(:prune_cache)
|
|
372
|
-
|
|
373
|
-
email_instance.valid_mx?
|
|
374
|
-
end
|
|
375
|
-
|
|
376
|
-
it "does not call the MX or A servers lookup" do
|
|
377
|
-
email_instance.valid_mx?
|
|
378
|
-
|
|
379
|
-
expect(Resolv::DNS).not_to have_received(:open)
|
|
380
|
-
end
|
|
381
|
-
|
|
382
|
-
context "and there are older cached entries" do
|
|
383
|
-
let(:mock_cache_data) { { ["another_domain.com", Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_a_records, cached_at - 100, ttl) } }
|
|
384
|
-
|
|
385
|
-
it "does not prune those entries" do
|
|
386
|
-
email_instance.valid_mx?
|
|
387
|
-
|
|
388
|
-
expect(ValidEmail2::Dns::CACHE.keys).to match_array([[email_instance.address.domain, Resolv::DNS::Resource::IN::MX], ["another_domain.com", Resolv::DNS::Resource::IN::MX]])
|
|
389
|
-
end
|
|
390
|
-
end
|
|
391
|
-
end
|
|
392
317
|
|
|
393
|
-
context "when the
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
end
|
|
397
|
-
|
|
398
|
-
it "prunes the cache" do
|
|
399
|
-
expect(dns_instance).to receive(:prune_cache).once
|
|
400
|
-
|
|
401
|
-
email_instance.valid_mx?
|
|
402
|
-
end
|
|
403
|
-
|
|
404
|
-
it "calls the MX or A servers lookup" do
|
|
318
|
+
context "when the time since last lookup is greater than the cached ttl entry" do
|
|
319
|
+
it "calls the MX or A servers lookup again" do
|
|
320
|
+
travel(ttl)
|
|
405
321
|
email_instance.valid_mx?
|
|
406
322
|
|
|
407
|
-
expect(Resolv::DNS).to have_received(:open).
|
|
408
|
-
end
|
|
409
|
-
|
|
410
|
-
context "and there are older cached entries" do
|
|
411
|
-
let(:mock_cache_data) { { ["another_domain.com", Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_a_records, cached_at - 100, ttl) } }
|
|
412
|
-
|
|
413
|
-
it "prunes those entries" do
|
|
414
|
-
email_instance.valid_mx?
|
|
415
|
-
|
|
416
|
-
expect(ValidEmail2::Dns::CACHE.keys).to match_array([[email_instance.address.domain, Resolv::DNS::Resource::IN::MX]])
|
|
417
|
-
end
|
|
323
|
+
expect(Resolv::DNS).to have_received(:open).twice
|
|
418
324
|
end
|
|
419
325
|
end
|
|
420
326
|
end
|
data/spec/benchmark_spec.rb
CHANGED
|
@@ -16,6 +16,16 @@ describe "Performance testing" do
|
|
|
16
16
|
expect { address.disposable_domain? }.to perform_under(0.0001).sec.sample(10).times
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
+
it "disposable_domain? has acceptable lookup performance with the sorted_list lookup" do
|
|
20
|
+
address = ValidEmail2::Address.new("test@example.com")
|
|
21
|
+
ValidEmail2.disposable_domains_lookup = :sorted_list
|
|
22
|
+
ValidEmail2.disposable_domains
|
|
23
|
+
|
|
24
|
+
expect { address.disposable_domain? }.to perform_under(0.0001).sec.sample(10).times
|
|
25
|
+
ensure
|
|
26
|
+
ValidEmail2.disposable_domains_lookup = :set
|
|
27
|
+
end
|
|
28
|
+
|
|
19
29
|
it "disposable_mx_server? has acceptable lookup performance" do
|
|
20
30
|
address = ValidEmail2::Address.new("test@gmail.com")
|
|
21
31
|
|
|
@@ -24,6 +34,6 @@ describe "Performance testing" do
|
|
|
24
34
|
expect(ValidEmail2.disposable_emails.count).to be > 30000
|
|
25
35
|
|
|
26
36
|
# check lookup timing
|
|
27
|
-
expect { address.send(:disposable_mx_server?) }.to perform_under(0.
|
|
37
|
+
expect { address.send(:disposable_mx_server?) }.to perform_under(0.00025).sec.sample(10).times
|
|
28
38
|
end
|
|
29
39
|
end
|