File: 06_hosts.t

package info (click to toggle)
libpoe-component-client-dns-perl 1%3A1.01-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 120 kB
  • ctags: 26
  • sloc: perl: 832; makefile: 16
file content (118 lines) | stat: -rw-r--r-- 2,867 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/perl
# $Id: 06_hosts.t 67 2008-10-15 03:18:31Z rcaputo $
# vim: filetype=perl

# Test the hosts file stuff.

use warnings;
use strict;
sub POE::Kernel::ASSERT_DEFAULT () { 1 }
use POE qw(Component::Client::DNS);
use Test::More tests => 3;

require Net::DNS;
my $can_resolve = Net::DNS::Resolver->new->search("poe.perl.org");

use constant HOSTS_FILE => "./test-hosts";

my $resolver = POE::Component::Client::DNS->spawn(
  Alias     => 'named',
  Timeout   => 15,
  HostsFile => HOSTS_FILE,
);

POE::Session->create(
  inline_states  => {
    _start                 => \&start_tests,
    _stop                  => sub { }, # avoid assert problems
    response_no_hosts      => \&response_no_hosts,
    response_hosts_match   => \&response_hosts_match,
    response_hosts_nomatch => \&response_hosts_nomatch,
  }
);

POE::Kernel->run();
exit;

sub start_tests {
  # 1. Test without a hosts file.
  unlink HOSTS_FILE;

  $resolver->resolve(
    event   => "response_no_hosts",
    host    => "poe.perl.org",
    context => "whatever",
  );
}

sub response_no_hosts {
  my $response = $_[ARG0];
  my $address = a_data($response);
  SKIP: {
    skip "Can't resolve with Net::DNS, network probably not available", 1
      unless($can_resolve);
    ok(
      ($address eq "67.207.145.70") || ($address eq "208.97.190.64"),
      "lookup with no hosts file ($address)"
    );
  }

  # 2. Test with a hosts file that contains a host match.
  unlink HOSTS_FILE;  # Changes inode!
  open(HF, ">" . HOSTS_FILE) or die "couldn't write hosts file: $!";
  print HF "123.456.789.012 poe.perl.org\n";
  close HF;

  $resolver->resolve(
    event   => "response_hosts_match",
    host    => "poe.perl.org",
    context => "whatever",
  );
}

sub response_hosts_match {
  my $response = $_[ARG0];
  my $address = a_data($response);
  ok(
    $address eq "123.456.789.012",
    "lookup when hosts file matches ($address)"
  );

  # 3. Test against a hosts file without a host match.
  unlink HOSTS_FILE;  # Changes inode!
  open(HF, ">" . HOSTS_FILE) or die "couldn't write hosts file: $!";
  print HF "123.456.789.012 narf.barf.warf\n";
  close HF;

  $resolver->resolve(
    event   => "response_hosts_nomatch",
    host    => "poe.perl.org",
    context => "whatever",
  );
}

sub response_hosts_nomatch {
  my $response = $_[ARG0];
  my $address = a_data($response);
  SKIP: {
    skip "Can't resolve with Net::DNS, network probably not available", 1
      unless($can_resolve);
    ok(
      ($address eq "67.207.145.70") || ($address eq "208.97.190.64"),
      "lookup with hosts file but no match ($address)"
    );
  }

  unlink HOSTS_FILE;
}

### Not a POE event handler.

sub a_data {
  my $response = shift;
  return "" unless defined $response->{response};

  return (
    grep { ref() eq "Net::DNS::RR::A" } $response->{response}->answer()
  )[0]->rdatastr();
}