mischacks 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,9 @@
1
+ === UNRELEASED / UNRELEASED
2
+
3
+ * When generating the gem, grab description from README.
4
+ * Update README.
5
+ * Password: Switch from match? to =~; implement ==.
6
+
1
7
  === 0.2.0 / 2010-08-29
2
8
 
3
9
  * Add Random.
data/README.txt CHANGED
@@ -6,7 +6,8 @@
6
6
 
7
7
  Miscellaneous methods that may or may not be useful.
8
8
 
9
- sh:: Safely pass untrusted parameters to sh scripts.
9
+ sh:: Safely pass untrusted parameters to sh scripts. Raise an exception if the
10
+ script returns a non-zero value.
10
11
 
11
12
  fork_and_check:: Run a block in a forked process and raise an exception if the
12
13
  process returns a non-zero value.
@@ -48,16 +49,23 @@ ever use an untrusted variable as a command.
48
49
 
49
50
  == SYNOPSIS:
50
51
 
51
- # sh
52
+ sh::
53
+
54
+ Note that the scripts are run with set -e.
55
+
56
+ MiscHacks.sh 'exec ls'
52
57
 
53
58
  MiscHacks.sh %q{
54
59
  diff -u "$1" "$2" | tr a-z A-Z >"$output"
55
60
  }, '/dev/null', '/etc/motd', :output => 'foo'
56
61
 
57
62
  unsafe_str = %q{" 'foo' $(bar) `baz` "}
58
- MiscHacks.sh 'printf "%s\n" "$1"', unsafe_str
63
+ MiscHacks.sh 'exec printf "%s\n" "$1"', unsafe_str
59
64
 
60
- # fork_and_check
65
+ # Raises MiscHacks::ChildError.
66
+ MiscHacks.sh 'exec false'
67
+
68
+ fork_and_check::
61
69
 
62
70
  # These examples raise MiscHacks::ChildError.
63
71
  MiscHacks.fork_and_check do exit! 42 end
@@ -67,7 +75,7 @@ ever use an untrusted variable as a command.
67
75
  # Does not raise an error.
68
76
  MiscHacks.fork_and_check do exit! 0 end
69
77
 
70
- # do_and_exit
78
+ do_and_exit::
71
79
 
72
80
  # Prints foo if there are no failures. If anything fails, raises an
73
81
  # exception.
@@ -77,23 +85,23 @@ ever use an untrusted variable as a command.
77
85
  end
78
86
  end
79
87
 
80
- # overwrite
88
+ overwrite::
81
89
 
82
90
  MiscHacks.overwrite 'myconfig' do |io|
83
- io << config.to_yaml
91
+ config.to_yaml io
84
92
  end
85
93
 
86
- # tempname_for
94
+ tempname_for::
87
95
 
88
96
  MiscHacks.tempname_for '/foo/bar/baz' # => '/foo/bar/.baz.klyce3f517qkh9l'
89
97
 
90
- # try_n_times
98
+ try_n_times::
91
99
 
92
100
  io = MiscHacks.try_n_times do
93
101
  File.open path, File::RDWR|File::CREAT|File::EXCL
94
102
  end
95
103
 
96
- # Exception#to_formatted_string
104
+ Exception#to_formatted_string::
97
105
 
98
106
  begin
99
107
  # Do something
@@ -101,14 +109,31 @@ ever use an untrusted variable as a command.
101
109
  warn e.to_formatted_string
102
110
  end
103
111
 
112
+ Random::
113
+
114
+ n = MiscHacks::RANDOM.exp 4 # 0 ≤ n < 2⁴
115
+ n = MiscHacks::RANDOM.float # 0.0 ≤ n < 1.0
116
+ n = MiscHacks::RANDOM.float 4 # 0.0 ≤ n < 4.0
117
+ n = MiscHacks::RANDOM.int 4 # 0 ≤ n < 4
118
+
119
+ Password::
120
+
121
+ # New password
122
+ password = MiscHacks::Password.new_from_password cleartext_from_user
123
+ store_in_database password.to_s # encrypted
124
+
125
+ # Verify password
126
+ password = MiscHacks::Password.new password_from_database
127
+ password =~ cleartext_from_user # ⇒ true/false
128
+
104
129
  == REQUIREMENTS:
105
130
 
106
- * POSIX sh
107
- * A system that implements fork
131
+ * POSIX sh for the sh method
132
+ * A system that implements fork for some methods
108
133
 
109
134
  == INSTALL:
110
135
 
111
- * sudo gem install ion1-mischacks --source http://gems.github.com/
136
+ * sudo gem install mischacks
112
137
 
113
138
  == LICENSE:
114
139
 
data/Rakefile CHANGED
@@ -28,12 +28,9 @@ begin
28
28
  Jeweler::Tasks.new do |gemspec|
29
29
  gemspec.name = "mischacks"
30
30
  gemspec.summary = "Miscellaneous methods that may or may not be useful"
31
- gemspec.description = \
32
- "sh: Safely pass untrusted parameters to sh scripts. " \
33
- "overwrite: Safely replace a file. " \
34
- "Exception#to_formatted_string: Return a string that looks like how Ruby would dump an uncaught exception. " \
35
- "Random: Generate various types of random numbers using SecureRandom. " \
36
- "Password: A small wrapper for String#crypt that does secure salt generation and easy password verification."
31
+ gemspec.description = File.read(File.dirname(__FILE__)+'/README.txt').
32
+ sub(/.*^=+ DESCRIPTION[^\n]*\n*/m, '').
33
+ sub(/\n*^=.*/m, '')
37
34
  gemspec.email = "devel@johan.kiviniemi.name"
38
35
  gemspec.homepage = "http://johan.kiviniemi.name/software/mischacks/"
39
36
  gemspec.authors = ["Johan Kiviniemi"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -34,7 +34,7 @@ module MiscHacks
34
34
  fork_and_check do
35
35
  do_and_exit! do
36
36
  env.each_pair do |k, v| ENV[k.to_s] = v.to_s end
37
- exec *(%W{sh -e -c #{cmd} sh} + args.map {|a| a.to_s })
37
+ exec *(%W{sh -e -c #{cmd} sh} + args.map(&:to_s))
38
38
  end
39
39
  end
40
40
 
@@ -39,10 +39,14 @@ module MiscHacks
39
39
  end
40
40
  end
41
41
 
42
- def match? password
42
+ def =~ password
43
43
  to_s == password.crypt(@salt)
44
44
  end
45
45
 
46
+ def == other
47
+ to_s == other.to_s
48
+ end
49
+
46
50
  def to_s
47
51
  [@salt, @encrypted].join
48
52
  end
@@ -19,14 +19,6 @@ require 'mischacks/password'
19
19
  require 'set'
20
20
 
21
21
  describe MiscHacks::Password do
22
- def match expected
23
- simple_matcher("match #{expected.inspect}") do |given, matcher|
24
- matcher.failure_message = "expected #{given.inspect} to match #{expected.inspect}"
25
- matcher.negative_failure_message = "expected #{given.inspect} not to match #{expected.inspect}"
26
- given.match? expected
27
- end
28
- end
29
-
30
22
  before :all do
31
23
  @many = 1000
32
24
 
@@ -117,17 +109,29 @@ describe MiscHacks::Password do
117
109
  end
118
110
  end
119
111
 
120
- describe 'match?' do
112
+ describe '=~' do
121
113
  it 'should verify whether the cleartext parameter matches the encrypted password' do
122
114
  %w{foo bar baz}.each do |password|
123
115
  pw = MiscHacks::Password.new_from_password password
124
- pw.should match password
125
- pw.should_not match "#{password}x"
126
- pw.should_not match ''
116
+ pw.should =~ password
117
+ pw.should_not =~ "#{password}x"
118
+ pw.should_not =~ ''
127
119
  end
128
120
  end
129
121
  end
130
122
 
123
+ describe '==' do
124
+ it 'should verify whether the encrypted parameter matches the encrypted password' do
125
+ pw = MiscHacks::Password.new @encrypted
126
+
127
+ pw.should == @encrypted
128
+ pw.should == MiscHacks::Password.new(@encrypted)
129
+
130
+ pw.should_not == 'foo'
131
+ pw.should_not == MiscHacks::Password.new_from_password("#{@password}x")
132
+ end
133
+ end
134
+
131
135
  describe 'to_s' do
132
136
  it 'should return the encrypted password' do
133
137
  MiscHacks::Password.new(@encrypted).to_s.should == @encrypted
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mischacks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johan Kiviniemi
@@ -13,7 +13,29 @@ date: 2010-08-29 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: "sh: Safely pass untrusted parameters to sh scripts. overwrite: Safely replace a file. Exception#to_formatted_string: Return a string that looks like how Ruby would dump an uncaught exception. Random: Generate various types of random numbers using SecureRandom. Password: A small wrapper for String#crypt that does secure salt generation and easy password verification."
16
+ description: "Miscellaneous methods that may or may not be useful.\n\n\
17
+ sh:: Safely pass untrusted parameters to sh scripts. Raise an exception if the\n\
18
+ script returns a non-zero value.\n\n\
19
+ fork_and_check:: Run a block in a forked process and raise an exception if the\n\
20
+ process returns a non-zero value.\n\n\
21
+ do_and_exit, do_and_exit!:: Run a block. If the block does not run exit!, a\n\
22
+ successful exec or equivalent, run exit(1) or exit!(1) ourselves. Useful to\n\
23
+ make sure a forked block either runs a successful exec or dies.\n\n\
24
+ Any exceptions from the block are printed to standard error.\n\n\
25
+ overwrite:: Safely replace a file. Writes to a temporary file and then moves it\n\
26
+ over the old file.\n\n\
27
+ tempname_for:: Generates an unique temporary path based on a filename. The\n\
28
+ generated filename resides in the same directory as the original one.\n\n\
29
+ try_n_times:: Retries a block of code until it succeeds or a maximum number of\n\
30
+ attempts (default 10) is exceeded.\n\n\
31
+ Exception#to_formatted_string:: Return a string that looks like how Ruby would\n\
32
+ dump an uncaught exception.\n\n\
33
+ IO#best_datasync:: Try fdatasync, falling back to fsync, falling back to flush.\n\n\
34
+ Random#exp:: Return a random integer 0 \xE2\x89\xA4 n < 2^argument (using SecureRandom).\n\n\
35
+ Random#float:: Return a random float 0.0 \xE2\x89\xA4 n < argument (using SecureRandom).\n\n\
36
+ Random#int:: Return a random integer 0 \xE2\x89\xA4 n < argument (using SecureRandom).\n\n\
37
+ Password:: A small wrapper for String#crypt that does secure salt generation\n\
38
+ and easy password verification."
17
39
  email: devel@johan.kiviniemi.name
18
40
  executables: []
19
41
 
@@ -25,7 +47,6 @@ files:
25
47
  - .gitignore
26
48
  - COPYING
27
49
  - History.txt
28
- - Manifest.txt
29
50
  - README.txt
30
51
  - Rakefile
31
52
  - VERSION
@@ -1,9 +0,0 @@
1
- COPYING
2
- History.txt
3
- Manifest.txt
4
- README.txt
5
- Rakefile
6
- lib/mischacks.rb
7
- mischacks.gemspec
8
- spec/mischacks_spec.rb
9
- spec/spec_helper.rb