Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

450 lines
9.2 KiB

  1. #include <tchar.h>
  2. #include <stdio.h>
  3. #include <io.h>
  4. #include <objbase.h>
  5. #include "sfstr.h"
  6. #include "dbg.h"
  7. #define ARRAYSIZE(a) (sizeof((a))/sizeof((a)[0]))
  8. const WCHAR szOne[] = TEXT("0");
  9. const WCHAR szTwo[] = TEXT("01");
  10. const WCHAR szThree[] = TEXT("012");
  11. const WCHAR szFour[] = TEXT("0123");
  12. const WCHAR szFive[] = TEXT("01234");
  13. int DoTest(int , wchar_t* [])
  14. {
  15. HRESULT hresTest = S_OK;
  16. // For all fcts call
  17. HRESULT hres;
  18. WCHAR szDestTwo[2];
  19. WCHAR szDestThree[3];
  20. WCHAR szDestFour[4];
  21. WCHAR szDestFive[5];
  22. WCHAR szDestSix[6];
  23. ///////////////////////////////////////////////////////////////////////////////
  24. //
  25. // SafeStrCxxN
  26. ///////////////////////////////////////////////////////////////////////////
  27. //
  28. // (1)
  29. if (SUCCEEDED(hresTest))
  30. {
  31. hres = SafeStrCpyN(szDestFour, szThree, ARRAYSIZE(szDestFour));
  32. if (FAILED(hres))
  33. {
  34. hresTest = E_FAIL;
  35. }
  36. }
  37. // (2)
  38. if (SUCCEEDED(hresTest))
  39. {
  40. hres = SafeStrCpyN(szDestThree, szThree, ARRAYSIZE(szDestThree));
  41. if (SUCCEEDED(hres))
  42. {
  43. hresTest = E_FAIL;
  44. }
  45. }
  46. // (3)
  47. if (SUCCEEDED(hresTest))
  48. {
  49. hres = SafeStrCpyN(szDestFive, szThree, ARRAYSIZE(szDestFive));
  50. if (FAILED(hres))
  51. {
  52. hresTest = E_FAIL;
  53. }
  54. }
  55. ///////////////////////////////////////////////////////////////////////////
  56. //
  57. // (1)
  58. if (SUCCEEDED(hresTest))
  59. {
  60. hres = SafeStrCpyN(szDestSix, szThree, ARRAYSIZE(szDestSix));
  61. if (FAILED(hres))
  62. {
  63. hresTest = E_FAIL;
  64. }
  65. }
  66. if (SUCCEEDED(hresTest))
  67. {
  68. hres = SafeStrCatN(szDestSix, szTwo, ARRAYSIZE(szDestSix));
  69. if (FAILED(hres))
  70. {
  71. hresTest = E_FAIL;
  72. }
  73. }
  74. // (2)
  75. if (SUCCEEDED(hresTest))
  76. {
  77. hres = SafeStrCpyN(szDestSix, szThree, ARRAYSIZE(szDestSix));
  78. if (FAILED(hres))
  79. {
  80. hresTest = E_FAIL;
  81. }
  82. }
  83. if (SUCCEEDED(hresTest))
  84. {
  85. hres = SafeStrCatN(szDestSix, szThree, ARRAYSIZE(szDestSix));
  86. if (SUCCEEDED(hres))
  87. {
  88. hresTest = E_FAIL;
  89. }
  90. }
  91. // (3)
  92. if (SUCCEEDED(hresTest))
  93. {
  94. hres = SafeStrCpyN(szDestSix, szTwo, ARRAYSIZE(szDestSix));
  95. if (FAILED(hres))
  96. {
  97. hresTest = E_FAIL;
  98. }
  99. }
  100. if (SUCCEEDED(hresTest))
  101. {
  102. hres = SafeStrCatN(szDestSix, szTwo, ARRAYSIZE(szDestSix));
  103. if (FAILED(hres))
  104. {
  105. hresTest = E_FAIL;
  106. }
  107. }
  108. ///////////////////////////////////////////////////////////////////////////////
  109. //
  110. // SafeStrCxxNEx
  111. ///////////////////////////////////////////////////////////////////////////
  112. //
  113. // (1)
  114. LPWSTR pszNext = 0;
  115. DWORD cchLeft = 0;
  116. if (SUCCEEDED(hresTest))
  117. {
  118. hres = SafeStrCpyNEx(szDestFour, szThree, ARRAYSIZE(szDestFour), &pszNext,
  119. &cchLeft);
  120. if (FAILED(hres) || (1 != cchLeft))
  121. {
  122. hresTest = E_FAIL;
  123. }
  124. }
  125. // (2)
  126. if (SUCCEEDED(hresTest))
  127. {
  128. hres = SafeStrCpyNEx(szDestThree, szThree, ARRAYSIZE(szDestThree),
  129. &pszNext, &cchLeft);
  130. if (SUCCEEDED(hres))
  131. {
  132. hresTest = E_FAIL;
  133. }
  134. }
  135. // (3)
  136. if (SUCCEEDED(hresTest))
  137. {
  138. hres = SafeStrCpyNEx(szDestFive, szThree, ARRAYSIZE(szDestFive), &pszNext,
  139. &cchLeft);
  140. if (FAILED(hres) || (2 != cchLeft))
  141. {
  142. hresTest = E_FAIL;
  143. }
  144. }
  145. ///////////////////////////////////////////////////////////////////////////
  146. //
  147. // (1)
  148. if (SUCCEEDED(hresTest))
  149. {
  150. hres = SafeStrCpyNEx(szDestSix, szThree, ARRAYSIZE(szDestSix), &pszNext,
  151. &cchLeft);
  152. if (FAILED(hres) || (3 != cchLeft))
  153. {
  154. hresTest = E_FAIL;
  155. }
  156. }
  157. if (SUCCEEDED(hresTest))
  158. {
  159. hres = SafeStrCpyN(pszNext, szTwo, cchLeft);
  160. if (FAILED(hres))
  161. {
  162. hresTest = E_FAIL;
  163. }
  164. }
  165. // (2)
  166. if (SUCCEEDED(hresTest))
  167. {
  168. hres = SafeStrCpyNEx(szDestSix, szThree, ARRAYSIZE(szDestSix), &pszNext,
  169. &cchLeft);
  170. if (FAILED(hres) || (3 != cchLeft))
  171. {
  172. hresTest = E_FAIL;
  173. }
  174. }
  175. if (SUCCEEDED(hresTest))
  176. {
  177. hres = SafeStrCpyN(pszNext, szThree, cchLeft);
  178. if (SUCCEEDED(hres))
  179. {
  180. hresTest = E_FAIL;
  181. }
  182. }
  183. // (3)
  184. if (SUCCEEDED(hresTest))
  185. {
  186. hres = SafeStrCpyNEx(szDestSix, szTwo, ARRAYSIZE(szDestSix), &pszNext,
  187. &cchLeft);
  188. if (FAILED(hres) || (4 != cchLeft))
  189. {
  190. hresTest = E_FAIL;
  191. }
  192. }
  193. if (SUCCEEDED(hresTest))
  194. {
  195. hres = SafeStrCpyN(pszNext, szTwo, cchLeft);
  196. if (FAILED(hres))
  197. {
  198. hresTest = E_FAIL;
  199. }
  200. }
  201. ///////////////////////////////////////////////////////////////////////////
  202. //
  203. // (1)
  204. if (SUCCEEDED(hresTest))
  205. {
  206. hres = SafeStrCpyN(szDestSix, szThree, ARRAYSIZE(szDestSix));
  207. if (FAILED(hres))
  208. {
  209. hresTest = E_FAIL;
  210. }
  211. }
  212. if (SUCCEEDED(hresTest))
  213. {
  214. hres = SafeStrCatNEx(szDestSix, szTwo, ARRAYSIZE(szDestSix), &pszNext,
  215. &cchLeft);
  216. if (FAILED(hres) || (1 != cchLeft))
  217. {
  218. hresTest = E_FAIL;
  219. }
  220. }
  221. // (2)
  222. if (SUCCEEDED(hresTest))
  223. {
  224. hres = SafeStrCpyN(szDestSix, szThree, ARRAYSIZE(szDestSix));
  225. if (FAILED(hres))
  226. {
  227. hresTest = E_FAIL;
  228. }
  229. }
  230. if (SUCCEEDED(hresTest))
  231. {
  232. hres = SafeStrCatNEx(szDestSix, szThree, ARRAYSIZE(szDestSix), &pszNext,
  233. &cchLeft);
  234. if (SUCCEEDED(hres))
  235. {
  236. hresTest = E_FAIL;
  237. }
  238. }
  239. // (3)
  240. if (SUCCEEDED(hresTest))
  241. {
  242. hres = SafeStrCpyN(szDestSix, szTwo, ARRAYSIZE(szDestSix));
  243. if (FAILED(hres))
  244. {
  245. hresTest = E_FAIL;
  246. }
  247. }
  248. if (SUCCEEDED(hresTest))
  249. {
  250. hres = SafeStrCatNEx(szDestSix, szTwo, ARRAYSIZE(szDestSix), &pszNext,
  251. &cchLeft);
  252. if (FAILED(hres) || (2 != cchLeft))
  253. {
  254. hresTest = E_FAIL;
  255. }
  256. }
  257. ///////////////////////////////////////////////////////////////////////////////
  258. //
  259. // SafeStrCpyNExact
  260. ///////////////////////////////////////////////////////////////////////////
  261. //
  262. // (1)
  263. if (SUCCEEDED(hresTest))
  264. {
  265. hres = SafeStrCpyNExact(szDestFour, szThree, ARRAYSIZE(szDestFour), 1);
  266. if (FAILED(hres))
  267. {
  268. hresTest = E_FAIL;
  269. }
  270. }
  271. //
  272. if (SUCCEEDED(hresTest))
  273. {
  274. hres = SafeStrCpyNExact(szDestFour, szThree, ARRAYSIZE(szDestFour), 2);
  275. if (FAILED(hres))
  276. {
  277. hresTest = E_FAIL;
  278. }
  279. }
  280. //
  281. if (SUCCEEDED(hresTest))
  282. {
  283. hres = SafeStrCpyNExact(szDestFour, szThree, ARRAYSIZE(szDestFour), 3);
  284. if (FAILED(hres))
  285. {
  286. hresTest = E_FAIL;
  287. }
  288. }
  289. //
  290. if (SUCCEEDED(hresTest))
  291. {
  292. hres = SafeStrCpyNExact(szDestFour, szThree, ARRAYSIZE(szDestFour), 4);
  293. if (FAILED(hres))
  294. {
  295. hresTest = E_FAIL;
  296. }
  297. }
  298. //
  299. if (SUCCEEDED(hresTest))
  300. {
  301. hres = SafeStrCpyNExact(szDestFour, szThree, ARRAYSIZE(szDestFour), 5);
  302. if (SUCCEEDED(hres) || (E_SOURCEBUFFERTOOSMALL != hres))
  303. {
  304. hresTest = E_FAIL;
  305. }
  306. }
  307. //
  308. if (SUCCEEDED(hresTest))
  309. {
  310. hres = SafeStrCpyNExact(szDestTwo, szThree, ARRAYSIZE(szDestTwo), 5);
  311. if (SUCCEEDED(hres) || (E_BUFFERTOOSMALL != hres))
  312. {
  313. hresTest = E_FAIL;
  314. }
  315. }
  316. ///////////////////////////////////////////////////////////////////////////////
  317. //
  318. // SafeStrCpyNExactEx
  319. ///////////////////////////////////////////////////////////////////////////
  320. //
  321. // (1)
  322. if (SUCCEEDED(hresTest))
  323. {
  324. hres = SafeStrCpyNExactEx(szDestFour, szThree, ARRAYSIZE(szDestFour), 1,
  325. &pszNext, &cchLeft);
  326. if (FAILED(hres) || (4 != cchLeft))
  327. {
  328. hresTest = E_FAIL;
  329. }
  330. }
  331. //
  332. if (SUCCEEDED(hresTest))
  333. {
  334. hres = SafeStrCpyNExactEx(szDestFour, szThree, ARRAYSIZE(szDestFour), 2,
  335. &pszNext, &cchLeft);
  336. if (FAILED(hres) || (3 != cchLeft))
  337. {
  338. hresTest = E_FAIL;
  339. }
  340. }
  341. //
  342. if (SUCCEEDED(hresTest))
  343. {
  344. hres = SafeStrCpyNExactEx(szDestFour, szThree, ARRAYSIZE(szDestFour), 3,
  345. &pszNext, &cchLeft);
  346. if (FAILED(hres) || (2 != cchLeft))
  347. {
  348. hresTest = E_FAIL;
  349. }
  350. }
  351. //
  352. if (SUCCEEDED(hresTest))
  353. {
  354. hres = SafeStrCpyNExactEx(szDestFour, szThree, ARRAYSIZE(szDestFour), 4,
  355. &pszNext, &cchLeft);
  356. if (FAILED(hres) || (1 != cchLeft))
  357. {
  358. hresTest = E_FAIL;
  359. }
  360. }
  361. //
  362. if (SUCCEEDED(hresTest))
  363. {
  364. hres = SafeStrCpyNExactEx(szDestFour, szThree, ARRAYSIZE(szDestFour), 5,
  365. &pszNext, &cchLeft);
  366. if (SUCCEEDED(hres))
  367. {
  368. hresTest = E_FAIL;
  369. }
  370. }
  371. return SUCCEEDED(hresTest);
  372. }