Thursday, November 8, 2012

Control the bandwidth with Mac Wifi sharing

During the development and test, you always need to consider the bad connectivity situation. Testing become hard for this situation, now find an easy way, when you share your wifi from mac, you can limit the bandwidth to simulate the bad connection.

Here is the detailed man page from Apple
https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man8/ipfw.8.html

copy someone else's introduction as well. But I prefer follow the Apple guide.



Traffic Shaping in Mac OS X

December 18, 2005 - 12:38am
As a result of a previous question I was informed that Tiger finally has dummynet support in the kernel. What this means to you is that now you can do traffic shaping with no additional software.
Here’s how the idea works: you create several pipes that have a set bandwidth and other properties for all packets that get filed into them; you then add queues to those pipes that determine what priority certain requests will get in that pipe; then you add actual firewall rules to identify packets and file them into queues.
So let’s say you create a pipe with 1Mbit/s of allowed traffic. Within this you would setup three queues: one for high-priority services like incoming mail, DNS requests, and such; one for medium priority services like web and FTP; then one for low-priority services like file sharing and other network “noise”. With this setup you would, on a saturated pipe have a large amount of traffic coming in and going out for web andFTP, giving it priority over the noise, and when someone wanted to talk about mail orDNS those would have a higher chance of getting handled quickly. It’s a good boost in chance, but keep in mind that traffic shaping is not Quality of Service (QoS). Traffic shaping introduces delays in lower-priority packets so that higher-priority packets get in faster, but it offers no advanced logic or guarantees on delivery. If you want QoS, use either a userspace filter that does this or another kernel (like Linux) that has this built-in. For home and moderate server use, shaping is more than acceptable.
Okay, some demonstrations are in order here. Let’s take a typical example, such as the one from the question given, and say that you’re on DSL and your upload is 384k (this is mine) and you want email uploads to only take up 300k of that so you can actually do other things. So, we create a pipe with a 300kbit/s limit for all traffic I send to it:
The hash symbol means you’ll need to be root.
# ipfw pipe 1 config bw 300kbit/s
We have a pipe now. Now we need to setup some queues for priority in this pipe. Because we set the pipe to be lower than the actual upload bandwidth, any traffic that isn’t caught by this will go through on that extra 84kbit/s of bandwidth, so we don’t need a queue for this example as nothing will be sharing this pipe; we only need one rule to catch this traffic:
# ipfw add pipe 1 dst-port smtp
This catches inbound SMTP traffic, too, if you run an SMTP server. But, of course, that’s the point: we don’t want mail to take over.
Now, of course, if you wanted to limit POP/IMAP traffic to the same queue, you could:
# ipfw add pipe 1 dst-port pop3
# ipfw add pipe 1 dst-port pop3s
# ipfw add pipe 1 dst-port imap
# ipfw add pipe 1 dst-port imaps
Now every mail-related transaction will be filed into the limited pipe causing the system to have some leftover upstream bandwidth for browsing.
Of course, within this one could see some problems. Sending a large mail will not affect web browsing, but would affect mail retrieval since they’re in the same pipe, so we need a queue for incoming and one for outgoing so that there’s some control over that share of bandwidth. You could do this with multiple pipes, but then you’re cutting out a swath of bandwidth for incoming that outbound can’t use (presuming you make two 150k pipes).
So, one would do this:
# ipfw pipe 1 config bw 300kbit/s
# ipfw queue 1 config pipe 1 weight 50
# ipfw queue 2 config pipe 1 weight 50
Now we have an equal weight between the two services. You can go from 1 to 100 on the weights if you like, but for this I’d like an even traffic stream on each. When one is finished, the other will get the full pipe. So, we add the rules in:
# ipfw add queue 1 dst-port smtp
# ipfw add queue 2 dst-port pop3
# ipfw add queue 2 dst-port pop3s
# ipfw add queue 2 dst-port imap
# ipfw add queue 2 dst-port imaps
All inbound mail goes through queue 2 and all outbound mail goes through queue 1. Each is given a high chance of getting half the bandwidth allotted to pipe 1.
You can, obviously, have a lot of fun with this, but there’s also some serious fun to be had. Web designers: ever wonder what it’s like to load your site at 56k, or 33, 28, 14, or even 9600? Do this:
# ipfw pipe 1 config bw 56kbit/s
# ipfw add pipe 1 dst-port http
Now browse the web like a modem user. This, obviously, only works for sites served on port 80, but it does work. For the record, MG should be very usable on a modem. Smiling
If you’ve had fun playing, or locked yourself out at some point, just flush the rules:
# ipfw flush
All of this requires Mac OS X 10.4 or a FreeBSD with dummynet support. Anything else will laugh at you and order Yanni CDs from Amazon and send them to all of your friends for Christmas right before erasing the data on your computer in a spectacular flash of light that will blind you and your progeny. Best not to try. Specifically, it’ll either error out or appear to work and drop packets. The Yanni fate might be worse, so don’t risk it.

Tuesday, November 6, 2012

KIF Testing

Spending sometime to setup the KIF testing environment, it is not bad and working well. Just the wiki page's guide has problem, did not discribe clearly. If you follow the Example in their package, it is easier.

OK, past someone else's document


Keep It Functional – iPhone Test Automation

How we implement automated tests for the native iPhone App by Daniel Knott

Currently all the XING Mobile Apps generate more than 20% of the overall XING traffic and this number will likely increase in the near future. In my last devblog post, I dealed with Android Apps and how to implement automated tests with the framework Robotium. This article describes how to implement automated tests using the tool KIF (Keep It Functional).
KIF is an open source test framework developed by the company square. It is an iOS integration test framework that allows you to implement test cases with objective C that can be executed, currently only, against the iPhone/ iPad simulator. The KIF tests are integrated to your Xcode workspace, there is no need for additional servers or services that must be started.
KIF is like a Grey Box Test Tool because you need knowledge of the structure of the App you want to test. KIF use the provided accessibility labels from iOS to interact with the UI elements in the App. The labels are normally used to make Apps available for people with visual disabilities.
With KIF you are able to simulate real user interaction like tap, click, drag, enter text and many more. Also you are able to verify elements on the screen. It is really fast in executing the test cases on the simulator. Also it is really easy to integrate to your xCode and Continuous Integration environment.
Don’t submit your tests to Apple
It is really important that the KIF tests are not part of your production code, otherwise Apple will deny your submission because of undocumented APIs. Why this?
KIF uses these undocumented Apple APIs, to interact with the App, but that is not a problem when it comes to developing the tests. Most of the available iOS test frameworks use these undocumented Apple APIs.
Installation
To install and configure KIF please follow the provided information on the github page. These instructions are always up to date to the latest version of KIF.
Accessibility Inspector
If your project is configured with KIF you have also to activate the “Accessibility Inspector” on the iPhone or iPad simulator to have access to the labels. Do the following steps on the simulator:
  1. Open the Settings
  2. Click on General
  3. Click on Accessibility
  4. And activate the Inspector
Afterwards you see the inspector with a colorful layer within your simulator. You can drag the layer wherever you want. Clicking the small (x) on the left side opens the inspector. If you now do a single tap on an app you get information about the accessibility label and some more information (Screenshot). If you do a double click you can e.g. enter the app. If you close the inspector layer with the (x) again, you have again single click on the simulator.
NOTE: The inspector must be activated to use KIF for testing. Now you can start implementing your test classes.
iPhone Simulator with Accessibility Inspector
Code Example
KIF has three main classes that can be used to implement tests within your project. There is a test runner the KIFTestController, a scenario the KIFTestScenario and a step the KIFTestStep. The KIFTestController is like a test suite where all the test scenarios are listed that should be executed. A KIFTestScenario is like a container that includes several steps that should be done within a test. A step is the smallest and simplest action that represents user interaction like tap, enter text or wait for views.
The following listings show the Controller h-file and m-file. The m-file includes the list of possible scenarios.
XINGTestController.h
#import
#import "KIFTestController.h"
@interface XINGTestController : KIFTestController {}
@end
XINGTestController.m
#import "XINGTestController.h"
@implementation XINGTestController
 
   - (void)initializeScenarios {
      [self addScenario:[KIFTestScenario scenarioLoginWithWrongCredentials]];
      // Add here additional scenarios
   }
@end
Now you need to implement the scenario scenarioLoginWithWrongCredentials. You need again an h-file and an m-file for that. The h-file includes the scenario definitions. The m-file is the class where the test methods are implemented.
KIFTestScenario+XINGLogin.h
#import
#import "KIFTestScenario.h"
 
@interface KIFTestScenario (Login)
 
   + (id) scenarioLoginWithWrongCredentials;
 
@end
KIFTestScenario+Login.m
#import "KIFTestScenario+Login.h"
#import "KIFTestStep.h"
 
@implementation KIFTestScenario (Login)
 
   + (id)scenarioLoginWithWrongCredentials{
      KIFTestScenario *scenario = [KIFTestScenario scenarioWithDescription:@"Wrong credentials"];
 
      // enter wrong credentials in the username and password field
      [scenario addStep:[KIFTestStep stepToEnterText:@"wrongusername" intoViewWithAccessibilityLabel:@"Username"]];
 
      [scenario addStep:[KIFTestStep stepToEnterText:@"wrongpassword" intoViewWithAccessibilityLabel:@"password"]];
 
      // click the done button on the keyboard
      [scenario addStep:[KIFTestStep stepToTapViewWithAccessibilityLabel:@"done"]];
 
      // verify that the error message is shown with a localized string
      [scenario addStep:[KIFTestStep stepToWaitForViewWithAccessibilityLabel:LocalizedString (@"FAILED_MESSAGE")]];
 
      // click the button on the error message
      [scenario addStep:[KIFTestStep stepToTapViewWithAccessibilityLabel:LocalizedString (@"OK_BUTTON")]];
 
      return scenario;
   }
 
@end
Now you have a really simple test that tries to login with wrong credentials against an app. After pressing the done button on the keyboard KIF verifies with the stepToWaitForView… method that the right error message is shown. The following screenshots show the login screen where KIF tries to login with the wrong credentials. Please also have a look at the provided screencast videos at the end of this article. You get a really good impression how KIF is working.
   
Continuous Integration
It is highly recommended to integrate KIF to a CI server like Jenkins to build up a regression testsuite to be sure that the existing functionality is still working after a commit. To integrate and execute your KIF tests from the CI server you must be able to start the simulator from the command line. To do this, you need an additional tool called WaxSim. One Note from the KIF github page: “Note that the Square fork of WaxSim provides a number of bug fixes and some useful additional functionality. Your CI script should resemble something like the this:”
Adapt the following script to your environment and add it to your Jenkins build configuration:
#!/bin/bash
 
killall "iPhone Simulator"
 
set -o errexit
set -o verbose
# Build the "Integration Tests" target to run in the simulator
# xcodebuild -target "Integration Tests" -configuration Release -sdk iphonesimulator build
 
# Run the app we just built in the simulator and send its output to a file
 
# /path/to/XINGApp.app should be the relative or absolute path to the application bundle that was built in the previous step
/path/to/waxsim -f "iphone" "/path/to/XINGApp.app" > /tmp/KIF-$$.out 2>&1
 
# WaxSim hides the return value from the app, so to determine success we search for a "no failures" line
grep -q "TESTING FINISHED: 0 failures" /tmp/KIF-$$.out
Store this script as a shell script within your KIF test folder and call the script from the CI server. The iPhone simulator should start automatically and should execute the tests on the simulator.
That is the way, how we at XING use KIF to automate our iPhone App. Besides all the manual testing we use the automated tests to check, that code changes doesn’t affect to current functionality.
We hope that this post gives you an impression on how to build a test automation suite for your iPhone or iPad apps!
Screencasts
The following screencasts show the functionality of KIF. The first video shows the example code mentioned in this blog post. The second video shows how KIF is starting the XING handshake with some testusers on our testsystem.
Login with wrong credentials
Handshake
The latest XING iPhone App is available here.
Further information can be found in this really good google group about KIF.
This post based on sources from:

About the Author

Daniel KnottDaniel Knott works as a Manager Quality Assurance in XING’s mobile team. He likes to automate test cases using technologies such as Robotium, KIF(Keep It Functional), Selenium and Java.
XING Profile »