Skip to content

Commit 4c8d5b5

Browse files
committed
Update digest to match methods spec and unexclude passing tests
1 parent ced6143 commit 4c8d5b5

File tree

6 files changed

+53
-42
lines changed

6 files changed

+53
-42
lines changed

lib/truffle/digest.rb

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# truffleruby_primitives: true
2+
13
# Copyright (c) 2015, 2020 Oracle and/or its affiliates. All rights reserved. This
24
# code is released under a tri EPL/GPL/LGPL license. You can use it,
35
# redistribute it and/or modify it under the terms of the:
@@ -78,13 +80,17 @@ def new
7880
copy
7981
end
8082

83+
def block_length
84+
raise RuntimeError, "#{self.class.name} does not implement block_length()"
85+
end
86+
8187
def update(message)
8288
raise RuntimeError, "#{self.class.name} does not implement update()"
8389
end
8490
alias_method :<<, :update
8591

8692
def reset
87-
Truffle::Digest.reset @digest
93+
raise RuntimeError, "#{self.class.name} does not implement reset()"
8894
end
8995

9096
def digest(message = NO_MESSAGE)
@@ -103,7 +109,6 @@ def hexdigest(message = NO_MESSAGE)
103109
Digest.hexencode(digest(message))
104110
end
105111
alias_method :to_s, :hexdigest
106-
alias_method :to_str, :hexdigest
107112

108113
def digest!
109114
digested = finish
@@ -122,7 +127,7 @@ def hexdigest!
122127
end
123128

124129
def digest_length
125-
Truffle::Digest.digest_length @digest
130+
StringValue(digest).length
126131
end
127132

128133
def size
@@ -131,7 +136,15 @@ def size
131136
alias_method :length, :size
132137

133138
def ==(other)
134-
hexdigest == other.to_str
139+
if Primitive.object_kind_of?(other, Digest::Instance)
140+
self_str = self.digest
141+
other_str = other.digest
142+
else
143+
self_str = self.to_s
144+
other_str = Truffle::Type.rb_check_convert_type(other, String, :to_str)
145+
return false if Primitive.nil?(other_str)
146+
end
147+
StringValue(self_str) == StringValue(other_str)
135148
end
136149

137150
def inspect
@@ -154,6 +167,19 @@ def self.hexdigest(str, *args)
154167
end
155168

156169
class Base < ::Digest::Class
170+
def block_length
171+
Truffle::Digest.digest_block_length @digest
172+
end
173+
174+
def digest_length
175+
Truffle::Digest.digest_length @digest
176+
end
177+
178+
def reset
179+
Truffle::Digest.reset @digest
180+
self
181+
end
182+
157183
def update(str)
158184
str = StringValue(str)
159185
Truffle::Digest.update(@digest, str)
@@ -166,50 +192,30 @@ class MD5 < Base
166192
def initialize
167193
@digest = Truffle::Digest.md5
168194
end
169-
170-
def block_length
171-
64
172-
end
173195
end
174196

175197
class SHA1 < Base
176198
def initialize
177199
@digest = Truffle::Digest.sha1
178200
end
179-
180-
def block_length
181-
64
182-
end
183201
end
184202

185203
class SHA256 < Base
186204
def initialize
187205
@digest = Truffle::Digest.sha256
188206
end
189-
190-
def block_length
191-
64
192-
end
193207
end
194208

195209
class SHA384 < Base
196210
def initialize
197211
@digest = Truffle::Digest.sha384
198212
end
199-
200-
def block_length
201-
128
202-
end
203213
end
204214

205215
class SHA512 < Base
206216
def initialize
207217
@digest = Truffle::Digest.sha512
208218
end
209-
210-
def block_length
211-
128
212-
end
213219
end
214220

215221
autoload :SHA2, 'digest/sha2'

spec/tags/truffle/methods_tags.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ fails:Public methods on Rational should not include divide
3232
fails:Public methods on String should include freeze
3333
fails:Public methods on Digest.singleton_class should include const_missing
3434
fails:Public methods on Digest::Instance should not include finish
35-
fails:Public methods on Digest::Instance should not include to_str
36-
fails:Public methods on Digest::Instance should include block_length
3735
fails:Public methods on Dir should not include closed?
3836
fails:Public methods on Exception should include respond_to?
3937
fails:Public methods on FalseClass should include ===
@@ -62,11 +60,6 @@ fails:Public methods on TracePoint should include instruction_sequence
6260
fails:Public methods on TracePoint should include parameters
6361
fails:Public methods on TracePoint should include raised_exception
6462
fails:Public methods on TracePoint should include return_value
65-
fails:Public methods on Digest::Base should include block_length
66-
fails:Public methods on Digest::Base should include digest_length
67-
fails:Public methods on Digest::Base should include reset
68-
fails:Public methods on Digest::MD5 should not include block_length
69-
fails:Public methods on Digest::SHA1 should not include block_length
7063
fails:Public methods on ENV.singleton_class should include freeze
7164
fails:Public methods on Enumerator::Lazy should include filter_map
7265
fails:Public methods on Enumerator::Lazy should include with_index

src/main/java/org/truffleruby/stdlib/digest/DigestAlgorithm.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@
1010
package org.truffleruby.stdlib.digest;
1111

1212
enum DigestAlgorithm {
13-
MD5("MD5", 16),
14-
SHA1("SHA1", 20),
15-
SHA256("SHA-256", 32),
16-
SHA384("SHA-384", 48),
17-
SHA512("SHA-512", 64);
13+
MD5("MD5", 16, 64),
14+
SHA1("SHA1", 20, 64),
15+
SHA256("SHA-256", 32, 64),
16+
SHA384("SHA-384", 48, 128),
17+
SHA512("SHA-512", 64, 128);
1818

1919
private final String name;
2020
private final int length;
21+
private final int blockLength;
2122

22-
DigestAlgorithm(String name, int length) {
23+
DigestAlgorithm(String name, int length, int blockLength) {
2324
this.name = name;
2425
this.length = length;
26+
this.blockLength = blockLength;
2527
}
2628

2729
public String getName() {
@@ -31,4 +33,8 @@ public String getName() {
3133
public int getLength() {
3234
return length;
3335
}
36+
37+
public int getBlockLength() {
38+
return blockLength;
39+
}
3440
}

src/main/java/org/truffleruby/stdlib/digest/DigestNodes.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ private static byte[] cloneAndDigest(MessageDigest digest) {
159159

160160
}
161161

162+
@CoreMethod(names = "digest_block_length", onSingleton = true, required = 1)
163+
public abstract static class DigestBlockLengthNode extends CoreMethodArrayArgumentsNode {
164+
165+
@Specialization
166+
protected int digestBlockLength(RubyDigest digestObject) {
167+
return digestObject.algorithm.getBlockLength();
168+
}
169+
170+
}
171+
162172
@CoreMethod(names = "digest_length", onSingleton = true, required = 1)
163173
public abstract static class DigestLengthNode extends CoreMethodArrayArgumentsNode {
164174

test/mri/excludes/TestDigestExtend.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
exclude :test_block_length, "needs investigation"
2-
exclude :test_class_reset, "needs investigation"
3-
exclude :test_digest_length, "needs investigation"
4-
exclude :test_new, "needs investigation"
1+
exclude :test_new, "probably not cloning the @digest correctly"

test/mri/failing.exclude

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ coverage/test_coverage.rb
9999
csv/test_encodings.rb # missing ObjectSpace define_finalizer specialization
100100
csv/test_features.rb # `const_missing': uninitialized constant Zlib::GzipWriter
101101
csv/test_interface.rb # missing ObjectSpace define_finalizer specialization
102-
digest/test_digest_extend.rb # all failed
103102
drb/test_acl.rb
104103
drb/test_drb.rb
105104
drb/test_drbssl.rb

0 commit comments

Comments
 (0)