diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:49:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:49:04 +0000 |
commit | 16f504a9dca3fe3b70568f67b7d41241ae485288 (patch) | |
tree | c60f36ada0496ba928b7161059ba5ab1ab224f9d /src/libs/xpcom18a4/xpcom/typelib/xpt/tests | |
parent | Initial commit. (diff) | |
download | virtualbox-upstream.tar.xz virtualbox-upstream.zip |
Adding upstream version 7.0.6-dfsg.upstream/7.0.6-dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/libs/xpcom18a4/xpcom/typelib/xpt/tests')
4 files changed, 411 insertions, 0 deletions
diff --git a/src/libs/xpcom18a4/xpcom/typelib/xpt/tests/.cvsignore b/src/libs/xpcom18a4/xpcom/typelib/xpt/tests/.cvsignore new file mode 100644 index 00000000..ebc62d94 --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/typelib/xpt/tests/.cvsignore @@ -0,0 +1,3 @@ +Makefile +PrimitiveTest +SimpleTypeLib diff --git a/src/libs/xpcom18a4/xpcom/typelib/xpt/tests/Makefile.in b/src/libs/xpcom18a4/xpcom/typelib/xpt/tests/Makefile.in new file mode 100644 index 00000000..afdc31b3 --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/typelib/xpt/tests/Makefile.in @@ -0,0 +1,59 @@ +# +# ***** BEGIN LICENSE BLOCK ***** +# Version: MPL 1.1/GPL 2.0/LGPL 2.1 +# +# The contents of this file are subject to the Mozilla Public License Version +# 1.1 (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License +# for the specific language governing rights and limitations under the +# License. +# +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# +# Alternatively, the contents of this file may be used under the terms of +# either of the GNU General Public License Version 2 or later (the "GPL"), +# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), +# in which case the provisions of the GPL or the LGPL are applicable instead +# of those above. If you wish to allow use of your version of this file only +# under the terms of either the GPL or the LGPL, and not to allow others to +# use your version of this file under the terms of the MPL, indicate your +# decision by deleting the provisions above and replace them with the notice +# and other provisions required by the GPL or the LGPL. If you do not delete +# the provisions above, a recipient may use your version of this file under +# the terms of any one of the MPL, the GPL or the LGPL. +# +# ***** END LICENSE BLOCK ***** + +DEPTH = ../../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +MODULE = xpcom +CPP_PROG_LINK = 1 + +SIMPLE_PROGRAMS = PrimitiveTest$(BIN_SUFFIX) SimpleTypeLib$(BIN_SUFFIX) + +CSRCS = PrimitiveTest.c SimpleTypeLib.c + +LIBS = \ + $(XPCOM_LIBS) \ + $(NSPR_LIBS) \ + $(NULL) + +include $(topsrcdir)/config/rules.mk + +DEFINES += -DEXPORT_XPT_API diff --git a/src/libs/xpcom18a4/xpcom/typelib/xpt/tests/PrimitiveTest.c b/src/libs/xpcom18a4/xpcom/typelib/xpt/tests/PrimitiveTest.c new file mode 100644 index 00000000..aad33ccc --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/typelib/xpt/tests/PrimitiveTest.c @@ -0,0 +1,157 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +/* Test the xdr primitives from xpt_xdr.c */ + +#include "xpt_xdr.h" +#include <stdio.h> +#include <string.h> /* for memcpy */ + +#define PASS(msg) \ + fprintf(stderr, "PASSED : %s\n", msg); + +#define FAIL(msg) \ + fprintf(stderr, "FAILURE: %s\n", msg); + +#define TRY_(msg, cond, silent) \ + if ((cond) && !silent) { \ + PASS(msg); \ + } else { \ + FAIL(msg); \ + return 1; \ + } + +#define TRY(msg, cond) TRY_(msg, cond, 0) +#define TRY_Q(msg, cond) TRY_(msg, cond, 1); + +XPTString in_str = { 4, "bazz" }; + +struct TestData { + PRUint32 bit32; + PRUint16 bit16; + PRUint8 bit8[2]; + char *cstr; + XPTString *str; +} input = { 0xdeadbeef, 0xcafe, {0xba, 0xbe}, "foobar", &in_str}, + output = {0, 0, {0, 0}, NULL, NULL }; + +void +dump_struct(char *label, struct TestData *str) +{ + fprintf(stderr, "%s: {%#08x, %#04x, {%#02x, %#02x}, %s, %d/%s}\n", + label, str->bit32, str->bit16, str->bit8[0], str->bit8[1], + str->cstr, str->str->length, str->str->bytes); +} + +PRBool +XDR(XPTArena *arena, XPTCursor *cursor, struct TestData *str) +{ + TRY("Do32", XPT_Do32(cursor, &str->bit32)); + TRY("Do16", XPT_Do16(cursor, &str->bit16)); + TRY("Do8", XPT_Do8 (cursor, &str->bit8[0])); + TRY("Do8", XPT_Do8 (cursor, &str->bit8[1])); + TRY("DoCString", XPT_DoCString(arena, cursor, &str->cstr)); + TRY("DoString", XPT_DoString(arena, cursor, &str->str)); + return 0; +} + +int +main(int argc, char **argv) +{ + XPTArena *arena; + XPTState *state; + XPTCursor curs, *cursor = &curs; + char *header, *data, *whole; + PRUint32 hlen, dlen, i; + + TRY("XPT_NewArena", (arena = XPT_NewArena(1024, sizeof(double), "main"))); + + TRY("NewState (ENCODE)", (state = XPT_NewXDRState(XPT_ENCODE, NULL, 0))); + + XPT_SetDataOffset(state, sizeof input); + + TRY("MakeCursor", XPT_MakeCursor(state, XPT_HEADER, sizeof input, cursor)); + + dump_struct("before", &input); + + if (XDR(arena, cursor, &input)) + return 1; + + fprintf(stderr, "ENCODE successful\n"); + XPT_GetXDRData(state, XPT_HEADER, &header, &hlen); + fprintf(stderr, "XDR header %d bytes at %p:", + hlen, header); + for (i = 0; i < hlen; i++) + fprintf(stderr, "%c%02x", i ? ',' : ' ', (uint8)header[i]); + fprintf(stderr, "\n"); + + XPT_GetXDRData(state, XPT_DATA, &data, &dlen); + + fprintf(stderr, "XDR data %d bytes at %p:", + dlen, data); + for (i = 0; i < dlen; i++) + fprintf(stderr, "%c%02x/%c", i ? ',' : ' ', (uint8)data[i], + (uint8)data[i]); + fprintf(stderr, "\n"); + + whole = malloc(dlen + hlen); + if (!whole) { + fprintf(stderr, "malloc %d failed!\n", dlen + hlen); + return 1; + } + + /* TRY_Q("malloc", (data2 = malloc(len))); */ + memcpy(whole, header, hlen); + memcpy(whole + hlen, data, dlen); + XPT_DestroyXDRState(state); + + TRY("NewState (DECODE)", (state = XPT_NewXDRState(XPT_DECODE, whole, + hlen + dlen))); + + TRY("MakeCursor", XPT_MakeCursor(state, XPT_HEADER, sizeof input, cursor)); + XPT_SetDataOffset(state, sizeof input); + + if (XDR(arena, cursor, &output)) + return 1; + + dump_struct("after", &output); + XPT_DestroyXDRState(state); + XPT_DestroyArena(arena); + free(whole); + + return 0; +} diff --git a/src/libs/xpcom18a4/xpcom/typelib/xpt/tests/SimpleTypeLib.c b/src/libs/xpcom18a4/xpcom/typelib/xpt/tests/SimpleTypeLib.c new file mode 100644 index 00000000..305b8520 --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/typelib/xpt/tests/SimpleTypeLib.c @@ -0,0 +1,192 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +/* Test the structure creation and serialization APIs from xpt_struct.c */ +#include <stdio.h> + +#include "xpt_xdr.h" +#include "xpt_struct.h" + +#define PASS(msg) \ + fprintf(stderr, "PASSED : %s\n", msg); + +#define FAIL(msg) \ + fprintf(stderr, "FAILURE: %s\n", msg); + +#define TRY_(msg, cond, silent) \ + if ((cond) && !silent) { \ + PASS(msg); \ + } else { \ + FAIL(msg); \ + return 1; \ + } + +#define TRY(msg, cond) TRY_(msg, cond, 0) +#define TRY_Q(msg, cond) TRY_(msg, cond, 1); + +struct nsID iid = { + 0x00112233, + 0x4455, + 0x6677, + {0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff} +}; + +XPTTypeDescriptor td_void; + +int +main(int argc, char **argv) +{ + XPTArena *arena; + XPTHeader *header; + XPTAnnotation *ann; + XPTInterfaceDescriptor *id; + XPTMethodDescriptor *meth; + + XPTState *state; + XPTCursor curs, *cursor = &curs; + char *data, *head; + FILE *out; + PRUint32 len, header_sz; + + PRBool ok; + + td_void.prefix.flags = TD_VOID; + +#ifdef XP_MAC + if (argc == 0) { + static char* args[] = { "SimpleTypeLib", "simple.xpt", NULL }; + argc = 2; + argv = args; + } +#endif + + if (argc != 2) { + fprintf(stderr, "Usage: %s <filename.xpt>\n" + " Creates a simple typelib file.\n", argv[0]); + + return 1; + } + + arena = XPT_NewArena(1024, sizeof(double), "main"); + TRY("XPT_NewArena", arena); + + /* construct a header */ + header = XPT_NewHeader(arena, 1, XPT_MAJOR_VERSION, XPT_MINOR_VERSION); + TRY("NewHeader", header); + + + ann = XPT_NewAnnotation(arena, XPT_ANN_LAST | XPT_ANN_PRIVATE, + XPT_NewStringZ(arena, "SimpleTypeLib 1.0"), + XPT_NewStringZ(arena, "See You In Rome")); + TRY("NewAnnotation", ann); + header->annotations = ann; + + header_sz = XPT_SizeOfHeaderBlock(header); + + id = XPT_NewInterfaceDescriptor(arena, 0, 2, 2, 0); + TRY("NewInterfaceDescriptor", id); + + ok = XPT_FillInterfaceDirectoryEntry(arena, header->interface_directory, &iid, + "Interface", "NS", id); + TRY("FillInterfaceDirectoryEntry", ok); + + /* void method1(void) */ + meth = &id->method_descriptors[0]; + ok = XPT_FillMethodDescriptor(arena, meth, 0, "method1", 0); + TRY("FillMethodDescriptor", ok); + meth->result->flags = 0; + meth->result->type.prefix.flags = TD_VOID; + + /* wstring method2(in uint32, in bool) */ + meth = &id->method_descriptors[1]; + ok = XPT_FillMethodDescriptor(arena, meth, 0, "method2", 2); + TRY("FillMethodDescriptor", ok); + + meth->result->flags = 0; + meth->result->type.prefix.flags = TD_PSTRING | XPT_TDP_POINTER; + meth->params[0].type.prefix.flags = TD_UINT32; + meth->params[0].flags = XPT_PD_IN; + meth->params[1].type.prefix.flags = TD_BOOL; + meth->params[1].flags = XPT_PD_IN; + +#if 0 + /* const one = 1; */ + id->const_descriptors[0].name = "one"; + id->const_descriptors[0].type.prefix.flags = TD_UINT16; + id->const_descriptors[0].value.ui16 = 1; + + /* const squeamish = "ossifrage"; */ + id->const_descriptors[1].name = "squeamish"; + id->const_descriptors[1].type.prefix.flags = TD_PBSTR | XPT_TDP_POINTER; + id->const_descriptors[1].value.string = XPT_NewStringZ(arena, "ossifrage"); +#endif + + /* serialize it */ + state = XPT_NewXDRState(XPT_ENCODE, NULL, 0); + TRY("NewState (ENCODE)", state); + + ok = XPT_MakeCursor(state, XPT_HEADER, header_sz, cursor); + TRY("MakeCursor", ok); + + ok = XPT_DoHeader(arena, cursor, &header); + TRY("DoHeader", ok); + + out = fopen(argv[1], "wb"); + if (!out) { + perror("FAILED: fopen"); + return 1; + } + + XPT_GetXDRData(state, XPT_HEADER, &head, &len); + fwrite(head, len, 1, out); + + XPT_GetXDRData(state, XPT_DATA, &data, &len); + fwrite(data, len, 1, out); + + if (ferror(out) != 0 || fclose(out) != 0) { + fprintf(stderr, "\nError writing file: %s\n\n", argv[1]); + } else { + fprintf(stderr, "\nFile written: %s\n\n", argv[1]); + } + XPT_DestroyXDRState(state); + + XPT_FreeHeader(arena, header); + XPT_DestroyArena(arena); + + return 0; +} + |