Randall Spangler | bebe53c | 2011-07-07 10:30:25 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | * |
| 5 | * Tests for string utility functions. |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include "test_common.h" |
| 13 | #include "utility.h" |
| 14 | #include "vboot_common.h" |
| 15 | |
| 16 | |
| 17 | /* Test string concatenation */ |
| 18 | static void StrncatTest(void) { |
| 19 | char dest[128]; |
| 20 | |
| 21 | /* Null inputs */ |
| 22 | TEST_EQ(0, Strncat(dest, NULL, sizeof(dest)), "Strncat('', null)"); |
| 23 | TEST_EQ(0, Strncat(NULL, "Hey!", sizeof(dest)), "Strncat(null, '')"); |
| 24 | |
| 25 | /* Empty <-- empty */ |
| 26 | *dest = 0; |
| 27 | TEST_EQ(0, Strncat(dest, "", sizeof(dest)), "Strncat('', '')"); |
| 28 | TEST_EQ(0, strcmp(dest, ""), "Strncat('', '') result"); |
| 29 | |
| 30 | /* Nonempty <-- empty */ |
| 31 | strcpy(dest, "Bob"); |
| 32 | TEST_EQ(3, Strncat(dest, "", sizeof(dest)), "Strncat(B, '')"); |
| 33 | TEST_EQ(0, strcmp(dest, "Bob"), "Strncat(B, '') result"); |
| 34 | |
| 35 | /* Empty <-- nonempty */ |
| 36 | *dest = 0; |
| 37 | TEST_EQ(5, Strncat(dest, "Alice", sizeof(dest)), "Strncat('', A)"); |
| 38 | TEST_EQ(0, strcmp(dest, "Alice"), "Strncat('', A) result"); |
| 39 | |
| 40 | /* Nonempty <-- nonempty */ |
| 41 | strcpy(dest, "Tigre"); |
| 42 | TEST_EQ(10, Strncat(dest, "Bunny", sizeof(dest)), "Strncat(T, B)"); |
| 43 | TEST_EQ(0, strcmp(dest, "TigreBunny"), "Strncat(T, B) result"); |
| 44 | |
| 45 | /* Test clipping */ |
| 46 | strcpy(dest, "YesI"); |
| 47 | TEST_EQ(7, Strncat(dest, "Can't", 8), "Strncat(Y, over)"); |
| 48 | TEST_EQ(0, strcmp(dest, "YesICan"), "Strncat(Y, over) result"); |
| 49 | |
| 50 | /* Test clipping if dest already overflows its claimed length */ |
| 51 | strcpy(dest, "BudgetDeficit"); |
| 52 | TEST_EQ(6, Strncat(dest, "Spending", 7), "Strncat(over, over)"); |
| 53 | TEST_EQ(0, strcmp(dest, "Budget"), "Strncat(over, over) result"); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | static void TestU64ToS(uint64_t value, uint32_t radix, uint32_t zero_pad_width, |
| 58 | const char *expect) { |
| 59 | char dest[UINT64_TO_STRING_MAX]; |
| 60 | |
| 61 | TEST_EQ(strlen(expect), |
| 62 | Uint64ToString(dest, sizeof(dest), value, radix, zero_pad_width), |
| 63 | "Uint64ToString"); |
| 64 | printf("Uint64ToString expect %s got %s\n", expect, dest); |
| 65 | TEST_EQ(0, strcmp(dest, expect), "Uint64ToString result"); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /* Test uint64 to string conversion */ |
| 70 | static void Uint64ToStringTest(void) { |
| 71 | char dest[UINT64_TO_STRING_MAX]; |
| 72 | |
| 73 | /* Test invalid inputs */ |
| 74 | TEST_EQ(0, Uint64ToString(NULL, 8, 123, 10, 8), "Uint64ToString null dest"); |
| 75 | TestU64ToS(0, 1, 0, ""); |
| 76 | TestU64ToS(0, 37, 0, ""); |
| 77 | |
| 78 | /* Binary */ |
| 79 | TestU64ToS(0, 2, 0, "0"); |
| 80 | TestU64ToS(0x9A, 2, 0, "10011010"); |
| 81 | TestU64ToS(0x71, 2, 12, "000001110001"); |
| 82 | TestU64ToS( |
| 83 | ~UINT64_C(0), 2, 0, |
| 84 | "1111111111111111111111111111111111111111111111111111111111111111"); |
| 85 | |
| 86 | /* Decimal */ |
| 87 | TestU64ToS(0, 10, 0, "0"); |
| 88 | TestU64ToS(12345, 10, 0, "12345"); |
| 89 | TestU64ToS(67890, 10, 8, "00067890"); |
| 90 | TestU64ToS(~UINT64_C(0), 10, 0, "18446744073709551615"); |
| 91 | |
| 92 | /* Hex */ |
| 93 | TestU64ToS(0, 16, 0, "0"); |
| 94 | TestU64ToS(0x12345678, 16, 0, "12345678"); |
| 95 | TestU64ToS(0x9ABCDEF, 16, 8, "09abcdef"); |
| 96 | TestU64ToS(~UINT64_C(0), 16, 0, "ffffffffffffffff"); |
| 97 | |
| 98 | /* Zero pad corner cases */ |
| 99 | /* Don't pad if over length */ |
Randall Spangler | 4ae77c5 | 2011-07-08 09:30:38 -0700 | [diff] [blame] | 100 | TestU64ToS(UINT64_C(0x1234567890), 16, 8, "1234567890"); |
Randall Spangler | bebe53c | 2011-07-07 10:30:25 -0700 | [diff] [blame] | 101 | /* Fail if padding won't fit in buffer */ |
| 102 | TEST_EQ(0, Uint64ToString(dest, 8, 123, 10, 8), "Uint64ToString bad pad"); |
| 103 | TEST_EQ(0, strcmp(dest, ""), "Uint64ToString bad pad result"); |
| 104 | |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /* disable MSVC warnings on unused arguments */ |
| 109 | __pragma(warning (disable: 4100)) |
| 110 | |
| 111 | int main(int argc, char* argv[]) { |
| 112 | int error_code = 0; |
| 113 | |
| 114 | StrncatTest(); |
| 115 | Uint64ToStringTest(); |
| 116 | |
| 117 | if (!gTestSuccess) |
| 118 | error_code = 255; |
| 119 | |
| 120 | return error_code; |
| 121 | } |