zold 0.0.8 → 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE.md +12 -0
  3. data/.github/PULL_REQUEST_TEMPLATE.md +11 -0
  4. data/.rubocop.yml +9 -1
  5. data/.simplecov +2 -2
  6. data/.travis.yml +1 -1
  7. data/Gemfile +1 -1
  8. data/LICENSE.txt +1 -1
  9. data/Procfile +1 -1
  10. data/README.md +208 -101
  11. data/Rakefile +2 -1
  12. data/bin/zold +135 -54
  13. data/features/cli.feature +1 -1
  14. data/features/step_definitions/steps.rb +1 -1
  15. data/features/support/env.rb +1 -1
  16. data/fixtures/scripts/push-and-pull.sh +35 -0
  17. data/lib/zold.rb +2 -2
  18. data/lib/zold/amount.rb +10 -2
  19. data/lib/zold/commands/{send.rb → clean.rb} +16 -16
  20. data/lib/zold/commands/create.rb +7 -5
  21. data/lib/zold/commands/diff.rb +59 -0
  22. data/lib/zold/commands/fetch.rb +74 -0
  23. data/lib/zold/commands/{pull.rb → list.rb} +11 -17
  24. data/lib/zold/commands/merge.rb +50 -0
  25. data/lib/zold/commands/node.rb +94 -0
  26. data/lib/zold/commands/pay.rb +58 -0
  27. data/lib/zold/commands/{check.rb → propagate.rb} +19 -20
  28. data/lib/zold/commands/push.rb +12 -12
  29. data/lib/zold/commands/remote.rb +115 -0
  30. data/lib/zold/commands/{balance.rb → show.rb} +11 -7
  31. data/lib/zold/copies.rb +126 -0
  32. data/lib/zold/http.rb +70 -0
  33. data/lib/zold/id.rb +8 -3
  34. data/lib/zold/key.rb +2 -2
  35. data/lib/zold/log.rb +51 -2
  36. data/lib/zold/node/farm.rb +81 -0
  37. data/lib/zold/node/front.rb +94 -46
  38. data/lib/zold/patch.rb +58 -0
  39. data/lib/zold/remotes.rb +106 -0
  40. data/lib/zold/score.rb +101 -0
  41. data/lib/zold/signature.rb +48 -0
  42. data/lib/zold/version.rb +3 -3
  43. data/lib/zold/wallet.rb +87 -55
  44. data/lib/zold/wallets.rb +13 -6
  45. data/resources/remotes +1 -0
  46. data/test/commands/test_clean.rb +41 -0
  47. data/test/commands/test_create.rb +2 -2
  48. data/test/commands/test_diff.rb +61 -0
  49. data/test/commands/test_fetch.rb +65 -0
  50. data/test/commands/test_list.rb +42 -0
  51. data/test/commands/test_merge.rb +62 -0
  52. data/test/commands/test_node.rb +56 -0
  53. data/test/commands/{test_send.rb → test_pay.rb} +10 -11
  54. data/test/commands/test_remote.rb +60 -0
  55. data/test/commands/{test_balance.rb → test_show.rb} +6 -8
  56. data/test/node/fake_node.rb +73 -0
  57. data/test/node/test_farm.rb +34 -0
  58. data/test/node/test_front.rb +26 -57
  59. data/test/test__helper.rb +1 -1
  60. data/test/test_amount.rb +10 -2
  61. data/test/test_copies.rb +73 -0
  62. data/test/test_http.rb +42 -0
  63. data/test/test_id.rb +2 -2
  64. data/test/test_key.rb +10 -10
  65. data/test/test_patch.rb +59 -0
  66. data/test/test_remotes.rb +72 -0
  67. data/test/test_score.rb +79 -0
  68. data/test/test_signature.rb +45 -0
  69. data/test/test_wallet.rb +18 -35
  70. data/test/test_wallets.rb +14 -3
  71. data/test/test_zold.rb +52 -5
  72. data/zold.gemspec +5 -3
  73. metadata +92 -21
  74. data/CONTRIBUTING.md +0 -19
  75. data/views/index.haml +0 -6
  76. data/views/layout.haml +0 -26
  77. data/views/not_found.haml +0 -3
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2018 Zerocracy, Inc.
1
+ # Copyright (c) 2018 Yegor Bugayenko
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the 'Software'), to deal
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2018 Zerocracy, Inc.
1
+ # Copyright (c) 2018 Yegor Bugayenko
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the 'Software'), to deal
@@ -24,7 +24,7 @@ require_relative '../lib/zold/amount.rb'
24
24
 
25
25
  # Amount test.
26
26
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
27
- # Copyright:: Copyright (c) 2018 Zerocracy, Inc.
27
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
28
28
  # License:: MIT
29
29
  class TestAmount < Minitest::Test
30
30
  def test_parses_zld
@@ -42,4 +42,12 @@ class TestAmount < Minitest::Test
42
42
  "#{amount} is not equal to '53.64ZLD'"
43
43
  )
44
44
  end
45
+
46
+ def test_compares_amounts
47
+ amount = Zold::Amount.new(coins: 700_000_000)
48
+ assert(
49
+ amount > Zold::Amount::ZERO,
50
+ "#{amount} is not greater than zero"
51
+ )
52
+ end
45
53
  end
@@ -0,0 +1,73 @@
1
+ # Copyright (c) 2018 Yegor Bugayenko
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the 'Software'), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ require 'minitest/autorun'
22
+ require 'tmpdir'
23
+ require 'time'
24
+ require_relative '../lib/zold/copies.rb'
25
+
26
+ # Copies test.
27
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
28
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
29
+ # License:: MIT
30
+ class TestCopies < Minitest::Test
31
+ def test_adds_copies
32
+ Dir.mktmpdir 'test' do |dir|
33
+ copies = Zold::Copies.new(File.join(dir, 'my/a/copies'))
34
+ copies.add('hello 1', '192.168.0.1', 80, 1)
35
+ copies.add('hello 2', '192.168.0.2', 80, 3)
36
+ copies.add('hello 2', '192.168.0.3', 80, 7)
37
+ copies.add('hello 1', '192.168.0.4', 80, 10)
38
+ assert(copies.all.count == 2, "#{copies.all.count} is not equal to 2")
39
+ assert(copies.all.find { |c| c[:name] == '1' }[:score] == 11)
40
+ end
41
+ end
42
+
43
+ def test_lists_empty_dir
44
+ Dir.mktmpdir 'test' do |dir|
45
+ copies = Zold::Copies.new(File.join(dir, 'xxx'))
46
+ assert(copies.all.empty?, "#{copies.all.count} is not zero")
47
+ end
48
+ end
49
+
50
+ def test_overwrites_host
51
+ Dir.mktmpdir 'test' do |dir|
52
+ copies = Zold::Copies.new(File.join(dir, 'my/a/copies'))
53
+ host = 'b1.zold.io'
54
+ copies.add('z1', host, 80, 5)
55
+ copies.add('z1', host, 80, 6)
56
+ copies.add('z1', host, 80, 7)
57
+ assert(copies.all.count == 1, "#{copies.all.count} is not equal to 1")
58
+ assert(copies.all[0][:score] == 7, "#{copies.all[0][:score]} is not 7")
59
+ end
60
+ end
61
+
62
+ def test_cleans_copies
63
+ Dir.mktmpdir 'test' do |dir|
64
+ copies = Zold::Copies.new(dir)
65
+ copies.add('h1', 'zold.io', 50, 80, Time.now - 25 * 60)
66
+ copies.add('h1', 'zold.io', 33, 80, Time.now - 26 * 60)
67
+ assert(File.exist?(File.join(dir, '1')))
68
+ copies.clean
69
+ assert(copies.all.empty?, "#{copies.all.count} is not empty")
70
+ assert(!File.exist?(File.join(dir, '1')))
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,42 @@
1
+ # Copyright (c) 2018 Yegor Bugayenko
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the 'Software'), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ require 'minitest/autorun'
22
+ require 'tmpdir'
23
+ require 'uri'
24
+ require 'webmock/minitest'
25
+ require_relative '../lib/zold/http.rb'
26
+
27
+ # Http test.
28
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
29
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
30
+ # License:: MIT
31
+ class TestHttp < Minitest::Test
32
+ def test_pings_broken_uri
33
+ stub_request(:get, 'http://bad-host/').to_return(status: 500)
34
+ assert_equal('500', Zold::Http.new(URI('http://bad-host/')).get.code)
35
+ end
36
+
37
+ def test_pings_live_uri
38
+ stub_request(:get, 'http://good-host/').to_return(status: 200)
39
+ res = Zold::Http.new(URI('http://good-host/')).get
40
+ assert_equal('200', res.code)
41
+ end
42
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2018 Zerocracy, Inc.
1
+ # Copyright (c) 2018 Yegor Bugayenko
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the 'Software'), to deal
@@ -24,7 +24,7 @@ require_relative '../lib/zold/id.rb'
24
24
 
25
25
  # ID test.
26
26
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
27
- # Copyright:: Copyright (c) 2018 Zerocracy, Inc.
27
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
28
28
  # License:: MIT
29
29
  class TestId < Minitest::Test
30
30
  def test_generates_new_id
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2018 Zerocracy, Inc.
1
+ # Copyright (c) 2018 Yegor Bugayenko
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the 'Software'), to deal
@@ -24,21 +24,21 @@ require_relative '../lib/zold/key.rb'
24
24
 
25
25
  # Key test.
26
26
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
27
- # Copyright:: Copyright (c) 2018 Zerocracy, Inc.
27
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
28
28
  # License:: MIT
29
29
  class TestKey < Minitest::Test
30
30
  def test_reads_public_rsa
31
31
  key = Zold::Key.new(file: 'fixtures/id_rsa.pub')
32
- assert key.to_pub.start_with?('MIICI')
33
- assert key.to_pub.end_with?('EAAQ==')
34
- assert !key.to_pub.include?("\n")
35
- assert Zold::Key.new(text: key.to_pub).to_pub.start_with?('MIICI')
32
+ assert(key.to_pub.start_with?('MIICI'))
33
+ assert(key.to_pub.end_with?('EAAQ=='))
34
+ assert(!key.to_pub.include?("\n"))
35
+ assert(Zold::Key.new(text: key.to_pub).to_pub.start_with?('MIICI'))
36
36
  end
37
37
 
38
38
  def test_reads_private_rsa
39
39
  key = Zold::Key.new(file: 'fixtures/id_rsa')
40
- assert key.to_pub.start_with?('MIIJJ')
41
- assert key.to_pub.end_with?('Sg==')
40
+ assert(key.to_pub.start_with?('MIIJJ'))
41
+ assert(key.to_pub.end_with?('Sg=='))
42
42
  end
43
43
 
44
44
  def test_signs_and_verifies
@@ -46,7 +46,7 @@ class TestKey < Minitest::Test
46
46
  pvt = Zold::Key.new(file: 'fixtures/id_rsa')
47
47
  text = 'How are you, my friend?'
48
48
  signature = pvt.sign(text)
49
- assert pub.verify(signature, text)
49
+ assert(pub.verify(signature, text))
50
50
  end
51
51
 
52
52
  def test_signs_and_verifies_with_random_key
@@ -59,7 +59,7 @@ class TestKey < Minitest::Test
59
59
  pvt = Zold::Key.new(file: file)
60
60
  text = 'How are you doing, dude?'
61
61
  signature = pvt.sign(text)
62
- assert pub.verify(signature, text)
62
+ assert(pub.verify(signature, text))
63
63
  end
64
64
  end
65
65
  end
@@ -0,0 +1,59 @@
1
+ # Copyright (c) 2018 Yegor Bugayenko
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the 'Software'), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ require 'minitest/autorun'
22
+ require 'tmpdir'
23
+ require_relative '../lib/zold/key.rb'
24
+ require_relative '../lib/zold/id.rb'
25
+ require_relative '../lib/zold/wallet.rb'
26
+ require_relative '../lib/zold/amount.rb'
27
+ require_relative '../lib/zold/patch.rb'
28
+
29
+ # Patch test.
30
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
32
+ # License:: MIT
33
+ class TestPatch < Minitest::Test
34
+ def test_builds_patch
35
+ Dir.mktmpdir 'test' do |dir|
36
+ id = Zold::Id.new
37
+ key = Zold::Key.new(file: 'fixtures/id_rsa')
38
+ first = Zold::Wallet.new(File.join(dir, 'first'))
39
+ second = Zold::Wallet.new(File.join(dir, 'second'))
40
+ third = Zold::Wallet.new(File.join(dir, 'third'))
41
+ first.init(id, Zold::Key.new(file: 'fixtures/id_rsa.pub'))
42
+ File.write(second.path, File.read(first.path))
43
+ first.sub(Zold::Amount.new(zld: 39), Zold::Id.new, key)
44
+ first.sub(Zold::Amount.new(zld: 11), Zold::Id.new, key)
45
+ first.sub(Zold::Amount.new(zld: 3), Zold::Id.new, key)
46
+ second.sub(Zold::Amount.new(zld: 44), Zold::Id.new, key)
47
+ File.write(third.path, File.read(first.path))
48
+ t = third.sub(Zold::Amount.new(zld: 10), Zold::Id.new, key)
49
+ third.add(t)
50
+ patch = Zold::Patch.new
51
+ patch.start(first)
52
+ patch.join(second)
53
+ patch.join(third)
54
+ FileUtils.rm(first.path)
55
+ patch.save(first.path)
56
+ assert_equal(Zold::Amount.new(zld: -53), first.balance)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,72 @@
1
+ # Copyright (c) 2018 Yegor Bugayenko
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the 'Software'), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ require 'minitest/autorun'
22
+ require 'tmpdir'
23
+ require_relative '../lib/zold/remotes.rb'
24
+
25
+ # Remotes test.
26
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
27
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
28
+ # License:: MIT
29
+ class TestRemotes < Minitest::Test
30
+ def test_adds_remotes
31
+ Dir.mktmpdir 'test' do |dir|
32
+ file = File.join(dir, 'remotes')
33
+ FileUtils.touch(file)
34
+ remotes = Zold::Remotes.new(file)
35
+ remotes.add('127.0.0.1')
36
+ remotes.add('127.0.0.1')
37
+ assert(remotes.total == 1, "#{remotes.total} is not equal to 1")
38
+ end
39
+ end
40
+
41
+ def test_removes_remotes
42
+ Dir.mktmpdir 'test' do |dir|
43
+ file = File.join(dir, 'remotes')
44
+ FileUtils.touch(file)
45
+ remotes = Zold::Remotes.new(file)
46
+ remotes.add('127.0.0.1')
47
+ remotes.add('localhost', 433)
48
+ remotes.remove('localhost', 433)
49
+ assert(remotes.total == 1, "#{remotes.total} is not equal to 1")
50
+ end
51
+ end
52
+
53
+ def test_resets_remotes
54
+ Dir.mktmpdir 'test' do |dir|
55
+ remotes = Zold::Remotes.new(File.join(dir, 'remotes'))
56
+ remotes.clean
57
+ remotes.reset
58
+ assert(!remotes.total.zero?)
59
+ end
60
+ end
61
+
62
+ def test_modifies_score
63
+ Dir.mktmpdir 'test' do |dir|
64
+ file = File.join(dir, 'remotes')
65
+ FileUtils.touch(file)
66
+ remotes = Zold::Remotes.new(file)
67
+ remotes.add('127.0.0.1', 80)
68
+ remotes.rescore('127.0.0.1', 80, 15)
69
+ assert_equal(remotes.score('127.0.0.1'), 15)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,79 @@
1
+ # Copyright (c) 2018 Yegor Bugayenko
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the 'Software'), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ require 'minitest/autorun'
22
+ require 'tmpdir'
23
+ require 'time'
24
+ require_relative '../lib/zold/score.rb'
25
+
26
+ # Score test.
27
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
28
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
29
+ # License:: MIT
30
+ class TestScore < Minitest::Test
31
+ def test_validates_score
32
+ score = Zold::Score.new(
33
+ Time.parse('2017-07-19T21:24:51Z'),
34
+ 'localhost', 443,
35
+ %w[9a81d5 40296e], strength: 6
36
+ )
37
+ assert(score.valid?)
38
+ assert_equal(score.value, 2)
39
+ end
40
+
41
+ def test_reduces_itself
42
+ score = Zold::Score.new(
43
+ Time.parse('2017-07-19T21:24:51Z'),
44
+ 'localhost', 443, %w[A B C D E F G]
45
+ ).reduced(2)
46
+ assert_equal(score.value, 2)
47
+ end
48
+
49
+ def test_validates_wrong_score
50
+ score = Zold::Score.new(
51
+ Time.parse('2017-07-19T21:24:51Z'),
52
+ 'localhost', 443, %w[xxx yyy zzz]
53
+ )
54
+ assert_equal(score.value, 3)
55
+ assert(!score.valid?)
56
+ end
57
+
58
+ def test_prints_and_parses
59
+ time = Time.now
60
+ score = Zold::Score.parse(
61
+ Zold::Score.new(
62
+ time, 'localhost', 999, %w[FIRST SECOND THIRD]
63
+ ).to_s
64
+ )
65
+ assert_equal(score.value, 3)
66
+ assert_equal(score.time.to_s, time.to_s)
67
+ assert_equal(score.host, 'localhost')
68
+ assert_equal(score.port, 999)
69
+ end
70
+
71
+ def test_finds_next_score
72
+ score = Zold::Score.new(
73
+ Time.parse('2017-07-19T21:24:51Z'), 'localhost', 443, strength: 4
74
+ ).next
75
+ assert_equal(score.value, 1)
76
+ assert_equal(score.to_s, '1: 2017-07-19T21:24:51Z localhost 443 1169e')
77
+ assert(score.valid?)
78
+ end
79
+ end
@@ -0,0 +1,45 @@
1
+ # Copyright (c) 2018 Yegor Bugayenko
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the 'Software'), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ require 'minitest/autorun'
22
+ require 'tmpdir'
23
+ require_relative '../lib/zold/key.rb'
24
+ require_relative '../lib/zold/id.rb'
25
+ require_relative '../lib/zold/amount.rb'
26
+ require_relative '../lib/zold/signature.rb'
27
+
28
+ # Signature test.
29
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
30
+ # Copyright:: Copyright (c) 2018 Yegor Bugayenko
31
+ # License:: MIT
32
+ class TestSignature < Minitest::Test
33
+ def test_signs_and_validates
34
+ pvt = Zold::Key.new(file: 'fixtures/id_rsa')
35
+ pub = Zold::Key.new(file: 'fixtures/id_rsa.pub')
36
+ txn = {
37
+ id: 123,
38
+ details: 'How are you?',
39
+ bnf: Zold::Id.new.to_s,
40
+ amount: Zold::Amount.new(zld: 14.95)
41
+ }
42
+ txn[:sign] = Zold::Signature.new.sign(pvt, txn)
43
+ assert(Zold::Signature.new.valid?(pub, txn))
44
+ end
45
+ end