Back to index

lightning-sunbird  0.9+nobinonly
PrimitiveTest.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /* Test the xdr primitives from xpt_xdr.c */
00039 
00040 #include "xpt_xdr.h"
00041 #include <stdio.h>
00042 #include <string.h> /* for memcpy */
00043 
00044 #define PASS(msg)                                                    \
00045   fprintf(stderr, "PASSED : %s\n", msg);
00046 
00047 #define FAIL(msg)                                                    \
00048   fprintf(stderr, "FAILURE: %s\n", msg);
00049 
00050 #define TRY_(msg, cond, silent)                                             \
00051   if ((cond) && !silent) {                                           \
00052     PASS(msg);                                                              \
00053   } else {                                                           \
00054     FAIL(msg);                                                              \
00055     return 1;                                                        \
00056   }
00057 
00058 #define TRY(msg, cond)             TRY_(msg, cond, 0)
00059 #define TRY_Q(msg, cond)    TRY_(msg, cond, 1);
00060 
00061 XPTString in_str = { 4, "bazz" };
00062 
00063 struct TestData {
00064     uint32    bit32;
00065     uint16      bit16;
00066     uint8       bit8[2];
00067     char      *cstr;
00068     XPTString   *str;
00069 } input = { 0xdeadbeef, 0xcafe, {0xba, 0xbe}, "foobar", &in_str},
00070   output = {0, 0, {0, 0}, NULL, NULL };
00071 
00072 void
00073 dump_struct(char *label, struct TestData *str)
00074 {
00075     fprintf(stderr, "%s: {%#08x, %#04x, {%#02x, %#02x}, %s, %d/%s}\n",
00076            label, str->bit32, str->bit16, str->bit8[0], str->bit8[1],
00077            str->cstr, str->str->length, str->str->bytes);
00078 }
00079 
00080 PRBool
00081 XDR(XPTArena *arena, XPTCursor *cursor, struct TestData *str)
00082 {
00083     TRY("Do32", XPT_Do32(cursor, &str->bit32));
00084     TRY("Do16", XPT_Do16(cursor, &str->bit16));
00085     TRY("Do8",  XPT_Do8 (cursor, &str->bit8[0]));
00086     TRY("Do8",  XPT_Do8 (cursor, &str->bit8[1]));
00087     TRY("DoCString", XPT_DoCString(arena, cursor, &str->cstr));
00088     TRY("DoString", XPT_DoString(arena, cursor, &str->str));
00089     return 0;
00090 }
00091 
00092 int
00093 main(int argc, char **argv)
00094 {
00095     XPTArena *arena;
00096     XPTState *state;
00097     XPTCursor curs, *cursor = &curs;
00098     char *header, *data, *whole;
00099     uint32 hlen, dlen, i;
00100 
00101     TRY("XPT_NewArena", (arena = XPT_NewArena(1024, sizeof(double), "main")));
00102     
00103     TRY("NewState (ENCODE)", (state = XPT_NewXDRState(XPT_ENCODE, NULL, 0)));
00104 
00105     XPT_SetDataOffset(state, sizeof input);
00106 
00107     TRY("MakeCursor", XPT_MakeCursor(state, XPT_HEADER, sizeof input, cursor));
00108 
00109     dump_struct("before", &input);
00110 
00111     if (XDR(arena, cursor, &input))
00112        return 1;
00113 
00114     fprintf(stderr, "ENCODE successful\n");
00115     XPT_GetXDRData(state, XPT_HEADER, &header, &hlen);
00116     fprintf(stderr, "XDR header %d bytes at %p:",
00117            hlen, header);
00118     for (i = 0; i < hlen; i++)
00119        fprintf(stderr, "%c%02x", i ? ',' : ' ', (uint8)header[i]);
00120     fprintf(stderr, "\n");
00121 
00122     XPT_GetXDRData(state, XPT_DATA, &data, &dlen);
00123 
00124     fprintf(stderr, "XDR data %d bytes at %p:",
00125            dlen, data);
00126     for (i = 0; i < dlen; i++)
00127        fprintf(stderr, "%c%02x/%c", i ? ',' : ' ', (uint8)data[i],
00128               (uint8)data[i]);
00129     fprintf(stderr, "\n");
00130 
00131     whole = malloc(dlen + hlen);
00132     if (!whole) {
00133        fprintf(stderr, "malloc %d failed!\n", dlen + hlen);
00134        return 1;
00135     }
00136 
00137     /* TRY_Q("malloc", (data2 = malloc(len))); */
00138     memcpy(whole, header, hlen);
00139     memcpy(whole + hlen, data, dlen);
00140     XPT_DestroyXDRState(state);
00141 
00142     TRY("NewState (DECODE)", (state = XPT_NewXDRState(XPT_DECODE, whole,
00143                                                 hlen + dlen)));
00144 
00145     TRY("MakeCursor", XPT_MakeCursor(state, XPT_HEADER, sizeof input, cursor));
00146     XPT_SetDataOffset(state, sizeof input);
00147 
00148     if (XDR(arena, cursor, &output))
00149        return 1;
00150     
00151     dump_struct("after", &output);
00152     XPT_DestroyXDRState(state);
00153     XPT_DestroyArena(arena);
00154     free(whole);
00155 
00156     return 0;
00157 }