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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
$VERBOSE = true
require 'minitest/autorun'
require 'fileutils'
$:.unshift(File.expand_path("..", "lib"))
require 'netrc'
require "rbconfig"
class TestNetrc < Minitest::Test
def setup
Dir.glob('data/*.netrc').each{|f| File.chmod(0600, f)}
File.chmod(0644, "data/permissive.netrc")
end
def teardown
Dir.glob('data/*.netrc').each{|f| File.chmod(0644, f)}
end
def test_parse_empty
pre, items = Netrc.parse(Netrc.lex([]))
assert_equal("", pre)
assert_equal([], items)
end
def test_parse_file
pre, items = Netrc.parse(Netrc.lex(IO.readlines("data/sample.netrc")))
assert_equal("# this is my netrc\n", pre)
exp = [["machine ",
"m",
"\n login ",
"l",
" # this is my username\n password ",
"p",
"\n"]]
assert_equal(exp, items)
end
def test_login_file
pre, items = Netrc.parse(Netrc.lex(IO.readlines("data/login.netrc")))
assert_equal("# this is my login netrc\n", pre)
exp = [["machine ",
"m",
"\n login ",
"l",
" # this is my username\n"]]
assert_equal(exp, items)
end
def test_password_file
pre, items = Netrc.parse(Netrc.lex(IO.readlines("data/password.netrc")))
assert_equal("# this is my password netrc\n", pre)
exp = [["machine ",
"m",
"\n password ",
"p",
" # this is my password\n"]]
assert_equal(exp, items)
end
def test_missing_file
n = Netrc.read("data/nonexistent.netrc")
assert_equal(0, n.length)
end
def test_permission_error
original_windows = Netrc::WINDOWS
Netrc.send(:remove_const, :WINDOWS)
Netrc.const_set(:WINDOWS, false)
Netrc.read("data/permissive.netrc")
assert false, "Should raise an error if permissions are wrong on a non-windows system."
rescue Netrc::Error
assert true, ""
ensure
Netrc.send(:remove_const, :WINDOWS)
Netrc.const_set(:WINDOWS, original_windows)
end
def test_allow_permissive_netrc_file_option
Netrc.configure do |config|
config[:allow_permissive_netrc_file] = true
end
original_windows = Netrc::WINDOWS
Netrc.send(:remove_const, :WINDOWS)
Netrc.const_set(:WINDOWS, false)
Netrc.read("data/permissive.netrc")
assert true, ""
rescue Netrc::Error
assert false, "Should not raise an error if allow_permissive_netrc_file option is set to true"
ensure
Netrc.send(:remove_const, :WINDOWS)
Netrc.const_set(:WINDOWS, original_windows)
Netrc.configure do |config|
config[:allow_permissive_netrc_file] = false
end
end
def test_permission_error_windows
original_windows = Netrc::WINDOWS
Netrc.send(:remove_const, :WINDOWS)
Netrc.const_set(:WINDOWS, true)
Netrc.read("data/permissive.netrc")
rescue Netrc::Error
assert false, "Should not raise an error if permissions are wrong on a non-windows system."
ensure
Netrc.send(:remove_const, :WINDOWS)
Netrc.const_set(:WINDOWS, original_windows)
end
def test_round_trip
n = Netrc.read("data/sample.netrc")
assert_equal(IO.read("data/sample.netrc"), n.unparse)
end
def test_set
n = Netrc.read("data/sample.netrc")
n["m"] = "a", "b"
exp = "# this is my netrc\n"+
"machine m\n"+
" login a # this is my username\n"+
" password b\n"
assert_equal(exp, n.unparse)
end
def test_set_get
n = Netrc.read("data/sample.netrc")
n["m"] = "a", "b"
assert_equal(["a", "b"], n["m"].to_a)
end
def test_add
n = Netrc.read("data/sample.netrc")
n.new_item_prefix = "# added\n"
n["x"] = "a", "b"
exp = "# this is my netrc\n"+
"machine m\n"+
" login l # this is my username\n"+
" password p\n"+
"# added\n"+
"machine x\n"+
" login a\n"+
" password b\n"
assert_equal(exp, n.unparse)
end
def test_add_newlineless
n = Netrc.read("data/newlineless.netrc")
n.new_item_prefix = "# added\n"
n["x"] = "a", "b"
exp = "# this is my netrc\n"+
"machine m\n"+
" login l # this is my username\n"+
" password p\n"+
"# added\n"+
"machine x\n"+
" login a\n"+
" password b\n"
assert_equal(exp, n.unparse)
end
def test_add_get
n = Netrc.read("data/sample.netrc")
n.new_item_prefix = "# added\n"
n["x"] = "a", "b"
assert_equal(["a", "b"], n["x"].to_a)
end
def test_get_missing
n = Netrc.read("data/sample.netrc")
assert_equal(nil, n["x"])
end
def test_save
n = Netrc.read("data/sample.netrc")
n.save
assert_equal(File.read("data/sample.netrc"), n.unparse)
end
def test_save_create
FileUtils.rm_f("/tmp/created.netrc")
n = Netrc.read("/tmp/created.netrc")
n.save
unless Netrc::WINDOWS
assert_equal(0600, File.stat("/tmp/created.netrc").mode & 0777)
end
end
def disabled_test_encrypted_roundtrip
if `gpg --list-keys 2> /dev/null` != ""
FileUtils.rm_f("/tmp/test.netrc.gpg")
n = Netrc.read("/tmp/test.netrc.gpg")
n["m"] = "a", "b"
n.save
assert_equal(0600, File.stat("/tmp/test.netrc.gpg").mode & 0777)
netrc = Netrc.read("/tmp/test.netrc.gpg")["m"]
assert_equal("a", netrc.login)
assert_equal("b", netrc.password)
end
end
def test_missing_environment
nil_home = nil
ENV["HOME"], nil_home = nil_home, ENV["HOME"]
assert_equal File.join(Netrc.home_path, '.netrc'), Netrc.default_path
ensure
ENV["HOME"], nil_home = nil_home, ENV["HOME"]
end
def test_netrc_environment_variable
ENV["NETRC"] = File.join(Dir.pwd, 'data')
assert_equal File.join(Dir.pwd, 'data', '.netrc'), Netrc.default_path
ensure
ENV.delete("NETRC")
end
def test_read_entry
entry = Netrc.read("data/sample.netrc")['m']
assert_equal 'l', entry.login
assert_equal 'p', entry.password
# hash-style
assert_equal 'l', entry[:login]
assert_equal 'p', entry[:password]
end
def test_write_entry
n = Netrc.read("data/sample.netrc")
entry = n['m']
entry.login = 'new_login'
entry.password = 'new_password'
n['m'] = entry
assert_equal(['new_login', 'new_password'], n['m'].to_a)
end
def test_entry_splat
e = Netrc::Entry.new("user", "pass")
user, pass = *e
assert_equal("user", user)
assert_equal("pass", pass)
end
def test_entry_implicit_splat
e = Netrc::Entry.new("user", "pass")
user, pass = e
assert_equal("user", user)
assert_equal("pass", pass)
end
def test_with_default
netrc = Netrc.read('data/sample_with_default.netrc')
assert_equal(['l', 'p'], netrc['m'].to_a)
assert_equal(['default_login', 'default_password'], netrc['unknown'].to_a)
end
def test_multi_without_default
netrc = Netrc.read('data/sample_multi.netrc')
assert_equal(['lm', 'pm'], netrc['m'].to_a)
assert_equal(['ln', 'pn'], netrc['n'].to_a)
assert_equal([], netrc['other'].to_a)
end
def test_multi_with_default
netrc = Netrc.read('data/sample_multi_with_default.netrc')
assert_equal(['lm', 'pm'], netrc['m'].to_a)
assert_equal(['ln', 'pn'], netrc['n'].to_a)
assert_equal(['ld', 'pd'], netrc['other'].to_a)
end
def test_default_only
netrc = Netrc.read('data/default_only.netrc')
assert_equal(['ld', 'pd'], netrc['m'].to_a)
assert_equal(['ld', 'pd'], netrc['other'].to_a)
end
end
|