2 // WOTestBundleInjector.m
5 // Created by Wincent Colaiuta on 06 August 2006.
6 // Copyright 2006-2007 Wincent Colaiuta.
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #import "WOTestBundleInjector.h"
25 #import <libkern/OSAtomic.h> /* OSAtomicIncrement32Barrier() */
27 @implementation WOTestBundleInjector
32 static int32_t initialized = 0;
33 if (OSAtomicIncrement32Barrier(&initialized) != 1)
36 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
37 char *inject = getenv("WOTestInjectBundle");
40 NSArray *bundles = [[NSString stringWithUTF8String:inject] componentsSeparatedByString:@":"];
41 for (NSString *bundlePath in bundles)
43 NSString *path = [bundlePath stringByStandardizingPath];
44 if (![path isAbsolutePath])
46 NSLog(@"WOTestBundleInjector: skipping bundle \"%@\" (absolute path required)", bundlePath);
49 NSBundle *bundle = [NSBundle bundleWithPath:path];
51 NSLog(@"WOTestBundleInjector: unable to get bundle for path \"%@\"", bundlePath);
52 else if ([bundle isLoaded])
53 NSLog(@"WOTestBundleInjector: skipping bundle \"%@\" (already loaded)", bundlePath);
54 else if ([bundle load])
55 NSLog(@"WOTestBundleInjector: bundle \"%@\" loaded", bundlePath);
57 // Note that you can't "load" application bundles this way, trying will wind up here
58 // not even with low-level functions like NSCreateObjectFileImageFromFile() and NSCreateObjectFileImageFromMemory()
59 // the docs for those functions say "you must build the Mach-O executable file using the -bundle linker option"
60 // dlopen() successfully loads application executables but the symbols aren't visible even with the RTLD_NOW flag
61 // CFBundle probably has the same limitations as well, altough haven't tested this
62 NSLog(@"WOTestBundleInjector: failed to load bundle \"%@\"", bundlePath);