PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/test/test_cipher.rb

https://bitbucket.org/keltia/old-crypto-spec
Ruby | 675 lines | 444 code | 102 blank | 129 comment | 1 complexity | 8bc52d7bb7fc0d4b8583979783a6068c MD5 | raw file
  1. # $Id$
  2. require 'test/unit'
  3. require 'yaml'
  4. require "key"
  5. require "cipher"
  6. module TestCipher
  7. # == TestSimpleCipher
  8. #
  9. class TestSimpleCipher < Test::Unit::TestCase
  10. # === setup
  11. #
  12. def setup
  13. @cipher = Cipher::SimpleCipher.new
  14. end # -- setup
  15. # === test_init
  16. #
  17. def test_init
  18. assert_not_nil(@cipher)
  19. end # -- test_init
  20. # === test_encode
  21. #
  22. def test_encode
  23. ct = @cipher.encode("plain text")
  24. assert_not_nil(ct)
  25. assert_equal ct, "plain text"
  26. end # -- test_encode
  27. # === test_decode
  28. #
  29. def test_decode
  30. ct = @cipher.decode("plain text")
  31. assert_not_nil(ct)
  32. assert_equal ct, "plain text"
  33. end # -- test_decode
  34. end # -- TestSimpleCipher
  35. # == TestSubstitution
  36. #
  37. class TestSubstitution < Test::Unit::TestCase
  38. # === setup
  39. #
  40. def setup
  41. @cipher = Cipher::Substitution.new("FOOBAR")
  42. end # -- setup
  43. # === test_init
  44. #
  45. def test_init
  46. assert_not_nil(@cipher.key)
  47. assert_not_nil(@cipher.key.alpha)
  48. assert_not_nil(@cipher.key.ralpha)
  49. end # -- test_init
  50. # === test_null_key
  51. #
  52. def test_null_key
  53. cipher = Cipher::Substitution.new()
  54. assert_not_nil cipher
  55. end # -- test_null_key
  56. # === test_encode_null
  57. #
  58. def test_encode_null
  59. cipher = Cipher::Substitution.new()
  60. pt = "TEST"
  61. ct = cipher.encode(pt)
  62. assert_not_nil(ct)
  63. assert_equal pt, ct, "Should degrade into 'give back plaintext'."
  64. end # -- test_encode_null
  65. # === test_encode_null
  66. #
  67. def test_decode_null
  68. cipher = Cipher::Substitution.new()
  69. ct = "TEST"
  70. pt = cipher.decode(ct)
  71. assert_not_nil(pt)
  72. assert_equal ct, pt, "Should degrade into 'give back plaintext'."
  73. end # -- test_decode_null
  74. # === test_both
  75. #
  76. def test_both
  77. cipher = Cipher::Substitution.new()
  78. assert_equal "PLAINTEXT", cipher.decode(cipher.encode("PLAINTEXT"))
  79. end # -- test_both
  80. end # -- TestSubstitution
  81. # == TestCipherCaesar
  82. #
  83. class TestCipherCaesar < Test::Unit::TestCase
  84. # === setup
  85. #
  86. def setup
  87. @cipher = Cipher::Caesar.new
  88. end # -- setup
  89. # === test_setup
  90. #
  91. def test_setup
  92. assert_not_nil @cipher
  93. assert_not_nil @cipher.key
  94. assert_equal 3, @cipher.key.offset
  95. end
  96. # === test_encode
  97. #
  98. def test_encode
  99. pt = "ABCDE"
  100. ct = @cipher.encode(pt)
  101. assert_not_nil(ct)
  102. assert_equal ct, "DEFGH"
  103. end # -- test_encode
  104. def test_decode
  105. ct = "ABCDE"
  106. pt = @cipher.decode(ct)
  107. assert_not_nil(pt)
  108. assert_equal pt, "XYZAB"
  109. end
  110. end # -- TestCipherCaesar
  111. # == TestCipherCaesar7
  112. #
  113. class TestCipherCaesar7 < Test::Unit::TestCase
  114. # === setup
  115. #
  116. def setup
  117. @cipher = Cipher::Caesar.new(7)
  118. end # -- setup
  119. # === test_setup
  120. #
  121. def test_setup
  122. assert_not_nil @cipher
  123. assert_not_nil @cipher.key
  124. assert_equal 7, @cipher.key.offset
  125. end
  126. # === test_encode
  127. #
  128. def test_encode
  129. pt = "ABCDE"
  130. ct = @cipher.encode(pt)
  131. assert_not_nil(ct)
  132. assert_equal ct, "HIJKL"
  133. end # -- test_encode
  134. def test_decode
  135. ct = "ABCDE"
  136. pt = @cipher.decode(ct)
  137. assert_not_nil(pt)
  138. assert_equal pt, "TUVWX"
  139. end
  140. end # -- TestCipherCaesar_7
  141. # == TestCipherRot13
  142. #
  143. class TestCipherRot13 < Test::Unit::TestCase
  144. # === setup
  145. #
  146. def setup
  147. @cipher = Cipher::Rot13.new
  148. end # -- setup
  149. # === test_setup
  150. #
  151. def test_setup
  152. assert_not_nil @cipher
  153. assert_not_nil @cipher.key
  154. assert_equal 13, @cipher.key.offset
  155. end
  156. # === test_encode
  157. #
  158. def test_encode
  159. pt = "ABCDE"
  160. ct = @cipher.encode(pt)
  161. assert_not_nil(ct)
  162. assert_equal ct, "NOPQR"
  163. end # -- test_encode
  164. def test_decode
  165. ct = "ABCDE"
  166. pt = @cipher.decode(ct)
  167. assert_not_nil(pt)
  168. assert_equal pt, "NOPQR"
  169. end
  170. end # -- TestCipherRot13
  171. # == TestTransposition
  172. #
  173. class TestTransposition < Test::Unit::TestCase
  174. # === setup
  175. #
  176. def setup
  177. @data = Hash.new
  178. File.open("test/test_cipher_transp.yaml") do |fh|
  179. @data = YAML.load(fh)
  180. end
  181. @keys = @data["keys"]
  182. end # -- setup
  183. # === test_encode
  184. #
  185. def test_encode
  186. pt = @data["plain"]
  187. @keys.keys.each do |word|
  188. cipher = Cipher::Transposition.new(word)
  189. assert_not_nil(cipher)
  190. ct = cipher.encode(pt)
  191. assert_not_nil(ct)
  192. assert_equal @keys[word]["ct"], ct, "key is #{word}"
  193. end
  194. end # -- test_encode
  195. # === test_decode
  196. #
  197. def test_decode
  198. plain = @data["plain"]
  199. @keys.keys.each do |word|
  200. cipher = Cipher::Transposition.new(word)
  201. assert_not_nil(cipher)
  202. pt = cipher.decode(@keys[word]["ct"])
  203. assert_not_nil(pt)
  204. assert_equal plain, pt, "key is #{word}"
  205. end
  206. end # -- test_decode
  207. end # -- TestTransposition
  208. class TestDisrupted < Test::Unit::TestCase
  209. # === setup
  210. #
  211. def setup
  212. @data = Hash.new
  213. File.open("test/test_cipher_disrupted.yaml") do |fh|
  214. @data = YAML.load(fh)
  215. end
  216. @keys = @data["keys"] || []
  217. end # -- setup
  218. def test_init
  219. @keys.keys.each do |key|
  220. cipher = Cipher::DisruptedTransposition.new(key)
  221. assert_not_nil cipher
  222. assert_not_nil cipher.key
  223. assert_equal cipher.key.length, key.length
  224. end
  225. end
  226. def test_encode
  227. pt = @data["plain"]
  228. @keys.keys.each do |key|
  229. cipher = Cipher::DisruptedTransposition.new(key)
  230. assert_not_nil cipher
  231. ct = cipher.encode(pt)
  232. assert_not_nil ct
  233. assert_equal pt.length, ct.length
  234. assert_equal @keys[key]["ct"], ct
  235. end
  236. end
  237. end # -- TestDisrupted
  238. # == TestPolybius
  239. #
  240. class TestPolybius < Test::Unit::TestCase
  241. # === setup
  242. #
  243. def setup
  244. @data = Hash.new
  245. File.open("test/test_cipher_polybius.yaml") do |fh|
  246. @data = YAML.load(fh)
  247. end
  248. @keys = @data["keys"]
  249. end # -- setup
  250. # === test_encode
  251. #
  252. def test_encode
  253. pt = @data["plain"]
  254. @keys.keys.each do |word|
  255. cipher = Cipher::Polybius.new(word, Key::SQKey::SQ_NUMBERS)
  256. assert_not_nil(cipher)
  257. ct = cipher.encode(pt)
  258. assert_not_nil(ct)
  259. assert_equal @keys[word]["ct"], ct, "key is #{word}"
  260. end
  261. end # -- test_encode
  262. # === test_decode
  263. #
  264. def test_decode
  265. plain = @data["plain"]
  266. @keys.keys.each do |word|
  267. cipher = Cipher::Polybius.new(word, Key::SQKey::SQ_NUMBERS)
  268. assert_not_nil(cipher)
  269. assert_raise(ArgumentError) { cipher.decode("AAA") }
  270. pt = cipher.decode(@keys[word]["ct"])
  271. assert_not_nil(pt)
  272. assert_equal plain, pt, "key is #{word}\ncipher is #{@keys[word]["ct"]}"
  273. end
  274. end # -- test_decode
  275. end # -- TestPolybius
  276. # == TestStraddlingCheckerboard
  277. #
  278. class TestStraddlingCheckerboard < Test::Unit::TestCase
  279. # === setup
  280. #
  281. def setup
  282. @data = Hash.new
  283. File.open("test/test_cipher_straddling.yaml") do |fh|
  284. @data = YAML.load(fh)
  285. end
  286. @keys = @data["keys"]
  287. end # -- setup
  288. # === test_encode
  289. #
  290. def test_encode
  291. pt = @data["plain"]
  292. @keys.keys.each do |word|
  293. cipher = Cipher::StraddlingCheckerboard.new(word)
  294. assert_not_nil(cipher)
  295. ct = cipher.encode(pt)
  296. assert_not_nil(ct)
  297. assert_equal @keys[word]["ct"], ct, "key is #{word}"
  298. end
  299. end # -- test_encode
  300. # === test_decode
  301. #
  302. def test_decode
  303. plain = @data["plain"]
  304. @keys.keys.each do |word|
  305. cipher = Cipher::StraddlingCheckerboard.new(word)
  306. assert_not_nil(cipher)
  307. pt = cipher.decode(@keys[word]["ct"])
  308. assert_not_nil(pt)
  309. assert_equal plain, pt, "key is #{word}\ncipher is #{@keys[word]["ct"]}"
  310. end
  311. end # -- test_decode
  312. end # -- TestStraddlingCheckerboard
  313. # == TestNihilistT
  314. #
  315. class TestNihilistT < Test::Unit::TestCase
  316. # === setup
  317. #
  318. def setup
  319. @data = Hash.new
  320. File.open("test/test_cipher_nihilistt.yaml") do |fh|
  321. @data = YAML.load(fh)
  322. end
  323. @keys = @data["keys"]
  324. end # -- setup
  325. # === test_encode
  326. #
  327. def test_encode
  328. pt = @data["plain"]
  329. @keys.keys.each do |word|
  330. s, t = word.split(%r{,})
  331. cipher = Cipher::NihilistT.new(s, t)
  332. assert_not_nil(cipher)
  333. ct = cipher.encode(pt)
  334. assert_not_nil(ct)
  335. assert_equal @keys[word]["ct"], ct, "key is #{word}"
  336. end
  337. end # -- test_encode
  338. # === test_decode
  339. #
  340. def test_decode
  341. plain = @data["plain"]
  342. @keys.keys.each do |word|
  343. s, t = word.split(%{,})
  344. cipher = Cipher::NihilistT.new(s, t)
  345. assert_not_nil(cipher)
  346. pt = cipher.decode(@keys[word]["ct"])
  347. assert_not_nil(pt)
  348. assert_equal plain, pt, "key is #{word}\ncipher is #{@keys[word]["ct"]}"
  349. end
  350. end # -- test_decode
  351. end # -- TestNihilistT
  352. # == TestADFGVX
  353. #
  354. class TestADFGVX < Test::Unit::TestCase
  355. # === setup
  356. #
  357. def setup
  358. @data = Hash.new
  359. File.open("test/test_cipher_adfgvx.yaml") do |fh|
  360. @data = YAML.load(fh)
  361. end
  362. @keys = @data["keys"]
  363. end # -- setup
  364. # === test_encode
  365. #
  366. def test_encode
  367. pt = @data["plain"]
  368. @keys.keys.each do |word|
  369. s, t = word.split(%r{,})
  370. cipher = Cipher::ADFGVX.new(s, t)
  371. assert_not_nil(cipher)
  372. ct = cipher.encode(pt)
  373. assert_not_nil(ct)
  374. assert_equal @keys[word]["ct"], ct, "key is #{word}"
  375. end
  376. end # -- test_encode
  377. # === test_decode
  378. #
  379. def test_decode
  380. plain = @data["plain"]
  381. @keys.keys.each do |word|
  382. s, t = word.split(%{,})
  383. cipher = Cipher::ADFGVX.new(s, t)
  384. assert_not_nil(cipher)
  385. pt = cipher.decode(@keys[word]["ct"])
  386. assert_not_nil(pt)
  387. assert_equal plain, pt, "key is #{word}\ncipher is #{@keys[word]["ct"]}"
  388. end
  389. end # -- test_decode
  390. end # -- TestADFGVX
  391. # == TestPlayfair_J
  392. #
  393. class TestPlayfair_J < Test::Unit::TestCase
  394. # === setup
  395. #
  396. def setup
  397. File.open("test/test_cipher_playfair_j.yaml") do |fh|
  398. @data = YAML.load(fh)
  399. end
  400. @keys = @data["keys"]
  401. end # -- setup
  402. # === test_encode
  403. #
  404. def test_encode
  405. pt = @data["plain"]
  406. @keys.keys.each do |word|
  407. cipher = Cipher::Playfair.new(word, Key::Playfair::WITH_J)
  408. assert_not_nil(cipher)
  409. ct = cipher.encode(pt)
  410. assert_not_nil(ct)
  411. assert_equal @keys[word]["ct"], ct, "key is #{word}"
  412. end
  413. end # -- test_encode
  414. # === test_encode_padding
  415. #
  416. def test_encode_padding
  417. pt = "PJRST"
  418. cipher = Cipher::Playfair.new("FOOBAR", Key::Playfair::WITH_J)
  419. ct = cipher.encode(pt)
  420. assert_equal "WPBUSY", ct, "Text should be padded with X"
  421. end # -- test_encode_padding
  422. # === test_encode_invalid
  423. #
  424. def test_encode_invalid
  425. pt = "PQRJTS"
  426. cipher = Cipher::Playfair.new("FOOBAR", Key::Playfair::WITH_J)
  427. assert_raise(ArgumentError) { cipher.encode(pt) }
  428. end # -- test_encode_invalid
  429. # === test_decode
  430. #
  431. def test_decode
  432. plain = @data["plain"]
  433. @keys.keys.each do |word|
  434. cipher = Cipher::Playfair.new(word, Key::Playfair::WITH_J)
  435. assert_not_nil(cipher)
  436. assert_raise(ArgumentError) { cipher.decode("AAA") }
  437. pt = cipher.decode(@keys[word]["ct"])
  438. assert_not_nil(pt)
  439. assert_equal plain, pt, "key: #{word}\ncipher: #{@keys[word]["ct"]}"
  440. end
  441. end # -- test_decode
  442. end # -- TestPlayfair_J
  443. # == TestPlayfair_Q
  444. #
  445. class TestPlayfair_Q < Test::Unit::TestCase
  446. # === setup
  447. #
  448. def setup
  449. File.open("test/test_cipher_playfair_q.yaml") do |fh|
  450. @data = YAML.load(fh)
  451. end
  452. @keys = @data["keys"]
  453. end # -- setup
  454. # === test_encode
  455. #
  456. def test_encode
  457. pt = @data["plain"]
  458. @keys.keys.each do |word|
  459. cipher = Cipher::Playfair.new(word)
  460. assert_not_nil(cipher)
  461. ct = cipher.encode(pt)
  462. assert_not_nil(ct)
  463. assert_equal @keys[word]["ct"], ct, "key is #{word}"
  464. end
  465. end # -- test_encode
  466. # === test_encode_padding
  467. #
  468. def test_encode_padding
  469. pt = "PQRST"
  470. cipher = Cipher::Playfair.new("FOOBAR")
  471. ct = cipher.encode(pt)
  472. assert_equal "QSBUSY", ct, "Text is padded with X"
  473. end # -- test_encode_padding
  474. # === test_encode_invalid
  475. #
  476. def test_encode_invalid
  477. pt = "PQRJTS"
  478. cipher = Cipher::Playfair.new("FOOBAR")
  479. assert_raise(ArgumentError) { cipher.encode(pt) }
  480. end # -- test_encode_invalid
  481. # === test_decode
  482. #
  483. def test_decode
  484. eplain = @data["eplain"]
  485. @keys.keys.each do |word|
  486. cipher = Cipher::Playfair.new(word)
  487. assert_not_nil(cipher)
  488. assert_raise(ArgumentError) { cipher.decode("AAA") }
  489. pt = cipher.decode(@keys[word]["ct"])
  490. assert_not_nil(pt)
  491. assert_equal eplain, pt, "key: #{word}\ncipher: #{@keys[word]["ct"]}"
  492. end
  493. end # -- test_decode
  494. # === test_decode_invalid
  495. #
  496. def test_decode_invalid
  497. ct = "PQRJTS"
  498. cipher = Cipher::Playfair.new("FOOBAR")
  499. assert_raise(ArgumentError) { cipher.decode(ct) }
  500. end # -- test_decode_invalid
  501. end # -- TestPlayfair_Q
  502. class TestWheatstone < Test::Unit::TestCase
  503. # === setup
  504. #
  505. def setup
  506. File.open("test/test_cipher_wheat.yaml") do |fh|
  507. @data = YAML.load(fh)
  508. end
  509. @keys = @data["keys"]
  510. end # -- setup
  511. # === test_encode
  512. #
  513. def test_encode
  514. @keys.each_value do |type|
  515. start = type["start"]
  516. cw = type["cw"]
  517. pw = type["pw"]
  518. pt = type["pt"]
  519. assert_equal type['ptr'], pt.replace_double
  520. cipher = Cipher::Wheatstone.new(start, pw, cw)
  521. ct = cipher.encode(pt)
  522. assert_equal type["ct"], ct
  523. end
  524. end # -- test_encode
  525. # === test_decode
  526. #
  527. def test_decode
  528. @keys.each_value do |type|
  529. start = type["start"]
  530. cw = type["cw"]
  531. pw = type["pw"]
  532. ct = type["ct"]
  533. cipher = Cipher::Wheatstone.new(start, pw, cw)
  534. pt = cipher.decode(ct)
  535. assert_equal type["ptr"], pt
  536. end
  537. end # -- test_encode
  538. end # -- TestWheatstone
  539. class TestChaoCipher < Test::Unit::TestCase
  540. # === setup
  541. #
  542. def setup
  543. File.open("test/test_cipher_chao.yaml") do |fh|
  544. @data = YAML.load(fh)
  545. end
  546. @keys = @data["keys"]
  547. end # -- setup
  548. # === test_encode
  549. #
  550. def test_encode
  551. @keys.each_value do |type|
  552. pt = type["pt"]
  553. cw = type["cw"]
  554. pw = type["pw"]
  555. cipher = Cipher::ChaoCipher.new(pw, cw)
  556. ct = cipher.encode(pt)
  557. assert_equal type["ct"], ct
  558. end
  559. end # -- test_encode
  560. # === test_decode
  561. #
  562. def test_decode
  563. @keys.each_value do |type|
  564. ct = type["ct"]
  565. cw = type["cw"]
  566. pw = type["pw"]
  567. cipher = Cipher::ChaoCipher.new(pw, cw)
  568. pt = cipher.decode(ct)
  569. assert_equal type["pt"], pt
  570. end
  571. end # -- test_encode
  572. end # -- TestChaoCipher
  573. end # -- TestCipher