tstGuestCtrlParseBuffer.cpp revision bc626ccdbb077b0b1c82484b5d1ef12002b02b46
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync * Output stream parsing test cases.
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync * Copyright (C) 2011 Oracle Corporation
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync * available from http://www.virtualbox.org. This file is free software;
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync * you can redistribute it and/or modify it under the terms of the GNU
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync * General Public License (GPL) as published by the Free Software
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync/** @todo Use original source of GuestCtrlImpl.cpp! */
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync} VBOXGUESTCTRL_BUFFER_VALUE, *PVBOXGUESTCTRL_BUFFER_VALUE;
154ed29e9462101350c438a9ace34cb60d92e56fvboxsynctypedef std::map< RTCString, VBOXGUESTCTRL_BUFFER_VALUE > GuestBufferMap;
154ed29e9462101350c438a9ace34cb60d92e56fvboxsynctypedef std::map< RTCString, VBOXGUESTCTRL_BUFFER_VALUE >::iterator GuestBufferMapIter;
154ed29e9462101350c438a9ace34cb60d92e56fvboxsynctypedef std::map< RTCString, VBOXGUESTCTRL_BUFFER_VALUE >::const_iterator GuestBufferMapIterConst;
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsyncchar pszUnterm2[] = { 'f', 'o', 'o', '3', '=', 'b', 'a', 'r', '3' };
84d86b6582f583d8f79920843e87d0589816fcd3vboxsyncstatic struct
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync const char *pbData;
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync /* Invalid stuff. */
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync { "foo=bar1", 0, 0, 0, 0, VERR_INVALID_PARAMETER },
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync { "foo=bar2", 0, 50, 50, 0, VERR_INVALID_PARAMETER },
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync /* Incomplete buffer (missing \0 termination). */
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync { "foo1", sizeof("foo1"), 0, 0, 0, VERR_MORE_DATA },
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync /* Incomplete buffer (missing components). */
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync { "=bar\0", sizeof("=bar"), 0, 0, 0, VERR_MORE_DATA },
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync /* Last sequence is incomplete -- new offset should point to it. */
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync { "hug=sub\0incomplete", sizeof("hug=sub\0incomplete"), 0, sizeof("hug=sub"), 1, VERR_MORE_DATA },
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync { "boo=hoo\0baz=boo\0qwer", sizeof("boo=hoo\0baz=boo\0qwer"), 0, sizeof("boo=hoo\0baz=boo"), 2, VERR_MORE_DATA },
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync /* Parsing good stuff. */
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync { "foo2=", sizeof("foo2="), 0, sizeof("foo2="), 1, VINF_SUCCESS },
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync { "har=hor", sizeof("har=hor"), 0, sizeof("har=hor"), 1, VINF_SUCCESS },
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync { "foo=bar\0baz=boo", sizeof("foo=bar\0baz=boo"), 0, sizeof("foo=bar\0baz=boo"), 2, VINF_SUCCESS },
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync /* Parsing until a different block (two terminations, returning offset to next block). */
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync { "off=rab\0\0zab=oob", sizeof("off=rab\0\0zab=oob"), 0, sizeof("zab=oob"), 1, VERR_MORE_DATA }
84d86b6582f583d8f79920843e87d0589816fcd3vboxsyncint outputBufferParse(const BYTE *pbData, size_t cbData, uint32_t *puOffset, GuestBufferMap& mapBuf)
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync AssertReturn(*puOffset < cbData, VERR_INVALID_PARAMETER);
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync /* Search and of current pair (key=value\0). */
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync /* Get key (if present). */
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync /* Get value (if present). */
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync int rc = RTTestInitAndCreate("tstParseBuffer", &hTest);
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync RTTestFailed(hTest, "Basic size test failed (%u <-> 10)", sizeof("sizecheck"));
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++)
84d86b6582f583d8f79920843e87d0589816fcd3vboxsync int iResult = outputBufferParse((BYTE*)aTests[iTest].pbData, aTests[iTest].cbData,
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync RTTestIPrintf(RTTESTLVL_DEBUG, "=> Test #%u\n", iTest);
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync RTTestFailed(hTest, "\tReturned %Rrc, expected %Rrc",
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync else if (bufMap.size() != aTests[iTest].uMapElements)
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync RTTestFailed(hTest, "\tMap has %u elements, expected %u",
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync else if (aTests[iTest].uOffsetStart != aTests[iTest].uOffsetAfter)
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync RTTestFailed(hTest, "\tOffset %u wrong, expected %u",
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync aTests[iTest].uOffsetStart, aTests[iTest].uOffsetAfter);
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync /* There is remaining data left in the buffer (which needs to be merged
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync * with a following buffer) -- print it. */
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync size_t uToWrite = aTests[iTest].cbData - uOffsetNew;
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync RTTestIPrintf(RTTESTLVL_DEBUG, "\tRemaining (%u):\n", uToWrite);
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync RTStrmWriteEx(g_pStdOut, &aTests[iTest].pbData[uOffsetNew], uToWrite - 1, NULL);
bc626ccdbb077b0b1c82484b5d1ef12002b02b46vboxsync for (GuestBufferMapIter it = bufMap.begin(); it != bufMap.end(); it++)