buffer_test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 #include <config.h>
00018 #include <stdlib.h>
00019 #include <unistd.h>
00020 #include <fcntl.h>
00021 
00022 #include <atf-c.h>
00023 
00024 #include "isctest.h"
00025 
00026 #include <isc/buffer.h>
00027 #include <isc/result.h>
00028 
00029 ATF_TC(isc_buffer_reserve);
00030 ATF_TC_HEAD(isc_buffer_reserve, tc) {
00031         atf_tc_set_md_var(tc, "descr", "reserve space in dynamic buffers");
00032 }
00033 
00034 ATF_TC_BODY(isc_buffer_reserve, tc) {
00035         isc_result_t result;
00036         isc_buffer_t *b;
00037 
00038         result = isc_test_begin(NULL, ISC_TRUE);
00039         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00040 
00041         b = NULL;
00042         result = isc_buffer_allocate(mctx, &b, 1024);
00043         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00044         ATF_CHECK_EQ(b->length, 1024);
00045 
00046         /*
00047          * 1024 bytes should already be available, so this call does
00048          * nothing.
00049          */
00050         result = isc_buffer_reserve(&b, 1024);
00051         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00052 
00053         ATF_CHECK(ISC_BUFFER_VALID(b));
00054         ATF_CHECK_EQ(b->length, 1024);
00055 
00056         /*
00057          * This call should grow it to 2048 bytes as only 1024 bytes are
00058          * available in the buffer.
00059          */
00060         result = isc_buffer_reserve(&b, 1025);
00061         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00062 
00063         ATF_CHECK(ISC_BUFFER_VALID(b));
00064         ATF_CHECK_EQ(b->length, 2048);
00065 
00066         /*
00067          * 2048 bytes should already be available, so this call does
00068          * nothing.
00069          */
00070         result = isc_buffer_reserve(&b, 2000);
00071         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00072 
00073         ATF_CHECK(ISC_BUFFER_VALID(b));
00074         ATF_CHECK_EQ(b->length, 2048);
00075 
00076         /*
00077          * This call should grow it to 4096 bytes as only 2048 bytes are
00078          * available in the buffer.
00079          */
00080         result = isc_buffer_reserve(&b, 3000);
00081         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00082 
00083         ATF_CHECK(ISC_BUFFER_VALID(b));
00084         ATF_CHECK_EQ(b->length, 4096);
00085 
00086         /* Consume some of the buffer so we can run the next test. */
00087         isc_buffer_add(b, 4096);
00088 
00089         /*
00090          * This call should fail and leave buffer untouched.
00091          */
00092         result = isc_buffer_reserve(&b, UINT_MAX);
00093         ATF_CHECK_EQ(result, ISC_R_NOMEMORY);
00094 
00095         ATF_CHECK(ISC_BUFFER_VALID(b));
00096         ATF_CHECK_EQ(b->length, 4096);
00097 
00098         isc_buffer_free(&b);
00099 
00100         isc_test_end();
00101 }
00102 
00103 ATF_TC(isc_buffer_reallocate);
00104 ATF_TC_HEAD(isc_buffer_reallocate, tc) {
00105         atf_tc_set_md_var(tc, "descr", "reallocate dynamic buffers");
00106 }
00107 
00108 ATF_TC_BODY(isc_buffer_reallocate, tc) {
00109         isc_result_t result;
00110         isc_buffer_t *b;
00111 
00112         result = isc_test_begin(NULL, ISC_TRUE);
00113         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00114 
00115         b = NULL;
00116         result = isc_buffer_allocate(mctx, &b, 1024);
00117         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00118         ATF_CHECK_EQ(b->length, 1024);
00119 
00120         result = isc_buffer_reallocate(&b, 512);
00121         ATF_CHECK_EQ(result, ISC_R_NOSPACE);
00122 
00123         ATF_CHECK(ISC_BUFFER_VALID(b));
00124         ATF_CHECK_EQ(b->length, 1024);
00125 
00126         result = isc_buffer_reallocate(&b, 1536);
00127         ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00128 
00129         ATF_CHECK(ISC_BUFFER_VALID(b));
00130         ATF_CHECK_EQ(b->length, 1536);
00131 
00132         isc_buffer_free(&b);
00133 
00134         isc_test_end();
00135 }
00136 
00137 /*
00138  * Main
00139  */
00140 ATF_TP_ADD_TCS(tp) {
00141         ATF_TP_ADD_TC(tp, isc_buffer_reserve);
00142         ATF_TP_ADD_TC(tp, isc_buffer_reallocate);
00143         return (atf_no_error());
00144 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha