<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div><span>FWIW, it seems to me that in a dynamic language, any dependency can be interpolated somehow. This is an existence argument only, not a constructive one.</span></div><div><br><span></span></div><div><span>Howard</span></div><div><br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; margin-top: 5px; padding-left: 5px;">  <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div dir="ltr"> <font face="Arial" size="2"> <hr size="1">  <b><span style="font-weight:bold;">From:</span></b> Steve Wedig &lt;stevewedig@gmail.com&gt;<br> <b><span style="font-weight: bold;">To:</span></b> SoCal Python Interest Group &lt;socal-piggies@lists.idyll.org&gt; <br> <b><span style="font-weight:
 bold;">Sent:</span></b> Wednesday, August 1, 2012 10:36 AM<br> <b><span style="font-weight: bold;">Subject:</span></b> Re: [socal-piggies] Mocking sockets<br> </font> </div> <br>
<meta http-equiv="x-dns-prefetch-control" content="off"><div id="yiv415946525">I see, Paramiko's socket dependency isn't inverted, so you are unable to provide an alternate implementation of that interface.<div><br></div><div>I don't know if this will work, but you could try overriding the socket module's import, replacing it with your own implementation of the same interface.</div>
<div><br></div><div>Here are some discussions that suggest it may work, as long as you override the import before Paramiko gets imported:</div><div>http://stackoverflow.com/questions/1096216/override-namespace-in-python</div>
<div>http://stackoverflow.com/questions/3012473/how-do-i-override-a-python-import<br><br><div class="yiv415946525gmail_quote">On Tue, Jul 31, 2012 at 4:17 PM, Jathan McCollum <span dir="ltr">&lt;<a rel="nofollow" ymailto="mailto:jathan@gmail.com" target="_blank" href="mailto:jathan@gmail.com">jathan@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="yiv415946525gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Ok, a little background...<div><br></div><div>On the heels of BlackHat and DefCon I discovered a new tool called FakeNet (http://practicalmalwareanalysis.com/fakenet/) that is intended to run on Windows XP. It allows you to hijack all socket connections received by the system, and has an embedded Python 2.7.3 interpreter. I want to try using this for a single-system dummy test network.</div>

<div><br></div><div>The catch is, because of the custom socket I/O, the _socket.so module was intentionally excluded.&nbsp;In order to do socket calls you must import FakeNet. Here is a simple example:</div><div><br></div><div>

<div>def FN_NewConnection(context):</div><div>&nbsp; &nbsp; """Simple echo server that prints Python version upon connection"""</div><div>&nbsp; &nbsp; import sys</div><div>&nbsp; &nbsp; FakeNet.sendData(context, sys.version)</div>

<div>&nbsp; &nbsp; while True:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; bytes = FakeNet.recvData(context, 4096)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if bytes == "":</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break</div><div>&nbsp; &nbsp; &nbsp; &nbsp; else:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FakeNet.sendData(context, bytes)</div>

<div><div><br></div><div>It's a greatly simplified interface. It handles the buildup/teardown of the session, and all you have to do is worry about send/recv.</div><div><br></div><div>The device allows for custom modules.&nbsp;I am trying to mock an SSH server and actually negotiate with the client. I want to use this for... You guessed it... testing interaction with SSH endpoints without actually having to connect to anything.</div>

<div><br></div><div>For this I want to use Paramiko, but the problem is that Paramiko imports the socket module all over the place, so I wanted to see if I could&nbsp;force it (somehow) to use FakeNet's send/recv calls, and&nbsp;just mock the rest.</div>

<div><br></div><div>Steve, thanks for the reminder of dependency inversion. :)</div><div><br></div><div><div><div class="yiv415946525h5"><div class="yiv415946525gmail_quote">On Mon, Jul 30, 2012 at 6:18 PM, Andrew Kou <span dir="ltr">&lt;<a rel="nofollow" ymailto="mailto:andrew.kou@gmail.com" target="_blank" href="mailto:andrew.kou@gmail.com">andrew.kou@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="yiv415946525gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div>Jathan,</div><div><br></div>What requirements do you have of the mocked object?<span><font color="#888888"><div>
<br></div></font></span><div><span><font color="#888888">- Andrew<br><div><br></div><div><br></div></font></span><div><br><div class="yiv415946525gmail_quote"><div>On Mon, Jul 30, 2012 at 4:38 PM, Jathan McCollum <span dir="ltr">&lt;<a rel="nofollow" ymailto="mailto:jathan@gmail.com" target="_blank" href="mailto:jathan@gmail.com">jathan@gmail.com</a>&gt;</span> wrote:<br>



</div><blockquote class="yiv415946525gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div>I have a need to mock the socket module, but without actually importing it first. I know some pretty sophisticated mocking can be done with Mox, but it requires that you import the real module into the namespace first.<div>




<br></div><div>I am doing some work on an embedded system that doesn't have _socket.so, which means I can't actually import the socket module, but I need to do some mocking of connections.</div><div><br></div><div>




Anyone have any pointers?&nbsp;Thanks in advance!</div><span><font color="#888888"><div><div><br></div>-- <br>Jathan.<br>--<br>
</div>
</font></span><br></div></div><div>_______________________________________________<br>
socal-piggies mailing list<br>
<a rel="nofollow" ymailto="mailto:socal-piggies@lists.idyll.org" target="_blank" href="mailto:socal-piggies@lists.idyll.org">socal-piggies@lists.idyll.org</a><br>
http://lists.idyll.org/listinfo/socal-piggies<br>
<br></div></blockquote></div><br></div></div>
<br>_______________________________________________<br>
socal-piggies mailing list<br>
<a rel="nofollow" ymailto="mailto:socal-piggies@lists.idyll.org" target="_blank" href="mailto:socal-piggies@lists.idyll.org">socal-piggies@lists.idyll.org</a><br>
<a rel="nofollow" target="_blank" href="http://lists.idyll.org/listinfo/socal-piggies">http://lists.idyll.org/listinfo/socal-piggies</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div></div></div><span class="yiv415946525HOEnZb"><font color="#888888">-- <br>Jathan.<br>--<br>
</font></span></div></div></div>
<br>_______________________________________________<br>
socal-piggies mailing list<br>
<a rel="nofollow" ymailto="mailto:socal-piggies@lists.idyll.org" target="_blank" href="mailto:socal-piggies@lists.idyll.org">socal-piggies@lists.idyll.org</a><br>
<a rel="nofollow" target="_blank" href="http://lists.idyll.org/listinfo/socal-piggies">http://lists.idyll.org/listinfo/socal-piggies</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div>Steve Wedig</div>http://www.linkedin.com/in/wedig<br>
</div>
</div><meta http-equiv="x-dns-prefetch-control" content="on"><br>_______________________________________________<br>socal-piggies mailing list<br><a ymailto="mailto:socal-piggies@lists.idyll.org" href="mailto:socal-piggies@lists.idyll.org">socal-piggies@lists.idyll.org</a><br><a href="http://lists.idyll.org/listinfo/socal-piggies" target="_blank">http://lists.idyll.org/listinfo/socal-piggies</a><br><br><br> </div> </div> </blockquote></div>   </div></body></html>