Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
MoreOpenTransport/OTClassicContextTest/OTClassicContextTest.c
/* |
File: OTClassicContextTest.c |
Contains: Code for testing OTClassicContext. |
Written by: Quinn |
Copyright: Copyright © 2000 by Apple Computer, Inc., All Rights Reserved. |
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. |
("Apple") in consideration of your agreement to the following terms, and your |
use, installation, modification or redistribution of this Apple software |
constitutes acceptance of these terms. If you do not agree with these terms, |
please do not use, install, modify or redistribute this Apple software. |
In consideration of your agreement to abide by the following terms, and subject |
to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs |
copyrights in this original Apple software (the "Apple Software"), to use, |
reproduce, modify and redistribute the Apple Software, with or without |
modifications, in source and/or binary forms; provided that if you redistribute |
the Apple Software in its entirety and without modifications, you must retain |
this notice and the following text and disclaimers in all such redistributions of |
the Apple Software. Neither the name, trademarks, service marks or logos of |
Apple Computer, Inc. may be used to endorse or promote products derived from the |
Apple Software without specific prior written permission from Apple. Except as |
expressly stated in this notice, no other rights or licenses, express or implied, |
are granted by Apple herein, including but not limited to any patent rights that |
may be infringed by your derivative works or by other works in which the Apple |
Software may be incorporated. |
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO |
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED |
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN |
COMBINATION WITH YOUR PRODUCTS. |
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION |
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT |
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN |
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Change History (most recent first): |
<2> 13/12/00 Quinn Now that InitOpenTranportInContext is fixed to accept nil, pass |
nil for the outClientContext parameter. |
<1> 12/10/00 Quinn First checked in. |
*/ |
///////////////////////////////////////////////////////////////// |
// MoreIsBetter Setup |
#include "MoreSetup.h" |
// Mac OS Interfaces |
#include <Events.h> |
// Standard C interfaces |
#include <stdio.h> |
// Our prototypes |
#include "OTClassicContext.h" |
///////////////////////////////////////////////////////////////// |
// The following routines are sufficiently obscure that I didn't bother |
// testing them. |
// |
// OTStreamPipeInContext |
// OTOpenProviderOnStreamInContext |
// OTOpenEndpointOnStreamInContext |
static OSStatus TransferTest(void) |
{ |
OSStatus err; |
OSStatus junk; |
EndpointRef ep; |
EndpointRef ep2; |
OTClient me; |
ep2 = nil; |
ep = OTOpenEndpointInContext(OTCreateConfiguration("tcp"), 0, nil, &err, nil); |
if (err == noErr) { |
me = OTWhoAmIInContext(nil); |
ep2 = OTTransferProviderOwnershipInContext(ep, me, &err, nil); |
if (err == noErr) { |
ep = nil; |
} |
} |
if (ep != nil) { |
junk = OTCloseProvider(ep); |
MoreAssertQ(junk == noErr); |
} |
if (ep2 != nil) { |
junk = OTCloseProvider(ep2); |
MoreAssertQ(junk == noErr); |
} |
return err; |
} |
static OSStatus MemTest(void) |
{ |
OSStatus err; |
OSStatus junk; |
EndpointRef ep; |
void *p1; |
void *p2; |
p1 = nil; |
p2 = nil; |
ep = OTOpenEndpointInContext(OTCreateConfiguration("tcp"), 0, nil, &err, nil); |
if (err == noErr) { |
p1 = OTAllocInContext(ep, T_BIND, T_ALL, &err, nil); |
} |
if (err == noErr) { |
p2 = OTAllocMemInContext(100, nil); |
} |
if (p2 != nil) { |
OTFreeMem(p2); |
} |
if (p1 != nil) { |
junk = OTFree(p1, T_BIND); |
MoreAssertQ(junk == noErr); |
} |
if (ep != nil) { |
junk = OTCloseProvider(ep); |
MoreAssertQ(junk == noErr); |
} |
return err; |
} |
static pascal void DummyNotifier(void *contextPtr, OTEventCode code, OTResult result, void *cookie) |
{ |
#pragma unused(result) |
if (code == T_OPENCOMPLETE) { |
if (cookie == nil) { |
cookie = (void *) -1; |
} |
*((ProviderRef *) contextPtr) = (ProviderRef) cookie; |
} else if (code == kStreamOpenEvent) { |
if (cookie == nil) { |
cookie = (void *) -1; |
} |
*((StreamRef *) contextPtr) = (StreamRef) cookie; |
} |
} |
static OSStatus OpenTest(void) |
{ |
OSStatus err; |
OSStatus junk; |
EndpointRef ep1; |
EndpointRef ep2; |
MapperRef map1; |
MapperRef map2; |
ATSvcRef at1; |
ATSvcRef at2; |
InetSvcRef inet1; |
InetSvcRef inet2; |
ProviderRef prov1; |
ProviderRef prov2; |
ep1 = nil; |
ep2 = nil; |
map1 = nil; |
map2 = nil; |
at1 = nil; |
at2 = nil; |
inet1 = nil; |
inet2 = nil; |
prov1 = nil; |
prov2 = nil; |
ep1 = OTOpenEndpointInContext(OTCreateConfiguration("tcp"), 0, nil, &err, nil); |
if (err == noErr) { |
err = OTAsyncOpenEndpointInContext(OTCreateConfiguration("tcp"), 0, nil, DummyNotifier, &ep2, nil); |
if (err == noErr) { |
while (ep2 == nil) { |
SystemTask(); |
} |
if (ep2 == (void *) -1) { |
err = -1; |
} |
} |
} |
if (err == noErr) { |
map1 = OTOpenMapperInContext(OTCreateConfiguration("nbp"), 0, &err, nil); |
} |
if (err == noErr) { |
err = OTAsyncOpenMapperInContext(OTCreateConfiguration("nbp"), 0, DummyNotifier, &map2, nil); |
if (err == noErr) { |
while (map2 == nil) { |
SystemTask(); |
} |
if (map2 == (void *) -1) { |
err = -1; |
} |
} |
} |
if (err == noErr) { |
inet1 = OTOpenInternetServicesInContext(kDefaultInternetServicesPath, 0, &err, nil); |
} |
if (err == noErr) { |
err = OTAsyncOpenInternetServicesInContext(kDefaultInternetServicesPath, 0, DummyNotifier, &inet2, nil); |
if (err == noErr) { |
while (inet2 == nil) { |
SystemTask(); |
} |
if (inet2 == (void *) -1) { |
err = -1; |
} |
} |
} |
if (err == noErr) { |
at1 = OTOpenAppleTalkServicesInContext(kDefaultAppleTalkServicesPath, 0, &err, nil); |
} |
if (err == noErr) { |
err = OTAsyncOpenAppleTalkServicesInContext(kDefaultAppleTalkServicesPath, 0, DummyNotifier, &at2, nil); |
if (err == noErr) { |
while (at2 == nil) { |
SystemTask(); |
} |
if (at2 == (void *) -1) { |
err = -1; |
} |
} |
} |
if (err == noErr) { |
prov1 = OTOpenProviderInContext(OTCreateConfiguration("enet"), 0, &err, nil); |
} |
if (err == noErr) { |
err = OTAsyncOpenProviderInContext(OTCreateConfiguration("enet"), 0, DummyNotifier, &prov2, nil); |
if (err == noErr) { |
while (prov2 == nil) { |
SystemTask(); |
} |
if (prov2 == (void *) -1) { |
err = -1; |
} |
} |
} |
if (ep1 != nil) { |
junk = OTCloseProvider(ep1); |
MoreAssertQ(junk == noErr); |
} |
if (ep2 != nil) { |
junk = OTCloseProvider(ep2); |
MoreAssertQ(junk == noErr); |
} |
if (map1 != nil) { |
junk = OTCloseProvider(map1); |
MoreAssertQ(junk == noErr); |
} |
if (map2 != nil) { |
junk = OTCloseProvider(map2); |
MoreAssertQ(junk == noErr); |
} |
if (at1 != nil) { |
junk = OTCloseProvider(at1); |
MoreAssertQ(junk == noErr); |
} |
if (at2 != nil) { |
junk = OTCloseProvider(at2); |
MoreAssertQ(junk == noErr); |
} |
if (inet1 != nil) { |
junk = OTCloseProvider(inet1); |
MoreAssertQ(junk == noErr); |
} |
if (inet2 != nil) { |
junk = OTCloseProvider(inet2); |
MoreAssertQ(junk == noErr); |
} |
if (prov1 != nil) { |
junk = OTCloseProvider(prov1); |
MoreAssertQ(junk == noErr); |
} |
if (prov2 != nil) { |
junk = OTCloseProvider(prov2); |
MoreAssertQ(junk == noErr); |
} |
return err; |
} |
static pascal void DummyProc(void *arg) |
{ |
#pragma unused(arg) |
} |
static OSStatus TaskTest(void) |
{ |
OSStatus err; |
OSStatus junk; |
OTDeferredTaskRef dt; |
OTTimerTask tt; |
OTSystemTaskRef st; |
dt = 0; |
tt = 0; |
st = 0; |
err = noErr; |
dt = OTCreateDeferredTaskInContext(DummyProc, nil, nil); |
if (dt == 0) { |
err = -1; |
} |
if (err == noErr) { |
tt = OTCreateTimerTaskInContext(DummyProc, nil, nil); |
} |
if (tt == 0) { |
err = -2; |
} |
if (err == noErr) { |
st = OTCreateSystemTaskInContext(DummyProc, nil, nil); |
} |
if (st == 0) { |
err = -3; |
} |
if (dt != 0) { |
junk = OTDestroyDeferredTask(dt); |
MoreAssertQ(junk == noErr); |
} |
if (tt != 0) { |
OTDestroyTimerTask(tt); |
} |
if (st != 0) { |
junk = OTDestroySystemTask(st); |
MoreAssertQ(junk == noErr); |
} |
return err; |
} |
static OSStatus StreamTest(void) |
{ |
OSStatus err; |
OSStatus junk; |
StreamRef strm1; |
StreamRef strm2; |
StreamRef strm3; |
StreamRef strm4; |
strm1 = nil; |
strm2 = nil; |
strm3 = nil; |
strm4 = nil; |
err = noErr; |
if (err == noErr) { |
strm1 = OTStreamOpenInContext("sad", 0, &err, nil); |
} |
if (err == noErr) { |
err = OTAsyncStreamOpenInContext("sad", 0, DummyNotifier, &strm2, nil); |
if (err == noErr) { |
while (strm2 == nil) { |
SystemTask(); |
} |
if (strm2 == (void *) -1) { |
err = -1; |
} |
} |
} |
if (err == noErr) { |
strm3 = OTCreateStreamInContext(OTCreateConfiguration("tcp"), 0, &err, nil); |
} |
if (err == noErr) { |
err = OTAsyncCreateStreamInContext(OTCreateConfiguration("tcp"), 0, DummyNotifier, &strm4, nil); |
if (err == noErr) { |
while (strm4 == nil) { |
SystemTask(); |
} |
if (strm4 == (void *) -1) { |
err = -1; |
} |
} |
} |
if (strm1 != nil) { |
junk = OTStreamClose(strm1); |
MoreAssertQ(junk == noErr); |
} |
if (strm2 != nil) { |
junk = OTStreamClose(strm2); |
MoreAssertQ(junk == noErr); |
} |
if (strm3 != nil) { |
junk = OTStreamClose(strm3); |
MoreAssertQ(junk == noErr); |
} |
if (strm4 != nil) { |
junk = OTStreamClose(strm4); |
MoreAssertQ(junk == noErr); |
} |
return err; |
} |
extern void main(void) |
{ |
OSStatus err; |
OSStatus junk; |
err = InitOpenTransportInContext(kInitOTForApplicationMask, nil); |
if (err == noErr) { |
err = OTRegisterAsClientInContext("\pOTClassicContextTest", DummyNotifier, nil); |
if (err == noErr) { |
err = TransferTest(); |
if (err == noErr) { |
err = MemTest(); |
} |
if (err == noErr) { |
err = OpenTest(); |
} |
if (err == noErr) { |
err = TaskTest(); |
} |
if (err == noErr) { |
err = StreamTest(); |
} |
junk = OTUnregisterAsClientInContext(nil); |
MoreAssertQ(junk == noErr); |
} |
CloseOpenTransportInContext(nil); |
} |
if (err == noErr) { |
printf("Success.\n"); |
} else { |
printf("Failed with error %ld.\n", err); |
} |
printf("Done. Press command-Q to Quit.\n"); |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-07-22