[PATCH 1/3] crypt32/tests: Add tests for CryptStringToBinaryW()

Aaro Altonen a.altonen at hotmail.com
Sun Mar 15 08:00:40 CDT 2020


Add tests for CRYPT_STRING_HEX format

Signed-off-by: Aaro Altonen <a.altonen at hotmail.com>
---
 dlls/crypt32/tests/base64.c | 175 ++++++++++++++++++++++++++++++++++++
 1 file changed, 175 insertions(+)

diff --git a/dlls/crypt32/tests/base64.c b/dlls/crypt32/tests/base64.c
index 7b9f4c1077..8af0c5bae0 100644
--- a/dlls/crypt32/tests/base64.c
+++ b/dlls/crypt32/tests/base64.c
@@ -783,8 +783,183 @@ static void testStringToBinaryA(void)
     }
 }
 
+static void testStringToBinaryW(void)
+{
+    DWORD skip_ = 0;
+    DWORD flags = 0;
+    DWORD data_len = 0;
+    WCHAR *input;
+    BYTE out[256];
+    BYTE expected[256];
+    BOOL ret;
+
+    /* invalid parameteres -> 87 */
+    SetLastError(0xdeadbeef);
+    ret = CryptStringToBinaryW(NULL, 0, 0, NULL, NULL, NULL, NULL);
+    ok(!ret, "Got %u, expected zero\n", ret);
+    ok(GetLastError() == ERROR_INVALID_PARAMETER, "Got %u, expected 87\n", GetLastError());
+
+    /* otherwise valid parameters but invalid data_len -> 87 */
+    SetLastError(0xdeadbeef);
+    input = strdupAtoW("213c73796d6c696e6b3efffe");
+    ret = CryptStringToBinaryW(input, 24, CRYPT_STRING_HEX, NULL, NULL, NULL, NULL);
+    ok(!ret, "Got %u, expected zero\n", ret);
+    ok(GetLastError() == ERROR_INVALID_PARAMETER, "Got %d, expected 87\n", GetLastError());
+    heap_free(input);
+
+    /* uneven amount of data -> 13 */
+    SetLastError(0xdeadbeef);
+    input = strdupAtoW("111");
+    data_len = 0xdeadbeef;
+    ret = CryptStringToBinaryW(input, 0, CRYPT_STRING_HEX, NULL, &data_len, &skip_, &flags);
+    ok(!ret, "Got %u, expected zero\n", ret);
+    todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError());
+    todo_wine ok(data_len == 0, "Got %u, expected 0\n", data_len);
+    heap_free(input);
+
+    /* length is uneven -> 13 */
+    SetLastError(0xdeadbeef);
+    input = strdupAtoW("1111");
+    data_len = 0xdeadbeef;
+    ret = CryptStringToBinaryW(input, 3, CRYPT_STRING_HEX, NULL, &data_len, &skip_, &flags);
+    ok(!ret, "Got %u, expected zero\n", ret);
+    todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError());
+    todo_wine ok(data_len == 0, "Got %u, expected 0\n", data_len);
+    heap_free(input);
+
+    /* invalid 0x prefix -> 13 */
+    SetLastError(0xdeadbeef);
+    input = strdupAtoW("0x213c73796d6c696e6b3efffe");
+    data_len = 0xdeadbeef;
+    ret = CryptStringToBinaryW(input, 0, CRYPT_STRING_HEX, NULL, &data_len, &skip_, &flags);
+    ok(!ret, "Got %u, expected zero\n", ret);
+    todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError());
+    todo_wine ok(data_len == 0, "Got %u, expected 0\n", data_len);
+    heap_free(input);
+
+    /* invalid characters -> 13 */
+    SetLastError(0xdeadbeef);
+    input = strdupAtoW("abchhh");
+    data_len = 0xdeadbeef;
+    ret = CryptStringToBinaryW(input, 0, CRYPT_STRING_HEX, NULL, &data_len, NULL, NULL);
+    ok(!ret, "Got %u, expected zero\n", ret);
+    todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError());
+    todo_wine ok(data_len == 0, "Got %u, expected 0\n", data_len);
+    heap_free(input);
+
+    /* insufficient buffer -> 234 */
+    SetLastError(0xdeadbeef);
+    data_len = 4;
+    memset(out, 0, sizeof(out));
+    expected[0] = 0x21;
+    expected[1] = 0x3c;
+    expected[2] = 0x73;
+    expected[3] = 0x79;
+    input = strdupAtoW("213c73796d6c696e6b3efffe");
+    ret = CryptStringToBinaryW(input, 24, CRYPT_STRING_HEX, out, &data_len, &skip_, &flags);
+    ok(!ret, "Got %u, expected zero\n", ret);
+    todo_wine ok(GetLastError() == ERROR_MORE_DATA, "Got %d, expected 234\n", GetLastError());
+    ok(data_len == 4, "Got %u, expected 4\n", data_len);
+    todo_wine ok(!memcmp(out, expected, 4), "Invalid output from CryptStringToBinaryW()!\n");
+    heap_free(input);
+
+    /* valid data */
+    SetLastError(0xdeadbeef);
+    input = strdupAtoW("213c73796d6c696e6b3efffe");
+    data_len = 0xdeadbeef;
+    ret = CryptStringToBinaryW(input, 24, CRYPT_STRING_HEX, NULL, &data_len, &skip_, &flags);
+    todo_wine ok(ret, "Got %u, expected one\n", ret);
+    todo_wine ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError());
+    todo_wine ok(data_len == 12, "Got %u, expected 12\n", data_len);
+    heap_free(input);
+
+    /* valid data with white spaces  */
+    SetLastError(0xdeadbeef);
+    input = strdupAtoW("21 3c\t\t \r\n 73 796d6c \t69\t");
+    data_len = 0xdeadbeef;
+    SetLastError(0xdeadbeef);
+    ret = CryptStringToBinaryW(input, 25, CRYPT_STRING_HEX, NULL, &data_len, &skip_, &flags);
+    todo_wine ok(ret, "Got %u, expected one\n", ret);
+    todo_wine ok(GetLastError() == 0xdeadbeef, "Got %d, expected 0xdeadbeef\n", GetLastError());
+    todo_wine ok(data_len == 7, "Got %u, expected 7\n", data_len);
+    heap_free(input);
+
+    /* valid data with white spaces but spacing breaks the valid data into invalid chunks */
+    SetLastError(0xdeadbeef);
+    input = strdupAtoW("21 3 c");
+    data_len = 0xdeadbeef;
+    ret = CryptStringToBinaryW(input, 0, CRYPT_STRING_HEX, NULL, &data_len, &skip_, &flags);
+    ok(!ret, "Got %u, expected zero\n", ret);
+    todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError());
+    todo_wine ok(data_len == 0, "Got %u, expected 0\n", data_len);
+    heap_free(input);
+
+    /* if "input" contains both valid and invalid data and "out" is valid, "out" shall contain all valid bytes
+     * until an invalid sequence is reached */
+    SetLastError(0xdeadbeef);
+    data_len = 4;
+    memset(out, 0, sizeof(out));
+    input = strdupAtoW("21 3 c ff");
+    ret = CryptStringToBinaryW(input, 0, CRYPT_STRING_HEX, out, &data_len, &skip_, &flags);
+    ok(!ret, "Got %u, expected zero\n", ret);
+    todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError());
+    ok(data_len == 4, "Got %u, expected 4\n", data_len);
+    heap_free(input);
+
+    /* valid data */
+    SetLastError(0xdeadbeef);
+    expected[0]  = 0x21; expected[1]  = 0x3c; expected[2]  = 0x73;
+    expected[3]  = 0x79; expected[4]  = 0x6d; expected[5]  = 0x6c;
+    expected[6]  = 0x69; expected[7]  = 0x6e; expected[8]  = 0x6b;
+    expected[9]  = 0x3e; expected[10] = 0xff; expected[11] = 0xfe;
+    input = strdupAtoW("213c73796d6c696e6b3efffe");
+    data_len = 256;
+    ret = CryptStringToBinaryW(input, 24, CRYPT_STRING_HEX, out, &data_len, &skip_, &flags);
+    todo_wine ok(ret, "Got %u, expected one\n", ret);
+    todo_wine ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError());
+    todo_wine ok(data_len == 12, "Got %u, expected 12\n", data_len);
+    todo_wine ok(!memcmp(out, expected, 12), "Invalid output from CryptStringToBinaryW()!\n");
+    heap_free(input);
+
+    /* invalid data but length small enough that it's never detected */
+    SetLastError(0xdeadbeef);
+    input = strdupAtoW("abcdefhhh");
+    data_len = 0xdeadbeef;
+    ret = CryptStringToBinaryW(input, 4, CRYPT_STRING_HEX, NULL, &data_len, NULL, NULL);
+    todo_wine ok(ret, "Got %u, expected one\n", ret);
+    todo_wine ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError());
+    todo_wine ok(data_len == 2, "Got %u, expected 2\n", data_len);
+    heap_free(input);
+
+    /* invalid data but length small enough that it's never detected, with whitespaces */
+    SetLastError(0xdeadbeef);
+    memset(out, 0, sizeof(out));
+    input = strdupAtoW("\t\t21 fe");
+    data_len = 256;
+    ret = CryptStringToBinaryW(input, 5, CRYPT_STRING_HEX, out, &data_len, &skip_, &flags);
+    todo_wine ok(ret, "Got %u, expected one\n", ret);
+    todo_wine ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError());
+    todo_wine ok(data_len == 1, "Got %u, expected 1\n", data_len);
+    heap_free(input);
+
+    /* valid data but parse only the first 6 bytes (12 chars) */
+    SetLastError(0xdeadbeef);
+    memset(out, 0, sizeof(out));
+    expected[0]  = 0x21; expected[1]  = 0x3c; expected[2]  = 0x73;
+    expected[3]  = 0x79; expected[4]  = 0x6d; expected[5]  = 0x6c;
+    input = strdupAtoW("213c73796d6c696e6b3efffe");
+    data_len = 256;
+    ret = CryptStringToBinaryW(input, 12, CRYPT_STRING_HEX, out, &data_len, &skip_, &flags);
+    todo_wine ok(ret, "Got %u, expected one\n", ret);
+    todo_wine ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError());
+    todo_wine ok(data_len == 6, "Got %u, expected 6\n", data_len);
+    todo_wine ok(!memcmp(out, expected, 6), "Invalid output from CryptStringToBinaryW()!\n");
+    heap_free(input);
+}
+
 START_TEST(base64)
 {
     test_CryptBinaryToString();
     testStringToBinaryA();
+    testStringToBinaryW();
 }
-- 
2.25.1




More information about the wine-devel mailing list