Allow for matching after null byte in generic send/expect test

Issue #84 resolved
Christian Rinjes created an issue

While it's possible to send messages containing any byte, expect can only match until the first null byte since both regexec and strncmp expect a null-terminated string.

For example using a simple echo server

send "\0xFFFoo\0x00Bar"
expect "\0xFFFoo\0x00Bar"

will not match, whereas

send "\0xFFFoo\0x00Bar"
expect "\0xFFFoo"

will.

My knowledge of C is fairly limited so I do not know if this is possible without jumping through some major hoops. I also do not know of any workarounds (sans writing your own protocol handler).

Comments (7)

  1. Tildeslash repo owner

    Good point.

    Not tested, but if you can escape \0 in the expect string returned from the server it might be possible to do a match. Alternatively, since we read the expect string into a fixed buffer we could easily do the escaping there. Not sure if it is worth the bother though. As long as the reply does not start with \0 you can always test a sub-string as you did in your example and be fairly certain the server works. However, there are strings out there such as SASL which does start with \0 and could be interesting to test.

  2. Christian Rinjes reporter

    I'm aware that this is an edge case, but wanted to bring it up anyway.

    If going the escaping route, both strings would need to be escaped, and would probably have to be done manually in the expect config to prevent more cans of worms with regex checks. Either way, even if it's a wontfix, this behaviour should at least be pointed out in the documentation IMHO.

  3. Tildeslash repo owner

    You can now test for '\0' in an expect string. We escape '\0' as "\0", that is, a '\' followed by the ASCII value for 0. For instance, here is how to test for an expect string that starts with zero followed by any number of characters:

    expect "^[\\]0.*". 
    

    If you care to test, download the latest build from https://bitbucket.org/tildeslash/monit/downloads, then build and install as follows

    • ./bootstrap && ./configure
    • make
    • make install

    If you do test, please let us know here how it went.

  4. Log in to comment