public_suffix 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rubocop.yml +36 -0
  4. data/.rubocop_defaults.yml +179 -0
  5. data/.ruby-gemset +1 -0
  6. data/.travis.yml +31 -0
  7. data/.yardopts +1 -0
  8. data/2.0-Upgrade.md +52 -0
  9. data/CHANGELOG.md +353 -0
  10. data/Gemfile +12 -0
  11. data/LICENSE.txt +22 -0
  12. data/README.md +202 -0
  13. data/Rakefile +51 -0
  14. data/bin/console +15 -0
  15. data/data/list.txt +12966 -0
  16. data/lib/public_suffix.rb +179 -0
  17. data/lib/public_suffix/domain.rb +235 -0
  18. data/lib/public_suffix/errors.rb +41 -0
  19. data/lib/public_suffix/list.rb +247 -0
  20. data/lib/public_suffix/rule.rb +350 -0
  21. data/lib/public_suffix/version.rb +13 -0
  22. data/public_suffix.gemspec +25 -0
  23. data/test/.empty +2 -0
  24. data/test/acceptance_test.rb +129 -0
  25. data/test/benchmarks/bm_find.rb +66 -0
  26. data/test/benchmarks/bm_find_all.rb +102 -0
  27. data/test/benchmarks/bm_names.rb +91 -0
  28. data/test/benchmarks/bm_select.rb +26 -0
  29. data/test/benchmarks/bm_select_incremental.rb +25 -0
  30. data/test/benchmarks/bm_valid.rb +101 -0
  31. data/test/profilers/domain_profiler.rb +12 -0
  32. data/test/profilers/find_profiler.rb +12 -0
  33. data/test/profilers/find_profiler_jp.rb +12 -0
  34. data/test/profilers/initialization_profiler.rb +11 -0
  35. data/test/profilers/list_profsize.rb +11 -0
  36. data/test/profilers/object_binsize.rb +57 -0
  37. data/test/psl_test.rb +52 -0
  38. data/test/test_helper.rb +18 -0
  39. data/test/tests.txt +98 -0
  40. data/test/unit/domain_test.rb +106 -0
  41. data/test/unit/errors_test.rb +25 -0
  42. data/test/unit/list_test.rb +241 -0
  43. data/test/unit/public_suffix_test.rb +188 -0
  44. data/test/unit/rule_test.rb +222 -0
  45. metadata +151 -0
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class ErrorsTest < Minitest::Test
6
+
7
+ # Inherits from StandardError
8
+ def test_error_inheritance
9
+ assert_kind_of StandardError,
10
+ PublicSuffix::Error.new
11
+ end
12
+
13
+ # Inherits from PublicSuffix::Error
14
+ def test_domain_invalid_inheritance
15
+ assert_kind_of PublicSuffix::Error,
16
+ PublicSuffix::DomainInvalid.new
17
+ end
18
+
19
+ # Inherits from PublicSuffix::DomainInvalid
20
+ def test_domain_not_allowed_inheritance
21
+ assert_kind_of PublicSuffix::DomainInvalid,
22
+ PublicSuffix::DomainNotAllowed.new
23
+ end
24
+
25
+ end
@@ -0,0 +1,241 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class PublicSuffix::ListTest < Minitest::Test
6
+
7
+ def setup
8
+ @list = PublicSuffix::List.new
9
+ end
10
+
11
+ def teardown
12
+ PublicSuffix::List.default = nil
13
+ end
14
+
15
+
16
+ def test_initialize
17
+ assert_instance_of PublicSuffix::List, @list
18
+ assert_equal 0, @list.size
19
+ end
20
+
21
+
22
+ def test_equality_with_self
23
+ list = PublicSuffix::List.new
24
+ assert_equal list, list
25
+ end
26
+
27
+ def test_equality_with_internals
28
+ rule = PublicSuffix::Rule.factory("com")
29
+ assert_equal PublicSuffix::List.new.add(rule), PublicSuffix::List.new.add(rule)
30
+ end
31
+
32
+ def test_each_without_block
33
+ list = PublicSuffix::List.parse(<<LIST)
34
+ alpha
35
+ beta
36
+ LIST
37
+
38
+ assert_kind_of Enumerator, list.each
39
+ assert_equal 2, list.each.count
40
+ assert_equal PublicSuffix::Rule.factory("alpha"), list.each.first
41
+ end
42
+
43
+ def test_each_with_block
44
+ list = PublicSuffix::List.parse(<<LIST)
45
+ alpha
46
+ beta
47
+ LIST
48
+
49
+ entries = []
50
+ list.each { |r| entries << r }
51
+
52
+ assert_equal 2, entries.count
53
+ assert_equal PublicSuffix::Rule.factory("alpha"), entries.first
54
+ end
55
+
56
+
57
+ def test_add
58
+ assert_equal @list, @list.add(PublicSuffix::Rule.factory("foo"))
59
+ assert_equal @list, @list << PublicSuffix::Rule.factory("bar")
60
+ assert_equal 2, @list.size
61
+ end
62
+
63
+ def test_add_should_recreate_index
64
+ @list = PublicSuffix::List.parse("com")
65
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
66
+ assert_equal @list.default_rule, @list.find("google.net")
67
+
68
+ @list << PublicSuffix::Rule.factory("net")
69
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
70
+ assert_equal PublicSuffix::Rule.factory("net"), @list.find("google.net")
71
+ end
72
+
73
+ def test_empty?
74
+ assert @list.empty?
75
+ @list.add(PublicSuffix::Rule.factory(""))
76
+ assert !@list.empty?
77
+ end
78
+
79
+ def test_size
80
+ assert_equal 0, @list.size
81
+ assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
82
+ assert_equal 1, @list.size
83
+ end
84
+
85
+ def test_clear
86
+ assert_equal 0, @list.size
87
+ assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
88
+ assert_equal 1, @list.size
89
+ assert_equal @list, @list.clear
90
+ assert_equal 0, @list.size
91
+ end
92
+
93
+
94
+ def test_find
95
+ list = PublicSuffix::List.parse(<<LIST)
96
+ // This Source Code Form is subject to the terms of the Mozilla Public
97
+ // License, v. 2.0. If a copy of the MPL was not distributed with this
98
+ // file, You can obtain one at https://mozilla.org/MPL/2.0/.
99
+
100
+ // ===BEGIN ICANN DOMAINS===
101
+
102
+ // com
103
+ com
104
+
105
+ // uk
106
+ *.uk
107
+ *.sch.uk
108
+ !bl.uk
109
+ !british-library.uk
110
+
111
+ // io
112
+ io
113
+
114
+ // ===END ICANN DOMAINS===
115
+ // ===BEGIN PRIVATE DOMAINS===
116
+
117
+ // Google, Inc.
118
+ blogspot.com
119
+
120
+ // ===END PRIVATE DOMAINS===
121
+ LIST
122
+
123
+ # match IANA
124
+ assert_equal PublicSuffix::Rule.factory("com"), list.find("example.com")
125
+ assert_equal PublicSuffix::Rule.factory("com"), list.find("foo.example.com")
126
+
127
+ # match wildcard
128
+ assert_equal PublicSuffix::Rule.factory("*.uk"), list.find("example.uk")
129
+ assert_equal PublicSuffix::Rule.factory("*.uk"), list.find("example.co.uk")
130
+ assert_equal PublicSuffix::Rule.factory("*.uk"), list.find("foo.example.co.uk")
131
+
132
+ # match exception
133
+ assert_equal PublicSuffix::Rule.factory("!british-library.uk"), list.find("british-library.uk")
134
+ assert_equal PublicSuffix::Rule.factory("!british-library.uk"), list.find("foo.british-library.uk")
135
+
136
+ # match default rule
137
+ assert_equal PublicSuffix::Rule.factory("*"), list.find("test")
138
+ assert_equal PublicSuffix::Rule.factory("*"), list.find("example.test")
139
+ assert_equal PublicSuffix::Rule.factory("*"), list.find("foo.example.test")
140
+
141
+ # match private
142
+ assert_equal PublicSuffix::Rule.factory("blogspot.com", private: true), list.find("blogspot.com")
143
+ assert_equal PublicSuffix::Rule.factory("blogspot.com", private: true), list.find("foo.blogspot.com")
144
+ end
145
+
146
+
147
+ def test_select
148
+ assert_equal 2, list.send(:select, "british-library.uk").size
149
+ end
150
+
151
+ def test_select_name_blank
152
+ assert_equal [], list.send(:select, nil)
153
+ assert_equal [], list.send(:select, "")
154
+ assert_equal [], list.send(:select, " ")
155
+ end
156
+
157
+ def test_select_ignore_private
158
+ list = PublicSuffix::List.new
159
+ list.add r1 = PublicSuffix::Rule.factory("io")
160
+ list.add r2 = PublicSuffix::Rule.factory("example.io", private: true)
161
+
162
+ assert_equal list.send(:select, "foo.io"), [r1]
163
+ assert_equal list.send(:select, "example.io"), [r1, r2]
164
+ assert_equal list.send(:select, "foo.example.io"), [r1, r2]
165
+
166
+ assert_equal list.send(:select, "foo.io", ignore_private: false), [r1]
167
+ assert_equal list.send(:select, "example.io", ignore_private: false), [r1, r2]
168
+ assert_equal list.send(:select, "foo.example.io", ignore_private: false), [r1, r2]
169
+
170
+ assert_equal list.send(:select, "foo.io", ignore_private: true), [r1]
171
+ assert_equal list.send(:select, "example.io", ignore_private: true), [r1]
172
+ assert_equal list.send(:select, "foo.example.io", ignore_private: true), [r1]
173
+ end
174
+
175
+
176
+ def test_self_default_getter
177
+ PublicSuffix::List.default = nil
178
+ assert_nil(PublicSuffix::List.class_eval { @default })
179
+ PublicSuffix::List.default
180
+ refute_nil(PublicSuffix::List.class_eval { @default })
181
+ end
182
+
183
+ def test_self_default_setter
184
+ PublicSuffix::List.default
185
+ refute_nil(PublicSuffix::List.class_eval { @default })
186
+ PublicSuffix::List.default = nil
187
+ assert_nil(PublicSuffix::List.class_eval { @default })
188
+ end
189
+
190
+ def test_self_parse
191
+ list = PublicSuffix::List.parse(<<LIST)
192
+ // This Source Code Form is subject to the terms of the Mozilla Public
193
+ // License, v. 2.0. If a copy of the MPL was not distributed with this
194
+ // file, You can obtain one at https://mozilla.org/MPL/2.0/.
195
+
196
+ // ===BEGIN ICANN DOMAINS===
197
+
198
+ // com
199
+ com
200
+
201
+ // uk
202
+ *.uk
203
+ !british-library.uk
204
+
205
+ // ===END ICANN DOMAINS===
206
+ // ===BEGIN PRIVATE DOMAINS===
207
+
208
+ // Google, Inc.
209
+ blogspot.com
210
+
211
+ // ===END PRIVATE DOMAINS===
212
+ LIST
213
+
214
+ assert_instance_of PublicSuffix::List, list
215
+ assert_equal 4, list.size
216
+
217
+ rules = %w( com *.uk !british-library.uk blogspot.com ).map { |name| PublicSuffix::Rule.factory(name) }
218
+ assert_equal rules, list.each.to_a
219
+
220
+ # private domains
221
+ assert_equal false, list.find("com").private
222
+ assert_equal true, list.find("blogspot.com").private
223
+ end
224
+
225
+
226
+ private
227
+
228
+ def list
229
+ @_list ||= PublicSuffix::List.parse(<<LIST)
230
+ // com : http://en.wikipedia.org/wiki/.com
231
+ com
232
+
233
+ // uk : http://en.wikipedia.org/wiki/.uk
234
+ *.uk
235
+ *.sch.uk
236
+ !bl.uk
237
+ !british-library.uk
238
+ LIST
239
+ end
240
+
241
+ end
@@ -0,0 +1,188 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class PublicSuffixTest < Minitest::Test
6
+
7
+ def test_private_domains_enabled_by_default
8
+ domain = PublicSuffix.parse("www.example.blogspot.com")
9
+ assert_equal "blogspot.com", domain.tld
10
+ end
11
+
12
+ def test_private_domains_disable
13
+ data = File.read(PublicSuffix::List::DEFAULT_LIST_PATH)
14
+ PublicSuffix::List.default = PublicSuffix::List.parse(data, private_domains: false)
15
+ domain = PublicSuffix.parse("www.example.blogspot.com")
16
+ assert_equal "com", domain.tld
17
+ ensure
18
+ PublicSuffix::List.default = nil
19
+ end
20
+
21
+
22
+ def test_self_parse_a_domain_with_tld_and_sld
23
+ domain = PublicSuffix.parse("example.com")
24
+ assert_instance_of PublicSuffix::Domain, domain
25
+ assert_equal "com", domain.tld
26
+ assert_equal "example", domain.sld
27
+ assert_nil domain.trd
28
+
29
+ domain = PublicSuffix.parse("example.co.uk")
30
+ assert_instance_of PublicSuffix::Domain, domain
31
+ assert_equal "co.uk", domain.tld
32
+ assert_equal "example", domain.sld
33
+ assert_nil domain.trd
34
+ end
35
+
36
+ def test_self_parse_a_domain_with_tld_and_sld_and_trd
37
+ domain = PublicSuffix.parse("alpha.example.com")
38
+ assert_instance_of PublicSuffix::Domain, domain
39
+ assert_equal "com", domain.tld
40
+ assert_equal "example", domain.sld
41
+ assert_equal "alpha", domain.trd
42
+
43
+ domain = PublicSuffix.parse("alpha.example.co.uk")
44
+ assert_instance_of PublicSuffix::Domain, domain
45
+ assert_equal "co.uk", domain.tld
46
+ assert_equal "example", domain.sld
47
+ assert_equal "alpha", domain.trd
48
+ end
49
+
50
+ def test_self_parse_a_domain_with_tld_and_sld_and_4rd
51
+ domain = PublicSuffix.parse("one.two.example.com")
52
+ assert_instance_of PublicSuffix::Domain, domain
53
+ assert_equal "com", domain.tld
54
+ assert_equal "example", domain.sld
55
+ assert_equal "one.two", domain.trd
56
+
57
+ domain = PublicSuffix.parse("one.two.example.co.uk")
58
+ assert_instance_of PublicSuffix::Domain, domain
59
+ assert_equal "co.uk", domain.tld
60
+ assert_equal "example", domain.sld
61
+ assert_equal "one.two", domain.trd
62
+ end
63
+
64
+ def test_self_parse_name_fqdn
65
+ domain = PublicSuffix.parse("www.example.com.")
66
+ assert_instance_of PublicSuffix::Domain, domain
67
+ assert_equal "com", domain.tld
68
+ assert_equal "example", domain.sld
69
+ assert_equal "www", domain.trd
70
+ end
71
+
72
+ def test_self_parse_with_custom_list
73
+ list = PublicSuffix::List.new
74
+ list << PublicSuffix::Rule.factory("test")
75
+
76
+ domain = PublicSuffix.parse("www.example.test", list: list)
77
+ assert_instance_of PublicSuffix::Domain, domain
78
+ assert_equal "test", domain.tld
79
+ assert_equal "example", domain.sld
80
+ assert_equal "www", domain.trd
81
+ end
82
+
83
+ def test_self_parse_with_notlisted_name
84
+ domain = PublicSuffix.parse("example.tldnotlisted")
85
+ assert_instance_of PublicSuffix::Domain, domain
86
+ assert_equal "tldnotlisted", domain.tld
87
+ assert_equal "example", domain.sld
88
+ assert_nil domain.trd
89
+ end
90
+
91
+ def test_self_parse_with_unallowed_domain
92
+ error = assert_raises(PublicSuffix::DomainNotAllowed) { PublicSuffix.parse("example.bd") }
93
+ assert_match(/example\.bd/, error.message)
94
+ end
95
+
96
+ def test_self_parse_with_uri
97
+ error = assert_raises(PublicSuffix::DomainInvalid) { PublicSuffix.parse("http://google.com") }
98
+ assert_match(%r{http://google\.com}, error.message)
99
+ end
100
+
101
+
102
+ def test_self_valid
103
+ assert PublicSuffix.valid?("google.com")
104
+ assert PublicSuffix.valid?("www.google.com")
105
+ assert PublicSuffix.valid?("google.co.uk")
106
+ assert PublicSuffix.valid?("www.google.co.uk")
107
+ end
108
+
109
+ def test_self_valid_with_notlisted_name
110
+ assert PublicSuffix.valid?("google.tldnotlisted")
111
+ assert PublicSuffix.valid?("www.google.tldnotlisted")
112
+ end
113
+
114
+ # def test_self_valid_with_fully_qualified_domain_name
115
+ # assert PublicSuffix.valid?("google.com.")
116
+ # assert PublicSuffix.valid?("google.co.uk.")
117
+ # assert !PublicSuffix.valid?("google.tldnotlisted.")
118
+ # end
119
+
120
+
121
+ def test_self_domain
122
+ assert_equal "google.com", PublicSuffix.domain("google.com")
123
+ assert_equal "google.com", PublicSuffix.domain("www.google.com")
124
+ assert_equal "google.co.uk", PublicSuffix.domain("google.co.uk")
125
+ assert_equal "google.co.uk", PublicSuffix.domain("www.google.co.uk")
126
+ end
127
+
128
+ def test_self_domain_with_notlisted_name
129
+ assert_equal "example.tldnotlisted", PublicSuffix.domain("example.tldnotlisted")
130
+ end
131
+
132
+ def test_self_domain_with_unallowed_name
133
+ assert_nil PublicSuffix.domain("example.bd")
134
+ end
135
+
136
+ def test_self_domain_with_blank_sld
137
+ assert_nil PublicSuffix.domain("com")
138
+ assert_nil PublicSuffix.domain(".com")
139
+ end
140
+
141
+
142
+ def test_self_normalize
143
+ [
144
+ ["com", "com"],
145
+ ["example.com", "example.com"],
146
+ ["www.example.com", "www.example.com"],
147
+
148
+ ["example.com.", "example.com"], # strip FQDN
149
+ [" example.com ", "example.com"], # strip spaces
150
+ ["Example.COM", "example.com"], # downcase
151
+ ].each do |input, output|
152
+ assert_equal output, PublicSuffix.normalize(input)
153
+ end
154
+ end
155
+
156
+ def test_normalize_blank
157
+ [
158
+ nil,
159
+ "",
160
+ " ",
161
+ ].each do |input|
162
+ error = PublicSuffix.normalize(input)
163
+ assert_instance_of PublicSuffix::DomainInvalid, error
164
+ assert_equal "Name is blank", error.message
165
+ end
166
+ end
167
+
168
+ def test_normalize_scheme
169
+ [
170
+ "https://google.com",
171
+ ].each do |input|
172
+ error = PublicSuffix.normalize(input)
173
+ assert_instance_of PublicSuffix::DomainInvalid, error
174
+ assert_match(/scheme/, error.message)
175
+ end
176
+ end
177
+
178
+ def test_normalize_leading_dot
179
+ [
180
+ ".google.com",
181
+ ].each do |input|
182
+ error = PublicSuffix.normalize(input)
183
+ assert_instance_of PublicSuffix::DomainInvalid, error
184
+ assert_match "Name starts with a dot", error.message
185
+ end
186
+ end
187
+
188
+ end