Permalink
Browse files

add test

  • Loading branch information...
sassembla committed Sep 24, 2013
1 parent 89c72fb commit 89c634a58e95a0dd42287490187800f99fd6a7f5
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
B7047AA517F1F61D00B1B752 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B77550DA17F081140029CBB0 /* Security.framework */; };
B775508B17F080460029CBB0 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B775508A17F080460029CBB0 /* Cocoa.framework */; };
B775509517F080460029CBB0 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = B775509317F080460029CBB0 /* InfoPlist.strings */; };
B775509717F080460029CBB0 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = B775509617F080460029CBB0 /* main.m */; };
@@ -44,6 +45,8 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
B7047AA317F1F61200B1B752 /* Accelerate.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Accelerate.framework; path = System/Library/Frameworks/Accelerate.framework; sourceTree = SDKROOT; };
B7047AA617F1F7D000B1B752 /* TestParams.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestParams.h; sourceTree = "<group>"; };
B775508717F080460029CBB0 /* EnteringOrbit.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = EnteringOrbit.app; sourceTree = BUILT_PRODUCTS_DIR; };
B775508A17F080460029CBB0 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
B775508D17F080460029CBB0 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
@@ -93,6 +96,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
B7047AA517F1F61D00B1B752 /* Security.framework in Frameworks */,
B77550AB17F080460029CBB0 /* Cocoa.framework in Frameworks */,
B77550AA17F080460029CBB0 /* XCTest.framework in Frameworks */,
);
@@ -125,6 +129,7 @@
B775508917F080460029CBB0 /* Frameworks */ = {
isa = PBXGroup;
children = (
B7047AA317F1F61200B1B752 /* Accelerate.framework */,
B77550DA17F081140029CBB0 /* Security.framework */,
B775508A17F080460029CBB0 /* Cocoa.framework */,
B77550A917F080460029CBB0 /* XCTest.framework */,
@@ -174,6 +179,7 @@
children = (
B77550B417F080460029CBB0 /* EnteringOrbitTests.m */,
B77550AF17F080460029CBB0 /* Supporting Files */,
B7047AA617F1F7D000B1B752 /* TestParams.h */,
);
path = EnteringOrbitTests;
sourceTree = "<group>";
@@ -534,6 +540,7 @@
B77550BA17F080460029CBB0 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
B77550BB17F080460029CBB0 /* Build configuration list for PBXNativeTarget "EnteringOrbitTests" */ = {
isa = XCConfigurationList;
@@ -542,6 +549,7 @@
B77550BD17F080460029CBB0 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
@@ -8,6 +8,13 @@
#import <Cocoa/Cocoa.h>
#define KEY_TAILTARGET (@"-t")
#define KEY_CONNECTTARGET (@"-c")
#define KEY_DEBUG (@"--d")
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@@ -11,57 +11,47 @@
#import "KSMessenger.h"
#import "WebSocketClientController.h"
#define KEY_LIMITSEC (@"-l")
#define KEY_TAILTARGET (@"-t")
#define KEY_CONNECTTARGET (@"-c")
#define COMMAND_TAIL (@"/usr/bin/tail")
@implementation AppDelegate {
NSDictionary * paramDict;
NSString * m_connectTarget;
NSString * m_tailTarget;
WebSocketClientController * wsCont;
}
- (id) initAppDelegateWithParam:(NSDictionary * )dict {
if (self = [super init]) {
// paramDict = [[NSDictionary alloc]initWithDictionary:dict];
paramDict = @{KEY_CONNECTTARGET:@"mondogrosso@mondogrosso.201104392.members.btmm.icloud.com",
KEY_TAILTARGET:@"./Desktop/testLog.txt"};
if (dict[@"-NSDocumentRevisionsDebugMode"]) {
NSLog(@"debug run -> exit");
exit(0);
}
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSFileManager * fileManager = [NSFileManager defaultManager];
NSError * error;
NSString * tempLogFilePath = [[NSString alloc]initWithFormat:@"%@/%@", [[NSBundle mainBundle] bundlePath], @"temp.txt"];
// if exist, delete
NSFileHandle * readHandle = [NSFileHandle fileHandleForReadingAtPath:tempLogFilePath];
if (readHandle) {// delete
[fileManager removeItemAtPath:tempLogFilePath error:&error];
if (dict[@"-XCTest"]) {
return nil;
}
NSAssert1(dict[KEY_CONNECTTARGET], @"connection target required. %@ targetUserName@targetMachineName", KEY_CONNECTTARGET);
m_connectTarget = paramDict[KEY_CONNECTTARGET];
// create temporary output
bool makeTempFile = [fileManager createFileAtPath:tempLogFilePath contents:nil attributes:nil];
NSAssert1(makeTempFile, @"failed to make tempfile @ %@, please chech chmod", tempLogFilePath);
NSFileHandle * outhand = [NSFileHandle fileHandleForWritingAtPath:tempLogFilePath];
NSAssert(outhand, @"nil");
NSAssert1(dict[KEY_TAILTARGET], @"tail target required. %@ ./something.txt", KEY_TAILTARGET);
m_tailTarget = paramDict[KEY_TAILTARGET];
if (self = [super init]) {
paramDict = [[NSDictionary alloc]initWithDictionary:dict];
}
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
/**
NSTaskでsshで繋いでlogがあるところまで移動してログの内容をtailしてWebSocketで出力する。
プロセスの開始時にWebSocketクライアントになる。
*/
NSString * expect = @"/usr/bin/expect";
// http://www.math.kobe-u.ac.jp/~kodama/tips-expect.html
@@ -72,38 +62,37 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// send \"tail -f ./Desktop/130715_2_テロップ.txt\n\";
// interact;"
NSString * cHead = @"-c";
int timeLimitSec = 30;
if (paramDict[KEY_LIMITSEC]) timeLimitSec = [paramDict[KEY_LIMITSEC] intValue];
NSString * timeLimitPhrase = [[NSString alloc] initWithFormat:@"set timeout %d;", timeLimitSec];
// connect
NSString * connectTarget = [[NSString alloc]initWithFormat:@"spawn ssh %@;", m_connectTarget];
NSAssert1(paramDict[KEY_CONNECTTARGET], @"connection target required. %@ targetUserName@targetMachineName", KEY_CONNECTTARGET);
NSString * connectTarget = [[NSString alloc]initWithFormat:@"spawn ssh %@;" ,paramDict[KEY_CONNECTTARGET]];
// echo
CFUUIDRef uuidObj = CFUUIDCreate(nil);
NSString * uuidString = (NSString * )CFBridgingRelease(CFUUIDCreateString(nil, uuidObj));
CFRelease(uuidObj);
NSString * echoPhrase = [[NSString alloc]initWithFormat:@"send \"echo %@\n\";", uuidString];
NSAssert1(paramDict[KEY_TAILTARGET], @"tail target required. %@ ./something.txt", KEY_TAILTARGET);
NSString * tailTarget = paramDict[KEY_TAILTARGET];
NSString * tailPhrase = [[NSString alloc]initWithFormat:@"send \"tail -f %@\n\";", tailTarget];
// tail
NSString * tailPhrase = [[NSString alloc]initWithFormat:@"send \"tail -f %@\n\";", m_tailTarget];
/*
combine lines
*/
NSArray * expectParamArray = @[timeLimitPhrase,
connectTarget,
NSArray * expectParamArray = @[connectTarget,
echoPhrase,
tailPhrase,
@"interact;"];
NSArray * paramArray = @[cHead, [expectParamArray componentsJoinedByString:@"\n"]];
// tail task
NSTask * tail = [[NSTask alloc]init];
[tail setLaunchPath:COMMAND_TAIL];
[tail setArguments:@[@"-f", tempLogFilePath]];
[tail launch];
// ssh task
NSTask * ssh = [[NSTask alloc]init];
@@ -117,38 +106,41 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[ssh launch];
// read & publish
NSFileHandle * publishHandle = [readPipe fileHandleForReading];
FILE * fp = fdopen([publishHandle fileDescriptor], "r");
// stopper
int ready = -2;
// start publish after echo +1
char buffer[BUFSIZ];
while(fgets(buffer, BUFSIZ, fp)) {
NSString * message = [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
[self send:message];
if (ready == 0) {
[self send:message];
} else if ([message hasPrefix:uuidString]) {
ready = -1;
} else if (ready == -1) {
ready = 0;
}
}
NSLog(@"over");
}
/**
特定のkey位置以降に開始されるsend
*/
- (void) send:(NSString * )input {
NSLog(@"読めるのかしら %@", input);
}
- (void) update:(NSNotification * )update {
if ([update name] == NSApplicationWillUpdateNotification) return;
if ([update name] == NSTextInputContextKeyboardSelectionDidChangeNotification) return;
if ([update name] == NSApplicationDidUpdateNotification) return;
if ([update name] == NSApplicationDidBecomeActiveNotification) return;
if ([update name] == NSApplicationWillBecomeActiveNotification) return;
if ([update name] == NSApplicationDidResignActiveNotification) return;
if ([update name] == NSApplicationWillResignActiveNotification) return;
NSLog(@"update %@", update);//もしかしたらキャッチできるかもしれない。
}
@end
@@ -7,28 +7,38 @@
//
#import <XCTest/XCTest.h>
#import "AppDelegate.h"
#import "TestParams.h"
@interface EnteringOrbitTests : XCTestCase
@end
@implementation EnteringOrbitTests
@implementation EnteringOrbitTests {
AppDelegate * delegate;
}
- (void)setUp
{
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown
{
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample
{
XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__);
- (void)testRunWithValidParam {
NSDictionary * paramDict = @{
KEY_CONNECTTARGET:TEST_CONNECT_TARGET,
KEY_TAILTARGET:TEST_TAIL_TARGET};
delegate = [[AppDelegate alloc] initAppDelegateWithParam:paramDict];
}
@end
@@ -0,0 +1,14 @@
//
// TestParams.h
// EnteringOrbit
//
// Created by sassembla on 2013/09/25.
// Copyright (c) 2013年 sassembla. All rights reserved.
//
#ifndef EnteringOrbit_TestParams_h
#define EnteringOrbit_TestParams_h
#endif

0 comments on commit 89c634a

Please sign in to comment.