Annotation of embedaddon/dhcp/common/tests/test_alloc.c, revision 1.1.1.1

1.1       misho       1: /*
1.1.1.1 ! misho       2:  * Copyright (c) 2007,2009,2012 by Internet Systems Consortium, Inc. ("ISC")
        !             3:  *
        !             4:  * We test the functions provided in alloc.c here. These are very
1.1       misho       5:  * basic functions, and it is very important that they work correctly.
                      6:  *
                      7:  * You can see two different styles of testing:
                      8:  *
                      9:  * - In the first, we have a single test for each function that tests
                     10:  *   all of the possible ways it can operate. (This is the case for
                     11:  *   the buffer tests.)
                     12:  *
                     13:  * - In the second, we have a separate test for each of the ways a
                     14:  *   function can operate. (This is the case for the data_string
                     15:  *   tests.)
                     16:  *
                     17:  * The advantage of a single test per function is that you have fewer
                     18:  * tests, and less duplicated and extra code. The advantage of having
                     19:  * a separate test is that each test is simpler. Plus if you need to
                     20:  * allow certain tests to fail for some reason (known bugs that are
1.1.1.1 ! misho      21:  * hard to fix for example), then
1.1       misho      22:  */
                     23: 
1.1.1.1 ! misho      24: /** @TODO: dmalloc() test */
1.1       misho      25: 
                     26: #include "config.h"
1.1.1.1 ! misho      27: #include <atf-c.h>
1.1       misho      28: #include "dhcpd.h"
                     29: 
1.1.1.1 ! misho      30: ATF_TC(buffer_allocate);
1.1       misho      31: 
1.1.1.1 ! misho      32: ATF_TC_HEAD(buffer_allocate, tc) {
        !            33:     atf_tc_set_md_var(tc, "descr", "buffer_allocate basic test");
        !            34: }
        !            35: 
        !            36: ATF_TC_BODY(buffer_allocate, tc) {
        !            37:     struct buffer *buf = 0;
        !            38: 
        !            39:     /*
        !            40:      * Check a 0-length buffer.
        !            41:      */
        !            42:     buf = NULL;
        !            43:     if (!buffer_allocate(&buf, 0, MDL)) {
        !            44:         atf_tc_fail("failed on 0-len buffer");
        !            45:     }
        !            46:     if (!buffer_dereference(&buf, MDL)) {
        !            47:         atf_tc_fail("buffer_dereference() failed");
        !            48:     }
        !            49:     if (buf != NULL) {
        !            50:         atf_tc_fail("buffer_dereference() did not NULL-out buffer");
        !            51:     }
        !            52: 
        !            53:     /*
        !            54:      * Check an actual buffer.
        !            55:      */
        !            56:     buf = NULL;
        !            57:     if (!buffer_allocate(&buf, 100, MDL)) {
        !            58:         atf_tc_fail("failed on allocate 100 bytes\n");
        !            59:     }
        !            60:     if (!buffer_dereference(&buf, MDL)) {
        !            61:         atf_tc_fail("buffer_dereference() failed");
        !            62:     }
        !            63:     if (buf != NULL) {
        !            64:         atf_tc_fail("buffer_dereference() did not NULL-out buffer");
        !            65:     }
        !            66: 
        !            67:     /*
        !            68:      * Okay, we're happy.
        !            69:      */
        !            70:     atf_tc_pass();
        !            71: }
        !            72: 
        !            73: ATF_TC(buffer_reference);
        !            74: 
        !            75: ATF_TC_HEAD(buffer_reference, tc) {
        !            76:     atf_tc_set_md_var(tc, "descr", "buffer_reference basic test");
        !            77: }
        !            78: 
        !            79: ATF_TC_BODY(buffer_reference, tc) {
        !            80: 
        !            81:     struct buffer *a, *b;
        !            82: 
        !            83:     /*
        !            84:      * Create a buffer.
        !            85:      */
        !            86:     a = NULL;
        !            87:     if (!buffer_allocate(&a, 100, MDL)) {
        !            88:         atf_tc_fail("failed on allocate 100 bytes");
        !            89:     }
        !            90: 
        !            91:     /**
        !            92:      * Confirm buffer_reference() doesn't work if we pass in NULL.
        !            93:      *
        !            94:      * @TODO: we should confirm we get an error message here.
        !            95:      */
        !            96:     if (buffer_reference(NULL, a, MDL)) {
        !            97:         atf_tc_fail("succeeded on an error input");
        !            98:     }
        !            99: 
        !           100:     /**
        !           101:      * @TODO: we should confirm we get an error message if we pass
        !           102:      *       a non-NULL target.
        !           103:      */
        !           104: 
        !           105:     /*
        !           106:      * Confirm we work under normal circumstances.
        !           107:      */
        !           108:     b = NULL;
        !           109:     if (!buffer_reference(&b, a, MDL)) {
        !           110:         atf_tc_fail("buffer_reference() failed");
        !           111:     }
        !           112: 
        !           113:     if (b != a) {
        !           114:         atf_tc_fail("incorrect pointer returned");
        !           115:     }
        !           116: 
        !           117:     if (b->refcnt != 2) {
        !           118:         atf_tc_fail("incorrect refcnt");
        !           119:     }
        !           120: 
        !           121:     /*
        !           122:      * Clean up.
        !           123:      */
        !           124:     if (!buffer_dereference(&b, MDL)) {
        !           125:         atf_tc_fail("buffer_dereference() failed");
        !           126:     }
        !           127:     if (!buffer_dereference(&a, MDL)) {
        !           128:         atf_tc_fail("buffer_dereference() failed");
        !           129:     }
        !           130: 
        !           131: }
        !           132: 
        !           133: 
        !           134: ATF_TC(buffer_dereference);
        !           135: 
        !           136: ATF_TC_HEAD(buffer_dereference, tc) {
        !           137:     atf_tc_set_md_var(tc, "descr", "buffer_dereference basic test");
        !           138: }
        !           139: 
        !           140: ATF_TC_BODY(buffer_dereference, tc) {
        !           141:     struct buffer *a, *b;
        !           142: 
        !           143:     /**
        !           144:      * Confirm buffer_dereference() doesn't work if we pass in NULL.
        !           145:      *
        !           146:      * TODO: we should confirm we get an error message here.
        !           147:      */
        !           148:     if (buffer_dereference(NULL, MDL)) {
        !           149:         atf_tc_fail("succeeded on an error input");
        !           150:     }
        !           151: 
        !           152:     /**
        !           153:      * Confirm buffer_dereference() doesn't work if we pass in
        !           154:      * a pointer to NULL.
        !           155:      *
        !           156:      * @TODO: we should confirm we get an error message here.
        !           157:      */
        !           158:     a = NULL;
        !           159:     if (buffer_dereference(&a, MDL)) {
        !           160:         atf_tc_fail("succeeded on an error input");
        !           161:     }
        !           162: 
        !           163:     /*
        !           164:      * Confirm we work under normal circumstances.
        !           165:      */
        !           166:     a = NULL;
        !           167:     if (!buffer_allocate(&a, 100, MDL)) {
        !           168:         atf_tc_fail("failed on allocate");
        !           169:     }
        !           170:     if (!buffer_dereference(&a, MDL)) {
        !           171:         atf_tc_fail("buffer_dereference() failed");
        !           172:     }
        !           173:     if (a != NULL) {
        !           174:         atf_tc_fail("non-null buffer after buffer_dereference()");
        !           175:     }
        !           176: 
        !           177:     /**
        !           178:      * Confirm we get an error from negative refcnt.
        !           179:      *
        !           180:      * @TODO: we should confirm we get an error message here.
        !           181:      */
        !           182:     a = NULL;
        !           183:     if (!buffer_allocate(&a, 100, MDL)) {
        !           184:         atf_tc_fail("failed on allocate");
        !           185:     }
        !           186:     b = NULL;
        !           187:     if (!buffer_reference(&b, a, MDL)) {
        !           188:         atf_tc_fail("buffer_reference() failed");
        !           189:     }
        !           190:     a->refcnt = 0;     /* purposely set to invalid value */
        !           191:     if (buffer_dereference(&a, MDL)) {
        !           192:         atf_tc_fail("buffer_dereference() succeeded on error input");
        !           193:     }
        !           194:     a->refcnt = 2;
        !           195:     if (!buffer_dereference(&b, MDL)) {
        !           196:         atf_tc_fail("buffer_dereference() failed");
        !           197:     }
        !           198:     if (!buffer_dereference(&a, MDL)) {
        !           199:         atf_tc_fail("buffer_dereference() failed");
        !           200:     }
        !           201: }
        !           202: 
        !           203: ATF_TC(data_string_forget);
        !           204: 
        !           205: ATF_TC_HEAD(data_string_forget, tc) {
        !           206:     atf_tc_set_md_var(tc, "descr", "data_string_forget basic test");
        !           207: }
        !           208: 
        !           209: ATF_TC_BODY(data_string_forget, tc) {
        !           210:     struct buffer *buf;
        !           211:     struct data_string a;
        !           212:     const char *str = "Lorem ipsum dolor sit amet turpis duis.";
        !           213: 
        !           214:     /*
        !           215:      * Create the string we want to forget.
        !           216:      */
        !           217:     memset(&a, 0, sizeof(a));
        !           218:     a.len = strlen(str);
        !           219:     buf = NULL;
        !           220:     if (!buffer_allocate(&buf, a.len, MDL)) {
        !           221:         atf_tc_fail("out of memory");
        !           222:     }
        !           223:     if (!buffer_reference(&a.buffer, buf, MDL)) {
        !           224:         atf_tc_fail("buffer_reference() failed");
        !           225:     }
        !           226:     a.data = a.buffer->data;
        !           227:     memcpy(a.buffer->data, str, a.len);
        !           228: 
        !           229:     /*
        !           230:      * Forget and confirm we've forgotten.
        !           231:      */
        !           232:     data_string_forget(&a, MDL);
        !           233: 
        !           234:     if (a.len != 0) {
        !           235:         atf_tc_fail("incorrect length");
        !           236:     }
        !           237: 
        !           238:     if (a.data != NULL) {
        !           239:         atf_tc_fail("incorrect data");
        !           240:     }
        !           241:     if (a.terminated) {
        !           242:         atf_tc_fail("incorrect terminated");
        !           243:     }
        !           244:     if (a.buffer != NULL) {
        !           245:         atf_tc_fail("incorrect buffer");
        !           246:     }
        !           247:     if (buf->refcnt != 1) {
        !           248:         atf_tc_fail("too many references to buf");
        !           249:     }
        !           250: 
        !           251:     /*
        !           252:      * Clean up buffer.
        !           253:      */
        !           254:     if (!buffer_dereference(&buf, MDL)) {
        !           255:         atf_tc_fail("buffer_reference() failed");
        !           256:     }
        !           257: }
        !           258: 
        !           259: ATF_TC(data_string_forget_nobuf);
        !           260: 
        !           261: ATF_TC_HEAD(data_string_forget_nobuf, tc) {
        !           262:     atf_tc_set_md_var(tc, "descr", "data_string_forget test, "
        !           263:                       "data_string without buffer");
        !           264: }
        !           265: 
        !           266: ATF_TC_BODY(data_string_forget_nobuf, tc) {
        !           267:     struct data_string a;
        !           268:     const char *str = "Lorem ipsum dolor sit amet massa nunc.";
        !           269: 
        !           270:     /*
        !           271:      * Create the string we want to forget.
        !           272:      */
        !           273:     memset(&a, 0, sizeof(a));
        !           274:     a.len = strlen(str);
        !           275:     a.data = (const unsigned char *)str;
        !           276:     a.terminated = 1;
        !           277: 
        !           278:     /*
        !           279:      * Forget and confirm we've forgotten.
        !           280:      */
        !           281:     data_string_forget(&a, MDL);
        !           282: 
        !           283:     if (a.len != 0) {
        !           284:         atf_tc_fail("incorrect length");
        !           285:     }
        !           286:     if (a.data != NULL) {
        !           287:         atf_tc_fail("incorrect data");
        !           288:     }
        !           289:     if (a.terminated) {
        !           290:         atf_tc_fail("incorrect terminated");
        !           291:     }
        !           292:     if (a.buffer != NULL) {
        !           293:         atf_tc_fail("incorrect buffer");
        !           294:     }
        !           295: }
        !           296: 
        !           297: ATF_TC(data_string_copy);
        !           298: 
        !           299: ATF_TC_HEAD(data_string_copy, tc) {
        !           300:     atf_tc_set_md_var(tc, "descr", "data_string_copy basic test");
        !           301: }
        !           302: 
        !           303: ATF_TC_BODY(data_string_copy, tc) {
        !           304:     struct data_string a, b;
        !           305:     const char *str = "Lorem ipsum dolor sit amet orci aliquam.";
        !           306: 
        !           307:     /*
        !           308:      * Create the string we want to copy.
        !           309:          */
        !           310:     memset(&a, 0, sizeof(a));
        !           311:     a.len = strlen(str);
        !           312:     if (!buffer_allocate(&a.buffer, a.len, MDL)) {
        !           313:         atf_tc_fail("out of memory");
        !           314:     }
        !           315:     a.data = a.buffer->data;
        !           316:     memcpy(a.buffer->data, str, a.len);
        !           317: 
        !           318:     /*
        !           319:      * Copy the string, and confirm it works.
        !           320:      */
        !           321:     memset(&b, 0, sizeof(b));
        !           322:     data_string_copy(&b, &a, MDL);
        !           323: 
        !           324:     if (b.len != a.len) {
        !           325:         atf_tc_fail("incorrect length");
        !           326:     }
        !           327:     if (b.data != a.data) {
        !           328:         atf_tc_fail("incorrect data");
        !           329:     }
        !           330:     if (b.terminated != a.terminated) {
        !           331:         atf_tc_fail("incorrect terminated");
        !           332:     }
        !           333:     if (b.buffer != a.buffer) {
        !           334:         atf_tc_fail("incorrect buffer");
        !           335:     }
        !           336: 
        !           337:     /*
        !           338:      * Clean up.
        !           339:      */
        !           340:     data_string_forget(&b, MDL);
        !           341:     data_string_forget(&a, MDL);
        !           342: }
        !           343: 
        !           344: ATF_TC(data_string_copy_nobuf);
        !           345: 
        !           346: ATF_TC_HEAD(data_string_copy_nobuf, tc) {
        !           347:     atf_tc_set_md_var(tc, "descr", "data_string_copy test, "
        !           348:                       "data_string without buffer");
        !           349: }
        !           350: 
        !           351: ATF_TC_BODY(data_string_copy_nobuf, tc) {
        !           352:     struct data_string a, b;
        !           353:     const char *str = "Lorem ipsum dolor sit amet cras amet.";
        !           354: 
        !           355:     /*
        !           356:      * Create the string we want to copy.
        !           357:      */
        !           358:     memset(&a, 0, sizeof(a));
        !           359:     a.len = strlen(str);
        !           360:     a.data = (const unsigned char *)str;
        !           361:     a.terminated = 1;
        !           362: 
        !           363:     /*
        !           364:      * Copy the string, and confirm it works.
        !           365:      */
        !           366:     memset(&b, 0, sizeof(b));
        !           367:     data_string_copy(&b, &a, MDL);
        !           368: 
        !           369:     if (b.len != a.len) {
        !           370:         atf_tc_fail("incorrect length");
        !           371:     }
        !           372:     if (b.data != a.data) {
        !           373:         atf_tc_fail("incorrect data");
        !           374:     }
        !           375:     if (b.terminated != a.terminated) {
        !           376:         atf_tc_fail("incorrect terminated");
        !           377:     }
        !           378:     if (b.buffer != a.buffer) {
        !           379:         atf_tc_fail("incorrect buffer");
        !           380:     }
        !           381: 
        !           382:     /*
        !           383:      * Clean up.
        !           384:      */
        !           385:     data_string_forget(&b, MDL);
        !           386:     data_string_forget(&a, MDL);
        !           387: 
        !           388: }
        !           389: 
        !           390: ATF_TP_ADD_TCS(tp)
        !           391: {
        !           392:     ATF_TP_ADD_TC(tp, buffer_allocate);
        !           393:     ATF_TP_ADD_TC(tp, buffer_reference);
        !           394:     ATF_TP_ADD_TC(tp, buffer_dereference);
        !           395:     ATF_TP_ADD_TC(tp, data_string_forget);
        !           396:     ATF_TP_ADD_TC(tp, data_string_forget_nobuf);
        !           397:     ATF_TP_ADD_TC(tp, data_string_copy);
        !           398:     ATF_TP_ADD_TC(tp, data_string_copy_nobuf);
1.1       misho     399: 
1.1.1.1 ! misho     400:     return (atf_no_error());
1.1       misho     401: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>