Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 1 | /*============================================================================== |
| 2 | Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. |
| 3 | |
| 4 | Redistribution and use in source and binary forms, with or without |
| 5 | modification, are permitted provided that the following conditions are |
| 6 | met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above |
| 10 | copyright notice, this list of conditions and the following |
| 11 | disclaimer in the documentation and/or other materials provided |
| 12 | with the distribution. |
| 13 | * Neither the name of The Linux Foundation nor the names of its |
| 14 | contributors may be used to endorse or promote products derived |
| 15 | from this software without specific prior written permission. |
| 16 | |
| 17 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | ==============================================================================*/ |
| 29 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 30 | /*============================================================================== |
| 31 | Modifications beyond the version released on CAF are under the MIT license: |
| 32 | |
| 33 | Copyright 2018 Laurence Lundblade |
| 34 | |
| 35 | Permission is hereby granted, free of charge, to any person obtaining |
| 36 | a copy of this software and associated documentation files (the |
| 37 | "Software"), to deal in the Software without restriction, including |
| 38 | without limitation the rights to use, copy, modify, merge, publish, |
| 39 | distribute, sublicense, and/or sell copies of the Software, and to |
| 40 | permit persons to whom the Software is furnished to do so, subject to |
| 41 | the following conditions: |
| 42 | |
| 43 | The above copyright notice and this permission notice shall be included |
| 44 | in all copies or substantial portions of the Software. |
| 45 | |
| 46 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 47 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 48 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 49 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 50 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 51 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 52 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 53 | SOFTWARE. |
| 54 | ==============================================================================*/ |
| 55 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 56 | #include "UsefulBuf.h" |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 57 | |
| 58 | |
| 59 | /* Basic exercise... |
| 60 | |
| 61 | Call all the main public functions. |
| 62 | |
| 63 | Binary compare the result to the expected. |
| 64 | |
| 65 | There is nothing adversarial in this test |
| 66 | */ |
| 67 | const char * NonAdversarialUOBTest() |
| 68 | { |
| 69 | const char *szReturn = NULL; |
| 70 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 71 | UsefulBuf_MakeStackUB(outbuf,50); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 72 | |
| 73 | UsefulOutBuf UOB; |
| 74 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 75 | UsefulOutBuf_Init(&UOB, outbuf); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 76 | |
| 77 | if(!UsefulOutBuf_AtStart(&UOB)) { |
| 78 | szReturn = "Not at start"; |
| 79 | goto Done; |
| 80 | } |
| 81 | |
| 82 | // Put 7 bytes at beginning of buf |
| 83 | UsefulOutBuf_AppendData(&UOB, "bluster", 7); |
| 84 | |
| 85 | if(UsefulOutBuf_AtStart(&UOB)) { |
| 86 | szReturn = "At start"; |
| 87 | goto Done; |
| 88 | } |
| 89 | |
| 90 | // add a space to end |
| 91 | UsefulOutBuf_AppendByte(&UOB, ' '); |
| 92 | |
| 93 | // Add 5 bytes to the end |
| 94 | UsefulBufC UBC = {"hunny", 5}; |
| 95 | UsefulOutBuf_AppendUsefulBuf(&UOB, UBC); |
| 96 | |
| 97 | // Insert 9 bytes at the beginning, slide the previous stuff right |
| 98 | UsefulOutBuf_InsertData(&UOB, "heffalump", 9, 0); |
| 99 | UsefulOutBuf_InsertByte(&UOB, ' ', 9); |
| 100 | |
| 101 | // Put 9 bytes in at position 10 -- just after "heffalump " |
| 102 | UsefulBufC UBC2 = {"unbounce ", 9}; |
| 103 | UsefulOutBuf_InsertUsefulBuf(&UOB, UBC2, 10); |
| 104 | |
| 105 | // Make it a null terminated string (because all the appends and inserts above not strcpy !) |
| 106 | UsefulOutBuf_AppendByte(&UOB, '\0'); |
| 107 | |
| 108 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 109 | UsefulBufC U = UsefulOutBuf_OutUBuf(&UOB); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 110 | |
| 111 | const char *expected = "heffalump unbounce bluster hunny"; |
| 112 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 113 | if(UsefulBuf_IsNULLC(U) || U.len-1 != strlen(expected) || strcmp(expected, U.ptr) || UsefulOutBuf_GetError(&UOB)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 114 | szReturn = "OutUBuf"; |
| 115 | } |
| 116 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 117 | UsefulBuf_MakeStackUB(buf, 50); |
| 118 | UsefulBufC Out = UsefulOutBuf_CopyOut(&UOB, buf); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 119 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 120 | if(UsefulBuf_IsNULLC(Out) || Out.len-1 != strlen(expected) || strcmp(expected, Out.ptr)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 121 | szReturn = "CopyOut"; |
| 122 | } |
| 123 | |
| 124 | Done: |
| 125 | return szReturn; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /* |
| 130 | Append test utility. |
| 131 | pUOB is the buffer to append too |
| 132 | num is the amount to append |
| 133 | expected is the expected return code, 0 or 1 |
| 134 | |
| 135 | returns 0 if test passed |
| 136 | |
| 137 | */ |
| 138 | static int AppendTest(UsefulOutBuf *pUOB, size_t num, int expected) |
| 139 | { |
| 140 | //reset |
| 141 | UsefulOutBuf_Reset(pUOB); |
| 142 | |
| 143 | // check status first |
| 144 | if(UsefulOutBuf_GetError(pUOB)) |
| 145 | return 1; |
| 146 | |
| 147 | // append the bytes |
| 148 | UsefulOutBuf_AppendData(pUOB, (const uint8_t *)"bluster", num); |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 149 | |
| 150 | // TODO: test UsefulOutBuf_AppendString |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 151 | |
| 152 | // check error status after |
| 153 | if(UsefulOutBuf_GetError(pUOB) != expected) |
| 154 | return 1; |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /* |
| 161 | Same as append, but takes a position param too |
| 162 | */ |
| 163 | static int InsertTest(UsefulOutBuf *pUOB, size_t num, size_t pos, int expected) |
| 164 | { |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 165 | // reset |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 166 | UsefulOutBuf_Reset(pUOB); |
| 167 | |
| 168 | // check |
| 169 | if(UsefulOutBuf_GetError(pUOB)) |
| 170 | return 1; |
| 171 | |
| 172 | UsefulOutBuf_InsertData(pUOB, (const uint8_t *)"bluster", num, pos); |
| 173 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 174 | // TODO: test UsefulOutBuf_InsertString |
| 175 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 176 | if(UsefulOutBuf_GetError(pUOB) != expected) |
| 177 | return 1; |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /* |
| 184 | Boundary conditions to test |
| 185 | - around 0 |
| 186 | - around the buffer size |
| 187 | - around MAX size_t |
| 188 | |
| 189 | |
| 190 | Test these for the buffer size and the cursor, the insert amount, the append amount and the insert position |
| 191 | |
| 192 | */ |
| 193 | |
| 194 | const char *BoundaryConditionsTest() |
| 195 | { |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 196 | UsefulBuf_MakeStackUB(outbuf,2); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 197 | |
| 198 | UsefulOutBuf UOB; |
| 199 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 200 | UsefulOutBuf_Init(&UOB, outbuf); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 201 | |
| 202 | // append 0 byte to a 2 byte buffer --> success |
| 203 | if(AppendTest(&UOB, 0, 0)) |
| 204 | return "Append 0 bytes failed"; |
| 205 | |
| 206 | // append 1 byte to a 2 byte buffer --> success |
| 207 | if(AppendTest(&UOB, 1, 0)) |
| 208 | return "Append of 1 byte failed"; |
| 209 | |
| 210 | // append 2 byte to a 2 byte buffer --> success |
| 211 | if(AppendTest(&UOB, 2, 0)) |
| 212 | return "Append to fill buffer failed"; |
| 213 | |
| 214 | // append 3 bytes to a 2 byte buffer --> failure |
| 215 | if(AppendTest(&UOB, 3, 1)) |
| 216 | return "Overflow of buffer not caught"; |
| 217 | |
| 218 | // append max size_t to a 2 byte buffer --> failure |
| 219 | if(AppendTest(&UOB, SIZE_MAX, 1)) |
| 220 | return "Append of SIZE_MAX error not caught"; |
| 221 | |
| 222 | if(InsertTest(&UOB, 1, 0, 0)) |
| 223 | return "Insert 1 byte at start failed"; |
| 224 | |
| 225 | if(InsertTest(&UOB, 2, 0, 0)) |
| 226 | return "Insert 2 bytes at start failed"; |
| 227 | |
| 228 | if(InsertTest(&UOB, 3, 0, 1)) |
| 229 | return "Insert overflow not caught"; |
| 230 | |
| 231 | if(InsertTest(&UOB, 1, 1, 1)) |
| 232 | return "Bad insertion point not caught"; |
| 233 | |
| 234 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 235 | UsefulBuf_MakeStackUB(outBuf2,10); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 236 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 237 | UsefulOutBuf_Init(&UOB, outBuf2); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 238 | |
| 239 | UsefulOutBuf_Reset(&UOB); |
| 240 | // put data in the buffer |
| 241 | UsefulOutBuf_AppendData(&UOB, "abc123", 6); |
| 242 | |
| 243 | UsefulOutBuf_InsertData(&UOB, "xyz*&^", 6, 0); |
| 244 | |
| 245 | if(!UsefulOutBuf_GetError(&UOB)) { |
| 246 | return "insert with data should have failed"; |
| 247 | } |
| 248 | |
| 249 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 250 | UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 251 | UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -6); |
| 252 | if(UsefulOutBuf_GetError(&UOB)) { |
| 253 | return "insert in huge should have succeeded"; |
| 254 | } |
| 255 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 256 | UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 257 | UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -5); |
| 258 | if(UsefulOutBuf_GetError(&UOB)) { |
| 259 | return "insert in huge should have succeeded"; |
| 260 | } |
| 261 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 262 | UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5}); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 263 | UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX - 4); |
| 264 | if(!UsefulOutBuf_GetError(&UOB)) { |
| 265 | return "lengths near max size"; |
| 266 | } |
| 267 | |
| 268 | return NULL; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | // Test function to get size and magic number check |
| 276 | |
| 277 | const char *TestBasicSanity() |
| 278 | { |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 279 | UsefulBuf_MakeStackUB(outbuf,10); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 280 | |
| 281 | UsefulOutBuf UOB; |
| 282 | |
| 283 | // First -- make sure that the room left function returns the right amount |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 284 | UsefulOutBuf_Init(&UOB, outbuf); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 285 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 286 | if(UsefulOutBuf_RoomLeft(&UOB) != 10) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 287 | return "room left failed"; |
| 288 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 289 | // TODO: test UsefulOutBuf_WillItFit |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 290 | |
| 291 | // Next -- make sure that the magic number checking is working right |
| 292 | UOB.magic = 8888; // make magic bogus |
| 293 | |
| 294 | UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7); |
| 295 | |
| 296 | if(!UsefulOutBuf_GetError(&UOB)) |
| 297 | return "magic corruption check failed"; |
| 298 | |
| 299 | |
| 300 | |
| 301 | // Next make sure that the valid data length check is working right |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 302 | UsefulOutBuf_Init(&UOB, outbuf); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 303 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 304 | UOB.data_len = UOB.UB.len+1; // make size bogus |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 305 | |
| 306 | UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7); |
| 307 | if(!UsefulOutBuf_GetError(&UOB)) |
| 308 | return "valid data check failed"; |
| 309 | |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | |
| 315 | const char *UBMacroConversionsTest() |
| 316 | { |
| 317 | char *szFoo = "foo"; |
| 318 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 319 | UsefulBufC Foo = UsefulBuf_FromSZ(szFoo); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 320 | if(Foo.len != 3 || strncmp(Foo.ptr, szFoo, 3)) |
| 321 | return "SZToUsefulBufC failed"; |
| 322 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 323 | UsefulBufC Too = UsefulBuf_FromSZLiteral("Toooo"); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 324 | if(Too.len != 5 || strncmp(Too.ptr, "Toooo", 5)) |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 325 | return "UsefulBuf_FromSZLiteral failed"; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 326 | |
| 327 | uint8_t pB[] = {0x42, 0x6f, 0x6f}; |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 328 | UsefulBufC Boo = UsefulBuf_FromByteArrayLiteral(pB); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 329 | if(Boo.len != 3 || strncmp(Boo.ptr, "Boo", 3)) |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 330 | return "UsefulBuf_FromByteArrayLiteral failed"; |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 331 | |
| 332 | char *sz = "not const"; // some data for the test |
| 333 | UsefulBuf B = (UsefulBuf){sz, sizeof(sz)}; |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 334 | UsefulBufC BC = UsefulBuf_Const(B); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 335 | if(BC.len != sizeof(sz) || BC.ptr != sz) |
| 336 | return "UsefulBufConst failed"; |
| 337 | |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | const char *UBUtilTests() |
| 343 | { |
| 344 | UsefulBuf UB = NULLUsefulBuf; |
| 345 | |
| 346 | if(!UsefulBuf_IsNULL(UB)){ |
| 347 | return "IsNull failed"; |
| 348 | } |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 349 | |
| 350 | if(!UsefulBuf_IsEmpty(UB)){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 351 | return "IsEmpty failed"; |
| 352 | } |
| 353 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 354 | if(!UsefulBuf_IsNULLOrEmpty(UB)) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 355 | return "IsNULLOrEmpty failed"; |
| 356 | } |
| 357 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 358 | UsefulBufC UBC = UsefulBuf_Const(UB); |
| 359 | |
| 360 | if(!UsefulBuf_IsNULLC(UBC)){ |
| 361 | return "IsNull const failed"; |
| 362 | } |
| 363 | |
| 364 | if(!UsefulBuf_IsEmptyC(UBC)){ |
| 365 | return "IsEmptyC failed"; |
| 366 | } |
| 367 | |
| 368 | if(!UsefulBuf_IsNULLOrEmptyC(UBC)){ |
| 369 | return "IsNULLOrEmptyC failed"; |
| 370 | } |
| 371 | |
| 372 | UsefulBuf UB2 = UsefulBuf_Unconst(UBC); |
| 373 | if(!UsefulBuf_IsEmpty(UB2)) { |
| 374 | return "Back to UB is Empty failed"; |
| 375 | } |
| 376 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 377 | UB.ptr = "x"; // just some valid pointer |
| 378 | |
| 379 | if(UsefulBuf_IsNULL(UB)){ |
| 380 | return "IsNull failed"; |
| 381 | } |
| 382 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 383 | if(!UsefulBuf_IsEmptyC(UBC)){ |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 384 | return "IsEmpty failed"; |
| 385 | } |
| 386 | |
| 387 | // test the Unconst. |
| 388 | if(UsefulBufC_Unconst(UBC).ptr != NULL) { |
| 389 | return "Unconst failed"; |
| 390 | } |
| 391 | |
| 392 | // Set 100 bytes of '+'; validated a few tests later |
| 393 | MakeUsefulBufOnStack(Temp, 100); |
| 394 | UsefulBuf_Set(&Temp, '+'); |
| 395 | |
| 396 | // Try to copy into a buf that is too small and see failure |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 397 | UsefulBuf_MakeStackUB(Temp2, 99); |
| 398 | if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp2, UsefulBuf_Const(Temp)))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 399 | return "Copy should have failed"; |
| 400 | } |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 401 | |
| 402 | // TODO: complete this test |
| 403 | // UsefulBuf_CopyOffset(); |
| 404 | // TODO: complete this test |
| 405 | // UsefulBuf_CopyPtr(); |
| 406 | // TODO: complete this test |
| 407 | // UsefulBuf_Head(); |
| 408 | // TODO: complete this test |
| 409 | // UsefulBuf_CopyPtr(); |
| 410 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 411 | // Try to copy into a NULL/empty buf and see failure |
| 412 | UsefulBuf UBNull = NULLUsefulBuf; |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 413 | if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(UBNull, UsefulBuf_Const(Temp)))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 414 | return "Copy to NULL should have failed"; |
| 415 | } |
| 416 | |
| 417 | // Try to set a NULL/empty buf; nothing should happen |
| 418 | UsefulBuf_Set(&UBNull, '+'); // This will crash on failure |
| 419 | |
| 420 | // Copy successfully to a buffer |
| 421 | MakeUsefulBufOnStack(Temp3, 101); |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 422 | if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp3, UsefulBuf_Const(Temp)))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 423 | return "Copy should not have failed"; |
| 424 | } |
| 425 | |
| 426 | static const uint8_t pExpected[] = { |
| 427 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 428 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 429 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 430 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 431 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 432 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 433 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 434 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 435 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 436 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 437 | }; |
| 438 | UsefulBufC Expected = ByteArrayLiteralToUsefulBufC(pExpected); |
| 439 | // This validates comparison for equality and the UsefulBuf_Set |
| 440 | if(UsefulBuf_Compare(Expected, UsefulBuf_Const(Temp))) { |
| 441 | return "Set / Copy / Compare failed"; |
| 442 | } |
| 443 | |
| 444 | // Compare two empties and expect success |
| 445 | if(UsefulBuf_Compare(NULLUsefulBufC, NULLUsefulBufC)){ |
| 446 | return "Compare Empties failed"; |
| 447 | } |
| 448 | |
| 449 | // Compare with empty and expect the first to be larger |
| 450 | if(UsefulBuf_Compare(Expected, NULLUsefulBufC) <= 0){ |
| 451 | return "Compare with empty failed"; |
| 452 | } |
| 453 | |
| 454 | |
| 455 | static const uint8_t pExpectedBigger[] = { |
| 456 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 457 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 458 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 459 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 460 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 461 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 462 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 463 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 464 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 465 | '+', '+', '+', '+', '+', '+', '+', '+', '+', ',', |
| 466 | }; |
| 467 | UsefulBufC ExpectedBigger = ByteArrayLiteralToUsefulBufC(pExpectedBigger); |
| 468 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 469 | // Expect -1 when the first arg is smaller |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 470 | if(UsefulBuf_Compare(Expected, ExpectedBigger) >= 0){ |
| 471 | return "Compare with bigger"; |
| 472 | } |
| 473 | |
| 474 | |
| 475 | static const uint8_t pExpectedSmaller[] = { |
| 476 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 477 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 478 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 479 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 480 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 481 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 482 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 483 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 484 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 485 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '*', |
| 486 | }; |
| 487 | UsefulBufC ExpectedSmaller = ByteArrayLiteralToUsefulBufC(pExpectedSmaller); |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 488 | // Expect +1 when the first arg is larger |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 489 | if(UsefulBuf_Compare(Expected, ExpectedSmaller) <= 0){ |
| 490 | return "Compare with smaller"; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | static const uint8_t pExpectedLonger[] = { |
| 495 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 496 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 497 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 498 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 499 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 500 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 501 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 502 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 503 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 504 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+' |
| 505 | }; |
| 506 | UsefulBufC ExpectedLonger = ByteArrayLiteralToUsefulBufC(pExpectedLonger); |
| 507 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 508 | // Expect -1 when the first arg is smaller |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 509 | if(UsefulBuf_Compare(Expected, ExpectedLonger) >= 0){ |
| 510 | return "Compare with longer"; |
| 511 | } |
| 512 | |
| 513 | |
| 514 | static const uint8_t pExpectedShorter[] = { |
| 515 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 516 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 517 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 518 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 519 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 520 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 521 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 522 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 523 | '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 524 | '+', '+', '+', '+', '+', '+', '+', '+', '+', |
| 525 | }; |
| 526 | UsefulBufC ExpectedShorter = ByteArrayLiteralToUsefulBufC(pExpectedShorter); |
| 527 | // Expect +1 with the first arg is larger |
| 528 | if(UsefulBuf_Compare(Expected, ExpectedShorter) <= 0){ |
| 529 | return "Compare with shorter"; |
| 530 | } |
| 531 | |
| 532 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 533 | if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp, NULLUsefulBufC))) { |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 534 | return "Copy null/empty failed"; |
| 535 | } |
| 536 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 537 | // Look for +++++... in +++++... and find it at the beginning |
| 538 | if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedShorter)){ |
| 539 | return "Failed to find"; |
| 540 | } |
| 541 | |
| 542 | // look for ++* in ....++* and find it at the end |
| 543 | static const uint8_t pToFind[] = {'+', '+', '*'}; |
| 544 | UsefulBufC ToBeFound = ByteArrayLiteralToUsefulBufC(pToFind); |
| 545 | |
| 546 | if(97 != UsefulBuf_FindBytes(ExpectedSmaller, ToBeFound)){ |
| 547 | return "Failed to find 2"; |
| 548 | } |
| 549 | |
| 550 | // look for ++* in ....++, and find it near the end |
| 551 | if(SIZE_MAX != UsefulBuf_FindBytes(ExpectedBigger, ToBeFound)){ |
| 552 | return "Failed to not find"; |
| 553 | } |
| 554 | |
| 555 | // Look for the whole buffer in itself and succeed. |
| 556 | if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedLonger)){ |
| 557 | return "Failed to find 3"; |
| 558 | } |
| 559 | |
| 560 | return NULL; |
| 561 | } |
| 562 | |
| 563 | |
| 564 | const char * UBIntegerFormatTests() |
| 565 | { |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 566 | UsefulOutBuf_MakeOnStack(UOB,100); |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 567 | |
| 568 | const uint32_t u32 = 0x0A0B0C0D; // from https://en.wikipedia.org/wiki/Endianness |
| 569 | const uint64_t u64 = 1984738472938472; |
| 570 | const uint16_t u16 = 40000; |
| 571 | const uint8_t u8 = 9; |
| 572 | const float f = (float)314.15; |
| 573 | const double d = 2.1e10; |
| 574 | |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 575 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 576 | UsefulOutBuf_AppendUint32(&UOB, u32); // Also tests UsefulOutBuf_InsertUint64 and UsefulOutBuf_GetEndPosition |
| 577 | UsefulOutBuf_AppendUint64(&UOB, u64); // Also tests UsefulOutBuf_InsertUint32 |
| 578 | UsefulOutBuf_AppendUint16(&UOB, u16); // Also tests UsefulOutBuf_InsertUint16 |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 579 | UsefulOutBuf_AppendByte(&UOB, u8); |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 580 | UsefulOutBuf_AppendFloat(&UOB, f); // Also tests UsefulOutBuf_InsertFloat |
| 581 | UsefulOutBuf_AppendDouble(&UOB, d); // Also tests UsefulOutBuf_InsertDouble |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 582 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 583 | UsefulBufC O = UsefulOutBuf_OutUBuf(&UOB); |
| 584 | if(UsefulBuf_IsNULLC(O)) |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 585 | return "Couldn't output integers"; |
| 586 | |
| 587 | // from https://en.wikipedia.org/wiki/Endianness |
| 588 | const uint8_t pExpectedNetworkOrder[4] = {0x0A, 0x0B, 0x0C, 0x0D}; |
| 589 | if(memcmp(O.ptr, pExpectedNetworkOrder, 4)) { |
| 590 | return "not in network order"; |
| 591 | } |
| 592 | |
| 593 | UsefulInputBuf UIB; |
| 594 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 595 | UsefulInputBuf_Init(&UIB, O); |
| 596 | |
| 597 | if(UsefulInputBuf_Tell(&UIB) != 0) { |
| 598 | return "UsefulInputBuf_Tell failed"; |
| 599 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 600 | |
| 601 | if(UsefulInputBuf_GetUint32(&UIB) != u32) { |
| 602 | return "u32 out then in failed"; |
| 603 | } |
| 604 | if(UsefulInputBuf_GetUint64(&UIB) != u64) { |
| 605 | return "u64 out then in failed"; |
| 606 | } |
| 607 | if(UsefulInputBuf_GetUint16(&UIB) != u16) { |
| 608 | return "u16 out then in failed"; |
| 609 | } |
| 610 | if(UsefulInputBuf_GetByte(&UIB) != u8) { |
| 611 | return "u8 out then in failed"; |
| 612 | } |
| 613 | if(UsefulInputBuf_GetFloat(&UIB) != f) { |
| 614 | return "float out then in failed"; |
| 615 | } |
| 616 | if(UsefulInputBuf_GetDouble(&UIB) != d) { |
| 617 | return "double out then in failed"; |
| 618 | } |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 619 | |
| 620 | // TODO: test UsefulInputBuf_Seek |
| 621 | // TODO: test UsefulInputBuf_BytesUnconsumed |
| 622 | // TODO: test UsefulInputBuf_BytesAvailable |
| 623 | // TODO: test UsefulInputBuf_GetBytes |
| 624 | // TODO: test UsefulInputBuf_GetUsefulBuf |
| 625 | // TODO: test UsefulInputBuf_GetError |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 626 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame^] | 627 | return NULL; |
| 628 | } |
| 629 | |
| 630 | |
| 631 | const char *UBCopyUtilTest() |
| 632 | { |
| 633 | if(UsefulBufUtil_CopyFloatToUint32(65536.0F) != 0x47800000) { |
| 634 | return "CopyFloatToUint32 failed"; |
| 635 | } |
| 636 | |
| 637 | if(UsefulBufUtil_CopyDoubleToUint64(4e-40F) != 0X37C16C2800000000ULL) { |
| 638 | return "CopyDoubleToUint64 failed"; |
| 639 | } |
| 640 | |
| 641 | if(UsefulBufUtil_CopyUint64ToDouble(0X37C16C2800000000ULL) != 4e-40F) { |
| 642 | return "CopyUint64ToDouble failed"; |
| 643 | } |
| 644 | |
| 645 | if(UsefulBufUtil_CopyUint32ToFloat(0x47800000) != 65536.0F) { |
| 646 | return "CopyUint32ToFloat failed"; |
| 647 | } |
Laurence Lundblade | 2ded3d9 | 2018-10-09 21:36:11 +0800 | [diff] [blame] | 648 | |
| 649 | return NULL; |
| 650 | } |
| 651 | |
| 652 | |
| 653 | |