Monday, November 12, 2012

Install ipa to iOS from commandline

Install ipa to iOS from commandline


using following command:
./transporter_chief.rb ./kulerios.ipa


file can be found at :https://docs.google.com/open?id=0Byq0T4vvPHLHNDJhcmtUcmcxbGM


Wednesday, October 17, 2012

JNI learning: Basic


1, Java app code :

package test.sample.jni.run;

public class Sample {

static int staticint = 0;
String strField="default";
int intField=0;
public native int intMethod(int n);
public native boolean booleanMethod(boolean bool);
public native String stringMethod(String text);
public native int intArrayMethod(int[] intArray);
public native boolean callJavaMethod(String text);
public String callmethod(String c,int  a){

String s = "intput value is "+String.valueOf(a)+":"+c;
System.out.println(s);
return s;
}
public void perf()
{
System.out.println(this.intMethod(5));
boolean bool = this.booleanMethod(true);
this.callJavaMethod("abc");
for(int i=0 ; i<10000;i++)
//System.out.println(this.stringMethod("Java"));
this.callJavaMethod("abc");
int sum = this.intArrayMethod(new int[]{1,2,3,4});
System.out.println(this.strField+":"+this.intField);
}
/**
* @param args
*/
public static void main(String[] args) {
System.loadLibrary("sample");
for(int j= 0 ; j < 10; j++){
new Sample().perf();
}
}
}

2. Native library header file

javah test.sample.jni.run.Sample at bin folder. there will be following .h generated:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class test_sample_jni_run_Sample */

#ifndef _Included_test_sample_jni_run_Sample
#define _Included_test_sample_jni_run_Sample
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     test_sample_jni_run_Sample
 * Method:    intMethod
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_test_sample_jni_run_Sample_intMethod
  (JNIEnv *, jobject, jint);

/*
 * Class:     test_sample_jni_run_Sample
 * Method:    booleanMethod
 * Signature: (Z)Z
 */
JNIEXPORT jboolean JNICALL Java_test_sample_jni_run_Sample_booleanMethod
  (JNIEnv *, jobject, jboolean);

/*
 * Class:     test_sample_jni_run_Sample
 * Method:    stringMethod
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_test_sample_jni_run_Sample_stringMethod
  (JNIEnv *, jobject, jstring);

/*
 * Class:     test_sample_jni_run_Sample
 * Method:    intArrayMethod
 * Signature: ([I)I
 */
JNIEXPORT jint JNICALL Java_test_sample_jni_run_Sample_intArrayMethod
  (JNIEnv *, jobject, jintArray);

/*
 * Class:     test_sample_jni_run_Sample
 * Method:    callJavaMethod
 * Signature: (Ljava/lang/String;)Z
 */
JNIEXPORT jboolean JNICALL Java_test_sample_jni_run_Sample_callJavaMethod
  (JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif

3. Native library 

create one dll project and implement the header file with following c++ file:
//#include "stdafx.h"
#include "test_sample_jni_run_Sample.h"
#include <string.h>

JNIEXPORT jint JNICALL Java_test_sample_jni_run_Sample_intMethod
  (JNIEnv *env, jobject ojb, jint num){
 
 jclass cls = env->GetObjectClass(ojb);
 jfieldID intfid = env->GetFieldID(cls,"intField","I");
 if(intfid)
 {
 jint intvalue = (jint)env->GetObjectField(ojb,intfid);
 printf("intfield value is %d \n", intvalue);
 env->SetIntField(ojb,intfid,100);
 }
 return num * num;
  }
JNIEXPORT jboolean JNICALL Java_test_sample_jni_run_Sample_booleanMethod
  (JNIEnv *env, jobject ojb, jboolean boolean){
    return !boolean;
  }

JNIEXPORT jboolean JNICALL Java_test_sample_jni_run_Sample_callJavaMethod
  (JNIEnv *env, jobject ojb, jstring string){

   jclass cls = env->GetObjectClass(ojb);
 
 jfieldID strfid = env->GetFieldID(cls,"strField","Ljava/lang/String;");
 //call java method
 jmethodID mid = env->GetMethodID(cls,"callmethod","(Ljava/lang/String;I)Ljava/lang/String;");
 if (mid)
 {
 jstring inputS = env->NewStringUTF("from nativefrom nativefrom nativefrom nativefrom nativefrom nativefrom nativefrom native");  
 jint inputV = 1;
 jstring  strvalue = (jstring )env->CallObjectMethod(ojb,mid,inputS,inputV);
 const char *str;
 str = env->GetStringUTFChars(strvalue, 0);
 printf("call java method return value is %s \n", str);
 env->DeleteLocalRef(inputS);
 }
 return true;
}
JNIEXPORT jstring JNICALL Java_test_sample_jni_run_Sample_stringMethod
  (JNIEnv *env, jobject ojb, jstring string){
 
 jclass cls = env->GetObjectClass(ojb);
 
 jfieldID strfid = env->GetFieldID(cls,"strField","Ljava/lang/String;");
 //call java feild
 if(strfid)
 {
 jstring  strvalue = (jstring )env->GetObjectField(ojb,strfid);
 const char *str;
 str = env->GetStringUTFChars(strvalue, 0);
 printf("strField value is %s \n", str);
 strvalue = env->NewStringUTF("from nativefrom nativefrom nativefrom nativefrom nativefrom nativefrom nativefrom native");  
 env->SetObjectField(ojb,strfid,(jobject)strvalue);
 env->DeleteLocalRef(strvalue);
 }

 //call java method
 jmethodID mid = env->GetMethodID(cls,"callmethod","(Ljava/lang/String;I)Ljava/lang/String;");
 if (mid)
 {
 jstring inputS = env->NewStringUTF("from nativefrom nativefrom nativefrom nativefrom nativefrom nativefrom nativefrom native");  
 jint inputV = 1;
 jstring  strvalue = (jstring )env->CallObjectMethod(ojb,mid,inputS,inputV);
 const char *str;
 str = env->GetStringUTFChars(strvalue, 0);
 printf("call java method return value is %s \n", str);
 env->DeleteLocalRef(inputS);
 }


 //return str;

 const char *str = env->GetStringUTFChars(string, 0);
 char cap[128];
 strcpy(cap, str);
 env->ReleaseStringUTFChars(string, str);
 return env->NewStringUTF(strupr(cap));
  }
  
JNIEXPORT jint JNICALL Java_test_sample_jni_run_Sample_intArrayMethod
  (JNIEnv *env, jobject obj, jintArray array) {
    int i, sum = 0;
    jsize len = env->GetArrayLength(array);
    jint *body = env->GetIntArrayElements(array, 0);
    for (i=0; i<len; i++)
    {
      sum += body[i];
    }
    env->ReleaseIntArrayElements(array, body, 0);;// avoid mem leaks
    return sum;
  }
  void main(){}

4. Java call native lib

place the native lib to the class path at Java application run time. and explicitly call system load like this: System.loadLibrary("sample"); //don't add extension.

5.Native Application call Java class

Project setting:
1. link following lib :
C:\Program Files (x86)\Java\jdk1.6.0_11\lib\jvm.lib
2. include following head file:
C:\Program Files (x86)\Java\jdk1.6.0_11\include;C:\Program Files (x86)\Java\jdk1.6.0_11\include\win32
3. add jvm.dll to dll search path by modify environment path :
C:\Program Files (x86)\Java\jdk1.6.0_11\jre\bin\client\jvm.dll


In the following source code, C++ create jvm and call the specified the java class:

#include <jni.h>
#include <string.h>
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif

static jmethodID gmid;

void checkGloabalMethod()
{
}
int main()
{

JavaVMOption options [1];
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
long status;
jclass cls;
jmethodID mid;
jint square;
jboolean not;

options[0].optionString = "-Djava.class.path=.;C:/Users/jiali/Documents/Visual Studio 2010/Projects/sample/Debug";
memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
status = JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);

if (status != JNI_ERR)
{
cls = env->FindClass("CalledbyNative");
if (cls)
{
jfieldID a = env->GetStaticFieldID(cls,"a","I");
if(a)
{
jint avalue = env->GetStaticIntField(cls,a);
printf("field a  value is %d \n",avalue);
}
mid = env->GetStaticMethodID(cls,"intMethod","(I)I");
gmid = (jmethodID)env->NewGlobalRef((jobject)mid);
if (mid)
{
square = env->CallStaticIntMethod(cls,mid,5);
printf("square is %d \n",square);
}
mid = env->GetStaticMethodID(cls, "booleanMethod","(Z)Z");
if(mid)
{
not = env->CallStaticBooleanMethod(cls,mid,1);
printf("Result of booleanmethod is %d \n",not);
}
}
status = JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);
square = env->CallStaticIntMethod(cls,gmid,6);
printf("square is %d \n",square);
jvm->DestroyJavaVM();
return 0;
}
return -1;
}





Tuesday, October 9, 2012

Tips OSGI and Decompile swf


OSGI:
1,install bundle:
install file:c:\net.adamsoftware.drive_4.0.0.jar
start bundleid

There is one tool called 'swfdump' at flex sdk folder.this tool can be used to show the swf info.

Friday, August 17, 2012

How to import certification to java cacert

1. clicking on the certificate error in your browser to view the certificate. and export the certification  as 1.cer.

2) Import to java cacert:

 keytool -import -trustcacerts -file C:\Users\jiali\Desktop\1\1.cer -keystore
"C:\ProgramData\Adobe\CS5\jre\lib\security\cacerts"
3. Add to Trusted Root Certification Authorities in windows MMC:
To manage trusted root certificates for a local computer
  1. Click Start, click Start Search, type mmc, and then press ENTER.
  2. On the File menu, click Add/Remove Snap-in.
  3. Under Available snap-ins, click Local Group Policy Object Editor,click Add, select the computer whose local Group Policy object (GPO) you want to edit, and then click Finish.
  4. If you have no more snap-ins to add to the console, click OK.
  5. In the console tree, go to Local Computer PolicyComputer ConfigurationWindows SettingsSecurity Settings, and then click Public Key Policies.
  6. Double-click Certificate Path Validation Settings,and thenclick the Stores tab.
  7. Select the Define these policy settings check box.
  8. Under Per user certificate stores, clear the Allow user trusted root CAs to be used to validate certificates and Allow users to trust peer trust certificates check boxes.
  9. Under Root certificate stores, select the root CAs that the client computers can trust, and then click OK to apply the new settings.
    http://technet.microsoft.com/en-us/library/cc754841.aspx
    https://www.globalsign.com/support/intermediate/intermediate_windows.php





After import to java cacert, then CMIS workbench tool can access the SSL server.

Monday, August 13, 2012

Using exec and sed to replace string in files

1. How to unlock files:

find . -exec chflags nouchg {} \;

2. How to replace string in files:

find . */info.xml -type f -exec sed -i '' 's/<\/testcase>/<serverinfo>CQ<\/serverinfo><\/abc>/g' {} \;


3. Rename files in the folder:

find . -name "1.idea*" -exec mv '{}' '{}.idea' \;

Notes: rename all the files start with '1.idea' to append '.idea' at the file name.

Wednesday, August 8, 2012

Check signature and version info on Mac and Windows


Mac:


Following command are very useful: codesign, defauls.


#!/bin/bash
files=(
"/Library/Services/AdobeDrive4.service" "/Volumes/AdobeDrive4-mul/Install.app"
"/Library/Application Support/Adobe/Adobe Drive 4/Adobe Drive.app"
)
echo "-----------------------------------------------------------------codesign -v -ddd -----------------------------------------------------------------"
for file in "${files[@]}"
do
echo "codesign -v -vvv " "$file"
codesign -v -vvv "$file"
done

echo "-----------------------------------------------------------------codesign -d -ddd -----------------------------------------------------------------"

for file in "${files[@]}"
do
echo "codesign -d -vvv " "$file"
codesign -d -vvv "$file"
done

echo "-----------------------------------------------------------------spctl --assess -v -----------------------------------------------------------------"

files=(
"/Volumes/AdobeDrive4-mul/Install.app"
)

for file in "${files[@]}"
do
echo "spctl --assess -v " "$file"
spctl --assess -v "$file"
done

#!/bin/bash

echo "-----------------------------------------------------------------get version -----------------------------------------------------------------"

files=(
"/Library/Application Support/Adobe/Adobe Drive 4/Adobe Drive.app"
)

for file in "${files[@]}"
do
VERSION=`defaults read "$file/Contents/Info" CFBundleShortVersionString`
echo "get file version " "$file" "$VERSION"
done



Win:

Useful commands : powershell -command , and wmic datafile 


import subprocess
import os

ADx86Folder = "C:\\'Program Files (x86)'\\'Common Files'\\Adobe\\'Adobe Drive 4'";
ADx64Folder = "C:\\'Program Files'\\'Common Files'\\Adobe\\'Adobe Drive 4'";
ExpectBuildNumber = "4.0.1.16";


W64BIT = "win64bit";
W32BIT = "win32bit";
Mac    = "osx";

platform = W32BIT;
if os.path.exists("C:\\Program Files (x86)\\"):
    platform = W64BIT;
if os.path.exists("/Volumes/"):
    platform = Mac;

files =[];    
if platform == W32BIT:
    files=["%s\\ADConnect.exe"%(ADx64Folder),
       "%s\\ADFSMenu.dll"%(ADx64Folder),];
    pass
def getSigNatureCommand(filename):
    sCmd = "powershell -command (get-authenticodesignature \"%s\").status -eq 'valid'"%(filename);
    return sCmd;
    pass
def getFileVersionCommand(filename):
    #sCmd = "powershell -command (get-childitem \"%s\").VersionInfo.FileVersion "%(filename);
    sCmd = "wmic datafile where name=\"%s\" get version "%(filename);
    sCmd = sCmd.replace("'","").replace("\\","/").replace("/","\\\\");
    return sCmd;
    pass

def runValidation():
    
    try:
        vResult = False;
        sResult = False;
        result  = True;
        for f in files:
            print f;
            vResult = False;
            callcmd = getFileVersionCommand(f);
            #print callcmd;
            process = subprocess.Popen(callcmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE);
            line = "";
            while process.poll() == None:
                t = process.stdout.readline().strip();
                if t != "":
                    line = t;
                if line == ExpectBuildNumber:
                    vResult = True;
                    print "Version is right,Expect is %s"%(ExpectBuildNumber);
                    break;
                    pass
            if vResult == False:
                print "~~~~~~~~~~~~~~~~~~~~~~:Version is wrong,Expect is %s, Result is %s.~~~~~~~~~~~~~~~~~~~~~~"%(ExpectBuildNumber,line);
                result = False;
                pass
            sResult = False;
            callcmd = getSigNatureCommand(f);
            #print callcmd;
            process = subprocess.Popen(callcmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE);
            [out,error] =process.communicate();
            line = out.strip();
            if line == "True":
                print "Signature is right"
                sResult = True;
                pass
            if sResult == False:
                print "~~~~~~~~~~~~~~~~~~~~~~:Signature is Wrong.~~~~~~~~~~~~~~~~~~~~~~"
                print callcmd;
                print line + str(error);
                result = False;
                pass            
        print "##########################################################";
        if result:
            print "Check result is PASS"
        else:
            print "Check result is Failure"
        print "##########################################################";
    except Exception,e:
        print str(e);
        pass
runValidation();

Thursday, July 26, 2012

Python subprocess.call() and Popen()


import subprocess
#import test
#callcmd = "test.py";
callcmd2 = "AdobePatchInstaller.exe --mode=silent";
callcmd = "full.bat";
callcmdun = "fullun.bat";

def openProcess(callcmd):
   
    try:
        process = subprocess.Popen(callcmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE);
        #print process.communicate();
        print "----------------";
        while process.poll() == None:
            line = process.stdout.readline();
        print "---Exit code is :"+str(process.poll())+"---"
        retcode =  process.poll();
        if retcode == 0:
            print "---install successfully---"
        #print subprocess.check_call(callcmd, shell=True,stderr=subprocess.STDOUT);
    except Exception,e:
        print str(e);
        pass
    #process.stdin.write("send to test from call");
   
    #print "stdoutdata is : "+str(stdoutdata) + ",stderrdata is : "+str(stderrdata);
openProcess(callcmd);

def callProcess(callcmd):
   
    try:
        process = subprocess.call(callcmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE);
        print "---Exit code is :"+str(process)+"---"
        if process== 0:
            print "---install successfully---"

    except Exception,e:
        print str(e);
        pass
    #process.stdin.write("send to test from call");
   
    #print "stdoutdata is : "+str(stdoutdata) + ",stderrdata is : "+str(stderrdata);
callProcess(callcmdun)