lwres_test.c revision e4f074a2c2340ea80099beebecc3b89aa234fa8f
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson/*
6fe42ff85ced80bd2ccc49b429d36831b5f2a5b9Tinderbox User * Copyright (C) 1999 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
207845805eb591b77ffbd99735617cab7e2ed804Evan Hunt#include <config.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <assert.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence#include <ctype.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#include <stdio.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#include <stdlib.h>
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#include <string.h>
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson#include <lwres/context.h>
cc0a5f714231709409b9e1b85f0f97ae50854451Mark Andrews#include <lwres/lwbuffer.h>
d9059b0c38bd630c367d81424d72b1308cd74b04Tatuya JINMEI 神明達哉#include <lwres/lwres.h>
6028d1ce0380d0ba7f6c6ecd1ad20b31ddd1becbDavid Lawrence#include <lwres/lwpacket.h>
364a82f7c25b62967678027043425201a5e5171aBob Halley
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssonstatic inline void
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid LawrenceCHECK(int val, char *msg)
743bbdc18f839499862e4fb28ec32f607b1632dcTatuya JINMEI 神明達哉{
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington if (val != 0) {
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson fprintf(stderr, "%s returned %d\n", msg, val);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington exit(1);
743bbdc18f839499862e4fb28ec32f607b1632dcTatuya JINMEI 神明達哉 }
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington}
743bbdc18f839499862e4fb28ec32f607b1632dcTatuya JINMEI 神明達哉
743bbdc18f839499862e4fb28ec32f607b1632dcTatuya JINMEI 神明達哉static void
743bbdc18f839499862e4fb28ec32f607b1632dcTatuya JINMEI 神明達哉hexdump(char *msg, void *base, size_t len)
71bd858d8ed62672e7c23999dc7c02fd16a55089Evan Hunt{
5afa531442369eed0e93a8af14422b30f400bd89Mark Andrews unsigned char *p;
8907d8fa04fdaa65baf0bc6b01230b2ebde93106Mark Andrews unsigned int cnt;
476386968b1f287a695f73c48862e961011af99bMark Andrews
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson p = base;
9259fed3d8ac5d1efa9b5a647969e40c9c934484Andreas Gustafsson cnt = 0;
3aca8e5bf3740bbcc3bb13dde242d7cc369abb27Mark Andrews
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington printf("*** %s (%u bytes @ %p)\n", msg, len, base);
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson while (cnt < len) {
62700b67eb8abb7d13f9c3c1bc4b60a1477d35d8Mark Andrews if (cnt % 16 == 0)
9259fed3d8ac5d1efa9b5a647969e40c9c934484Andreas Gustafsson printf("%p: ", p);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson else if (cnt % 8 == 0)
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt printf(" |");
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt printf(" %02x", *p++);
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt cnt++;
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt
222d38735f97f771054e223b03f84c5858252332Evan Hunt if (cnt % 16 == 0)
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt printf("\n");
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt }
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt if (cnt % 16 != 0)
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt printf("\n");
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson}
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafssonstatic char *TESTSTRING = "This is a test. This is only a test. !!!";
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafssonint
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafssonmain(int argc, char *argv[])
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt{
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt int ret;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt lwres_context_t *ctx;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt lwres_lwpacket_t pkt, pkt2;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt lwres_nooprequest_t nooprequest, *nooprequest2;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt lwres_noopresponse_t noopresponse, *noopresponse2;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein lwres_buffer_t b;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson ctx = NULL;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson ret = lwres_context_create(&ctx, NULL, NULL, NULL);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews CHECK(ret, "lwres_context_create");
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater pkt.serial = 0x11223344;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson pkt.recvlength = 0x55667788;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson pkt.result = 0;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson nooprequest.datalength = strlen(TESTSTRING);
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt nooprequest.data = TESTSTRING;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews ret = lwres_nooprequest_render(ctx, &nooprequest, &pkt, &b);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington CHECK(ret, "lwres_nooprequest_render");
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt hexdump("rendered noop request", b.base, b.used);
11156f82bab19b2e7f5d4df6184ae0c99518442fAutomatic Updater
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt /*
11156f82bab19b2e7f5d4df6184ae0c99518442fAutomatic Updater * Now, parse it into a new structure.
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt */
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt lwres_buffer_first(&b);
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt ret = lwres_lwpacket_parseheader(&b, &pkt2);
11156f82bab19b2e7f5d4df6184ae0c99518442fAutomatic Updater CHECK(ret, "lwres_lwpacket_parseheader");
11156f82bab19b2e7f5d4df6184ae0c99518442fAutomatic Updater
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt hexdump("parsed pkt2", &pkt2, sizeof(pkt2));
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt nooprequest2 = NULL;
11156f82bab19b2e7f5d4df6184ae0c99518442fAutomatic Updater ret = lwres_nooprequest_parse(ctx, &b, &pkt2, &nooprequest2);
11156f82bab19b2e7f5d4df6184ae0c99518442fAutomatic Updater CHECK(ret, "lwres_nooprequest_parse");
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt assert(nooprequest.datalength == nooprequest2->datalength);
222d38735f97f771054e223b03f84c5858252332Evan Hunt assert(memcmp(nooprequest.data, nooprequest2->data,
222d38735f97f771054e223b03f84c5858252332Evan Hunt nooprequest.datalength) == 0);
222d38735f97f771054e223b03f84c5858252332Evan Hunt
222d38735f97f771054e223b03f84c5858252332Evan Hunt lwres_nooprequest_free(ctx, &nooprequest2);
222d38735f97f771054e223b03f84c5858252332Evan Hunt
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt lwres_context_freemem(ctx, b.base, b.length);
11156f82bab19b2e7f5d4df6184ae0c99518442fAutomatic Updater b.base = NULL;
11156f82bab19b2e7f5d4df6184ae0c99518442fAutomatic Updater b.length = 0;
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt lwres_context_destroy(&ctx);
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt
11156f82bab19b2e7f5d4df6184ae0c99518442fAutomatic Updater return (0);
11156f82bab19b2e7f5d4df6184ae0c99518442fAutomatic Updater}
5a17fe2916ce37793c12b243ab08c16095a59cf7Evan Hunt