| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*============================================================================ |
| 6 | ** |
| 7 | ** Source: test2.c |
| 8 | ** |
| 9 | ** Purpose: Tests that WideCharToMultiByte respects the length of the wide |
| 10 | ** character string. |
| 11 | ** |
| 12 | ** |
| 13 | **==========================================================================*/ |
| 14 | |
| 15 | #include <palsuite.h> |
| 16 | |
| 17 | |
| 18 | int __cdecl main(int argc, char *argv[]) |
| 19 | { |
| 20 | char mbStr[128]; |
| 21 | WCHAR wideStr[128]; |
| 22 | int ret; |
| 23 | int i; |
| 24 | int k; |
| 25 | BOOL bRet=TRUE; |
| 26 | |
| 27 | /* These codepages are currently supported by the PAL */ |
| 28 | int codePages[] ={ |
| 29 | CP_ACP, |
| 30 | CP_UTF8 |
| 31 | }; |
| 32 | |
| 33 | if (PAL_Initialize(argc, argv)) |
| 34 | { |
| 35 | return FAIL; |
| 36 | } |
| 37 | |
| 38 | /* Go through all of the code pages */ |
| 39 | for(i=0; i<(sizeof(codePages)/sizeof(int)); i++) |
| 40 | { |
| 41 | |
| 42 | /* Filling the arrays */ |
| 43 | for (k=0; k<128; k++) |
| 44 | { |
| 45 | wideStr[k] = 'a'; |
| 46 | mbStr[i] = 0; |
| 47 | } |
| 48 | |
| 49 | wideStr[127] = 0; |
| 50 | |
| 51 | /* Passing a buffer that is too small */ |
| 52 | ret = WideCharToMultiByte(codePages[i], 0, wideStr, 10, |
| 53 | mbStr, 0, NULL, NULL); |
| 54 | if (ret != 10) |
| 55 | { |
| 56 | Trace("WideCharToMultiByte did not return correct string length!\n" |
| 57 | "Got %d, expected %d for %d with error %u.\n" , ret, 10, |
| 58 | codePages[i], GetLastError()); |
| 59 | bRet = FALSE; |
| 60 | } |
| 61 | |
| 62 | /* Passing a sufficiently large buffer */ |
| 63 | mbStr[10] = 'b'; |
| 64 | ret = WideCharToMultiByte(codePages[i], 0, wideStr, 10, |
| 65 | mbStr, 128, NULL, NULL); |
| 66 | if (ret != 10) |
| 67 | { |
| 68 | Trace("WideCharToMultiByte did not return correct string length!\n" |
| 69 | "Got %d, expected %d for code page %d with error %u.\n" , |
| 70 | ret, 10, codePages[i], GetLastError()); |
| 71 | bRet = FALSE; |
| 72 | } |
| 73 | |
| 74 | /* Verifying overflow of the destination string did not occur */ |
| 75 | if (mbStr[10] != 'b') |
| 76 | { |
| 77 | Trace("WideCharToMultiByte overflowed the destination buffer for " |
| 78 | "code page %d.\n" , codePages[i]); |
| 79 | bRet = FALSE; |
| 80 | } |
| 81 | |
| 82 | } |
| 83 | |
| 84 | int result = bRet ? PASS : FAIL; |
| 85 | PAL_TerminateEx(result); |
| 86 | return result; |
| 87 | } |
| 88 | |
| 89 | |