hosts 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ # A comment
2
+ 127.0.0.1 myhost localhost.localdomain localhost
3
+ 127.0.1.1 myhost.mydomain.tld myhost
4
+ # Another comment
5
+ # Two lines long
6
+
7
+ # The following lines are desirable for IPv6 capable hosts
8
+ ::1 localhost ip6-localhost ip6-loopback
9
+ fe00::0 ip6-localnet
10
+ ff00::0 ip6-mcastprefix
11
+ ff02::1 ip6-allnodes
12
+ ff02::2 ip6-allrouters
13
+ ff02::3 ip6-allhosts
14
+
15
+ 127.0.0.1 example.net
16
+
17
+ 2620:0:2d0:200::10 example.com
18
+ 192.0.32.10 example.com
19
+
20
+ # Last comment
21
+
@@ -0,0 +1,14 @@
1
+ # A comment
2
+ 102.54.94.97 rhino.acme.com # source server
3
+ 38.25.63.10 x.acme.com # x client host
4
+ # Another comment
5
+ # Two lines long
6
+ 127.0.0.1 localhost otherhost evenotherhost
7
+ ::1 localhost
8
+
9
+ 127.0.0.1 example.net
10
+
11
+ 2620:0:2d0:200::10 example.com
12
+ 192.0.32.10 example.com
13
+
14
+ # Last comment
@@ -0,0 +1,384 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2012
4
+
5
+ This file is part of Hosts.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require 'spec_helper'
21
+
22
+ describe 'Hosts library' do
23
+ it "should be able to interpret a Windows hosts file" do
24
+ @fixture_file = fixtures_dir + 'windows_hosts'
25
+ @file = Aef::Hosts::File.new(@fixture_file)
26
+
27
+ @file.read
28
+
29
+ @file.elements.each_with_index do |element, index|
30
+ line_number = index + 1
31
+ case line_number
32
+ when 1
33
+ element.should be_a(Aef::Hosts::Comment)
34
+ element.comment.should == " A comment"
35
+ element.to_s.should ==
36
+ "# A comment\n"
37
+ element.to_s(:force_generation => true).should ==
38
+ "# A comment\n"
39
+ when 2
40
+ element.should be_a(Aef::Hosts::Entry)
41
+ element.address.should == "102.54.94.97"
42
+ element.name.should == "rhino.acme.com"
43
+ element.aliases.should be_empty
44
+ element.comment.should == " source server"
45
+ element.to_s.should ==
46
+ " 102.54.94.97 rhino.acme.com # source server\n"
47
+ element.to_s(:force_generation => true).should ==
48
+ "102.54.94.97 rhino.acme.com # source server\n"
49
+ when 3
50
+ element.should be_a(Aef::Hosts::Entry)
51
+ element.address.should == "38.25.63.10"
52
+ element.name.should == "x.acme.com"
53
+ element.aliases.should be_empty
54
+ element.comment.should == " x client host"
55
+ element.to_s.should ==
56
+ " 38.25.63.10 x.acme.com # x client host\n"
57
+ element.to_s(:force_generation => true).should ==
58
+ "38.25.63.10 x.acme.com # x client host\n"
59
+ when 4
60
+ element.should be_a(Aef::Hosts::Comment)
61
+ element.comment.should == " Another comment"
62
+ element.to_s.should ==
63
+ " # Another comment\n"
64
+ element.to_s(:force_generation => true).should ==
65
+ "# Another comment\n"
66
+ when 5
67
+ element.should be_a(Aef::Hosts::Comment)
68
+ element.comment.should == " Two lines long"
69
+ element.to_s.should ==
70
+ " # Two lines long\n"
71
+ element.to_s(:force_generation => true).should ==
72
+ "# Two lines long\n"
73
+ when 6
74
+ element.should be_a(Aef::Hosts::Entry)
75
+ element.address.should == "127.0.0.1"
76
+ element.name.should == "localhost"
77
+ element.aliases.to_set.should == ['otherhost', 'evenotherhost'].to_set
78
+ element.comment.should be_nil
79
+ element.to_s.should ==
80
+ "\t127.0.0.1 localhost otherhost evenotherhost\n"
81
+ element.to_s(:force_generation => true).should ==
82
+ "127.0.0.1 localhost otherhost evenotherhost\n"
83
+ when 7
84
+ element.should be_a(Aef::Hosts::Entry)
85
+ element.address.should == "::1"
86
+ element.name.should == "localhost"
87
+ element.aliases.should be_empty
88
+ element.comment.should be_nil
89
+ element.to_s.should ==
90
+ "\t::1 localhost\n"
91
+ element.to_s(:force_generation => true).should ==
92
+ "::1 localhost\n"
93
+ when 8
94
+ element.should be_a(Aef::Hosts::EmptyElement)
95
+ element.to_s.should ==
96
+ " \n"
97
+ element.to_s(:force_generation => true).should ==
98
+ "\n"
99
+ when 9
100
+ element.should be_a(Aef::Hosts::Entry)
101
+ element.address.should == "127.0.0.1"
102
+ element.name.should == "example.net"
103
+ element.aliases.should be_empty
104
+ element.comment.should be_nil
105
+ element.to_s.should ==
106
+ "127.0.0.1\t\t\t\texample.net\n"
107
+ element.to_s(:force_generation => true).should ==
108
+ "127.0.0.1 example.net\n"
109
+ when 10
110
+ element.should be_a(Aef::Hosts::EmptyElement)
111
+ element.to_s.should ==
112
+ "\t\n"
113
+ element.to_s(:force_generation => true).should ==
114
+ "\n"
115
+ when 11
116
+ element.should be_a(Aef::Hosts::Entry)
117
+ element.address.should == "2620:0:2d0:200::10"
118
+ element.name.should == "example.com"
119
+ element.aliases.should be_empty
120
+ element.comment.should be_nil
121
+ element.to_s.should ==
122
+ "2620:0:2d0:200::10 example.com\n"
123
+ element.to_s(:force_generation => true).should ==
124
+ "2620:0:2d0:200::10 example.com\n"
125
+ when 12
126
+ element.should be_a(Aef::Hosts::Entry)
127
+ element.address.should == "192.0.32.10"
128
+ element.name.should == "example.com"
129
+ element.aliases.should be_empty
130
+ element.comment.should be_nil
131
+ element.to_s.should ==
132
+ "192.0.32.10 example.com\n"
133
+ element.to_s(:force_generation => true).should ==
134
+ "192.0.32.10 example.com\n"
135
+ when 13
136
+ element.should be_a(Aef::Hosts::EmptyElement)
137
+ element.to_s.should ==
138
+ "\n"
139
+ element.to_s(:force_generation => true).should ==
140
+ "\n"
141
+ when 14
142
+ element.should be_a(Aef::Hosts::Comment)
143
+ element.comment.should == " Last comment"
144
+ element.to_s.should ==
145
+ "\t# Last comment\n"
146
+ element.to_s(:force_generation => true).should ==
147
+ "# Last comment\n"
148
+ end
149
+ end
150
+ end
151
+
152
+ it "should be able to interpret a Linux hosts file" do
153
+ @fixture_file = fixtures_dir + 'linux_hosts'
154
+ @file = Aef::Hosts::File.new(@fixture_file)
155
+
156
+ @file.read
157
+
158
+ @file.elements.each_with_index do |element, index|
159
+ line_number = index + 1
160
+ case line_number
161
+ when 1
162
+ element.should be_a(Aef::Hosts::Comment)
163
+ element.comment.should == " A comment"
164
+ element.to_s.should ==
165
+ "# A comment\n"
166
+ element.to_s(:force_generation => true).should ==
167
+ "# A comment\n"
168
+ when 2
169
+ element.should be_a(Aef::Hosts::Entry)
170
+ element.address.should == "127.0.0.1"
171
+ element.name.should == "myhost"
172
+ element.aliases.to_set.should == ['localhost.localdomain', 'localhost'].to_set
173
+ element.comment.should be_nil
174
+ element.to_s.should ==
175
+ "127.0.0.1\tmyhost\tlocalhost.localdomain\tlocalhost\n"
176
+ element.to_s(:force_generation => true).should ==
177
+ "127.0.0.1 myhost localhost.localdomain localhost\n"
178
+ when 3
179
+ element.should be_a(Aef::Hosts::Entry)
180
+ element.address.should == "127.0.1.1"
181
+ element.name.should == "myhost.mydomain.tld"
182
+ element.aliases.to_set.should == ['myhost'].to_set
183
+ element.comment.should be_nil
184
+ element.to_s.should ==
185
+ "127.0.1.1\tmyhost.mydomain.tld myhost\n"
186
+ element.to_s(:force_generation => true).should ==
187
+ "127.0.1.1 myhost.mydomain.tld myhost\n"
188
+ when 4
189
+ element.should be_a(Aef::Hosts::Comment)
190
+ element.comment.should == " Another comment"
191
+ element.to_s.should ==
192
+ " # Another comment\n"
193
+ element.to_s(:force_generation => true).should ==
194
+ "# Another comment\n"
195
+ when 5
196
+ element.should be_a(Aef::Hosts::Comment)
197
+ element.comment.should == " Two lines long"
198
+ element.to_s.should ==
199
+ " # Two lines long\n"
200
+ element.to_s(:force_generation => true).should ==
201
+ "# Two lines long\n"
202
+ when 6
203
+ element.should be_a(Aef::Hosts::EmptyElement)
204
+ element.to_s.should ==
205
+ "\n"
206
+ element.to_s(:force_generation => true).should ==
207
+ "\n"
208
+ when 7
209
+ element.should be_a(Aef::Hosts::Comment)
210
+ element.comment.should == " The following lines are desirable for IPv6 capable hosts"
211
+ element.to_s.should ==
212
+ "# The following lines are desirable for IPv6 capable hosts\n"
213
+ element.to_s(:force_generation => true).should ==
214
+ "# The following lines are desirable for IPv6 capable hosts\n"
215
+ when 8
216
+ element.should be_a(Aef::Hosts::Entry)
217
+ element.address.should == "::1"
218
+ element.name.should == "localhost"
219
+ element.aliases.to_set.should == ['ip6-localhost', 'ip6-loopback'].to_set
220
+ element.comment.should be_nil
221
+ element.to_s.should ==
222
+ "::1 localhost ip6-localhost ip6-loopback\n"
223
+ element.to_s(:force_generation => true).should ==
224
+ "::1 localhost ip6-localhost ip6-loopback\n"
225
+ when 9
226
+ element.should be_a(Aef::Hosts::Entry)
227
+ element.address.should == "fe00::0"
228
+ element.name.should == "ip6-localnet"
229
+ element.aliases.should be_empty
230
+ element.comment.should be_nil
231
+ element.to_s.should ==
232
+ "fe00::0 ip6-localnet\n"
233
+ element.to_s(:force_generation => true).should ==
234
+ "fe00::0 ip6-localnet\n"
235
+ when 10
236
+ element.should be_a(Aef::Hosts::Entry)
237
+ element.address.should == "ff00::0"
238
+ element.name.should == "ip6-mcastprefix"
239
+ element.aliases.should be_empty
240
+ element.comment.should be_nil
241
+ element.to_s.should ==
242
+ "ff00::0 ip6-mcastprefix\n"
243
+ element.to_s(:force_generation => true).should ==
244
+ "ff00::0 ip6-mcastprefix\n"
245
+ when 11
246
+ element.should be_a(Aef::Hosts::Entry)
247
+ element.address.should == "ff02::1"
248
+ element.name.should == "ip6-allnodes"
249
+ element.aliases.should be_empty
250
+ element.comment.should be_nil
251
+ element.to_s.should ==
252
+ "ff02::1 ip6-allnodes\n"
253
+ element.to_s(:force_generation => true).should ==
254
+ "ff02::1 ip6-allnodes\n"
255
+ when 12
256
+ element.should be_a(Aef::Hosts::Entry)
257
+ element.address.should == "ff02::2"
258
+ element.name.should == "ip6-allrouters"
259
+ element.aliases.should be_empty
260
+ element.comment.should be_nil
261
+ element.to_s.should ==
262
+ "ff02::2 ip6-allrouters\n"
263
+ element.to_s(:force_generation => true).should ==
264
+ "ff02::2 ip6-allrouters\n"
265
+ when 13
266
+ element.should be_a(Aef::Hosts::Entry)
267
+ element.address.should == "ff02::3"
268
+ element.name.should == "ip6-allhosts"
269
+ element.aliases.should be_empty
270
+ element.comment.should be_nil
271
+ element.to_s.should ==
272
+ "ff02::3 ip6-allhosts\n"
273
+ element.to_s(:force_generation => true).should ==
274
+ "ff02::3 ip6-allhosts\n"
275
+ when 14
276
+ element.should be_a(Aef::Hosts::EmptyElement)
277
+ element.to_s.should ==
278
+ " \n"
279
+ element.to_s(:force_generation => true).should ==
280
+ "\n"
281
+ when 15
282
+ element.should be_a(Aef::Hosts::Entry)
283
+ element.address.should == "127.0.0.1"
284
+ element.name.should == "example.net"
285
+ element.aliases.should be_empty
286
+ element.comment.should be_nil
287
+ element.to_s.should ==
288
+ "127.0.0.1\t\t\t\texample.net\n"
289
+ element.to_s(:force_generation => true).should ==
290
+ "127.0.0.1 example.net\n"
291
+ when 16
292
+ element.should be_a(Aef::Hosts::EmptyElement)
293
+ element.to_s.should ==
294
+ "\t\n"
295
+ element.to_s(:force_generation => true).should ==
296
+ "\n"
297
+ when 17
298
+ element.should be_a(Aef::Hosts::Entry)
299
+ element.address.should == "2620:0:2d0:200::10"
300
+ element.name.should == "example.com"
301
+ element.aliases.should be_empty
302
+ element.comment.should be_nil
303
+ element.to_s.should ==
304
+ "2620:0:2d0:200::10 example.com\n"
305
+ element.to_s(:force_generation => true).should ==
306
+ "2620:0:2d0:200::10 example.com\n"
307
+ when 18
308
+ element.should be_a(Aef::Hosts::Entry)
309
+ element.address.should == "192.0.32.10"
310
+ element.name.should == "example.com"
311
+ element.aliases.should be_empty
312
+ element.comment.should be_nil
313
+ element.to_s.should ==
314
+ "192.0.32.10 example.com\n"
315
+ element.to_s(:force_generation => true).should ==
316
+ "192.0.32.10 example.com\n"
317
+ when 19
318
+ element.should be_a(Aef::Hosts::EmptyElement)
319
+ element.to_s.should ==
320
+ "\n"
321
+ element.to_s(:force_generation => true).should ==
322
+ "\n"
323
+ when 20
324
+ element.should be_a(Aef::Hosts::Comment)
325
+ element.comment.should == " Last comment"
326
+ element.to_s.should ==
327
+ "\t# Last comment\n"
328
+ element.to_s(:force_generation => true).should ==
329
+ "# Last comment\n"
330
+ end
331
+ end
332
+ end
333
+
334
+ it "should be able to append a new entry" do
335
+ @fixture_file = fixtures_dir + 'linux_hosts'
336
+ @file = Aef::Hosts::File.new(@fixture_file)
337
+
338
+ @file.read
339
+
340
+ @file.elements << Aef::Hosts::Entry.new(
341
+ '192.168.23.5', 'the-new-host', :aliases => [
342
+ 'the-new-host.domain.tld',
343
+ 'service.domain.tld'
344
+ ]
345
+ )
346
+
347
+ lines = @file.to_s.lines.to_a
348
+ lines[21].should == "192.168.23.5 the-new-host the-new-host.domain.tld service.domain.tld\n"
349
+ end
350
+
351
+ it "should be able to insert a new entry" do
352
+ @fixture_file = fixtures_dir + 'linux_hosts'
353
+ @file = Aef::Hosts::File.new(@fixture_file)
354
+
355
+ @file.read
356
+
357
+ @file.elements.insert(9, Aef::Hosts::Entry.new(
358
+ '192.168.23.5', 'the-new-host', :aliases => [
359
+ 'the-new-host.domain.tld',
360
+ 'service.domain.tld'
361
+ ]
362
+ ))
363
+
364
+ lines = @file.to_s.lines.to_a
365
+ lines[9].should == "192.168.23.5 the-new-host the-new-host.domain.tld service.domain.tld\n"
366
+ end
367
+
368
+ it "should be able to prepend a new entry" do
369
+ @fixture_file = fixtures_dir + 'linux_hosts'
370
+ @file = Aef::Hosts::File.new(@fixture_file)
371
+
372
+ @file.read
373
+
374
+ @file.elements.unshift(Aef::Hosts::Entry.new(
375
+ '192.168.23.5', 'the-new-host', :aliases => [
376
+ 'the-new-host.domain.tld',
377
+ 'service.domain.tld'
378
+ ]
379
+ ))
380
+
381
+ lines = @file.to_s.lines.to_a
382
+ lines[0].should == "192.168.23.5 the-new-host the-new-host.domain.tld service.domain.tld\n"
383
+ end
384
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2012
4
+
5
+ This file is part of Hosts.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ unless defined?(Rubinius)
21
+ require 'simplecov'
22
+ SimpleCov.start do
23
+ add_filter "/spec/"
24
+ end
25
+ end
26
+
27
+ require 'set'
28
+ require 'pathname'
29
+ require 'tmpdir'
30
+ require 'rspec'
31
+ require 'pry'
32
+ require 'aef/hosts'
33
+
34
+ module Aef::Hosts::SpecHelper
35
+ def create_temp_dir
36
+ Pathname(Dir.mktmpdir('hosts_spec'))
37
+ end
38
+
39
+ def fixtures_dir
40
+ Pathname(__FILE__).dirname + 'fixtures'
41
+ end
42
+ end
43
+
44
+ RSpec.configure do |config|
45
+ config.include Aef::Hosts::SpecHelper
46
+ end
47
+