File: test_netrc.rb

package info (click to toggle)
ruby-net-netrc 0.2.2-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 84 kB
  • ctags: 16
  • sloc: ruby: 215; makefile: 2
file content (115 lines) | stat: -rw-r--r-- 3,011 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
$:.unshift File.join(File.dirname(__FILE__), "../lib")

require 'net/netrc'
require 'test/unit'

class TestNetrc < Test::Unit::TestCase

  if Net::Netrc::IS_WIN32
    SAMPLE_NETRC = File.join(File.dirname(__FILE__), '_netrc')
  else
    SAMPLE_NETRC = File.join(File.dirname(__FILE__), '.netrc')
  end

  def setup
    @path = SAMPLE_NETRC
    File.open(@path, 'w') do |f|
      f << <<EOT
machine example.com
  login example_login
  password example_password
  account example_account

machine twowords.com
  login "twowords login"
  password "twowords password"
  account "twowords account"

macdef foo
  this is a macro definition.
  it starts with the line following 'macdef',
  and ends with a null line (consecutive newline characters)

default
  login default_login
  password default_password

machine unreached.com
  login unreached_login
  password unreached_password

EOT
    end
    File.chmod(0600, @path)
    ENV['NETRC'] = @path
  end

  def teardown
    File.unlink @path
  end

  def test_path
    assert_not_nil(ENV['NETRC'])
    assert(File.exists?(ENV['NETRC']), '#{SAMPLE_NETRC} not found')
    assert_equal(Net::Netrc.rcname, @path)
  end

  unless Net::Netrc::IS_WIN32
    def test_security
      File.chmod(0666, @path)
      assert_raise(SecurityError) { Net::Netrc.rcopen }
      File.chmod(0600, @path)
      assert_nothing_raised { Net::Netrc.rcopen }
    end
  end

  def test_entries

    entry = Net::Netrc.locate('example.com')
    assert_not_nil(entry)
    assert_equal('example.com', entry.machine)
    assert_equal('example_login', entry.login)
    assert_equal('example_password', entry.password)
    assert_equal('example_account', entry.account)

    entry = Net::Netrc.locate('twowords.com')
    assert_not_nil(entry)
    assert_equal('twowords.com', entry.machine)
    assert_equal('twowords login', entry.login)
    assert_equal('twowords password', entry.password)
    assert_equal('twowords account', entry.account)

    default = Net::Netrc.locate('')
    assert_not_nil(default)
    assert_nil(default.machine)
    assert_equal('default_login', default.login)
    assert_equal('default_password', default.password)
    assert_nil(default.account)

    entry = Net::Netrc.locate('unreached.com')
    assert_not_nil(entry)
    assert_equal(default.machine, entry.machine)
    assert_equal(default.login, entry.login)
    assert_equal(default.password, entry.password)
    assert_equal(default.account, entry.account)

  end

  def test_rcname
    assert_equal SAMPLE_NETRC, Net::Netrc.rcname
    ENV.delete('NETRC')
    home = Etc.getpwuid.dir rescue nil
    if home && File.exists?(File.join(home, '.netrc'))
      assert_equal File.join(home, '.netrc'), Net::Netrc.rcname
    else
      ENV['HOME'] = File.dirname(__FILE__)
      assert_equal SAMPLE_NETRC, Net::Netrc.rcname
      if Net::Netrc::IS_WIN32
        ENV.delete('HOME')
        ENV['USERPROFILE'] = File.dirname(__FILE__)
        assert_equal SAMPLE_NETRC, Net::Netrc.rcname
      end
    end
  end

end