title: How to stub a socket in C?
body: <p>I've written client code that's supposed to send some data through a socket and read back an answer from the remote server.</p>
  
  <p>I would like to unit-test that code. The function's signature is something along the lines of:</p>
  
  <pre><code>double call_remote(double[] args, int fd);
  </code></pre>
  
  <p>where <code>fd</code> is the file descriptor of the socket to the remote server.</p>
  
  <p>Now the <code>call_remote</code> function will, after sending the data, block on reading the answer from the server. How can I stub such a remote server for unit-testing the code?</p>
  
  <p>Ideally I would like something like:</p>
  
  <pre><code>int main() {
    int stub = /* initialize stub */
    double expected = 42.0;
  
    assert(expected == call_remote(/* args */, stub);
  
    return 0;
  }
  
  double stub_behavior(double[] args) {
    return 42.0;
  }
  </code></pre>
  
  <p>I would like <code>stub_behavior</code> to be called and send the <code>42.0</code> value down the stubbed file descriptor.</p>
  
  <p>Any easy way I can do that?</p>