You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iobuf_test.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * I/O buffer tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. #include <ipxe/iobuf.h>
  35. #include <ipxe/io.h>
  36. #include <ipxe/test.h>
  37. /* Forward declaration */
  38. struct self_test iobuf_test __self_test;
  39. /**
  40. * Report I/O buffer allocation test result
  41. *
  42. * @v len Required length of buffer
  43. * @v align Physical alignment
  44. * @v offset Offset from physical alignment
  45. * @v file Test code file
  46. * @v line Test code line
  47. */
  48. static inline void alloc_iob_okx ( size_t len, size_t align, size_t offset,
  49. const char *file, unsigned int line ) {
  50. struct io_buffer *iobuf;
  51. /* Allocate I/O buffer */
  52. iobuf = alloc_iob_raw ( len, align, offset );
  53. okx ( iobuf != NULL, file, line );
  54. DBGC ( &iobuf_test, "IOBUF %p (%#08lx+%#zx) for %#zx align %#zx "
  55. "offset %#zx\n", iobuf, virt_to_phys ( iobuf->data ),
  56. iob_tailroom ( iobuf ), len, align, offset );
  57. /* Validate requested length and alignment */
  58. okx ( ( ( ( intptr_t ) iobuf ) & ( __alignof__ ( *iobuf ) - 1 ) ) == 0,
  59. file, line );
  60. okx ( iob_tailroom ( iobuf ) >= len, file, line );
  61. okx ( ( ( align == 0 ) ||
  62. ( ( virt_to_phys ( iobuf->data ) & ( align - 1 ) ) ==
  63. ( offset & ( align - 1 ) ) ) ), file, line );
  64. /* Overwrite entire content of I/O buffer (for Valgrind) */
  65. memset ( iob_put ( iobuf, len ), 0x55, len );
  66. /* Free I/O buffer */
  67. free_iob ( iobuf );
  68. }
  69. #define alloc_iob_ok( len, align, offset ) \
  70. alloc_iob_okx ( len, align, offset, __FILE__, __LINE__ )
  71. /**
  72. * Report I/O buffer allocation failure test result
  73. *
  74. * @v len Required length of buffer
  75. * @v align Physical alignment
  76. * @v offset Offset from physical alignment
  77. * @v file Test code file
  78. * @v line Test code line
  79. */
  80. static inline void alloc_iob_fail_okx ( size_t len, size_t align, size_t offset,
  81. const char *file, unsigned int line ) {
  82. struct io_buffer *iobuf;
  83. /* Allocate I/O buffer */
  84. iobuf = alloc_iob_raw ( len, align, offset );
  85. okx ( iobuf == NULL, file, line );
  86. }
  87. #define alloc_iob_fail_ok( len, align, offset ) \
  88. alloc_iob_fail_okx ( len, align, offset, __FILE__, __LINE__ )
  89. /**
  90. * Perform I/O buffer self-tests
  91. *
  92. */
  93. static void iobuf_test_exec ( void ) {
  94. /* Check zero-length allocations */
  95. alloc_iob_ok ( 0, 0, 0 );
  96. alloc_iob_ok ( 0, 0, 1 );
  97. alloc_iob_ok ( 0, 1, 0 );
  98. alloc_iob_ok ( 0, 1024, 0 );
  99. alloc_iob_ok ( 0, 139, -17 );
  100. /* Check various sensible allocations */
  101. alloc_iob_ok ( 1, 0, 0 );
  102. alloc_iob_ok ( 16, 16, 0 );
  103. alloc_iob_ok ( 64, 0, 0 );
  104. alloc_iob_ok ( 65, 0, 0 );
  105. alloc_iob_ok ( 65, 1024, 19 );
  106. alloc_iob_ok ( 1536, 1536, 0 );
  107. alloc_iob_ok ( 2048, 2048, 0 );
  108. alloc_iob_ok ( 2048, 2048, -10 );
  109. /* Excessively large or excessively aligned allocations should fail */
  110. alloc_iob_fail_ok ( -1UL, 0, 0 );
  111. alloc_iob_fail_ok ( -1UL, 1024, 0 );
  112. alloc_iob_fail_ok ( 0, -1UL, 0 );
  113. alloc_iob_fail_ok ( 1024, -1UL, 0 );
  114. }
  115. /** I/O buffer self-test */
  116. struct self_test iobuf_test __self_test = {
  117. .name = "iobuf",
  118. .exec = iobuf_test_exec,
  119. };