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