dbi-dbrc 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,82 @@
1
+ == 1.0.0 - 15-Jun-2005
2
+ * Ditches the use of 'etc'. Now requires the 'sys-admin' package as its
3
+ replacement (for all platforms).
4
+ * Moved project to RubyForge.
5
+ * Minor updates to tests, README and gemspec.
6
+ * Now hosted on RubyForge.
7
+
8
+ == 0.5.1 - 17-Mar-2005
9
+ * Removed the 'doc' directory completely, and moved the primary
10
+ documentation into the README file.
11
+ * Removed the INSTALL file. Moved the installation directions into the
12
+ README file.
13
+ * Moved the examples into a toplevel 'examples' directory.
14
+ * Made the README and CHANGES files rdoc friendly.
15
+ * Added a gemspec.
16
+
17
+ == 0.5.0 - 15-Oct-2004
18
+ * Added a YAML subclass. Use this if you want to store your information in
19
+ the .dbrc file in YAML format.
20
+ * On Win32 systems, the .dbrc file must now be "hidden". Also, it will
21
+ automatically decrypt/encrypt the file if it is encrypted. This also
22
+ means that the win32-file package is also required on Win32 systems.
23
+ * Massive refactoring of the XML subclass. It should work better now.
24
+ * Moved the YML and XML subclasses directly into the dbrc.rb file.
25
+ * For the aliases, you can now write as well as read.
26
+ * In lieu of the namespace fix for "timeout" in 1.8, I have renamed "time_out"
27
+ to simply "timeout". For backwards compatability, I have created an alias,
28
+ so you may still use the old method name. However, this means that you
29
+ should only use this package with 1.8.0 or later.
30
+ * Removed the dbrc.html file. You can generate this on your own if you wish
31
+ using rdtool.
32
+ * Test suite changes.
33
+
34
+ == 0.4.0 - 3-Sep-2004
35
+ * Removed redundant error handling for cases when the database and/or login
36
+ are not found.
37
+ * Added an XML subclass. Use this if you want to store your information in
38
+ the .dbrc file in XML format.
39
+
40
+ == 0.3.0 - 26-Oct-2003
41
+ * Win32 support added. Requires the win32-etc package.
42
+ * rd doc separated from source. Moved to 'doc' directory.
43
+ * Documentation updates and corrections.
44
+ * Minor test suite tweaks for Win32 systems.
45
+
46
+ == 0.2.1 - 28-Aug-2003
47
+ * Removed VERSION class method. Just use the constant.
48
+ * Bug fix with regards to split and Ruby 1.8. Thanks Michael Garriss.
49
+ * Changed 'changelog' to 'CHANGES'.
50
+ * Added a vanilla test script (test.rb)
51
+ * Minor code optimization (IO.foreach)
52
+ * Test unit cleanup
53
+ * Minor internal directory layout and doc changes
54
+
55
+ == 0.2.0 - 13-Jan-2003
56
+ * DBRC class now under the DBI module namespace
57
+ * Changed "timeout" to "time_out" to avoid confusion with the timeout
58
+ module.
59
+ * The 'time_out', 'max_reconn', and 'interval' methods now return Integers,
60
+ rather than Strings.
61
+ * Only the database, user and password fields are required now within the
62
+ .dbrc file. The driver, time_out and max_reconn entries can be left blank
63
+ without causing an error. Note that the 'dsn' method will return 'nil'
64
+ if the driver isn't set.
65
+ * Added a DBRCException class instead of just raising an error string.
66
+ * Changed 'db' method to 'database' (but 'db' is an alias)
67
+ * Changed 'max_reconn' method to 'maximum_reconnects' (but 'max_reconn'
68
+ is an alias')
69
+ * Added 'passwd' alias for 'password' method
70
+ * Added a VERSION class method
71
+ * Methods that should have been private are now private
72
+ * Internal directory layout change
73
+ * Tests added
74
+ * Install script improved
75
+ * Documentation additions, including plain text doc
76
+
77
+ == 0.1.1 - 26-Jul-2002
78
+ * Added 'dsn()' method
79
+ * Minor documentation additions
80
+
81
+ == 0.1.0 - 26-Jul-2002
82
+ * Initial release
data/README ADDED
@@ -0,0 +1,244 @@
1
+ == Description
2
+ This is a supplement to the dbi module, allowing you to avoid hard-coding
3
+ passwords in your programs that make database connections.
4
+
5
+ == Synopsis
6
+ require 'dbi/dbrc'
7
+ include DBI
8
+
9
+ dbrc = DBRC.new("mydb")
10
+
11
+ or
12
+
13
+ dbrc = DBRC.new("mydb","someUser")
14
+
15
+ puts dbrc.db
16
+ puts dbrc.user
17
+ puts dbrc.driver
18
+ puts dbrc.timeout
19
+ puts dbrc.max_reconn
20
+ puts dbrc.interval
21
+ puts dbrc.dsn
22
+
23
+ dbh = DBI.connect(dbrc.dsn, dbrc.user, dbrc.password)
24
+
25
+ == Requirements
26
+ * Ruby 1.8.0 or later
27
+ * sys-admin
28
+ * win32-file (Win32 only)
29
+
30
+ == Installation
31
+ === Manual Installation
32
+ * ruby test/ts_all.rb (optional)
33
+ * ruby install.rb
34
+ === Gem Installation
35
+ * ruby dbi-dbrc.gemspec
36
+ * gem install dbi-dbrc-<version>.gem
37
+
38
+ == Notes on the .dbrc file
39
+ This module relies on a file in your home directory called ".dbrc", and it
40
+ is meant to be analogous to the ".netrc" file used by programs such as
41
+ telnet. The .dbrc file has several conditions that must be met by the
42
+ module or it will fail:
43
+
44
+ * Permissions must be set to 600 (Unix only).
45
+ * Must be hidden (Win32 only).
46
+ * Must be owned by the current user.
47
+ * Must have database, user and password. Other fields are optional.
48
+ * Must be in the following space-separated format (in the 'plain' version):
49
+
50
+ database user password driver timeout maximum_reconnects interval
51
+
52
+ e.g. mydb dan mypass oracle 10 2 30
53
+
54
+ You may include comments in the .dbrc file by starting the line with a
55
+ "#" symbol
56
+
57
+ A failure in any of the rules mentioned above will result in a DBRCError
58
+ being raised. In addition, the file may also be encrypted on Win32 systems,
59
+ in which case the file will automatically be (temporarily) decrypted.
60
+
61
+ The format for XML (using the example above) is as follows:
62
+
63
+ <dbrc>
64
+ <database name="mydb">
65
+ <user>dan</user>
66
+ <password>mypass</password>
67
+ <driver>oracle</driver>
68
+ <interval>30</interval>
69
+ <timeout>10</timeout>
70
+ <maximum_reconnects>2</maximum_reconnects>
71
+ </database>
72
+ </dbrc>
73
+
74
+ The format for YAML is as follows:
75
+
76
+ - mydb:
77
+ user: dan
78
+ password: mypass
79
+ driver: oracle
80
+ interval: 30
81
+ timeout: 10
82
+ max_reconn: 2
83
+
84
+ == Constants
85
+ VERSION
86
+ The current version of this packages, returned as a String.
87
+
88
+ == Class Methods
89
+ DBRC.new(db, user=nil, dir=nil)
90
+ The constructor takes one to three arguments. The first argument is the
91
+ database name. This *must* be provided. If only the database name is
92
+ passed, the module will look for the first database entry in the .dbrc
93
+ file that matches.
94
+
95
+ The second argument, a user name, is optional. If it is passed, the
96
+ module will look for the first entry in the .dbrc file where both the
97
+ database *and* user name match.
98
+
99
+ The third argument, also optional, specifies the directory where DBRC will
100
+ look for the .dbrc file. By default, it looks in the pwuid (present
101
+ working user id) home directory. The rules for a .dbrc file still apply.
102
+
103
+ Win32 users should read the "Notes" section for how your home directory
104
+ is determined.
105
+
106
+ == Instance Methods
107
+ DBRC#database
108
+ The name of the database. Note that the same entry can appear more than
109
+ once, presumably because you have multiple user id's for the same
110
+ database.
111
+
112
+ DBRC#db
113
+ An alias for DBRC#database.
114
+
115
+ DBRC#database=(database)
116
+ Sets the database to +database+. This is generally discouraged because
117
+ it does not automatically reset the dsn.
118
+
119
+ DBRC#db=(database)
120
+ An alias for DBRC#database=.
121
+
122
+ DBRC#user
123
+ A valid user name for that database.
124
+
125
+ DBRC#user=(user)
126
+ Sets the user name to +user+.
127
+
128
+ DBRC#password
129
+ The password for that user.
130
+
131
+ DBRC#passwd
132
+ An alias for DBRC#password.
133
+
134
+ DBRC#password=(password)
135
+ Sets the password to +password+.
136
+
137
+ DBRC#passwd=(password)
138
+ An alias for DBRC#password=.
139
+
140
+ DBRC#driver
141
+ The driver type for that database (Oracle, MySql, etc).
142
+
143
+ DBRC#driver=(driver)
144
+ Sets the driver to +driver+. This use is discouraged because it does
145
+ not reset the dsn.
146
+
147
+ DBRC#timeout
148
+ The timeout period for a connection before the attempt is dropped.
149
+
150
+ DBRC#time_out
151
+ An alias for DBRC#timeout, provided purely for the sake of backwards
152
+ compatability.
153
+
154
+ DBRC#timeout=(int)
155
+ Sets the timeout value to +int+.
156
+
157
+ DBRC#maximum_reconnects
158
+ The maximum number of reconnect attempts that should be made for the the
159
+ database. Presumablly, you would use this with a "retry" within a rescue
160
+ block.
161
+
162
+ DBRC#max_reconn
163
+ An alias for DBRC#maximum_reconnects.
164
+
165
+ DBRC#maximum_reconnects=(max)
166
+ Sets the maximum number of reconnect attempts to +max+.
167
+
168
+ DBRC#max_reconn=(max)
169
+ An alias for DBRC#maximum_reconnects.
170
+
171
+ DBRC#interval
172
+ The number of seconds to wait before attempting to reconnect to the database
173
+ again should a network/database glitch occur.
174
+
175
+ DBRC#interval=(int)
176
+ Sets the interval seconds between connection attempts.
177
+
178
+ DBRC#dsn
179
+ Returns a string in "dbi:<driver>:<database>" format.
180
+
181
+ DBRC#dsn=(dsn)
182
+ Sets the dsn string to +dsn+. This method is discouraged because it does
183
+ not automatically reset the driver or database.
184
+
185
+ == Canonical Example
186
+ This is a basic template for how I do things:
187
+
188
+ require "dbi/dbrc"
189
+ require "timeout"
190
+
191
+ db = DBI::DBRC.new("somedb")
192
+ n = db.max_reconn
193
+
194
+ begin
195
+ timeout(db.time_out){
196
+ DBI.connect(db.dsn,db.user,db.passwd)
197
+ }
198
+ rescue DBI::Error
199
+ n -= 1
200
+ sleep db.interval if n > 0
201
+ retry if n > 0
202
+ raise
203
+ rescue TimeoutError
204
+ # handle timeout error
205
+ end
206
+
207
+ == Notes for Win32 Users
208
+ The 'home' directory for Win32 users is determined by ENV['USERPROFILE'].
209
+ If that is not set, ENV['HOME'] is used. If that is not set, then
210
+ 'C:\Documents and Settings\' + Admin.get_user(Admin.get_login).home_dir is
211
+ used.
212
+
213
+ To make your file hidden, right click on the .dbrc file in your Explorer
214
+ window, select "Properties" and check the "Hidden" checkbox.
215
+
216
+ I was going to require that the .dbrc file be encrypted on Win32 systems,
217
+ but that may require an official "certificate", assigned to you by a third
218
+ party, which is a bit much to expect. However, if the file is encrypted,
219
+ DBRC will attempt to decrypt it, parse it, and encrypt it again when done
220
+ parsing.
221
+
222
+ == Summary
223
+ These "methods" don't really do anything. They're simply meant as a
224
+ convenience mechanism for you dbi connections, plus a little bit of
225
+ obfuscation (for passwords).
226
+
227
+ == Adding your own configuration
228
+ If you want to add your own type of configuration file, you can still use
229
+ the dbi-dbrc package. All you need to do is:
230
+
231
+ * subclass DBRC
232
+ * redefine the +parse_dbrc_config_file+ method
233
+
234
+ Take a look at the XML and YML subclasses in dbrc.rb for two examples that
235
+ you can work from.
236
+
237
+ == Known Bugs
238
+ I'm not positive about the dsn strings for databases other than Oracle.
239
+ If it's not correct, please let me know.
240
+
241
+ == Author
242
+ Daniel J. Berger
243
+ djberg96 at gmail dot com
244
+ imperator on IRC (irc.freenode.net)
@@ -0,0 +1,21 @@
1
+ ###########################################################################
2
+ # test.rb
3
+ #
4
+ # This script is provided for those without TestUnit installed and/or for
5
+ # general futzing.
6
+ ###########################################################################
7
+ if File.basename(Dir.pwd) == "plain"
8
+ Dir.chdir "../.."
9
+ $LOAD_PATH.unshift Dir.pwd + "/lib"
10
+ Dir.chdir "examples/plain"
11
+ end
12
+
13
+ require "pp"
14
+ require "dbi/dbrc"
15
+ include DBI
16
+
17
+ puts "VERSION: " + DBRC::VERSION
18
+
19
+ db = DBRC.new("foo","user1",Dir.pwd)
20
+
21
+ pp db
@@ -0,0 +1,28 @@
1
+ #######################################################################
2
+ # test_xml.rb
3
+ #
4
+ # Simple test script that uses the DBRC::XML subclass.
5
+ #######################################################################
6
+ if File.basename(Dir.pwd) == "xml"
7
+ Dir.chdir "../.."
8
+ $LOAD_PATH.unshift Dir.pwd + "/lib"
9
+ Dir.chdir "examples/xml"
10
+ end
11
+
12
+ require "dbi/dbrc"
13
+ require "pp"
14
+ include DBI
15
+
16
+ puts "VERSION: " + DBRC::XML::VERSION
17
+
18
+ # Use the .dbrc file in this directory
19
+ db1 = DBRC::XML.new("foo",nil,Dir.pwd) # Get first entry found for 'foo'
20
+ db2 = DBRC::XML.new("foo","user1",Dir.pwd) # Specify user
21
+
22
+ puts "First entry found for 'foo': "
23
+ pp db1
24
+ puts "=" * 20
25
+
26
+ puts "Entry for 'foo' with user 'bar': "
27
+ pp db2
28
+ puts "=" * 20
@@ -0,0 +1,28 @@
1
+ #######################################################################
2
+ # test_xml.rb
3
+ #
4
+ # Simple test script that uses the DBRC::XML subclass.
5
+ #######################################################################
6
+ if File.basename(Dir.pwd) == "yml"
7
+ Dir.chdir "../.."
8
+ $LOAD_PATH.unshift Dir.pwd + "/lib"
9
+ Dir.chdir "examples/yml"
10
+ end
11
+
12
+ require "dbi/dbrc"
13
+ require "pp"
14
+ include DBI
15
+
16
+ puts "VERSION: " + DBRC::YML::VERSION
17
+
18
+ # Use the .dbrc file in this directory
19
+ db1 = DBRC::YML.new("foo",nil,Dir.pwd) # Get first entry found for 'foo'
20
+ db2 = DBRC::YML.new("foo","user1",Dir.pwd) # Specify user
21
+
22
+ puts "First entry found for 'foo': "
23
+ pp db1
24
+ puts "=" * 20
25
+
26
+ puts "Entry for 'foo' with user 'bar': "
27
+ pp db2
28
+ puts "=" * 20
@@ -0,0 +1,216 @@
1
+ if File::ALT_SEPARATOR
2
+ require "win32/file"
3
+ include Win32
4
+ end
5
+
6
+ require "sys/admin"
7
+ include Sys
8
+
9
+ module DBI
10
+
11
+ # The base error class for dbi-dbrc. If anything goes wrong, this is
12
+ # the error that is raised. A subclass of StandardError.
13
+ class DBRCError < StandardError; end
14
+
15
+ # The main class.
16
+ class DBRC
17
+ VERSION = "1.0.0"
18
+ attr_accessor :database, :user, :password, :driver, :dsn
19
+ attr_accessor :maximum_reconnects, :timeout, :interval, :dbrc_dir
20
+
21
+ # Returns a new DBRC object. The contents of the object depend on the
22
+ # arguments passed to the constructor. If only a database name is
23
+ # passed, then the first entry found in the .dbrc file that matches that
24
+ # database is parsed. If a user name is also included, then the first
25
+ # entry that matches both the database and user name is parsed.
26
+ #
27
+ # If a directory is passed as the third argument, then DBRC will look
28
+ # in that directory, instead of the default directory, for the .dbrc
29
+ # file.
30
+ #
31
+ # If an entry cannot be found for the database, or database plus user
32
+ # combination, then a DBRCError is raised. If the .dbrc file cannot
33
+ # be found, or is setup improperly with regards to permissions or
34
+ # properties, a DBRCError is raised.
35
+ def initialize(database,user=nil,dbrc_dir=nil)
36
+ if dbrc_dir.nil?
37
+ if File::ALT_SEPARATOR
38
+ home = ENV["USERPROFILE"] || ENV["HOME"]
39
+ file = nil
40
+
41
+ if home
42
+ file = home + "\\.dbrc"
43
+ else
44
+ file = "C:\\Documents and Settings\\"
45
+ file += Admin.get_user(Admin.get_login).home_dir + "\\.dbrc"
46
+ end
47
+
48
+ @dbrc_file = file
49
+ else
50
+ @dbrc_file = Admin.get_user(Process.uid).dir + "/.dbrc"
51
+ end
52
+ else
53
+ @dbrc_file = dbrc_dir + "/.dbrc"
54
+ end
55
+
56
+ @database = database
57
+ @user = user
58
+ encrypted = false # Win32 only
59
+
60
+ check_file()
61
+
62
+ # If on Win32 and the file is encrypted, decrypt it.
63
+ if File::ALT_SEPARATOR && File.encrypted?(@dbrc_file)
64
+ encrypted = true
65
+ File.decrypt(@dbrc_file)
66
+ end
67
+
68
+ parse_dbrc_config_file()
69
+ validate_data()
70
+ convert_numeric_strings()
71
+ create_dsn_string()
72
+
73
+ # If on Win32 and the file was encrypted, re-encrypt it
74
+ if File::ALT_SEPARATOR && encrypted
75
+ File.encrypt(@dbrc_file)
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ # Ensure that the user/password has been set
82
+ def validate_data
83
+ unless @user
84
+ raise DBRCError, "no user found associated with #{@database}"
85
+ end
86
+
87
+ unless @password
88
+ raise DBRCError, "password not defined for #{@user}@#{@database}"
89
+ end
90
+ end
91
+
92
+ # Converts strings that should be numbers into actual numbers
93
+ def convert_numeric_strings
94
+ @interval = @interval.to_i if @interval
95
+ @timeout = @timeout.to_i if @timeout
96
+ @maximum_reconnects = @maximum_reconnects.to_i if @maximum_reconnects
97
+ end
98
+
99
+ # Create the dsn string if the driver is defined
100
+ def create_dsn_string
101
+ @dsn = "dbi:#{@driver}:#{@database}" if @driver
102
+ end
103
+
104
+ # Check ownership and permissions
105
+ def check_file(file=@dbrc_file)
106
+ File.open(file){ |f|
107
+
108
+ # Permissions must be set to 600 or better on Unix systems.
109
+ # Must be hidden on Win32 systems.
110
+ if File::ALT_SEPARATOR
111
+ unless File.hidden?(file)
112
+ raise DBRCError, "The .dbrc file must be hidden"
113
+ end
114
+ else
115
+ unless (f.stat.mode & 077) == 0
116
+ raise DBRCError, "Bad .dbrc file permissions"
117
+ end
118
+ end
119
+
120
+ # Only the owner may use it
121
+ unless f.stat.owned?
122
+ raise DBRCError, "Not owner of .dbrc file"
123
+ end
124
+ }
125
+ end
126
+
127
+ # Parse the text out of the .dbrc file. This is the only method you
128
+ # need to redefine if writing your own config handler.
129
+ def parse_dbrc_config_file(file=@dbrc_file)
130
+ IO.foreach(file){ |line|
131
+ next if line =~ /^#/ # Ignore comments
132
+ db, user, pwd, driver, timeout, max, interval = line.split
133
+
134
+ next unless @database == db
135
+
136
+ if @user
137
+ next unless @user == user
138
+ end
139
+
140
+ @user = user
141
+ @password = pwd
142
+ @driver = driver
143
+ @timeout = timeout
144
+ @maximum_reconnects = max
145
+ @interval = interval
146
+ return
147
+ }
148
+
149
+ # If we reach here it means the database and/or user wasn't found
150
+ raise DBRCError, "No record found for #{@user}@#{@database}"
151
+ end
152
+
153
+ alias_method(:db,:database)
154
+ alias_method(:db=,:database=)
155
+ alias_method(:passwd,:password)
156
+ alias_method(:passwd=,:password=)
157
+ alias_method(:max_reconn,:maximum_reconnects)
158
+ alias_method(:max_reconn=,:maximum_reconnects=)
159
+ alias_method(:time_out,:timeout)
160
+ alias_method(:time_out=,:timeout=)
161
+ end
162
+
163
+ # A subclass of DBRC designed to handle .dbrc files in XML format. The
164
+ # public methods of this class are identical to DBRC.
165
+ class XML < DBRC
166
+ require "rexml/document"
167
+ include REXML
168
+ private
169
+ def parse_dbrc_config_file(file=@dbrc_file)
170
+ doc = Document.new(File.new(file))
171
+ fields = %w/user password driver interval timeout maximum_reconnects/
172
+ doc.elements.each("/dbrc/database"){ |element|
173
+ next unless element.attributes["name"] == database
174
+ if @user
175
+ next unless element.elements["user"].text == @user
176
+ end
177
+ fields.each{ |field|
178
+ val = element.elements[field]
179
+ unless val.nil?
180
+ send("#{field}=",val.text)
181
+ end
182
+ }
183
+ return
184
+ }
185
+ # If we reach here it means the database and/or user wasn't found
186
+ raise DBRCError, "No record found for #{@user}@#{@database}"
187
+ end
188
+ end
189
+
190
+ # A subclass of DBRC designed to handle .dbrc files in YAML format. The
191
+ # public methods of this class are identical to DBRC.
192
+ class YML < DBRC
193
+ require "yaml"
194
+ private
195
+ def parse_dbrc_config_file(file=@dbrc_file)
196
+ config = YAML.load(File.open(file))
197
+ config.each{ |hash|
198
+ hash.each{ |db,info|
199
+ next unless db == @database
200
+ if @user
201
+ next unless @user == info["user"]
202
+ end
203
+ @user = info["user"]
204
+ @password = info["password"]
205
+ @driver = info["driver"]
206
+ @interval = info["interval"]
207
+ @timeout = info["timeout"]
208
+ @maximum_reconnects = info["max_reconn"]
209
+ return
210
+ }
211
+ }
212
+ # If we reach this point, it means the database wasn't found
213
+ raise DBRCError, "No entry found for #{@user}@#{@database}"
214
+ end
215
+ end
216
+ end
@@ -0,0 +1,183 @@
1
+ ######################################################
2
+ # tc_dbrc.rb
3
+ #
4
+ # Test suite for the base class of DBI::DBRC.
5
+ ######################################################
6
+ base = File.basename(Dir.pwd)
7
+
8
+ if base == "test" || base =~ /dbi-dbrc/
9
+ Dir.chdir("..") if base == "test"
10
+ $LOAD_PATH.unshift(Dir.pwd + "/lib")
11
+ Dir.chdir("test") rescue nil
12
+ end
13
+
14
+ require "dbi/dbrc"
15
+ require "test/unit"
16
+ include DBI
17
+
18
+ class TC_DBRC < Test::Unit::TestCase
19
+ def setup
20
+ @dir = "../examples/plain"
21
+ @file = @dir + "/.dbrc"
22
+ @db1 = "foo"
23
+ @db2 = "bar"
24
+ @user1 = "user1"
25
+ @user2 = "user2"
26
+ @db_bad = "blah" # Doesn't exist
27
+ @user_bad = "user8" # Doesn't exist
28
+
29
+ if File::ALT_SEPARATOR
30
+ File.set_attr(@file,File::HIDDEN)
31
+ else
32
+ File.chmod(0600, @file)
33
+ end
34
+
35
+ @dbrc = DBRC.new(@db1, nil, @dir)
36
+ end
37
+
38
+ def test_version
39
+ assert_equal("1.0.0", DBRC::VERSION)
40
+ end
41
+
42
+ def test_bad_dbrc_properties
43
+ if File::ALT_SEPARATOR
44
+ File.unset_attr(@file, File::HIDDEN)
45
+ assert_raises(DBRCError){ DBRC.new(@db1, @user1, @dir) }
46
+ else
47
+ File.chmod(0555,@file)
48
+ assert_raises(DBRCError){ DBRC.new(@db1, @user1, @dir) }
49
+ end
50
+ end
51
+
52
+ def test_constructor
53
+ assert_raises(ArgumentError){ DBRC.new }
54
+ assert_nothing_raised{ DBRC.new(@db1, @user1, @dir) }
55
+ assert_nothing_raised{ DBRC.new(@db1, nil, @dir) }
56
+ end
57
+
58
+ def test_bad_database
59
+ assert_raises(DBRCError){ DBRC.new(@db_bad, nil, @dir) }
60
+ end
61
+
62
+ def test_bad_user
63
+ assert_raises(DBRCError){ DBRC.new(@db1, @user_bad, @dir) }
64
+ end
65
+
66
+ def test_database
67
+ assert_respond_to(@dbrc, :database)
68
+ assert_respond_to(@dbrc, :database=)
69
+ assert_respond_to(@dbrc, :db)
70
+ assert_respond_to(@dbrc, :db=)
71
+ assert_kind_of(String, @dbrc.db)
72
+ end
73
+
74
+ def test_dsn
75
+ assert_respond_to(@dbrc, :dsn)
76
+ assert_respond_to(@dbrc, :dsn=)
77
+ end
78
+
79
+ def test_user
80
+ assert_respond_to(@dbrc, :user)
81
+ assert_respond_to(@dbrc, :user=)
82
+ assert_kind_of(String, @dbrc.user)
83
+ end
84
+
85
+ def test_password
86
+ assert_respond_to(@dbrc, :password)
87
+ assert_respond_to(@dbrc, :password=)
88
+ assert_respond_to(@dbrc, :passwd)
89
+ assert_respond_to(@dbrc, :passwd=)
90
+ assert_kind_of(String, @dbrc.password)
91
+ end
92
+
93
+ def test_driver
94
+ assert_respond_to(@dbrc, :driver)
95
+ assert_respond_to(@dbrc, :driver=)
96
+ assert_kind_of(String, @dbrc.driver)
97
+ end
98
+
99
+ def test_interval
100
+ assert_respond_to(@dbrc, :interval)
101
+ assert_respond_to(@dbrc, :interval=)
102
+ assert_kind_of(Fixnum, @dbrc.interval)
103
+ end
104
+
105
+ def test_timeout
106
+ assert_respond_to(@dbrc, :timeout)
107
+ assert_respond_to(@dbrc, :timeout=)
108
+ assert_respond_to(@dbrc, :time_out)
109
+ assert_respond_to(@dbrc, :time_out=)
110
+ assert_kind_of(Fixnum, @dbrc.timeout)
111
+ end
112
+
113
+ def test_max_reconn
114
+ assert_respond_to(@dbrc, :max_reconn)
115
+ assert_respond_to(@dbrc, :max_reconn=)
116
+ assert_respond_to(@dbrc, :maximum_reconnects)
117
+ assert_respond_to(@dbrc, :maximum_reconnects=)
118
+ assert_kind_of(Fixnum, @dbrc.maximum_reconnects)
119
+ end
120
+
121
+ def test_sample_values
122
+ assert_equal("foo", @dbrc.database)
123
+ assert_equal("user1", @dbrc.user)
124
+ assert_equal("pwd1", @dbrc.passwd)
125
+ assert_equal("Oracle", @dbrc.driver)
126
+ assert_equal(60, @dbrc.interval)
127
+ assert_equal(40, @dbrc.timeout)
128
+ assert_equal(3, @dbrc.max_reconn)
129
+ assert_equal("dbi:Oracle:foo", @dbrc.dsn)
130
+ end
131
+
132
+ # Same database, different user
133
+ def test_duplicate_database
134
+ db = DBRC.new("foo", "user2", @dir)
135
+ assert_equal("user2", db.user)
136
+ assert_equal("pwd2", db.passwd)
137
+ assert_equal("OCI8", db.driver)
138
+ assert_equal(60, db.interval)
139
+ assert_equal(60, db.timeout)
140
+ assert_equal(4, db.max_reconn)
141
+ assert_equal("dbi:OCI8:foo", db.dsn)
142
+ end
143
+
144
+ # Different database, different user
145
+ def test_different_database
146
+ db = DBRC.new("bar", "user1", @dir)
147
+ assert_equal("user1", db.user)
148
+ assert_equal("pwd3", db.passwd)
149
+ assert_equal("Oracle", db.driver)
150
+ assert_equal(30, db.interval)
151
+ assert_equal(30, db.timeout)
152
+ assert_equal(2, db.max_reconn)
153
+ assert_equal("dbi:Oracle:bar", db.dsn)
154
+ end
155
+
156
+ # A database with only a couple fields defined
157
+ def test_nil_values
158
+ db = DBRC.new("baz", "user3", @dir)
159
+ assert_equal("user3", db.user)
160
+ assert_equal("pwd4", db.passwd)
161
+ assert_nil(db.driver)
162
+ assert_nil(db.interval)
163
+ assert_nil(db.timeout)
164
+ assert_nil(db.max_reconn)
165
+ assert_nil(db.dsn)
166
+ end
167
+
168
+ def teardown
169
+ @dir = nil
170
+ @db1 = nil
171
+ @db2 = nil
172
+ @user1 = nil
173
+ @user2 = nil
174
+ @db_bad = nil
175
+ @user_bad = nil
176
+ @dbrc = nil
177
+ if File::ALT_SEPARATOR
178
+ File.set_attr(@file, File::HIDDEN)
179
+ else
180
+ File.chmod(0600, @file)
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,165 @@
1
+ #################################################################
2
+ # tc_dbrc_xml.rb
3
+ #
4
+ # Test suite for the XML specific version of DBI::DBRC.
5
+ #################################################################
6
+ base = File.basename(Dir.pwd)
7
+
8
+ if base == "test" || base =~ /dbi-dbrc/
9
+ Dir.chdir("..") if base == "test"
10
+ $LOAD_PATH.unshift(Dir.pwd + "/lib")
11
+ Dir.chdir("test") rescue nil
12
+ end
13
+
14
+ require "dbi/dbrc"
15
+ require "test/unit"
16
+ require "rexml/document"
17
+ include REXML
18
+ include DBI
19
+
20
+ class TC_DBRC_XML < Test::Unit::TestCase
21
+ def setup
22
+ @dir = "../examples/xml"
23
+ @file = @dir + "/.dbrc"
24
+ @db1 = "foo"
25
+ @db2 = "bar"
26
+ @user1 = "user1"
27
+ @user2 = "user2"
28
+ @db_bad = "blah" # Doesn't exist
29
+ @user_bad = "user8" # Doesn't exist
30
+
31
+ @dbrc = DBRC::XML.new(@db1, nil, @dir)
32
+ File.chmod(0600, @file)
33
+ end
34
+
35
+ def test_version
36
+ assert_equal("1.0.0", DBRC::XML::VERSION)
37
+ end
38
+
39
+ def test_constructor
40
+ assert_raises(ArgumentError){ DBRC::XML.new }
41
+ assert_nothing_raised{ DBRC::XML.new(@db1, @user1, @dir) }
42
+ assert_nothing_raised{ DBRC::XML.new(@db1, nil, @dir) }
43
+ end
44
+
45
+ def test_bad_database
46
+ assert_raises(DBRCError){ DBRC::XML.new(@db_bad, nil, @dir) }
47
+ end
48
+
49
+ def test_bad_user
50
+ assert_raises(DBRCError){ DBRC::XML.new(@db1, @user_bad, @dir) }
51
+ end
52
+
53
+ def test_database
54
+ assert_respond_to(@dbrc, :database)
55
+ assert_respond_to(@dbrc, :database=)
56
+ assert_respond_to(@dbrc, :db)
57
+ assert_respond_to(@dbrc, :db=)
58
+ assert_kind_of(String, @dbrc.db)
59
+ end
60
+
61
+ def test_dsn
62
+ assert_respond_to(@dbrc, :dsn)
63
+ assert_respond_to(@dbrc, :dsn=)
64
+ end
65
+
66
+ def test_user
67
+ assert_respond_to(@dbrc, :user)
68
+ assert_respond_to(@dbrc, :user=)
69
+ assert_kind_of(String, @dbrc.user)
70
+ end
71
+
72
+ def test_password
73
+ assert_respond_to(@dbrc, :password)
74
+ assert_respond_to(@dbrc, :password=)
75
+ assert_respond_to(@dbrc, :passwd)
76
+ assert_respond_to(@dbrc, :passwd=)
77
+ assert_kind_of(String, @dbrc.password)
78
+ end
79
+
80
+ def test_driver
81
+ assert_respond_to(@dbrc, :driver)
82
+ assert_respond_to(@dbrc, :driver=)
83
+ assert_kind_of(String, @dbrc.driver)
84
+ end
85
+
86
+ def test_interval
87
+ assert_respond_to(@dbrc, :interval)
88
+ assert_respond_to(@dbrc, :interval=)
89
+ assert_kind_of(Fixnum, @dbrc.interval)
90
+ end
91
+
92
+ def test_timeout
93
+ assert_respond_to(@dbrc, :timeout)
94
+ assert_respond_to(@dbrc, :timeout=)
95
+ assert_respond_to(@dbrc, :time_out)
96
+ assert_respond_to(@dbrc, :time_out=)
97
+ assert_kind_of(Fixnum, @dbrc.timeout)
98
+ end
99
+
100
+ def test_max_reconn
101
+ assert_respond_to(@dbrc, :max_reconn)
102
+ assert_respond_to(@dbrc, :max_reconn=)
103
+ assert_respond_to(@dbrc, :maximum_reconnects)
104
+ assert_respond_to(@dbrc, :maximum_reconnects=)
105
+ assert_kind_of(Fixnum, @dbrc.maximum_reconnects)
106
+ end
107
+
108
+ def test_sample_values
109
+ assert_equal("foo", @dbrc.database)
110
+ assert_equal("user1", @dbrc.user)
111
+ assert_equal("pwd1", @dbrc.passwd)
112
+ assert_equal("Oracle", @dbrc.driver)
113
+ assert_equal(60, @dbrc.interval)
114
+ assert_equal(40, @dbrc.timeout)
115
+ assert_equal(3, @dbrc.max_reconn)
116
+ assert_equal("dbi:Oracle:foo", @dbrc.dsn)
117
+ end
118
+
119
+ # Same database, different user
120
+ def test_duplicate_database
121
+ db = DBRC::XML.new("foo", "user2", @dir)
122
+ assert_equal("user2", db.user)
123
+ assert_equal("pwd2", db.passwd)
124
+ assert_equal("OCI8", db.driver)
125
+ assert_equal(60, db.interval)
126
+ assert_equal(60, db.timeout)
127
+ assert_equal(4, db.max_reconn)
128
+ assert_equal("dbi:OCI8:foo", db.dsn)
129
+ end
130
+
131
+ # Different database, different user
132
+ def test_different_database
133
+ db = DBRC::XML.new("bar", "user1", @dir)
134
+ assert_equal("user1", db.user)
135
+ assert_equal("pwd3", db.passwd)
136
+ assert_equal("Oracle", db.driver)
137
+ assert_equal(30, db.interval)
138
+ assert_equal(30, db.timeout)
139
+ assert_equal(2, db.max_reconn)
140
+ assert_equal("dbi:Oracle:bar", db.dsn)
141
+ end
142
+
143
+ # A database with only a couple fields defined
144
+ def test_nil_values
145
+ db = DBRC::XML.new("baz", "user3", @dir)
146
+ assert_equal("user3", db.user)
147
+ assert_equal("pwd4", db.passwd)
148
+ assert_nil(db.driver)
149
+ assert_nil(db.interval)
150
+ assert_nil(db.timeout)
151
+ assert_nil(db.max_reconn)
152
+ assert_nil(db.dsn)
153
+ end
154
+
155
+ def teardown
156
+ @dir = nil
157
+ @db1 = nil
158
+ @db2 = nil
159
+ @user1 = nil
160
+ @user2 = nil
161
+ @db_bad = nil
162
+ @user_bad = nil
163
+ @dbrc = nil
164
+ end
165
+ end
@@ -0,0 +1,174 @@
1
+ #################################################################
2
+ # tc_dbrc_yml.rb
3
+ #
4
+ # Test suite for the YAML specific version of DBI::DBRC.
5
+ #################################################################
6
+ base = File.basename(Dir.pwd)
7
+
8
+ if base == "test" || base =~ /dbi-dbrc/
9
+ Dir.chdir("..") if base == "test"
10
+ $LOAD_PATH.unshift(Dir.pwd + "/lib")
11
+ Dir.chdir("test") rescue nil
12
+ end
13
+
14
+ require "dbi/dbrc"
15
+ require "test/unit"
16
+ include DBI
17
+
18
+ class TC_DBRC_YML < Test::Unit::TestCase
19
+ def setup
20
+ @dir = "../examples/yml"
21
+ @file = @dir + "/.dbrc"
22
+ @db1 = "foo"
23
+ @db2 = "bar"
24
+ @user1 = "user1"
25
+ @user2 = "user2"
26
+ @db_bad = "blah" # Doesn't exist
27
+ @user_bad = "user8" # Doesn't exist
28
+
29
+ if File::ALT_SEPARATOR
30
+ File.set_attr(@file, File::HIDDEN)
31
+ else
32
+ File.chmod(0600, @file)
33
+ end
34
+
35
+ @dbrc = DBRC::YML.new(@db1, nil, @dir)
36
+ end
37
+
38
+ def test_version
39
+ assert_equal("1.0.0", DBRC::YML::VERSION)
40
+ end
41
+
42
+ def test_constructor
43
+ assert_raises(ArgumentError){ DBRC::YML.new }
44
+ assert_nothing_raised{ DBRC::YML.new(@db1, @user1, @dir) }
45
+ assert_nothing_raised{ DBRC::YML.new(@db1, nil, @dir) }
46
+ end
47
+
48
+ def test_bad_database
49
+ assert_raises(DBRCError){ DBRC::YML.new(@db_bad, nil, @dir) }
50
+ end
51
+
52
+ def test_bad_user
53
+ assert_raises(DBRCError){ DBRC::YML.new(@db1, @user_bad, @dir) }
54
+ end
55
+
56
+ def test_database
57
+ assert_respond_to(@dbrc, :database)
58
+ assert_respond_to(@dbrc, :database=)
59
+ assert_respond_to(@dbrc, :db)
60
+ assert_respond_to(@dbrc, :db=)
61
+ assert_kind_of(String, @dbrc.db)
62
+ end
63
+
64
+ def test_dsn
65
+ assert_respond_to(@dbrc, :dsn)
66
+ assert_respond_to(@dbrc, :dsn=)
67
+ end
68
+
69
+ def test_user
70
+ assert_respond_to(@dbrc, :user)
71
+ assert_respond_to(@dbrc, :user=)
72
+ assert_kind_of(String, @dbrc.user)
73
+ end
74
+
75
+ def test_password
76
+ assert_respond_to(@dbrc, :password)
77
+ assert_respond_to(@dbrc, :password=)
78
+ assert_respond_to(@dbrc, :passwd)
79
+ assert_respond_to(@dbrc, :passwd=)
80
+ assert_kind_of(String, @dbrc.password)
81
+ end
82
+
83
+ def test_driver
84
+ assert_respond_to(@dbrc, :driver)
85
+ assert_respond_to(@dbrc, :driver=)
86
+ assert_kind_of(String, @dbrc.driver)
87
+ end
88
+
89
+ def test_interval
90
+ assert_respond_to(@dbrc, :interval)
91
+ assert_respond_to(@dbrc, :interval=)
92
+ assert_kind_of(Fixnum, @dbrc.interval)
93
+ end
94
+
95
+ def test_timeout
96
+ assert_respond_to(@dbrc, :timeout)
97
+ assert_respond_to(@dbrc, :timeout=)
98
+ assert_respond_to(@dbrc, :time_out)
99
+ assert_respond_to(@dbrc, :time_out=)
100
+ assert_kind_of(Fixnum, @dbrc.timeout)
101
+ end
102
+
103
+ def test_max_reconn
104
+ assert_respond_to(@dbrc, :max_reconn)
105
+ assert_respond_to(@dbrc, :max_reconn=)
106
+ assert_respond_to(@dbrc, :maximum_reconnects)
107
+ assert_respond_to(@dbrc, :maximum_reconnects=)
108
+ assert_kind_of(Fixnum, @dbrc.maximum_reconnects)
109
+ end
110
+
111
+ def test_sample_values
112
+ assert_equal("foo", @dbrc.database)
113
+ assert_equal("user1", @dbrc.user)
114
+ assert_equal("pwd1", @dbrc.passwd)
115
+ assert_equal("Oracle", @dbrc.driver)
116
+ assert_equal(60, @dbrc.interval)
117
+ assert_equal(40, @dbrc.timeout)
118
+ assert_equal(3, @dbrc.max_reconn)
119
+ assert_equal("dbi:Oracle:foo", @dbrc.dsn)
120
+ end
121
+
122
+ # Same database, different user
123
+ def test_duplicate_database
124
+ db = DBRC::YML.new("foo", "user2", @dir)
125
+ assert_equal("user2", db.user)
126
+ assert_equal("pwd2", db.passwd)
127
+ assert_equal("OCI8", db.driver)
128
+ assert_equal(60, db.interval)
129
+ assert_equal(60, db.timeout)
130
+ assert_equal(4, db.max_reconn)
131
+ assert_equal("dbi:OCI8:foo", db.dsn)
132
+ end
133
+
134
+ # Different database, different user
135
+ def test_different_database
136
+ db = DBRC::YML.new("bar", "user1", @dir)
137
+ assert_equal("user1", db.user)
138
+ assert_equal("pwd3", db.passwd)
139
+ assert_equal("Oracle", db.driver)
140
+ assert_equal(30, db.interval)
141
+ assert_equal(30, db.timeout)
142
+ assert_equal(2, db.max_reconn)
143
+ assert_equal("dbi:Oracle:bar", db.dsn)
144
+ end
145
+
146
+ # A database with only a couple fields defined
147
+ def test_nil_values
148
+ db = DBRC::YML.new("baz", "user3", @dir)
149
+ assert_equal("user3", db.user)
150
+ assert_equal("pwd4", db.passwd)
151
+ assert_nil(db.driver)
152
+ assert_nil(db.interval)
153
+ assert_nil(db.timeout)
154
+ assert_nil(db.max_reconn)
155
+ assert_nil(db.dsn)
156
+ end
157
+
158
+ def teardown
159
+ @dir = nil
160
+ @db1 = nil
161
+ @db2 = nil
162
+ @user1 = nil
163
+ @user2 = nil
164
+ @db_bad = nil
165
+ @user_bad = nil
166
+ @dbrc = nil
167
+
168
+ if File::ALT_SEPARATOR
169
+ File.set_attr(@file, File::HIDDEN)
170
+ else
171
+ File.chmod(0600, @file)
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift(Dir.pwd)
2
+ $LOAD_PATH.unshift(Dir.pwd + "/test")
3
+
4
+ require "tc_dbrc"
5
+ require "tc_dbrc_yml"
6
+ require "tc_dbrc_xml"
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: dbi-dbrc
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2005-06-15
8
+ summary: A simple way to avoid hard-coding passwords with DBI
9
+ require_paths:
10
+ - lib
11
+ email: djberg96@gmail.com
12
+ homepage: http://www.rubyforge.org/projects/shards
13
+ rubyforge_project:
14
+ description: A simple way to avoid hard-coding passwords with DBI
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ authors:
27
+ - Daniel Berger
28
+ files:
29
+ - examples/plain
30
+ - examples/xml
31
+ - examples/yml
32
+ - examples/plain/test.rb
33
+ - examples/xml/test_xml.rb
34
+ - examples/yml/test_yml.rb
35
+ - lib/dbi
36
+ - lib/dbi/dbrc.rb
37
+ - test/tc_dbrc.rb
38
+ - test/tc_dbrc_xml.rb
39
+ - test/tc_dbrc_yml.rb
40
+ - test/ts_all.rb
41
+ - README
42
+ - CHANGES
43
+ test_files:
44
+ - test/ts_all.rb
45
+ rdoc_options: []
46
+
47
+ extra_rdoc_files:
48
+ - README
49
+ - CHANGES
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ requirements: []
55
+
56
+ dependencies: []
57
+