iPXE
iobuf_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * I/O buffer tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <stdint.h>
00036 #include <string.h>
00037 #include <assert.h>
00038 #include <ipxe/iobuf.h>
00039 #include <ipxe/io.h>
00040 #include <ipxe/test.h>
00041 
00042 /* Forward declaration */
00043 struct self_test iobuf_test __self_test;
00044 
00045 /**
00046  * Report I/O buffer allocation test result
00047  *
00048  * @v len               Required length of buffer
00049  * @v align             Physical alignment
00050  * @v offset            Offset from physical alignment
00051  * @v file              Test code file
00052  * @v line              Test code line
00053  */
00054 static inline void alloc_iob_okx ( size_t len, size_t align, size_t offset,
00055                                    const char *file, unsigned int line ) {
00056         struct io_buffer *iobuf;
00057 
00058         /* Allocate I/O buffer */
00059         iobuf = alloc_iob_raw ( len, align, offset );
00060         okx ( iobuf != NULL, file, line );
00061         DBGC ( &iobuf_test, "IOBUF %p (%#08lx+%#zx) for %#zx align %#zx "
00062                "offset %#zx\n", iobuf, virt_to_phys ( iobuf->data ),
00063                iob_tailroom ( iobuf ), len, align, offset );
00064 
00065         /* Validate requested length and alignment */
00066         okx ( ( ( ( intptr_t ) iobuf ) & ( __alignof__ ( *iobuf ) - 1 ) ) == 0,
00067                 file, line );
00068         okx ( iob_tailroom ( iobuf ) >= len, file, line );
00069         okx ( ( ( align == 0 ) ||
00070                 ( ( virt_to_phys ( iobuf->data ) & ( align - 1 ) ) ==
00071                   ( offset & ( align - 1 ) ) ) ), file, line );
00072 
00073         /* Overwrite entire content of I/O buffer (for Valgrind) */
00074         memset ( iob_put ( iobuf, len ), 0x55, len );
00075 
00076         /* Free I/O buffer */
00077         free_iob ( iobuf );
00078 }
00079 #define alloc_iob_ok( len, align, offset ) \
00080         alloc_iob_okx ( len, align, offset, __FILE__, __LINE__ )
00081 
00082 /**
00083  * Report I/O buffer allocation failure test result
00084  *
00085  * @v len               Required length of buffer
00086  * @v align             Physical alignment
00087  * @v offset            Offset from physical alignment
00088  * @v file              Test code file
00089  * @v line              Test code line
00090  */
00091 static inline void alloc_iob_fail_okx ( size_t len, size_t align, size_t offset,
00092                                         const char *file, unsigned int line ) {
00093         struct io_buffer *iobuf;
00094 
00095         /* Allocate I/O buffer */
00096         iobuf = alloc_iob_raw ( len, align, offset );
00097         okx ( iobuf == NULL, file, line );
00098 }
00099 #define alloc_iob_fail_ok( len, align, offset ) \
00100         alloc_iob_fail_okx ( len, align, offset, __FILE__, __LINE__ )
00101 
00102 /**
00103  * Perform I/O buffer self-tests
00104  *
00105  */
00106 static void iobuf_test_exec ( void ) {
00107 
00108         /* Check zero-length allocations */
00109         alloc_iob_ok ( 0, 0, 0 );
00110         alloc_iob_ok ( 0, 0, 1 );
00111         alloc_iob_ok ( 0, 1, 0 );
00112         alloc_iob_ok ( 0, 1024, 0 );
00113         alloc_iob_ok ( 0, 139, -17 );
00114 
00115         /* Check various sensible allocations */
00116         alloc_iob_ok ( 1, 0, 0 );
00117         alloc_iob_ok ( 16, 16, 0 );
00118         alloc_iob_ok ( 64, 0, 0 );
00119         alloc_iob_ok ( 65, 0, 0 );
00120         alloc_iob_ok ( 65, 1024, 19 );
00121         alloc_iob_ok ( 1536, 1536, 0 );
00122         alloc_iob_ok ( 2048, 2048, 0 );
00123         alloc_iob_ok ( 2048, 2048, -10 );
00124 
00125         /* Excessively large or excessively aligned allocations should fail */
00126         alloc_iob_fail_ok ( -1UL, 0, 0 );
00127         alloc_iob_fail_ok ( -1UL, 1024, 0 );
00128         alloc_iob_fail_ok ( 0, -1UL, 0 );
00129         alloc_iob_fail_ok ( 1024, -1UL, 0 );
00130 }
00131 
00132 /** I/O buffer self-test */
00133 struct self_test iobuf_test __self_test = {
00134         .name = "iobuf",
00135         .exec = iobuf_test_exec,
00136 };