string_tests.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2018 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "audio_utils_string_tests"
  18. #include <audio_utils/string.h>
  19. #include <gtest/gtest.h>
  20. // fills the string buffer with a increasing ramp of values from start.
  21. template <size_t size>
  22. void fill(char (&s)[size], int start) {
  23. for (size_t i = 0; i < size - 1; ++i) {
  24. s[i] = start++;
  25. }
  26. s[size - 1] = 0;
  27. }
  28. // checks that the fill counts from start, as expected to actual chars,
  29. // whereupon the rest is expected to be zeroes.
  30. template <size_t size>
  31. void check(char (&s)[size], int start, size_t actual) {
  32. size_t lim = std::min(actual, size);
  33. size_t i = 0;
  34. if (lim > 0) {
  35. for (; i < lim - 1; ++i) {
  36. EXPECT_EQ(start, s[i]);
  37. ++start;
  38. }
  39. }
  40. for (; i < size; ++i) {
  41. EXPECT_EQ(0, s[i]);
  42. }
  43. }
  44. TEST(audio_utils_string, check_zero_fill) {
  45. // we use string arrays whose size is known by compiler, not vectors
  46. constexpr size_t STRING_SIZE = 50;
  47. union {
  48. char dst[STRING_SIZE];
  49. char dst_mirror[STRING_SIZE + 10]; // verifier that we don't overwrite
  50. };
  51. char over[sizeof(dst) + 5];
  52. char under[sizeof(dst) - 5];
  53. // fill with a value ramp
  54. constexpr int DST_START = 1;
  55. constexpr int OVER_START = 2;
  56. constexpr int UNDER_START = 3;
  57. fill(dst_mirror, DST_START);
  58. fill(over, OVER_START);
  59. fill(under, UNDER_START);
  60. // union should overlay dst and dst_mirror.
  61. dst[sizeof(dst) - 1] = 0;
  62. check(dst, DST_START, sizeof(dst));
  63. EXPECT_EQ(sizeof(dst) + DST_START, dst_mirror[sizeof(dst)]);
  64. // make sure we truncate when copying a larger string.
  65. audio_utils_strlcpy_zerofill(dst, over);
  66. check(dst, OVER_START, sizeof(dst));
  67. // check we didn't overwrite
  68. EXPECT_EQ(sizeof(dst) + DST_START, dst_mirror[sizeof(dst)]);
  69. // make sure we fill remaining buffer with zeros.
  70. audio_utils_strlcpy_zerofill(dst, under);
  71. check(dst, UNDER_START, sizeof(under));
  72. // check we didn't overwrite
  73. EXPECT_EQ(sizeof(dst) + DST_START, dst_mirror[sizeof(dst)]);
  74. }