-
-
Notifications
You must be signed in to change notification settings - Fork 388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add specs for OpenSSL::KDF.pbkdf2_hmac #1092
Conversation
Looks like there is one failure: https://github.com/ruby/spec/actions/runs/6472919326/job/17574520140?pr=1092 |
length = 16 | ||
hash = "sha1" | ||
key = OpenSSL::KDF.pbkdf2_hmac(pass, salt: salt, iterations: iterations, length: length, hash: hash) | ||
key.should == "!\x99+\xF0^\xD0\x8BM\x158\xC4\xAC\x9C\xF1\xF0\xE0".b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: as far as salt/iterations/length... are passed as keyword arguments - using local variables seems excessive to me. It doesn't improve readability of a test and don't add context (as far as meaning is already expressed with keyword argument names) but makes test case longer:
pass = "secret"
key = OpenSSL::KDF.pbkdf2_hmac(pass, salt: "\x00".b * 16, iterations: 20_000, length: 16, hash: "sha1")
key.should == "!\x99+\xF0^\xD0\x8BM\x158\xC4\xAC\x9C\xF1\xF0\xE0".b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I wasn't exactly proud of this repetitive code either.
Since it's mostly just a bunch of default with one argument changed or removed, I was thinking maybe it's better to create a set of defaults, and the tests should explicitly alter these values. Something like this:
before :each do
@pass = "secret"
@defaults = { salt: "\x00".b * 16, iterations: 20_000, ... }
end
it "works with 10_000 iterations do"
key = OpenSSL::KDF.pbkdf2_hmac(@pass, **@defaults, iterations: 10_000)
key.should == SOME_VALUE
end
This reduces the length of the tests, removes a lot of duplication, and makes it a bit more clear which parameter we're testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed an update with the defaults hash, I would think this is much clearer and concise.
654eb5c
to
461f532
Compare
Today I Learned: the value of |
Could you please squash commits? |
7cbe6f5
to
2516f65
Compare
They're squashed into a single commit. |
Thank you! |
A lot of error messages depend on the used OpenSSL version, so I had to be a bit lenient with the error matching.