-
Notifications
You must be signed in to change notification settings - Fork 12
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
Feature/serialtube #90
Feature/serialtube #90
Conversation
Thanks for contributing! But please fix these issues before we start to review:
Thanks again! |
And it would be great if you file an issue to describe how the bug effects in tube.rb |
… into feature/serialtube
lib/pwnlib/timer.rb
Outdated
@@ -54,8 +54,9 @@ def countdown(timeout = nil) | |||
begin | |||
yield | |||
ensure | |||
raise ::Pwnlib::Errors::TimeoutError unless active? | |||
was_active = active? |
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.
👍
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.
Thanks for pointing out this,
it's fixed by #116
lib/pwnlib/tubes/tube.rb
Outdated
@@ -159,7 +166,7 @@ def recvuntil(delims, drop: false, timeout: nil) | |||
while @timer.active? | |||
s = recv(1) | |||
|
|||
return '' if s.empty? |
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.
Is this change needed?
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.
No. I raised it in issue #91, and it was classified as "notbug".
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.
So please rollback it
lib/pwnlib/tubes/tube.rb
Outdated
data << c | ||
end | ||
data.slice!(0..-1) | ||
ensure |
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.
Why this?
Original code seems correct
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.
This might be a disagreement on what the function is supposed to do. In my opinion, it should not return until the timeout expires unless it has a match with the predicate. Just because a particular recv(1) didn't return anything doesn't mean that one .5s later won't.
Swap from rescue->raise is different. That's because we must not unrecv(data) if we match the predicate. With ensure, we always unrecv.
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.
But recv_raw is a block call
The situation you mentioned should never happen
test/tubes/serialtube_test.rb
Outdated
|
||
class SerialTest < MiniTest::Test | ||
include ::Pwnlib::Tubes | ||
|
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.
Is the serial tube intend to work on macOS?
If yes, let the tests pass on macOS (install socat)
If no, skip the test unless the environment is on Linux
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.
And skip on Windows as well
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 don't have access to a mac to test it on. It seems likely that it will work off-the-shelf.
How do I "install socat"?
rubyserial is supported on Windows, and so it's possible that serialtube will work there. I don't have a Windows/ruby setup in-place, nor do I have any idea how to test it there.
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.
To install socat, simply add brew install socat
in travis/install.sh#setup_osx
Is that possible to test serialtube on Windows? I said skip on Windows because the tests you added (socat ...
) can't be supported on Windows.
If serialtube is supposed to support on Windows, maybe you should try another testing method to make it work on Windows as well.
Otherwise, just forget Windows supports and skip the tests.
lib/pwnlib/pwn.rb
Outdated
@@ -13,6 +13,7 @@ | |||
require 'pwnlib/reg_sort' | |||
require 'pwnlib/shellcraft/shellcraft' | |||
require 'pwnlib/tubes/sock' | |||
require 'pwnlib/tubes/serialtube' | |||
|
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.
Put this line before 'pwnlib/tubes/sock'
lib/pwnlib/tubes/serialtube.rb
Outdated
# | ||
# @!macro raise_eof | ||
def recv_raw(numbytes) | ||
raise EOFError if @conn.nil? |
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.
raise ::Pwnlib::Errors::EndOfTubeError
lib/pwnlib/tubes/serialtube.rb
Outdated
return data | ||
rescue RubySerial::Error | ||
close | ||
raise EOFError |
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.
Same here
lib/pwnlib/tubes/serialtube.rb
Outdated
# | ||
# @!macro raise_eof | ||
def send_raw(data) | ||
raise EOFError if @conn.nil? |
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.
Same
lib/pwnlib/tubes/serialtube.rb
Outdated
return @conn.write(data) | ||
rescue RubySerial::Error | ||
close | ||
raise EOFError |
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.
Here
lib/pwnlib/tubes/serialtube.rb
Outdated
|
||
# Sets the +timeout+ to use for subsequent +recv_raw+ calls. | ||
# | ||
# @param [Integer] timeout |
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.
The type is Float?
And it's ok to not document private methods if there's nothing worth to be mentioned
# @return [String] | ||
# A string containing read bytes. | ||
# | ||
# @!macro raise_eof |
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.
Yard doesn't support cross-file macro definition naturally
https://stackoverflow.com/questions/10324926/how-to-make-yard-macros-apply-to-multiple-files
@@ -51,6 +51,9 @@ def initialize(timeout: nil) | |||
# @return [String] | |||
# A string contains bytes received from the tube, or +''+ if a timeout occurred while | |||
# waiting. | |||
# | |||
# @!macro raise_eof | |||
# @!macro raise_timeout |
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.
Same as above
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.
This one isn't cross-file.
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.
my fault
lib/pwnlib/tubes/tube.rb
Outdated
@@ -159,7 +166,7 @@ def recvuntil(delims, drop: false, timeout: nil) | |||
while @timer.active? | |||
s = recv(1) | |||
|
|||
return '' if s.empty? |
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.
So please rollback it
lib/pwnlib/tubes/tube.rb
Outdated
data << c | ||
end | ||
data.slice!(0..-1) | ||
ensure |
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.
But recv_raw is a block call
The situation you mentioned should never happen
lib/pwnlib/tubes/tube.rb
Outdated
@@ -105,9 +108,13 @@ def recvpred(timeout: nil) | |||
data << c | |||
end | |||
data.slice!(0..-1) | |||
ensure | |||
# rubocop:disable Style/RescueStandardError |
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.
And this must be changed back to ensure
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 think I finally get what I misunderstood about this code, but I'll raise it as an issue so it can be discussed properly.
lib/pwnlib/tubes/serialtube.rb
Outdated
# @param [Integer] baudrate | ||
# Baud rate. | ||
# @param [Boolean] convert_newlines | ||
# If +true+, will convert local +context.newline+ to +"\\r\\n"+ for remote |
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 guess no escape needed here?
+"\r\n"+
should be enough
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.
And this sentence seems not complete
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.
What I try it without the double escape, in the .html files generated by YARD, I see
If true, will MARKER convert local context.newline to "rn" for remote
I copied the syntax from tube.rb, which does the same thing. Note that in @example
blocks the rules are different.
lib/pwnlib/tubes/serialtube.rb
Outdated
break unless data.empty? | ||
sleep 0.1 | ||
end | ||
# TODO: should we reverse @convert_newlines here? |
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.
TODO(username)
…ley/pwntools-ruby into JonathanBeverley-feature/serialtube
lib/pwnlib/tubes/serialtube.rb
Outdated
|
||
module Pwnlib | ||
module Tubes | ||
# Serial Connections |
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.
change here to
# @!macro [new] raise_eof
# @raise [Pwnlib::Errors::EndOfTubeError]
# If the request is not satisfied when all data is received.
# Serial Connections
class SerialTube < Tube
otherwise YARD will have wrong result
lib/pwnlib/tubes/serialtube.rb
Outdated
# XXX(JonathanBeverey): should we reverse @convert_newlines here? | ||
return data | ||
rescue RubySerial::Error | ||
close |
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.
Current tests don't cover this rescue
add tests and ensure 100% coverage
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 can't see any way to do this. open(2) checks for all the same errors as read(2). Nothing I've done will cause an error in the right place, without writing bad code.
If you know a way the test code can break Ruby's encapsulation model and directly invoke SerialTube.conn.close(), that could do it. But I really don't want to write a method to allow that, and honestly, if I did, I would then tighten up the error checking around @conn and close off that avenue as well.
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 found a way.
begin | ||
return @conn.write(data) | ||
rescue RubySerial::Error | ||
close |
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.
same here
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.
done.
Seems the tests sometimes hang forever |
I've tried chasing it down, but I can't reproduce it locally. It's probably happening because we're stuck in the Open3.popen3() block. Before I got the ensure working properly something similar would happen when an exception tried to break out of that block, but popen3 would wait for socat to complete before allowing it. |
…ley/pwntools-ruby into JonathanBeverley-feature/serialtube
With With quickly review I know the |
Got it. Now dealing with another failed case.. https://travis-ci.org/peter50216/pwntools-ruby/jobs/419754587 |
@JonathanBeverley I fixed the issues The fixes contained in two commits: bda6f8b and 764ada5 (plz ignore the debug messages) |
done. |
Sorry those commits contain debug code.. |
Add a class to handle serial connections.
There is a significant bugfix in
lib/pwnlib/tubes/tube.rb
, which is necessary for it to work properly.I've bumped version to 1.0.2 (mostly so I could tell I was using it in testing.)
This change is