152a38012Sejakowatz//------------------------------------------------------------------------------
252a38012Sejakowatz//	InstantiateObjectTester.cpp
352a38012Sejakowatz//
452a38012Sejakowatz/**
552a38012Sejakowatz	Testing of instantiate_object(BMessage* archive, image_id* id)
652a38012Sejakowatz	@note	No cases are currently defined for NULL 'id' parameter, since NULL
752a38012Sejakowatz			is a valid value for it.  Perhaps there should be to ensure that the
852a38012Sejakowatz			instantiate_object is, in fact, dealing with that case correctly.
952a38012Sejakowatz			There are also no tests against instantiate_object(BMessage*) as it
1052a38012Sejakowatz			simply calls instantiate_object(BMessage*, image_id*) with NULL for
1152a38012Sejakowatz			the image_id parameter.
1252a38012Sejakowatz */
1352a38012Sejakowatz//------------------------------------------------------------------------------
1452a38012Sejakowatz
158a8c62d5SAxel Dörfler#include "InstantiateObjectTester.h"
168a8c62d5SAxel Dörfler
1752a38012Sejakowatz// Standard Includes -----------------------------------------------------------
1852a38012Sejakowatz#include <errno.h>
1952a38012Sejakowatz#include <stdexcept>
208a8c62d5SAxel Dörfler#include <iostream>
2152a38012Sejakowatz
2252a38012Sejakowatz// System Includes -------------------------------------------------------------
2376a5f348Sbeveloper#include <Roster.h>
2476a5f348Sbeveloper#include <Entry.h>
2576a5f348Sbeveloper#include <Path.h>
2652a38012Sejakowatz
2752a38012Sejakowatz// Project Includes ------------------------------------------------------------
28a6b33ea3STyler Dauwalder#include <cppunit/Exception.h>
296b3c7cc3STyler Dauwalder#include <TestShell.h>
3052a38012Sejakowatz
3152a38012Sejakowatz// Local Includes --------------------------------------------------------------
32a6b33ea3STyler Dauwalder#include "remoteobjectdef/RemoteTestObject.h"
3352a38012Sejakowatz#include "LocalTestObject.h"
3452a38012Sejakowatz
358a8c62d5SAxel Dörflerusing namespace std;
368a8c62d5SAxel Dörfler
3752a38012Sejakowatz// Local Defines ---------------------------------------------------------------
3852a38012Sejakowatz#define FORMAT_AND_THROW(MSG, ERR)	\
3952a38012Sejakowatz	FormatAndThrow(__LINE__, __FILE__, MSG, ERR)
4052a38012Sejakowatz
4152a38012Sejakowatz// Globals ---------------------------------------------------------------------
4252a38012Sejakowatzconst char* gInvalidClassName	= "TInvalidClassName";
4352a38012Sejakowatzconst char* gInvalidSig			= "application/x-vnd.InvalidSignature";
4452a38012Sejakowatzconst char* gLocalClassName		= "TIOTest";
4552a38012Sejakowatzconst char* gLocalSig			= "application/x-vnd.LocalSignature";
4652a38012Sejakowatzconst char* gRemoteClassName	= "TRemoteTestObject";
4752a38012Sejakowatzconst char* gRemoteSig			= "application/x-vnd.RemoteObjectDef";
4852a38012Sejakowatzconst char* gValidSig			= gRemoteSig;
496b3c7cc3STyler Dauwalder#if !TEST_R5
506b3c7cc3STyler Dauwalderconst char* gRemoteLib			= "/lib/libsupporttest_RemoteTestObject.so";
516b3c7cc3STyler Dauwalder#else
522605a4f5Sejakowatzconst char* gRemoteLib			= "/lib/libsupporttest_RemoteTestObject_r5.so";
536b3c7cc3STyler Dauwalder#endif
5452a38012Sejakowatz
5552a38012Sejakowatzvoid FormatAndThrow(int line, const char* file, const char* msg, int err);
5652a38012Sejakowatz
5752a38012Sejakowatz//------------------------------------------------------------------------------
5852a38012SejakowatzTInstantiateObjectTester::TInstantiateObjectTester(string name)
59a6b33ea3STyler Dauwalder	:	BTestCase(name), fAddonId(B_ERROR)
6052a38012Sejakowatz{
6152a38012Sejakowatz	;
6252a38012Sejakowatz}
6352a38012Sejakowatz//------------------------------------------------------------------------------
6452a38012Sejakowatz/**
6552a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
6652a38012Sejakowatz	@case			Invalid archive
6752a38012Sejakowatz	@param archive	NULL
6852a38012Sejakowatz	@param id		Valid image_id pointer
6952a38012Sejakowatz	@results		Returns NULL.
7052a38012Sejakowatz					*id is set to B_BAD_VALUE.
7152a38012Sejakowatz					errno is set to B_BAD_VALUE.
7252a38012Sejakowatz */
7352a38012Sejakowatzvoid TInstantiateObjectTester::Case1()
7452a38012Sejakowatz{
7552a38012Sejakowatz	errno = B_OK;
7652a38012Sejakowatz	image_id id = B_OK;
7752a38012Sejakowatz	TIOTest* Test = (TIOTest*)instantiate_object(NULL, &id);
78a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test == NULL);
79a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id == B_BAD_VALUE);
80a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_BAD_VALUE);
8152a38012Sejakowatz}
8252a38012Sejakowatz//------------------------------------------------------------------------------
8352a38012Sejakowatz/**
8452a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
8552a38012Sejakowatz	@case			No class name
8652a38012Sejakowatz	@param archive	Valid BMessage pointer without string field "class"
8752a38012Sejakowatz	@param id		Valid image_id pointer
8852a38012Sejakowatz	@results		Returns NULL.
8952a38012Sejakowatz					*id is set to B_BAD_VALUE.
9052a38012Sejakowatz					errno is set to B_OK.
9152a38012Sejakowatz */
9252a38012Sejakowatzvoid TInstantiateObjectTester::Case2()
9352a38012Sejakowatz{
9452a38012Sejakowatz	errno = B_OK;
9552a38012Sejakowatz	BMessage Archive;
9652a38012Sejakowatz	image_id id = B_OK;
9752a38012Sejakowatz	TIOTest* Test = (TIOTest*)instantiate_object(&Archive, &id);
98a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test == NULL);
99a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id == B_BAD_VALUE);
100a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_OK);
10152a38012Sejakowatz}
10252a38012Sejakowatz//------------------------------------------------------------------------------
10352a38012Sejakowatz
10452a38012Sejakowatz//------------------------------------------------------------------------------
10552a38012Sejakowatz//	Invalid class name tests
10652a38012Sejakowatz//------------------------------------------------------------------------------
10752a38012Sejakowatz/**
10852a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
10952a38012Sejakowatz	@case			Invalid class name
11052a38012Sejakowatz	@param archive	Valid BMessage pointer, with string field labeled "class"
11152a38012Sejakowatz					containing an invalid class name
11252a38012Sejakowatz	@param id		Valid image_id pointer
11352a38012Sejakowatz	@results		Returns NULL.
11452a38012Sejakowatz					*id is set to B_BAD_VALUE.
11552a38012Sejakowatz					errno is set to B_BAD_VALUE.
11652a38012Sejakowatz */
11752a38012Sejakowatzvoid TInstantiateObjectTester::Case3()
11852a38012Sejakowatz{
11952a38012Sejakowatz	errno = B_OK;
12052a38012Sejakowatz	BMessage Archive;
12152a38012Sejakowatz	Archive.AddString("class", gInvalidClassName);
12252a38012Sejakowatz	image_id id = B_OK;
12352a38012Sejakowatz	TIOTest* Test = (TIOTest*)instantiate_object(&Archive, &id);
124a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test == NULL);
125a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id == B_BAD_VALUE);
126a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_BAD_VALUE);
12752a38012Sejakowatz}
12852a38012Sejakowatz//------------------------------------------------------------------------------
12952a38012Sejakowatz/**
13052a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
13152a38012Sejakowatz	@case			Invalid class name and signature
13252a38012Sejakowatz	@param archive	Valid BMessage pointer, with string fields labeled "class"
13352a38012Sejakowatz					and "add_on", containing invalid class name and signature,
13452a38012Sejakowatz					respectively
13552a38012Sejakowatz	@param id		Valid image_id pointer
13652a38012Sejakowatz	@results		Returns NULL.
13752a38012Sejakowatz					*id is set to B_BAD_VALUE.
13852a38012Sejakowatz					errno is set to B_LAUNCH_FAILED_APP_NOT_FOUND.
13952a38012Sejakowatz */
14052a38012Sejakowatzvoid TInstantiateObjectTester::Case4()
14152a38012Sejakowatz{
14252a38012Sejakowatz	errno = B_OK;
14352a38012Sejakowatz	BMessage Archive;
14452a38012Sejakowatz	Archive.AddString("class", gInvalidClassName);
14552a38012Sejakowatz	Archive.AddString("add_on", gInvalidSig);
14652a38012Sejakowatz	image_id id = B_OK;
14752a38012Sejakowatz	TIOTest* Test = (TIOTest*)instantiate_object(&Archive, &id);
148a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test == NULL);
149a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id == B_BAD_VALUE);
150a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_LAUNCH_FAILED_APP_NOT_FOUND);
15152a38012Sejakowatz}
15252a38012Sejakowatz//------------------------------------------------------------------------------
15352a38012Sejakowatz/**
15452a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
15552a38012Sejakowatz	@case			Invalid class name, valid signature
15652a38012Sejakowatz	@param archive	Valid BMessage pointer with string fields labeled "class"
15752a38012Sejakowatz					and "add_on", containing invalid class name and valid
15852a38012Sejakowatz					signature, respectively
15952a38012Sejakowatz	@param id		Valid image_id pointer
16052a38012Sejakowatz	@requires		RemoteObjectDef add-on must be built and accessible
16152a38012Sejakowatz	@results		Returns NULL.
16252a38012Sejakowatz					*id is > 0 (add-on was loaded)
16352a38012Sejakowatz					errno is set to B_BAD_VALUE.
16452a38012Sejakowatz */
16552a38012Sejakowatzvoid TInstantiateObjectTester::Case5()
16652a38012Sejakowatz{
16752a38012Sejakowatz	errno = B_OK;
16852a38012Sejakowatz	BMessage Archive;
16952a38012Sejakowatz	Archive.AddString("class", gInvalidClassName);
17052a38012Sejakowatz	Archive.AddString("add_on", gValidSig);
17152a38012Sejakowatz	image_id id = B_OK;
17252a38012Sejakowatz	TIOTest* Test = (TIOTest*)instantiate_object(&Archive, &id);
173a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test == NULL);
17452a38012Sejakowatz	// The system implementation returns the image_id of the last addon searched
17552a38012Sejakowatz	// Implies the addon is not unloaded.  How to verify this behaviour?  Should
17652a38012Sejakowatz	// the addon be unloaded if it doesn't contain our function?  Addons do,
17752a38012Sejakowatz	// after all, eat into our allowable memory.
17852a38012Sejakowatz
17952a38012Sejakowatz	// Verified that addon is *not* unloaded in the Be implementation.  If Case8
18052a38012Sejakowatz	// runs after this case without explicitely unloaded the addon here, it
18152a38012Sejakowatz	// fails because it depends on the addon image not being available within
18252a38012Sejakowatz	// the team.
183a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id > 0);
18452a38012Sejakowatz	unload_add_on(id);
185a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_BAD_VALUE);
18652a38012Sejakowatz}
18752a38012Sejakowatz//------------------------------------------------------------------------------
18852a38012Sejakowatz
18952a38012Sejakowatz
19052a38012Sejakowatz//------------------------------------------------------------------------------
19152a38012Sejakowatz//	Valid class name tests
19252a38012Sejakowatz//------------------------------------------------------------------------------
19352a38012Sejakowatz/**
19452a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
19552a38012Sejakowatz	@case			Valid archive of class defined in local image
19652a38012Sejakowatz	@param archive	Valid BMessage pointer with string field "class" containing
19752a38012Sejakowatz					name of locally defined class which can be instantiated via
19852a38012Sejakowatz					archiving mechanism
19952a38012Sejakowatz	@param id		Valid image_id pointer
20052a38012Sejakowatz	@requires		locally defined class which can be instantiated via
20152a38012Sejakowatz					archiving mechanism
20252a38012Sejakowatz	@results		Returns valid TIOTest instance.
20352a38012Sejakowatz					*id is set to B_BAD_VALUE (no image was loaded).
20452a38012Sejakowatz					errno is set to B_OK.
20552a38012Sejakowatz */
20652a38012Sejakowatz//	No sig
20752a38012Sejakowatz//		Local app -- local class
20852a38012Sejakowatzvoid TInstantiateObjectTester::Case6()
20952a38012Sejakowatz{
21052a38012Sejakowatz	errno = B_OK;
21152a38012Sejakowatz	BMessage Archive;
21252a38012Sejakowatz	Archive.AddString("class", gLocalClassName);
21352a38012Sejakowatz	image_id id = B_OK;
21452a38012Sejakowatz	TIOTest* Test = (TIOTest*)instantiate_object(&Archive, &id);
215a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test != NULL);
216a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id == B_BAD_VALUE);
217a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_OK);
21852a38012Sejakowatz}
21952a38012Sejakowatz//------------------------------------------------------------------------------
22052a38012Sejakowatz/**
22152a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
22252a38012Sejakowatz	@case			Valid archive of class defined in add-on explicitely loaded
22352a38012Sejakowatz					by this team
22452a38012Sejakowatz	@param archive	Valid BMessage pointer with string field "class" containing
22552a38012Sejakowatz					name of remotely defined class which can be instantiated via
22652a38012Sejakowatz					archiving mechanism
22752a38012Sejakowatz	@param id		Valid image_id pointer
22852a38012Sejakowatz	@requires		RemoteObjectDef add-on must be built and accessible
22952a38012Sejakowatz	@results		Returns valid TRemoteTestObject instance.
23052a38012Sejakowatz					*id is set to B_BAD_VALUE (no image was loaded).
23152a38012Sejakowatz					errno is set to B_OK.
23252a38012Sejakowatz */
23352a38012Sejakowatzvoid TInstantiateObjectTester::Case7()
23452a38012Sejakowatz{
23552a38012Sejakowatz	errno = B_OK;
23652a38012Sejakowatz	LoadAddon();
23752a38012Sejakowatz
23852a38012Sejakowatz	BMessage Archive;
23952a38012Sejakowatz	Archive.AddString("class", gRemoteClassName);
24052a38012Sejakowatz	image_id id = B_OK;
24152a38012Sejakowatz	TRemoteTestObject* Test = (TRemoteTestObject*)instantiate_object(&Archive,
24252a38012Sejakowatz																	 &id);
243a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test != NULL);
244a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id == B_BAD_VALUE);
245a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_OK);
24652a38012Sejakowatz
24752a38012Sejakowatz	UnloadAddon();
24852a38012Sejakowatz}
24952a38012Sejakowatz//------------------------------------------------------------------------------
25052a38012Sejakowatz/**
25152a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
25252a38012Sejakowatz	@case			Valid archive of remotely-defined class, without required
25352a38012Sejakowatz					signature of the defining add-on
25452a38012Sejakowatz	@param archive	Valid BMessage pointer with string field "class" containing
25552a38012Sejakowatz					name of remotely-defined class; no "add-on" field
25652a38012Sejakowatz	@param id		Valid image_id pointer
25752a38012Sejakowatz	@results		Returns NULL.
25852a38012Sejakowatz					*id is set to B_BAD_VALUE (no image loaded).
25952a38012Sejakowatz					errno is set to B_BAD_VALUE.
26052a38012Sejakowatz */
26152a38012Sejakowatzvoid TInstantiateObjectTester::Case8()
262df8b692aSKacper Kasper{
26352a38012Sejakowatz	errno = B_OK;
26452a38012Sejakowatz	BMessage Archive;
2652605a4f5Sejakowatz	CPPUNIT_ASSERT(Archive.AddString("class", gRemoteClassName) == B_OK);
26652a38012Sejakowatz	image_id id = B_OK;
26752a38012Sejakowatz	TRemoteTestObject* Test = (TRemoteTestObject*)instantiate_object(&Archive,
26852a38012Sejakowatz																	 &id);
269a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test == NULL);
270a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id == B_BAD_VALUE);
271a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_BAD_VALUE);
27252a38012Sejakowatz}
27352a38012Sejakowatz//------------------------------------------------------------------------------
27452a38012Sejakowatz/**
27552a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
27652a38012Sejakowatz	@case			Valid archive naming locally defined class with invalid
27752a38012Sejakowatz					signature
27852a38012Sejakowatz	@param archive	Valid BMessage pointer with string field "class" containing
27952a38012Sejakowatz					name of locally defined class and string field "add_on"
28052a38012Sejakowatz					containing invalid signature
28152a38012Sejakowatz	@param id		Valid image_id pointer
28252a38012Sejakowatz	@results		Returns NULL.
28352a38012Sejakowatz					*id is set to B_BAD_VALUE (no image loaded).
28452a38012Sejakowatz					errno is set to B_LAUNCH_FAILED_APP_NOT_FOUND.
28552a38012Sejakowatz */
28652a38012Sejakowatzvoid TInstantiateObjectTester::Case9()
28752a38012Sejakowatz{
28852a38012Sejakowatz	errno = B_OK;
28952a38012Sejakowatz	BMessage Archive;
2902605a4f5Sejakowatz	CPPUNIT_ASSERT(Archive.AddString("class", gLocalClassName) == B_OK);
2912605a4f5Sejakowatz	CPPUNIT_ASSERT(Archive.AddString("add_on", gInvalidSig) == B_OK);
29252a38012Sejakowatz	image_id id = B_OK;
29352a38012Sejakowatz	TIOTest* Test = (TIOTest*)instantiate_object(&Archive, &id);
294a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test == NULL);
295a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id == B_BAD_VALUE);
296a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_LAUNCH_FAILED_APP_NOT_FOUND);
29752a38012Sejakowatz}
29852a38012Sejakowatz//------------------------------------------------------------------------------
29952a38012Sejakowatz/**
30052a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
30152a38012Sejakowatz	@case			Valid archive of class defined in add-on explicitely loaded
30252a38012Sejakowatz					by this team, but with an invalid signature
30352a38012Sejakowatz	@param archive	Valid BMessage pointer with string field "class" containing
30452a38012Sejakowatz					name of remotely-defined class and string field "add_on"
30552a38012Sejakowatz					containing invalid signature
30652a38012Sejakowatz	@param id		Valid image_id pointer
30752a38012Sejakowatz	@requires		RemoteObjectDef add-on must be built and accessible
30852a38012Sejakowatz	@results		Returns NULL.
30952a38012Sejakowatz					*id is set to B_BAD_VALUE (no image loaded).
31052a38012Sejakowatz					errno is set to B_LAUNCH_FAILED_APP_NOT_FOUND.
31152a38012Sejakowatz */
31252a38012Sejakowatzvoid TInstantiateObjectTester::Case10()
31352a38012Sejakowatz{
31452a38012Sejakowatz	errno = B_OK;
31552a38012Sejakowatz	LoadAddon();
31652a38012Sejakowatz
31752a38012Sejakowatz	BMessage Archive;
31852a38012Sejakowatz	Archive.AddString("class", gRemoteClassName);
31952a38012Sejakowatz	Archive.AddString("add_on", gInvalidSig);
32052a38012Sejakowatz	image_id id = B_OK;
32152a38012Sejakowatz	TIOTest* Test = (TIOTest*)instantiate_object(&Archive, &id);
322a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test == NULL);
323a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id == B_BAD_VALUE);
324a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_LAUNCH_FAILED_APP_NOT_FOUND);
32552a38012Sejakowatz
32652a38012Sejakowatz	UnloadAddon();
32752a38012Sejakowatz}
32852a38012Sejakowatz//------------------------------------------------------------------------------
32952a38012Sejakowatz/**
33052a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
33152a38012Sejakowatz	@case			Valid archive of remotely-defined class, with invalid
33252a38012Sejakowatz					signature
33352a38012Sejakowatz	@param archive	Valid BMessage pointer with string field "class" containing
33452a38012Sejakowatz					name of remotely-defined class and string field add-on
33552a38012Sejakowatz					containing invalid signature
33652a38012Sejakowatz	@param id		Valid image_id pointer
33752a38012Sejakowatz	@results		Returns NULL.
33852a38012Sejakowatz					*id is set to B_BAD_VALUE.
33952a38012Sejakowatz					errno is set to B_LAUNCH_FAILED_APP_NOT_FOUND
34052a38012Sejakowatz */
34152a38012Sejakowatzvoid TInstantiateObjectTester::Case11()
34252a38012Sejakowatz{
34352a38012Sejakowatz	errno = B_OK;
34452a38012Sejakowatz	BMessage Archive;
34552a38012Sejakowatz	Archive.AddString("class", gRemoteClassName);
34652a38012Sejakowatz	Archive.AddString("add_on", gInvalidSig);
34752a38012Sejakowatz	image_id id = B_OK;
34852a38012Sejakowatz	TIOTest* Test = (TIOTest*)instantiate_object(&Archive, &id);
349a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test == NULL);
350a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id == B_BAD_VALUE);
351a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_LAUNCH_FAILED_APP_NOT_FOUND);
35252a38012Sejakowatz}
35352a38012Sejakowatz//------------------------------------------------------------------------------
35452a38012Sejakowatz/**
35552a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
35652a38012Sejakowatz	@case			Valid archive of locally-defined class with correct
35752a38012Sejakowatz					signature
35852a38012Sejakowatz	@param archive	Valid BMessage pointer with string field "class" containing
35952a38012Sejakowatz					name of locally-defined class and string field "add_on"
36052a38012Sejakowatz					containing signature of current team
36152a38012Sejakowatz	@param id		Valid image_id pointer
36252a38012Sejakowatz	@requires		locally defined class which can be instantiated via
36352a38012Sejakowatz					archiving mechanism
36452a38012Sejakowatz	@results		Returns valid TIOTest instance.
36552a38012Sejakowatz					*id is set to B_BAD_VALUE (no image loaded).
36652a38012Sejakowatz					errno is set to B_OK.
36752a38012Sejakowatz	@note			This test is not currently used; GetLocalSignature() doesn't
36852a38012Sejakowatz					seem to work without a BApplication instance constructed.
36952a38012Sejakowatz					See GetLocalSignature() for more info.
37052a38012Sejakowatz */
37152a38012Sejakowatzvoid TInstantiateObjectTester::Case12()
37252a38012Sejakowatz{
37352a38012Sejakowatz	errno = B_OK;
37452a38012Sejakowatz	BMessage Archive;
37552a38012Sejakowatz	Archive.AddString("class", gLocalClassName);
37652a38012Sejakowatz	Archive.AddString("add_on", GetLocalSignature().c_str());
37752a38012Sejakowatz	image_id id = B_OK;
37852a38012Sejakowatz	TIOTest* Test = (TIOTest*)instantiate_object(&Archive, &id);
379a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test != NULL);
380a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id == B_BAD_VALUE);
381a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_OK);
38252a38012Sejakowatz}
38352a38012Sejakowatz//------------------------------------------------------------------------------
38452a38012Sejakowatz/**
38552a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
38652a38012Sejakowatz	@case			Valid archive of class defined in add-on explicitely loaded
38752a38012Sejakowatz					by this team with signature of add-on
38852a38012Sejakowatz	@param archive	Valid BMessage pointer with string field "class" containing
38952a38012Sejakowatz					name of remotely-defined class and string field "add_on"
39052a38012Sejakowatz					containing signature of loaded add-on
39152a38012Sejakowatz	@param id		Valid image_id pointer
39252a38012Sejakowatz	@requires		RemoteObjectDef add-on must be built and accessible
39352a38012Sejakowatz	@results		Returns valid instance of TRemoteTestObject.
39452a38012Sejakowatz					*id is set to B_BAD_VALUE (image load not necessary).
39552a38012Sejakowatz					errno is set to B_OK.
39652a38012Sejakowatz */
39752a38012Sejakowatzvoid TInstantiateObjectTester::Case13()
39852a38012Sejakowatz{
39952a38012Sejakowatz	errno = B_OK;
40052a38012Sejakowatz	LoadAddon();
40152a38012Sejakowatz
40252a38012Sejakowatz	BMessage Archive;
40352a38012Sejakowatz	Archive.AddString("class", gRemoteClassName);
40452a38012Sejakowatz	Archive.AddString("add_on", gRemoteSig);
40552a38012Sejakowatz	image_id id = B_OK;
40652a38012Sejakowatz	TRemoteTestObject* Test = (TRemoteTestObject*)instantiate_object(&Archive, &id);
407a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test != NULL);
408a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id == B_BAD_VALUE);
409a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_OK);
41052a38012Sejakowatz
41152a38012Sejakowatz	UnloadAddon();
41252a38012Sejakowatz}
41352a38012Sejakowatz//------------------------------------------------------------------------------
41452a38012Sejakowatz/**
41552a38012Sejakowatz	instantiate_object(BMessage* archive, image_id* id)
41652a38012Sejakowatz	@case			Valid archive of remotely-defined class with correct
41752a38012Sejakowatz					signature
41852a38012Sejakowatz	@param archive	Valid BMessage pointer with string field "class" containing
41952a38012Sejakowatz					name of remotely-defined class and string field "add_on"
42052a38012Sejakowatz					containing signature of defining add-on
42152a38012Sejakowatz	@param id		Valid image_id pointer
42252a38012Sejakowatz	@requires		RemoteObjectDef must be built and accessible
42352a38012Sejakowatz	@results		Returns valid instance of TRemoteTestObject.
42452a38012Sejakowatz					*id > 0 (image was loaded).
42552a38012Sejakowatz					errno is set to B_OK.
42652a38012Sejakowatz */
42752a38012Sejakowatzvoid TInstantiateObjectTester::Case14()
42852a38012Sejakowatz{
42952a38012Sejakowatz	errno = B_OK;
43052a38012Sejakowatz	BMessage Archive;
43152a38012Sejakowatz	Archive.AddString("class", gRemoteClassName);
43252a38012Sejakowatz	Archive.AddString("add_on", gRemoteSig);
43352a38012Sejakowatz	image_id id = B_OK;
43452a38012Sejakowatz	TRemoteTestObject* Test = (TRemoteTestObject*)instantiate_object(&Archive, &id);
435a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(Test != NULL);
436a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(id > 0);
43752a38012Sejakowatz	unload_add_on(id);
438a6b33ea3STyler Dauwalder	CPPUNIT_ASSERT(errno == B_OK);
43952a38012Sejakowatz}
44052a38012Sejakowatz//------------------------------------------------------------------------------
441a6b33ea3STyler DauwalderCppUnit::Test* TInstantiateObjectTester::Suite()
44252a38012Sejakowatz{
443a6b33ea3STyler Dauwalder	CppUnit::TestSuite* SuiteOfTests = new CppUnit::TestSuite;
44452a38012Sejakowatz
445be2939caSTyler Dauwalder//	SuiteOfTests->addTest(
446be2939caSTyler Dauwalder//		new CppUnit::TestCaller<TInstantiateObjectTester>("BArchivable::instantiate_object() Test",
447be2939caSTyler Dauwalder//			&TInstantiateObjectTester::RunTests));
448be2939caSTyler Dauwalder
44952a38012Sejakowatz	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case1);
45052a38012Sejakowatz	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case2);
45152a38012Sejakowatz	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case3);
45252a38012Sejakowatz	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case4);
45352a38012Sejakowatz	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case5);
45452a38012Sejakowatz	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case6);
45552a38012Sejakowatz	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case8);
4562605a4f5Sejakowatz	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case7);
45752a38012Sejakowatz	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case9);
45852a38012Sejakowatz	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case10);
45952a38012Sejakowatz	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case11);
46052a38012Sejakowatz//	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case12);
46152a38012Sejakowatz	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case13);
46252a38012Sejakowatz	ADD_TEST(SuiteOfTests, TInstantiateObjectTester, Case14);
46352a38012Sejakowatz
46452a38012Sejakowatz	return SuiteOfTests;
46552a38012Sejakowatz}
46652a38012Sejakowatz//------------------------------------------------------------------------------
46752a38012Sejakowatzvoid TInstantiateObjectTester::LoadAddon()
46852a38012Sejakowatz{
46952a38012Sejakowatz	if (fAddonId > 0)
47052a38012Sejakowatz		return;
47152a38012Sejakowatz
4726b3c7cc3STyler Dauwalder	// We're not testing the roster, so I'm going to just
4736b3c7cc3STyler Dauwalder	// find the add-on manually.
4746b3c7cc3STyler Dauwalder	std::string libPath = std::string(BTestShell::GlobalTestDir()) + gRemoteLib;
4756b3c7cc3STyler Dauwalder	cout << "dir == '" << libPath << "'" << endl;
4766b3c7cc3STyler Dauwalder	fAddonId = load_add_on(libPath.c_str());
47752a38012Sejakowatz
4786b3c7cc3STyler Dauwalder	RES(fAddonId);
47952a38012Sejakowatz	if (fAddonId <= 0)
48052a38012Sejakowatz	{
48152a38012Sejakowatz		FORMAT_AND_THROW(" failed to load addon: ", fAddonId);
48252a38012Sejakowatz	}
48352a38012Sejakowatz}
48452a38012Sejakowatz//------------------------------------------------------------------------------
48552a38012Sejakowatzvoid TInstantiateObjectTester::UnloadAddon()
48652a38012Sejakowatz{
48752a38012Sejakowatz	if (fAddonId > 0)
48852a38012Sejakowatz	{
48952a38012Sejakowatz		status_t err = unload_add_on(fAddonId);
49052a38012Sejakowatz		fAddonId = B_ERROR;
49152a38012Sejakowatz		if (err)
49252a38012Sejakowatz		{
49352a38012Sejakowatz			FORMAT_AND_THROW(" failed to unload addon: ", err);
49452a38012Sejakowatz		}
49552a38012Sejakowatz	}
49652a38012Sejakowatz}
49752a38012Sejakowatz//------------------------------------------------------------------------------
49852a38012Sejakowatzstd::string TInstantiateObjectTester::GetLocalSignature()
49952a38012Sejakowatz{
50052a38012Sejakowatz	BRoster Roster;
50152a38012Sejakowatz	app_info ai;
50252a38012Sejakowatz	team_id team;
50352a38012Sejakowatz
50452a38012Sejakowatz	// Get the team_id of this app
50552a38012Sejakowatz	thread_id tid = find_thread(NULL);
50652a38012Sejakowatz	thread_info ti;
50752a38012Sejakowatz	status_t err = get_thread_info(tid, &ti);
50852a38012Sejakowatz	if (err)
50952a38012Sejakowatz	{
51052a38012Sejakowatz		FORMAT_AND_THROW(" failed to get thread_info: ", err);
51152a38012Sejakowatz	}
51252a38012Sejakowatz
51352a38012Sejakowatz	// Get the app_info via the team_id
51452a38012Sejakowatz	team = ti.team;
51552a38012Sejakowatz	team_info info;
51652a38012Sejakowatz	err = get_team_info(team, &info);
51752a38012Sejakowatz	if (err)
51852a38012Sejakowatz	{
51952a38012Sejakowatz		FORMAT_AND_THROW(" failed to get team_info: ", err);
52052a38012Sejakowatz	}
52152a38012Sejakowatz
52252a38012Sejakowatz	team = info.team;
52352a38012Sejakowatz
52452a38012Sejakowatz	// It seems that this call to GetRunningAppInfo() is not working because we
52552a38012Sejakowatz	// don't have an instance of BApplication somewhere -- the roster, therefore,
52652a38012Sejakowatz	// doesn't know about us.
52752a38012Sejakowatz	err = Roster.GetRunningAppInfo(team, &ai);
52852a38012Sejakowatz	if (err)
52952a38012Sejakowatz	{
53052a38012Sejakowatz		FORMAT_AND_THROW(" failed to get app_info: ", err);
53152a38012Sejakowatz	}
53252a38012Sejakowatz
53352a38012Sejakowatz	// Return the signature from the app_info
53452a38012Sejakowatz	return ai.signature;
53552a38012Sejakowatz}
53652a38012Sejakowatz//------------------------------------------------------------------------------
53752a38012Sejakowatz
53852a38012Sejakowatz//------------------------------------------------------------------------------
53952a38012Sejakowatzvoid FormatAndThrow(int line, const char *file, const char *msg, int err)
54052a38012Sejakowatz{
541a6b33ea3STyler Dauwalder	std::string s("line: ");
542a6b33ea3STyler Dauwalder	s += IntToStr(line);
54352a38012Sejakowatz	s += " ";
54452a38012Sejakowatz	s += file;
54552a38012Sejakowatz	s += msg;
54652a38012Sejakowatz	s += strerror(err);
54752a38012Sejakowatz	s += "(";
548a6b33ea3STyler Dauwalder	s += IntToStr(err);
54952a38012Sejakowatz	s += ")";
550a6b33ea3STyler Dauwalder	CppUnit::Exception re(s.c_str());
55152a38012Sejakowatz	throw re;
55252a38012Sejakowatz}
55352a38012Sejakowatz//------------------------------------------------------------------------------
55452a38012Sejakowatz
555be2939caSTyler Dauwaldervoid
556be2939caSTyler DauwalderTInstantiateObjectTester::RunTests() {
557be2939caSTyler Dauwalder	NextSubTest();
558be2939caSTyler Dauwalder	Case1();
559be2939caSTyler Dauwalder	NextSubTest();
560be2939caSTyler Dauwalder	Case2();
561be2939caSTyler Dauwalder	NextSubTest();
562be2939caSTyler Dauwalder	Case3();
563be2939caSTyler Dauwalder	NextSubTest();
564be2939caSTyler Dauwalder	Case4();
565be2939caSTyler Dauwalder	NextSubTest();
566be2939caSTyler Dauwalder	Case5();
567be2939caSTyler Dauwalder	NextSubTest();
568be2939caSTyler Dauwalder	Case6();
569be2939caSTyler Dauwalder	NextSubTest();
570be2939caSTyler Dauwalder	Case7();
571be2939caSTyler Dauwalder	NextSubTest();
572be2939caSTyler Dauwalder	Case8();
573be2939caSTyler Dauwalder	NextSubTest();
574be2939caSTyler Dauwalder	Case9();
575be2939caSTyler Dauwalder	NextSubTest();
576be2939caSTyler Dauwalder	Case10();
577be2939caSTyler Dauwalder	NextSubTest();
578be2939caSTyler Dauwalder	Case11();
579be2939caSTyler Dauwalder	NextSubTest();
580be2939caSTyler Dauwalder	Case12();
581be2939caSTyler Dauwalder	NextSubTest();
582be2939caSTyler Dauwalder	Case13();
583be2939caSTyler Dauwalder	NextSubTest();
584be2939caSTyler Dauwalder	Case14();
585be2939caSTyler Dauwalder}
586be2939caSTyler Dauwalder
58752a38012Sejakowatz/*
58852a38012Sejakowatz * $Log $
58952a38012Sejakowatz *
59052a38012Sejakowatz * $Id  $
59152a38012Sejakowatz *
59252a38012Sejakowatz */
59352a38012Sejakowatz
594a6b33ea3STyler Dauwalder
595