utility_string_tests.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "test_common.h"
  12. #include "utility.h"
  13. #include "vboot_common.h"
  14. /* Test string concatenation */
  15. static void StrncatTest(void) {
  16. char dest[128];
  17. /* Null inputs */
  18. TEST_EQ(0, StrnAppend(dest, NULL, sizeof(dest)), "StrnAppend('', null)");
  19. TEST_EQ(0, StrnAppend(NULL, "Hey!", sizeof(dest)), "StrnAppend(null, '')");
  20. /* Empty <-- empty */
  21. *dest = 0;
  22. TEST_EQ(0, StrnAppend(dest, "", sizeof(dest)), "StrnAppend('', '')");
  23. TEST_EQ(0, strcmp(dest, ""), "StrnAppend('', '') result");
  24. /* Nonempty <-- empty */
  25. strcpy(dest, "Bob");
  26. TEST_EQ(3, StrnAppend(dest, "", sizeof(dest)), "StrnAppend(B, '')");
  27. TEST_EQ(0, strcmp(dest, "Bob"), "StrnAppend(B, '') result");
  28. /* Empty <-- nonempty */
  29. *dest = 0;
  30. TEST_EQ(5, StrnAppend(dest, "Alice", sizeof(dest)), "StrnAppend('', A)");
  31. TEST_EQ(0, strcmp(dest, "Alice"), "StrnAppend('', A) result");
  32. /* Nonempty <-- nonempty */
  33. strcpy(dest, "Tigre");
  34. TEST_EQ(10, StrnAppend(dest, "Bunny", sizeof(dest)), "StrnAppend(T, B)");
  35. TEST_EQ(0, strcmp(dest, "TigreBunny"), "StrnAppend(T, B) result");
  36. /* Test clipping */
  37. strcpy(dest, "YesI");
  38. TEST_EQ(7, StrnAppend(dest, "Can't", 8), "StrnAppend(Y, over)");
  39. TEST_EQ(0, strcmp(dest, "YesICan"), "StrnAppend(Y, over) result");
  40. /* Test clipping if dest already overflows its claimed length */
  41. strcpy(dest, "BudgetDeficit");
  42. TEST_EQ(6, StrnAppend(dest, "Spending", 7), "StrnAppend(over, over)");
  43. TEST_EQ(0, strcmp(dest, "Budget"), "StrnAppend(over, over) result");
  44. }
  45. static void TestU64ToS(uint64_t value, uint32_t radix, uint32_t zero_pad_width,
  46. const char *expect) {
  47. char dest[UINT64_TO_STRING_MAX];
  48. TEST_EQ(strlen(expect),
  49. Uint64ToString(dest, sizeof(dest), value, radix, zero_pad_width),
  50. "Uint64ToString");
  51. printf("Uint64ToString expect %s got %s\n", expect, dest);
  52. TEST_EQ(0, strcmp(dest, expect), "Uint64ToString result");
  53. }
  54. /* Test uint64 to string conversion */
  55. static void Uint64ToStringTest(void) {
  56. char dest[UINT64_TO_STRING_MAX];
  57. /* Test invalid inputs */
  58. TEST_EQ(0, Uint64ToString(NULL, 8, 123, 10, 8), "Uint64ToString null dest");
  59. TestU64ToS(0, 1, 0, "");
  60. TestU64ToS(0, 37, 0, "");
  61. /* Binary */
  62. TestU64ToS(0, 2, 0, "0");
  63. TestU64ToS(0x9A, 2, 0, "10011010");
  64. TestU64ToS(0x71, 2, 12, "000001110001");
  65. TestU64ToS(
  66. ~0ULL, 2, 0,
  67. "1111111111111111111111111111111111111111111111111111111111111111");
  68. /* Decimal */
  69. TestU64ToS(0, 10, 0, "0");
  70. TestU64ToS(12345, 10, 0, "12345");
  71. TestU64ToS(67890, 10, 8, "00067890");
  72. TestU64ToS(~0ULL, 10, 0, "18446744073709551615");
  73. /* Hex */
  74. TestU64ToS(0, 16, 0, "0");
  75. TestU64ToS(0x12345678, 16, 0, "12345678");
  76. TestU64ToS(0x9ABCDEF, 16, 8, "09abcdef");
  77. TestU64ToS(~0ULL, 16, 0, "ffffffffffffffff");
  78. /* Zero pad corner cases */
  79. /* Don't pad if over length */
  80. TestU64ToS(0x1234567890ULL, 16, 8, "1234567890");
  81. /* Fail if padding won't fit in buffer */
  82. TEST_EQ(0, Uint64ToString(dest, 8, 123, 10, 8), "Uint64ToString bad pad");
  83. TEST_EQ(0, strcmp(dest, ""), "Uint64ToString bad pad result");
  84. }
  85. int main(int argc, char* argv[]) {
  86. int error_code = 0;
  87. StrncatTest();
  88. Uint64ToStringTest();
  89. if (!gTestSuccess)
  90. error_code = 255;
  91. return error_code;
  92. }