Sunday 24 April 2016

Bulk task in one script using selenium (python) : Web automation


Hey I want create crash web hook for my 100 apps hosted on hockeyApp and I'm programmer so I cannot do it manually fill that form 100 times the same data for different apps, no public API provided by hockeyApp, so I wrote some python code using selenium tool for this task and voila, I feel I'm programmer.

Here is the codes 

iOS / iPhone Network ETag / HTTP 304 support

Your server team now enabled the ETag (server no change response HTTP 304) then how could you identify that this is going fine with your app, now NSURLConnection can do this, but not implicit. You need to do it by your way.
Nothing to worry just one line of code will do it.

NSMutableURLRequest *request = ...
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;

A very good article about NSURLCache.

Don't forget to increase the cache size by

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
                                                       diskCapacity:20 * 1024 * 1024
                                                           diskPath:nil];
  [NSURLCache setSharedURLCache:URLCache];
}


iOS Network compression gzip

Most of the server enabled gzip compression on demand as header value contain Accept-Encoding:gzip. Now your boss ask that prove me that your app send this request as gzip, then you surf some  articles on internet that says NSURLConnection already send this header, don't need to send it externally. But your boss ask prove me that you got the compressed data, and you cannot find a way linear way to prove that, because NSURLConnection always decompress the data. 
Now you want to see that server send the compressed gzip data and NSURLConnection decompress for you. 

Then use below code for all explanations...



- (void)networkGzipTest {
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.google.co.in"]];
    //    [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
    
    CFURLRequestRef requestRef = (__bridge CFURLRequestRef)[request performSelector:@selector(_CFURLRequest)];
    _CFURLRequestSetProtocolProperty(requestRef,kCFURLRequestDoNotDecodeData,kCFBooleanTrue);
    
    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [urlConnection start];
}

Actually these two lines make you happy
CFURLRequestRef requestRef = (__bridge CFURLRequestRef)[request performSelector:@selector(_CFURLRequest)];
_CFURLRequestSetProtocolProperty(requestRef,kCFURLRequestDoNotDecodeData,kCFBooleanTrue);

Now you can print the size of your data without using above two line and with two lines. And all explanation you have now. [Read more]
Happy coding....Voila!

Disable ATS in iOS 9

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

192.168.1.2.xio.io for running ATS and request transfer host to 192.168.1.2. http://xip.io/

Python color terminal output

python terminal color print 
def prRed(prt): print("\033[91m {}\033[00m" .format(prt))
def prGreen(prt): print("\033[92m {}\033[00m" .format(prt))
def prYellow(prt): print("\033[93m {}\033[00m" .format(prt))
def prLightPurple(prt): print("\033[94m {}\033[00m" .format(prt))
def prPurple(prt): print("\033[95m {}\033[00m" .format(prt))
def prCyan(prt): print("\033[96m {}\033[00m" .format(prt))
def prLightGray(prt): print("\033[97m {}\033[00m" .format(prt))
def prBlack(prt): print("\033[98m {}\033[00m" .format(prt))

Add actual debug mode in iOS insist of DEBUG macro

You need to check that your app in debug mode or release mode and you edit the scheme, where DEBUG macro will defined by Xcode. But what happen if you change to release mode that time, it is hard to find that app build given by Xcode or archived. Also there is problem with preprocessor macro, if you are writing framework or dynamic lib for iOS, you cannot use preprocessors.

So find a scenario which mean that app is running by the Xcode build (debug) or archived (release), below method would help you. This method is popularly used by hockey app and most famous analytics. [Taken from Hockeyapp SDK]

#include <sys/sysctl.h>
- (BOOL)debug {
    static BOOL debuggerIsAttached = NO;
    static dispatch_once_t debuggerPredicate;
    dispatch_once(&debuggerPredicate, ^{
        struct kinfo_proc info;
        size_t info_size = sizeof(info);
        int name[4];
        
        name[0] = CTL_KERN;
        name[1] = KERN_PROC;
        name[2] = KERN_PROC_PID;
        name[3] = getpid();
        
        if (sysctl(name, 4, &info, &info_size, NULL, 0) == -1) {
            NSLog(@"App is running in debug mode");
            debuggerIsAttached = false;
        }
        
        if (!debuggerIsAttached && (info.kp_proc.p_flag & P_TRACED) != 0)
            debuggerIsAttached = true;
    });
    return debuggerIsAttached;
}

add .DS_Store to .gitignore

git ignore add .DS_Store
http://stackoverflow.com/questions/107701/how-can-i-remove-ds-store-files-from-a-git-repository
Remove existing files from the repository:
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
Add the line
.DS_Store
to the file .gitignore, which can be found at the top level of your repository (or created if it isn't there already). Then
git add .gitignore
git commit -m '.DS_Store banished!'

iOS / iPhone network packet tracker

It is hard to find that what packet request send by your iOS device. Most of apps doesn't use https, so the packets are in plane text, also most of the request doesn't use encryption over network so all packets are transmitted to plain text. So track the app network vulnerable, this may help you.
  1. Connect your iOS device by USB
  2. $ rvictl -s <UDID>
    where UDID is the UDID of your device (located in XCode under Devices, shortcut to with 2)
  3. $ sudo launchctl list com.apple.rpmuxd
  4. $ sudo tcpdump -n -t -i rvi0 -q -A tcp