PageRenderTime 55ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/unit/network/authstore.rb

https://github.com/CodeNursery/puppet
Ruby | 370 lines | 345 code | 24 blank | 1 comment | 5 complexity | ee16371bdf2c7ce2c3df92cec17239fd MD5 | raw file
Possible License(s): GPL-2.0
  1. #!/usr/bin/env ruby
  2. require File.dirname(__FILE__) + '/../../spec_helper'
  3. require 'puppet/network/authconfig'
  4. describe Puppet::Network::AuthStore do
  5. describe "when checking if the acl has some entries" do
  6. before :each do
  7. @authstore = Puppet::Network::AuthStore.new
  8. end
  9. it "should be empty if no ACE have been entered" do
  10. @authstore.should be_empty
  11. end
  12. it "should not be empty if it is a global allow" do
  13. @authstore.allow('*')
  14. @authstore.should_not be_empty
  15. end
  16. it "should not be empty if at least one allow has been entered" do
  17. @authstore.allow('1.1.1.*')
  18. @authstore.should_not be_empty
  19. end
  20. it "should not be empty if at least one deny has been entered" do
  21. @authstore.deny('1.1.1.*')
  22. @authstore.should_not be_empty
  23. end
  24. end
  25. end
  26. describe Puppet::Network::AuthStore::Declaration do
  27. ['100.101.99.98','100.100.100.100','1.2.3.4','11.22.33.44'].each { |ip|
  28. describe "when the pattern is a simple numeric IP such as #{ip}" do
  29. before :each do
  30. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,ip)
  31. end
  32. it "should match the specified IP" do
  33. @declaration.should be_match('www.testsite.org',ip)
  34. end
  35. it "should not match other IPs" do
  36. @declaration.should_not be_match('www.testsite.org','200.101.99.98')
  37. end
  38. end
  39. (1..3).each { |n|
  40. describe "when the pattern is a IP mask with #{n} numeric segments and a *" do
  41. before :each do
  42. @ip_pattern = ip.split('.')[0,n].join('.')+'.*'
  43. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,@ip_pattern)
  44. end
  45. it "should match an IP in the range" do
  46. @declaration.should be_match('www.testsite.org',ip)
  47. end
  48. it "should not match other IPs" do
  49. @declaration.should_not be_match('www.testsite.org','200.101.99.98')
  50. end
  51. it "should not match IPs that differ in the last non-wildcard segment" do
  52. other = ip.split('.')
  53. other[n-1].succ!
  54. @declaration.should_not be_match('www.testsite.org',other.join('.'))
  55. end
  56. end
  57. }
  58. }
  59. describe "when the pattern is a numeric IP with a back reference" do
  60. before :each do
  61. @ip = '100.101.$1'
  62. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,@ip).interpolate('12.34'.match(/(.*)/))
  63. end
  64. it "should match an IP with the appropriate interpolation" do
  65. @declaration.should be_match('www.testsite.org',@ip.sub(/\$1/,'12.34'))
  66. end
  67. it "should not match other IPs" do
  68. @declaration.should_not be_match('www.testsite.org',@ip.sub(/\$1/,'66.34'))
  69. end
  70. end
  71. [
  72. "02001:0000:1234:0000:0000:C1C0:ABCD:0876",
  73. "2001:0000:1234:0000:00001:C1C0:ABCD:0876",
  74. " 2001:0000:1234:0000:0000:C1C0:ABCD:0876  0",
  75. "2001:0000:1234: 0000:0000:C1C0:ABCD:0876",
  76. "3ffe:0b00:0000:0001:0000:0000:000a",
  77. "FF02:0000:0000:0000:0000:0000:0000:0000:0001",
  78. "3ffe:b00::1::a",
  79. "1:2:3::4:5::7:8",
  80. "12345::6:7:8",
  81. "1::5:400.2.3.4",
  82. "1::5:260.2.3.4",
  83. "1::5:256.2.3.4",
  84. "1::5:1.256.3.4",
  85. "1::5:1.2.256.4",
  86. "1::5:1.2.3.256",
  87. "1::5:300.2.3.4",
  88. "1::5:1.300.3.4",
  89. "1::5:1.2.300.4",
  90. "1::5:1.2.3.300",
  91. "1::5:900.2.3.4",
  92. "1::5:1.900.3.4",
  93. "1::5:1.2.900.4",
  94. "1::5:1.2.3.900",
  95. "1::5:300.300.300.300",
  96. "1::5:3000.30.30.30",
  97. "1::400.2.3.4",
  98. "1::260.2.3.4",
  99. "1::256.2.3.4",
  100. "1::1.256.3.4",
  101. "1::1.2.256.4",
  102. "1::1.2.3.256",
  103. "1::300.2.3.4",
  104. "1::1.300.3.4",
  105. "1::1.2.300.4",
  106. "1::1.2.3.300",
  107. "1::900.2.3.4",
  108. "1::1.900.3.4",
  109. "1::1.2.900.4",
  110. "1::1.2.3.900",
  111. "1::300.300.300.300",
  112. "1::3000.30.30.30",
  113. "::400.2.3.4",
  114. "::260.2.3.4",
  115. "::256.2.3.4",
  116. "::1.256.3.4",
  117. "::1.2.256.4",
  118. "::1.2.3.256",
  119. "::300.2.3.4",
  120. "::1.300.3.4",
  121. "::1.2.300.4",
  122. "::1.2.3.300",
  123. "::900.2.3.4",
  124. "::1.900.3.4",
  125. "::1.2.900.4",
  126. "::1.2.3.900",
  127. "::300.300.300.300",
  128. "::3000.30.30.30",
  129. "2001:DB8:0:0:8:800:200C:417A:221", # unicast, full
  130. "FF01::101::2" # multicast, compressed
  131. ].each { |invalid_ip|
  132. describe "when the pattern is an invalid IPv6 address such as #{invalid_ip}" do
  133. it "should raise an exception" do
  134. lambda { Puppet::Network::AuthStore::Declaration.new(:allow,invalid_ip) }.should raise_error
  135. end
  136. end
  137. }
  138. [
  139. "1.2.3.4",
  140. "2001:0000:1234:0000:0000:C1C0:ABCD:0876",
  141. "3ffe:0b00:0000:0000:0001:0000:0000:000a",
  142. "FF02:0000:0000:0000:0000:0000:0000:0001",
  143. "0000:0000:0000:0000:0000:0000:0000:0001",
  144. "0000:0000:0000:0000:0000:0000:0000:0000",
  145. "::ffff:192.168.1.26",
  146. "2::10",
  147. "ff02::1",
  148. "fe80::",
  149. "2002::",
  150. "2001:db8::",
  151. "2001:0db8:1234::",
  152. "::ffff:0:0",
  153. "::1",
  154. "::ffff:192.168.1.1",
  155. "1:2:3:4:5:6:7:8",
  156. "1:2:3:4:5:6::8",
  157. "1:2:3:4:5::8",
  158. "1:2:3:4::8",
  159. "1:2:3::8",
  160. "1:2::8",
  161. "1::8",
  162. "1::2:3:4:5:6:7",
  163. "1::2:3:4:5:6",
  164. "1::2:3:4:5",
  165. "1::2:3:4",
  166. "1::2:3",
  167. "1::8",
  168. "::2:3:4:5:6:7:8",
  169. "::2:3:4:5:6:7",
  170. "::2:3:4:5:6",
  171. "::2:3:4:5",
  172. "::2:3:4",
  173. "::2:3",
  174. "::8",
  175. "1:2:3:4:5:6::",
  176. "1:2:3:4:5::",
  177. "1:2:3:4::",
  178. "1:2:3::",
  179. "1:2::",
  180. "1::",
  181. "1:2:3:4:5::7:8",
  182. "1:2:3:4::7:8",
  183. "1:2:3::7:8",
  184. "1:2::7:8",
  185. "1::7:8",
  186. "1:2:3:4:5:6:1.2.3.4",
  187. "1:2:3:4:5::1.2.3.4",
  188. "1:2:3:4::1.2.3.4",
  189. "1:2:3::1.2.3.4",
  190. "1:2::1.2.3.4",
  191. "1::1.2.3.4",
  192. "1:2:3:4::5:1.2.3.4",
  193. "1:2:3::5:1.2.3.4",
  194. "1:2::5:1.2.3.4",
  195. "1::5:1.2.3.4",
  196. "1::5:11.22.33.44",
  197. "fe80::217:f2ff:254.7.237.98",
  198. "fe80::217:f2ff:fe07:ed62",
  199. "2001:DB8:0:0:8:800:200C:417A", # unicast, full
  200. "FF01:0:0:0:0:0:0:101", # multicast, full
  201. "0:0:0:0:0:0:0:1", # loopback, full
  202. "0:0:0:0:0:0:0:0", # unspecified, full
  203. "2001:DB8::8:800:200C:417A", # unicast, compressed
  204. "FF01::101", # multicast, compressed
  205. "::1", # loopback, compressed, non-routable
  206. "::", # unspecified, compressed, non-routable
  207. "0:0:0:0:0:0:13.1.68.3", # IPv4-compatible IPv6 address, full, deprecated
  208. "0:0:0:0:0:FFFF:129.144.52.38", # IPv4-mapped IPv6 address, full
  209. "::13.1.68.3", # IPv4-compatible IPv6 address, compressed, deprecated
  210. "::FFFF:129.144.52.38", # IPv4-mapped IPv6 address, compressed
  211. "2001:0DB8:0000:CD30:0000:0000:0000:0000/60", # full, with prefix
  212. "2001:0DB8::CD30:0:0:0:0/60", # compressed, with prefix
  213. "2001:0DB8:0:CD30::/60", # compressed, with prefix #2
  214. "::/128", # compressed, unspecified address type, non-routable
  215. "::1/128", # compressed, loopback address type, non-routable
  216. "FF00::/8", # compressed, multicast address type
  217. "FE80::/10", # compressed, link-local unicast, non-routable
  218. "FEC0::/10", # compressed, site-local unicast, deprecated
  219. "127.0.0.1", # standard IPv4, loopback, non-routable
  220. "0.0.0.0", # standard IPv4, unspecified, non-routable
  221. "255.255.255.255", # standard IPv4
  222. "fe80:0000:0000:0000:0204:61ff:fe9d:f156",
  223. "fe80:0:0:0:204:61ff:fe9d:f156",
  224. "fe80::204:61ff:fe9d:f156",
  225. "fe80:0000:0000:0000:0204:61ff:254.157.241.086",
  226. "fe80:0:0:0:204:61ff:254.157.241.86",
  227. "fe80::204:61ff:254.157.241.86",
  228. "::1",
  229. "fe80::",
  230. "fe80::1"
  231. ].each { |ip|
  232. describe "when the pattern is a valid IP such as #{ip}" do
  233. before :each do
  234. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,ip)
  235. end
  236. it "should match the specified IP" do
  237. @declaration.should be_match('www.testsite.org',ip)
  238. end
  239. it "should not match other IPs" do
  240. @declaration.should_not be_match('www.testsite.org','200.101.99.98')
  241. end
  242. end unless ip =~ /:.*\./ # Hybrid IPs aren't supported by ruby's ipaddr
  243. }
  244. {
  245. 'spirit.mars.nasa.gov' => 'a PQDN',
  246. 'ratchet.2ndsiteinc.com' => 'a PQDN with digits',
  247. 'a.c.ru' => 'a PQDN with short segments',
  248. }.each {|pqdn,desc|
  249. describe "when the pattern is #{desc}" do
  250. before :each do
  251. @host = pqdn
  252. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,@host)
  253. end
  254. it "should match the specified PQDN" do
  255. @declaration.should be_match(@host,'200.101.99.98')
  256. end
  257. it "should not match a similar FQDN" do
  258. pending "FQDN consensus"
  259. @declaration.should_not be_match(@host+'.','200.101.99.98')
  260. end
  261. end
  262. }
  263. ['abc.12seps.edu.phisher.biz','www.google.com','slashdot.org'].each { |host|
  264. (1...(host.split('.').length)).each { |n|
  265. describe "when the pattern is #{"*."+host.split('.')[-n,n].join('.')}" do
  266. before :each do
  267. @pattern = "*."+host.split('.')[-n,n].join('.')
  268. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,@pattern)
  269. end
  270. it "should match #{host}" do
  271. @declaration.should be_match(host,'1.2.3.4')
  272. end
  273. it "should not match www.testsite.gov" do
  274. @declaration.should_not be_match('www.testsite.gov','200.101.99.98')
  275. end
  276. it "should not match hosts that differ in the first non-wildcard segment" do
  277. other = host.split('.')
  278. other[-n].succ!
  279. @declaration.should_not be_match(other.join('.'),'1.2.3.4')
  280. end
  281. end
  282. }
  283. }
  284. describe "when the pattern is a FQDN" do
  285. before :each do
  286. @host = 'spirit.mars.nasa.gov.'
  287. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,@host)
  288. end
  289. it "should match the specified FQDN" do
  290. pending "FQDN consensus"
  291. @declaration.should be_match(@host,'200.101.99.98')
  292. end
  293. it "should not match a similar PQDN" do
  294. @declaration.should_not be_match(@host[0..-2],'200.101.99.98')
  295. end
  296. end
  297. describe "when the pattern is an opaque string with a back reference" do
  298. before :each do
  299. @host = 'c216f41a-f902-4bfb-a222-850dd957bebb'
  300. @item = "/catalog/#{@host}"
  301. @pattern = %{^/catalog/([^/]+)$}
  302. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,'$1')
  303. end
  304. it "should match an IP with the appropriate interpolation" do
  305. @declaration.interpolate(@item.match(@pattern)).should be_match(@host,'10.0.0.5')
  306. end
  307. end
  308. describe "when the pattern is an opaque string with a back reference and the matched data contains dots" do
  309. before :each do
  310. @host = 'admin.mgmt.nym1'
  311. @item = "/catalog/#{@host}"
  312. @pattern = %{^/catalog/([^/]+)$}
  313. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,'$1')
  314. end
  315. it "should match a name with the appropriate interpolation" do
  316. @declaration.interpolate(@item.match(@pattern)).should be_match(@host,'10.0.0.5')
  317. end
  318. end
  319. describe "when the pattern is an opaque string with a back reference and the matched data contains dots with an initial prefix that looks like an IP address" do
  320. before :each do
  321. @host = '01.admin.mgmt.nym1'
  322. @item = "/catalog/#{@host}"
  323. @pattern = %{^/catalog/([^/]+)$}
  324. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,'$1')
  325. end
  326. it "should match a name with the appropriate interpolation" do
  327. @declaration.interpolate(@item.match(@pattern)).should be_match(@host,'10.0.0.5')
  328. end
  329. end
  330. describe "when comparing patterns" do
  331. before :each do
  332. @ip = Puppet::Network::AuthStore::Declaration.new(:allow,'127.0.0.1')
  333. @host_name = Puppet::Network::AuthStore::Declaration.new(:allow,'www.hard_knocks.edu')
  334. @opaque = Puppet::Network::AuthStore::Declaration.new(:allow,'hey_dude')
  335. end
  336. it "should consider ip addresses before host names" do
  337. (@ip < @host_name).should be_true
  338. end
  339. it "should consider ip addresses before opaque strings" do
  340. (@ip < @opaque).should be_true
  341. end
  342. it "should consider host_names before opaque strings" do
  343. (@host_name < @opaque).should be_true
  344. end
  345. end
  346. end