So... To close this loop. I decided it was too much work to mock almost the entire socket library just for this thing. <div><br></div><div>One of the features I liked about FakeNet was the DNS mocking, where it resolves every hostname to the same address, so I found a simple way to do that in Python on ActiveState (<a href="http://code.activestate.com/recipes/491264-mini-fake-dns-server/">http://code.activestate.com/recipes/491264-mini-fake-dns-server/</a>).<div>
<br></div><div>I decided to go with sshim (<a href="http://packages.python.org/sshim/">http://packages.python.org/sshim/</a>) to create a scriptable SSH server to have it behave like network devices. The project is not complete, but the design is! :)</div>
<div><br></div><div>Much, much simpler, and can all be done within a VM. Stay tuned for code... Someday.</div><div><br></div><div>Thanks for all the input!</div><div><br></div><div>jathan.<br><br><div class="gmail_quote">
On Wed, Aug 1, 2012 at 12:15 PM, Howard B. Golden <span dir="ltr">&lt;<a href="mailto:howard_b_golden@yahoo.com" target="_blank">howard_b_golden@yahoo.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div style="font-size:12pt;font-family:times new roman,new york,times,serif"><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"> <hr size="1">  <b><span style="font-weight:bold">From:</span></b> Steve Wedig &lt;<a href="mailto:stevewedig@gmail.com" target="_blank">stevewedig@gmail.com</a>&gt;<br>
 <b><span style="font-weight:bold">To:</span></b> SoCal Python Interest Group &lt;<a href="mailto:socal-piggies@lists.idyll.org" target="_blank">socal-piggies@lists.idyll.org</a>&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><div><div class="h5"> <br>
<div>I see, Paramiko&#39;s socket dependency isn&#39;t inverted, so you are unable to provide an alternate implementation of that interface.<div><br></div><div>I don&#39;t know if this will work, but you could try overriding the socket module&#39;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><a href="http://stackoverflow.com/questions/1096216/override-namespace-in-python" target="_blank">http://stackoverflow.com/questions/1096216/override-namespace-in-python</a></div>

<div><a href="http://stackoverflow.com/questions/3012473/how-do-i-override-a-python-import" target="_blank">http://stackoverflow.com/questions/3012473/how-do-i-override-a-python-import</a><br><br><div>On Tue, Jul 31, 2012 at 4:17 PM, Jathan McCollum <span dir="ltr">&lt;<a rel="nofollow" href="mailto:jathan@gmail.com" target="_blank">jathan@gmail.com</a>&gt;</span> wrote:<br>

<blockquote 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 (<a href="http://practicalmalwareanalysis.com/fakenet/" target="_blank">http://practicalmalwareanalysis.com/fakenet/</a>) 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. 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>    &quot;&quot;&quot;Simple echo server that prints Python version upon connection&quot;&quot;&quot;</div><div>    import sys</div><div>    FakeNet.sendData(context, sys.version)</div>


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


<div><div><br></div><div>It&#39;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. 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 force it (somehow) to use FakeNet&#39;s send/recv calls, and just mock the rest.</div>


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


<blockquote 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><div>On Mon, Jul 30, 2012 at 4:38 PM, Jathan McCollum <span dir="ltr">&lt;<a rel="nofollow" href="mailto:jathan@gmail.com" target="_blank">jathan@gmail.com</a>&gt;</span> wrote:<br>




</div><blockquote 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&#39;t have _socket.so, which means I can&#39;t actually import the socket module, but I need to do some mocking of connections.</div><div><br></div><div>





Anyone have any pointers? 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" href="mailto:socal-piggies@lists.idyll.org" target="_blank">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></div></blockquote></div><br></div></div>
<br>_______________________________________________<br>
socal-piggies mailing list<br>
<a rel="nofollow" href="mailto:socal-piggies@lists.idyll.org" target="_blank">socal-piggies@lists.idyll.org</a><br>
<a rel="nofollow" href="http://lists.idyll.org/listinfo/socal-piggies" target="_blank">http://lists.idyll.org/listinfo/socal-piggies</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div></div></div><span><font color="#888888">-- <br>Jathan.<br>--<br>
</font></span></div></div></div>
<br>_______________________________________________<br>
socal-piggies mailing list<br>
<a rel="nofollow" href="mailto:socal-piggies@lists.idyll.org" target="_blank">socal-piggies@lists.idyll.org</a><br>
<a rel="nofollow" href="http://lists.idyll.org/listinfo/socal-piggies" target="_blank">http://lists.idyll.org/listinfo/socal-piggies</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div>Steve Wedig</div><a href="http://www.linkedin.com/in/wedig" target="_blank">http://www.linkedin.com/in/wedig</a><br>
</div>
</div><br>_______________________________________________<br>socal-piggies mailing list<br><a href="mailto:socal-piggies@lists.idyll.org" target="_blank">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></div> </div> </blockquote></div>   </div></div><br>_______________________________________________<br>
socal-piggies mailing list<br>
<a 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></blockquote></div><br><br clear="all"><div><br></div>-- <br>Jathan.<br>--<br>
</div></div>