rubius2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/helper.rb ADDED
@@ -0,0 +1,98 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'test/unit'
13
+ require 'shoulda'
14
+ require 'mocha'
15
+
16
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
17
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
18
+ require 'rubius'
19
+
20
+ class Test::Unit::TestCase
21
+ end
22
+
23
+ RADIUS_DICTIONARY = [
24
+ "# -*- text -*-",
25
+ "#",
26
+ "# Attributes and values defined in RFC 2865.",
27
+ "ATTRIBUTE User-Name 1 string",
28
+ "ATTRIBUTE User-Password 2 string encrypt=1",
29
+ "ATTRIBUTE Service-Type 6 integer",
30
+ "",
31
+ "# Values",
32
+ "VALUE Service-Type Login-User 1",
33
+ "VALUE Service-Type Framed-User 2",
34
+ "VALUE Service-Type Callback-Login-User 3",
35
+ "",
36
+ "# -*- text -*-",
37
+ "#",
38
+ "# dictionary.cisco",
39
+ "VENDOR Cisco 9",
40
+ "",
41
+ "BEGIN-VENDOR Cisco",
42
+ "",
43
+ "ATTRIBUTE Cisco-AVPair 1 string",
44
+ "ATTRIBUTE Cisco-NAS-Port 2 string",
45
+ "ATTRIBUTE Cisco-Disconnect-Cause 195 integer",
46
+ "",
47
+ "VALUE Cisco-Disconnect-Cause Unknown 2",
48
+ "VALUE Cisco-Disconnect-Cause CLID-Authentication-Failure 4",
49
+ "#",
50
+ "",
51
+ "VENDOR Juniper 2636",
52
+ "",
53
+ "BEGIN-VENDOR Juniper",
54
+ "",
55
+ "ATTRIBUTE Juniper-Local-User-Name 1 string"
56
+ ]
57
+
58
+ DICT_BROKEN_VENDOR = [
59
+ "# broken vendor line follows (missing id):",
60
+ "VENDOR Microsoft",
61
+ "",
62
+ "ATTRIBUTE Not-Included 8344 string",
63
+ "",
64
+ "# non-broken vendor:",
65
+ "VENDOR Apple 25",
66
+ "ATTRIBUTE Is-Cool 1337 integer",
67
+ "",
68
+ "VENDOR NoId id"
69
+ ]
70
+
71
+ DICT_BROKEN_ATTR = [
72
+ "# broken attribute line:",
73
+ "VENDOR IBM 123",
74
+ "",
75
+ "ATTRIBUTE IBM-Attr-Included 5137 string",
76
+ "ATTRIBUTE IBM-Attr-NotIncluded 5138",
77
+ "ATTRIBUTE IBM-Attr-NotIncluded2 asdf integer",
78
+ "ATTRIBUTE IBM-Attr-Included2 5139 string",
79
+ ""
80
+ ]
81
+
82
+ CONFIG_FILE = {
83
+ 'development' => {
84
+ 'host' => '127.0.0.1',
85
+ 'port' => 1812,
86
+ 'timeout' => 10,
87
+ 'secret' => 'development-secret',
88
+ 'dictionary' => 'radius-dictionary'
89
+ },
90
+ 'production' => {
91
+ 'host' => '10.1.0.254',
92
+ 'port' => 1812,
93
+ 'timeout' => 10,
94
+ 'secret' => 'production-secret',
95
+ 'dictionary' => 'radius-dictionary',
96
+ 'nas_ip' => '10.1.0.1'
97
+ }
98
+ }
@@ -0,0 +1,84 @@
1
+ require 'helper'
2
+
3
+ class TestAuthenticator < Test::Unit::TestCase
4
+ context "A Rubius::Authenticator instance" do
5
+ setup do
6
+ Process.stubs(:pid).returns(93354)
7
+
8
+ UDPSocket.any_instance
9
+ UDPSocket.any_instance.stubs(:addr).returns(["AF_INET", 65194, "10.1.0.45", "10.1.0.45"])
10
+
11
+ @authenticator = Rubius::Authenticator.instance
12
+ end
13
+
14
+ should "generate an identifier" do
15
+ assert_equal 170, @authenticator.instance_eval { @identifier }
16
+ end
17
+
18
+ should "increment the identifier" do
19
+ identifier = @authenticator.instance_eval { @identifier }
20
+ identifier += 1
21
+
22
+ assert @authenticator.instance_eval { increment_identifier! }
23
+
24
+ assert_equal identifier, @authenticator.instance_eval { @identifier }
25
+ end
26
+
27
+ should "overflow the identifier beyond 255" do
28
+ @authenticator.instance_eval { @identifier = 254 }
29
+ assert_equal 254, @authenticator.instance_eval { @identifier }
30
+
31
+ assert @authenticator.instance_eval { increment_identifier! }
32
+ assert_equal 255, @authenticator.instance_eval { @identifier }
33
+
34
+ assert @authenticator.instance_eval { increment_identifier! }
35
+ assert_equal 0, @authenticator.instance_eval { @identifier }
36
+
37
+ assert @authenticator.instance_eval { increment_identifier! }
38
+ assert_equal 1, @authenticator.instance_eval { @identifier }
39
+ end
40
+
41
+ context "supplied with a configuration file" do
42
+ should "read and parse the configuration file" do
43
+ YAML.stubs(:load_file).returns(CONFIG_FILE)
44
+
45
+ @authenticator.init_from_config("rubius.yml", 'development')
46
+ assert_equal 'development-secret', @authenticator.instance_eval { @secret }
47
+ end
48
+
49
+ should "use Rails.env if env is not passed and running in a Rails application" do
50
+ YAML.stubs(:load_file).returns(CONFIG_FILE)
51
+ ::Rails.stubs(:env).returns('production')
52
+
53
+ @authenticator.init_from_config("rubius.yml")
54
+ assert_equal 'production-secret', @authenticator.instance_eval { @secret }
55
+ end
56
+
57
+ should "handle a non-existent config file" do
58
+ YAML.stubs(:load_file).raises(Errno::ENOENT)
59
+ ::Rails.stubs(:env).returns('production')
60
+
61
+ assert_raises Rubius::MissingConfiguration do
62
+ @authenticator.init_from_config("/does/not/exist/rubius.yml")
63
+ end
64
+ end
65
+
66
+ should "handle an empty config section" do
67
+ YAML.stubs(:load_file).returns(CONFIG_FILE)
68
+ ::Rails.stubs(:env).returns('staging')
69
+
70
+ assert_raises Rubius::MissingEnvironmentConfiguration do
71
+ @authenticator.init_from_config("rubius.yml")
72
+ end
73
+ end
74
+
75
+ should "setup a connection to the specified server" do
76
+ YAML.stubs(:load_file).returns(CONFIG_FILE)
77
+ ::Rails.stubs(:env).returns('production')
78
+ UDPSocket.any_instance.stubs(:connect).with('10.1.0.254', 1812).returns(true)
79
+
80
+ assert @authenticator.init_from_config("rubius.yml")
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,68 @@
1
+ require 'helper'
2
+
3
+ class TestDictionary < Test::Unit::TestCase
4
+ context "A Rubius::Dictionary" do
5
+ setup do
6
+ @dictionary = Rubius::Dictionary.new
7
+ end
8
+
9
+ context "provided with a valid RADIUS dictionary file" do
10
+ setup do
11
+ IO.stubs(:readlines).returns(RADIUS_DICTIONARY)
12
+ end
13
+
14
+ should "load and parse it" do
15
+ assert @dictionary.load("filename")
16
+
17
+ assert_equal "Cisco", @dictionary.vendor_name(9)
18
+ assert_equal "Juniper", @dictionary.vendor_name(2636)
19
+
20
+ assert_equal "Service-Type", @dictionary.attribute_name(6)
21
+ assert_equal "string", @dictionary.attribute_type(2, 9)
22
+ assert_equal 1, @dictionary.attribute_id("Juniper-Local-User-Name", 2636)
23
+ end
24
+ end
25
+
26
+ context "provided with a non-existant RADIUS dictionary file" do
27
+ setup do
28
+ IO.stubs(:readlines).raises(Errno::ENOENT)
29
+ end
30
+
31
+ should "raise an exception" do
32
+ assert_raise Rubius::InvalidDictionaryError do
33
+ @dictionary.load("/no/existing/file")
34
+ end
35
+ end
36
+ end
37
+
38
+ context "provided with a malformed RADIUS dictionary file" do
39
+ should "handle broken VENDOR lines" do
40
+ IO.stubs(:readlines).returns(RADIUS_DICTIONARY+DICT_BROKEN_VENDOR)
41
+
42
+ assert @dictionary.load("filename")
43
+
44
+ assert !@dictionary.vendors.include?("Microsoft")
45
+ assert_nil @dictionary.attribute_name(8344)
46
+
47
+ assert @dictionary.vendors.include?("Apple")
48
+ assert_equal "Is-Cool", @dictionary.attribute_name(1337, 25)
49
+
50
+ assert !@dictionary.vendors.include?("NoId")
51
+ end
52
+
53
+ should "handle broken ATTRIBUTE lines" do
54
+ IO.stubs(:readlines).returns(RADIUS_DICTIONARY+DICT_BROKEN_ATTR)
55
+
56
+ assert @dictionary.load("filename")
57
+
58
+ assert_equal 'IBM-Attr-Included', @dictionary.attribute_name(5137, 123)
59
+ assert_nil @dictionary.attribute_name(5138, 123)
60
+ assert_equal 'IBM-Attr-Included2', @dictionary.attribute_name(5139, 123)
61
+ end
62
+
63
+ should "handle broken VALUE lines" do
64
+
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,62 @@
1
+ require 'helper'
2
+
3
+ module Rails; end
4
+
5
+ class TestRails < Test::Unit::TestCase
6
+ context "a Rubius::Rails.init call" do
7
+ context "in a standalone application" do
8
+ setup do
9
+ Object.instance_eval{ remove_const("Rails") } if defined?(::Rails)
10
+ end
11
+
12
+ context "without any parameters" do
13
+ should "try to load the config file at current_dir/config/rubius.yml with the 'development' environment" do
14
+ Rubius::Authenticator.any_instance.stubs(:init_from_config).with(File.join(FileUtils.pwd, 'config', 'rubius.yml'), 'development').returns(true)
15
+ assert Rubius::Rails.init
16
+ end
17
+ end
18
+
19
+ context "with path specified" do
20
+ should "try to load the config at the specified path with the 'development' environment" do
21
+ config_file = File.join('path', 'to', 'config')
22
+
23
+ Rubius::Authenticator.any_instance.stubs(:init_from_config).with(File.join(config_file, 'rubius.yml'), 'development').returns(true)
24
+
25
+ assert Rubius::Rails.init(config_file)
26
+ end
27
+ end
28
+ end
29
+
30
+ context "in a Rails application" do
31
+ setup do
32
+ ::Rails.stubs(:env).returns('stubbed')
33
+ ::Rails.stubs(:root).returns('/home/rails')
34
+ end
35
+
36
+ should "use Rails path and environment" do
37
+ Rubius::Authenticator.any_instance.stubs(:init_from_config).with(File.join('/', 'home', 'rails', 'config', 'rubius.yml'), 'stubbed').returns(true)
38
+ assert Rubius::Rails.init
39
+ end
40
+ end
41
+
42
+ context "with a non-existant configuration file" do
43
+ should "raise an exception" do
44
+ Rubius::Authenticator.any_instance.stubs(:init_from_config).raises(Rubius::MissingConfiguration)
45
+
46
+ assert_raises Rubius::MissingConfiguration do
47
+ Rubius::Rails.init('/bla')
48
+ end
49
+ end
50
+ end
51
+
52
+ context "with a configuration file thats missing the current environment" do
53
+ should "raise an exception" do
54
+ Rubius::Authenticator.any_instance.stubs(:init_from_config).raises(Rubius::MissingEnvironmentConfiguration)
55
+
56
+ assert_raises Rubius::MissingEnvironmentConfiguration do
57
+ Rubius::Rails.init('/bla')
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,40 @@
1
+ require 'helper'
2
+
3
+ class TestString < Test::Unit::TestCase
4
+ should "define a xor method" do
5
+ assert String.method_defined?(:xor)
6
+ end
7
+
8
+ should "return correct string when xor-ed with an empty string" do
9
+ str1 = "test string"
10
+ str2 = ""
11
+
12
+ result = str1.xor(str2)
13
+ assert_equal str1, result
14
+
15
+ result2 = result.xor(str2)
16
+ assert_equal str1, result2
17
+ end
18
+
19
+ should "return correct string when xor-ed with a longer string" do
20
+ str1 = "test string"
21
+ str2 = "longer test string"
22
+
23
+ result = str1.xor(str2)
24
+ assert_equal "\x18\n\x1D\x13E\x01T\x06\f\x1D\x13", result
25
+
26
+ result2 = result.xor(str2)
27
+ assert_equal str1, result2
28
+ end
29
+
30
+ should "return correct string when xor-ed with a shorter string" do
31
+ str1 = "test string"
32
+ str2 = "short s"
33
+
34
+ result = str1.xor(str2)
35
+ assert_equal "\a\r\x1C\x06TS\a\x01\x01\x01\x15", result
36
+
37
+ result2 = result.xor(str2)
38
+ assert_equal str1, result2
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubius2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ralph Rooding
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shoulda
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: jeweler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.5.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.5.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rcov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 0.4.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.4.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: autotest-standalone
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 4.5.5
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 4.5.5
97
+ - !ruby/object:Gem::Dependency
98
+ name: mocha
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.9.12
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.9.12
111
+ description: Rubius provides a simple interface to RADIUS authentication
112
+ email: ralph@izerion.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files:
116
+ - LICENSE.txt
117
+ - README.rdoc
118
+ files:
119
+ - ".autotest"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.rdoc
123
+ - Rakefile
124
+ - VERSION
125
+ - lib/generators/rubius/install_generator.rb
126
+ - lib/generators/rubius/templates/radius-dictionary
127
+ - lib/generators/rubius/templates/rubius.yml
128
+ - lib/generators/rubius/templates/rubius_initializer.rb
129
+ - lib/rubius.rb
130
+ - lib/rubius/authenticator.rb
131
+ - lib/rubius/dictionary.rb
132
+ - lib/rubius/exceptions.rb
133
+ - lib/rubius/packet.rb
134
+ - lib/rubius/rails.rb
135
+ - lib/rubius/string.rb
136
+ - rubius.gemspec
137
+ - test/helper.rb
138
+ - test/test_authenticator.rb
139
+ - test/test_dictionary.rb
140
+ - test/test_rails.rb
141
+ - test/test_string.rb
142
+ homepage: http://github.com/bytemine/rubius
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.2.2
163
+ signing_key:
164
+ specification_version: 3
165
+ summary: A simple ruby RADIUS authentication gem
166
+ test_files:
167
+ - test/helper.rb
168
+ - test/test_authenticator.rb
169
+ - test/test_dictionary.rb
170
+ - test/test_rails.rb
171
+ - test/test_string.rb