title: Faking an input device for testing purpose
body: <h2>What I want to do</h2>
  
  <p>I'm writing a daemon which listen to the input devices for keys presses and send signals via D-Bus. The main goal is to manage audio volume and screen backlight level by requesting changes or informing about changes.
  I use <em>libevdev</em> to handle the input device events.</p>
  
  <p>I wrote a function for opening an input device located at a specified path:</p>
  
  <pre><code>Device device_open(const char *path);
  </code></pre>
  
  <p>That function works well, but while I'm writing unit tests for it, I wanted to create file fixtures with different properties (existence of the file, read access, etc.) to check the error handling of my function and memory management (as I store data in a structure).</p>
  
  <h2>What I have already done</h2>
  
  <p>But testing it with a real input device (located at /dev/input/event*) needs <em>root</em> access rights. Setting read access for everyone on /dev/input/event* files works but seems risky to me. Executing my tests as root is worse !</p>
  
  <p>Creating a device using <code>mknod</code> works but needs to be done as root.</p>
  
  <p>I also tried to use character special files (because input devices are one of those) allowing read for everyone (like /dev/random, /dev/zero, /dev/null and even the terminal device i'm currently using: /dev/tty2).</p>
  
  <p>But those devices does not handles <code>ioctl</code> requests needed by libevdev: <em>EVIOCGBIT</em> is the first request returning an error "Inappropriate ioctl for device".</p>
  
  <h2>What I'm looking for</h2>
  
  <p>I want to be able to create device files as a regular user (the user executing the unit tests). Then, by setting access rights I should be able to test my function behavior for different kinds of file (read only, no read allowed, bad device type, etc.).
  If it appears to be impossible, I will certainly refactor my function using private helpers. But how to do it. Any examples ?</p>
  
  <p>Thanks.</p>
  
  <p>Edit: I tried to express better my needs.</p>