SlideShare a Scribd company logo
1 of 20
Download to read offline
Test your code in
real environment
Short introduc,on
Peter Adam Wiesner
So0ware Engineer @ Skyscanner
iOS run(me, code architectures
cyberpunk
Twi$er - @peteee24
E-mail - peter.wiesner@skyscanner.net
Github - @wiesnerpe<
What is the dream of every developer?
Development of apps, that the users love and find them useful :)
What is the coders worst nightmare?
Human nature
One key characteris.c of the human evolu.on is , that we try to
create tools for the problems we already solved. This helps us
crea.ng more complex systems
Same for apps. More and more classes are working together to
achieve a goal
.. and we trust every class will do its job correctly
Try to handle the situa0on
Unit tests << Integra-on tests << System tests << ?
tes%ng level == number of dependency, that need to be
covered
we should simulate real environments
can't write unit test for every inout-output configura5on, hope we
covered the edge cases
Best defence is a,ack
Not new idea(Ne,lix)
How to apply this to mobile apps?
Change some dependencies' behaviour, try to keep the system in
unstable state
Theory
Need a tool that can replace arbitary method of arbitary class to
behave in an arbitary different way
Dixie history
1. Unit tests were strangely succeeding ➡ Chaos Monkey
2. Collec9ng ideas ➡ make a library
3. Research lab ➡ Beta
4. Open source
Result
[Dixie new]
.Profile([DixieProfileEntry entry:[MyClass class]
selector:@selector(doStuff:)
chaosProvider:[DixieNilChaosProvider new]])
.Apply();
How does ObjC work?
1. [self doStuff:@2];
2. (id(*)(id, SEL,
id))objc_msgSend(self,@selector(doStuff:), @2);
3. Looking up in the methods of the class and superclass
4. if found ➡ jump to the registered IMP func>on-pointer
Swizzling
"replacing regitered IMP func3on-pointers with the help of
Objec3ve-C run3me C func3ons"
— NSHipster
• class_addMethod
• class_replaceMethod
• imp_implementationWithBlock
• We need to know the signature of the target method, when
wri5ng the code
Prepare for the unknown
• Need a general block, that can be used to swizzle arbitrary
method
• Long list of return and param types: BOOL, short, double, id,
selector, block...
• Idea:
• use variadic parameters
• parse them so they will be objects
• copy the code for every type by defining macros
Something like this
imp_implementationWithBlock([type]^(id victim, ...){
//Create call environment
//Parse parameters
//Call chaos provider block
block(victim, environment)
//Decide what to return
return ([type]*)environment.returnValue
})
Using the unknown
Some%mes we need to call an unknown func%on (previous problem
from the side of the caller)
if (numOfParams == 1)
f(obj1);
else if (numOfParams == 2)
f(obj1, obj2)
else ...
➡ Not scalable :(
Using the unknown
Macro magic
#define o(object) /
type(object) == @encode(int) ? (int)object : /
type(object) == @encode(double) ? (double)object : /
...
➡ ?: does not support different return types (C++ macros are
working in compile 8me)
Using the unknown
FFI(Foreign Func-on interface)
ffi_cif cif; //call object
ffi_type *args[1]; //arg types
void *values[1]; //arg values
ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, &ffi_type_uint, args)
ffi_call(&cif, IMP, &returnValue, values);
➡ 10% unknown crashes for class methods + dependency
Using the unknown
NSInvoca)on - FFI by Apple
invokeUsingIMP - private method, that does not use
objc_msgSend
NSMethodSignature* signature = ...;
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature];
//set args
[invocation invokeUsingIMP:implementation];
//get return value
Toolbox of opportuni.es
This is only the first step, how to advance?
➡ Combine behaviours -> Nil, Block, Composit, ExcepAon
➡ Invent new tools -> AST parsing, dependency recogniAon
To takeaway
1. Hope for the best, expect the worst, beside wri4ng unit tests,
challenge the applica4on with different inputs, in real life
environment
2. To simulate strange scenarios, Dixie is a good tool:
h@ps://github.com/Skyscanner/Dixie
Ques%ons?
Twi$er - @peteee24
E-mail - peter.wiesner@skyscanner.net
Github - @wiesnerpe<
Skyscanner Budapest - h2ps://www.facebook.com/hashtag/
skyscannerbp
Skyscanner - h2p://www.skyscanner.net/jobs/

More Related Content

Similar to Mobile Weekend Budapest presentation

Adventures in Asymmetric Warfare
Adventures in Asymmetric WarfareAdventures in Asymmetric Warfare
Adventures in Asymmetric WarfareWill Schroeder
 
Typhoon Managed Execution Toolkit
Typhoon Managed Execution ToolkitTyphoon Managed Execution Toolkit
Typhoon Managed Execution ToolkitDimitry Snezhkov
 
cf.Objective() 2017 - Design patterns - Brad Wood
cf.Objective() 2017 - Design patterns - Brad Woodcf.Objective() 2017 - Design patterns - Brad Wood
cf.Objective() 2017 - Design patterns - Brad WoodOrtus Solutions, Corp
 
Static code analysis: what? how? why?
Static code analysis: what? how? why?Static code analysis: what? how? why?
Static code analysis: what? how? why?Andrey Karpov
 
Test-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxTest-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxVictor Rentea
 
Java Programming
Java ProgrammingJava Programming
Java ProgrammingTracy Clark
 
Automated JavaScript Deobfuscation - PacSec 2007
Automated JavaScript Deobfuscation - PacSec 2007Automated JavaScript Deobfuscation - PacSec 2007
Automated JavaScript Deobfuscation - PacSec 2007Stephan Chenette
 
Behaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About TestingBehaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About Testingdn
 
Bdd and-testing
Bdd and-testingBdd and-testing
Bdd and-testingmalcolmt
 
Extending JS WU2016 Toronto
Extending JS WU2016 TorontoExtending JS WU2016 Toronto
Extending JS WU2016 TorontoFrancis Bourre
 
Mapping Detection Coverage
Mapping Detection CoverageMapping Detection Coverage
Mapping Detection CoverageJared Atkinson
 
The operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerThe operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerAndrey Karpov
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applicationsBabak Naffas
 
Code instrumentation
Code instrumentationCode instrumentation
Code instrumentationMennan Tekbir
 
Monitoring a program that monitors computer networks
Monitoring a program that monitors computer networksMonitoring a program that monitors computer networks
Monitoring a program that monitors computer networksAndrey Karpov
 
Training report anish
Training report anishTraining report anish
Training report anishAnish Yadav
 
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven DevelopmentABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven DevelopmentHendrik Neumann
 
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAPABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAPABAPCodeRetreat
 

Similar to Mobile Weekend Budapest presentation (20)

Adventures in Asymmetric Warfare
Adventures in Asymmetric WarfareAdventures in Asymmetric Warfare
Adventures in Asymmetric Warfare
 
Typhoon Managed Execution Toolkit
Typhoon Managed Execution ToolkitTyphoon Managed Execution Toolkit
Typhoon Managed Execution Toolkit
 
cf.Objective() 2017 - Design patterns - Brad Wood
cf.Objective() 2017 - Design patterns - Brad Woodcf.Objective() 2017 - Design patterns - Brad Wood
cf.Objective() 2017 - Design patterns - Brad Wood
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Static code analysis: what? how? why?
Static code analysis: what? how? why?Static code analysis: what? how? why?
Static code analysis: what? how? why?
 
Test-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxTest-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptx
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Automated JavaScript Deobfuscation - PacSec 2007
Automated JavaScript Deobfuscation - PacSec 2007Automated JavaScript Deobfuscation - PacSec 2007
Automated JavaScript Deobfuscation - PacSec 2007
 
Behaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About TestingBehaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About Testing
 
Bdd and-testing
Bdd and-testingBdd and-testing
Bdd and-testing
 
Extending JS WU2016 Toronto
Extending JS WU2016 TorontoExtending JS WU2016 Toronto
Extending JS WU2016 Toronto
 
Pyramid of-developer-skills
Pyramid of-developer-skillsPyramid of-developer-skills
Pyramid of-developer-skills
 
Mapping Detection Coverage
Mapping Detection CoverageMapping Detection Coverage
Mapping Detection Coverage
 
The operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerThe operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzer
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applications
 
Code instrumentation
Code instrumentationCode instrumentation
Code instrumentation
 
Monitoring a program that monitors computer networks
Monitoring a program that monitors computer networksMonitoring a program that monitors computer networks
Monitoring a program that monitors computer networks
 
Training report anish
Training report anishTraining report anish
Training report anish
 
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven DevelopmentABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
 
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAPABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
 

Mobile Weekend Budapest presentation

  • 1. Test your code in real environment
  • 2. Short introduc,on Peter Adam Wiesner So0ware Engineer @ Skyscanner iOS run(me, code architectures cyberpunk Twi$er - @peteee24 E-mail - peter.wiesner@skyscanner.net Github - @wiesnerpe<
  • 3. What is the dream of every developer? Development of apps, that the users love and find them useful :) What is the coders worst nightmare?
  • 4. Human nature One key characteris.c of the human evolu.on is , that we try to create tools for the problems we already solved. This helps us crea.ng more complex systems Same for apps. More and more classes are working together to achieve a goal .. and we trust every class will do its job correctly
  • 5. Try to handle the situa0on Unit tests << Integra-on tests << System tests << ? tes%ng level == number of dependency, that need to be covered we should simulate real environments can't write unit test for every inout-output configura5on, hope we covered the edge cases
  • 6. Best defence is a,ack Not new idea(Ne,lix) How to apply this to mobile apps? Change some dependencies' behaviour, try to keep the system in unstable state
  • 7. Theory Need a tool that can replace arbitary method of arbitary class to behave in an arbitary different way
  • 8. Dixie history 1. Unit tests were strangely succeeding ➡ Chaos Monkey 2. Collec9ng ideas ➡ make a library 3. Research lab ➡ Beta 4. Open source
  • 9. Result [Dixie new] .Profile([DixieProfileEntry entry:[MyClass class] selector:@selector(doStuff:) chaosProvider:[DixieNilChaosProvider new]]) .Apply();
  • 10. How does ObjC work? 1. [self doStuff:@2]; 2. (id(*)(id, SEL, id))objc_msgSend(self,@selector(doStuff:), @2); 3. Looking up in the methods of the class and superclass 4. if found ➡ jump to the registered IMP func>on-pointer
  • 11. Swizzling "replacing regitered IMP func3on-pointers with the help of Objec3ve-C run3me C func3ons" — NSHipster • class_addMethod • class_replaceMethod • imp_implementationWithBlock • We need to know the signature of the target method, when wri5ng the code
  • 12. Prepare for the unknown • Need a general block, that can be used to swizzle arbitrary method • Long list of return and param types: BOOL, short, double, id, selector, block... • Idea: • use variadic parameters • parse them so they will be objects • copy the code for every type by defining macros
  • 13. Something like this imp_implementationWithBlock([type]^(id victim, ...){ //Create call environment //Parse parameters //Call chaos provider block block(victim, environment) //Decide what to return return ([type]*)environment.returnValue })
  • 14. Using the unknown Some%mes we need to call an unknown func%on (previous problem from the side of the caller) if (numOfParams == 1) f(obj1); else if (numOfParams == 2) f(obj1, obj2) else ... ➡ Not scalable :(
  • 15. Using the unknown Macro magic #define o(object) / type(object) == @encode(int) ? (int)object : / type(object) == @encode(double) ? (double)object : / ... ➡ ?: does not support different return types (C++ macros are working in compile 8me)
  • 16. Using the unknown FFI(Foreign Func-on interface) ffi_cif cif; //call object ffi_type *args[1]; //arg types void *values[1]; //arg values ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, &ffi_type_uint, args) ffi_call(&cif, IMP, &returnValue, values); ➡ 10% unknown crashes for class methods + dependency
  • 17. Using the unknown NSInvoca)on - FFI by Apple invokeUsingIMP - private method, that does not use objc_msgSend NSMethodSignature* signature = ...; NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature]; //set args [invocation invokeUsingIMP:implementation]; //get return value
  • 18. Toolbox of opportuni.es This is only the first step, how to advance? ➡ Combine behaviours -> Nil, Block, Composit, ExcepAon ➡ Invent new tools -> AST parsing, dependency recogniAon
  • 19. To takeaway 1. Hope for the best, expect the worst, beside wri4ng unit tests, challenge the applica4on with different inputs, in real life environment 2. To simulate strange scenarios, Dixie is a good tool: h@ps://github.com/Skyscanner/Dixie
  • 20. Ques%ons? Twi$er - @peteee24 E-mail - peter.wiesner@skyscanner.net Github - @wiesnerpe< Skyscanner Budapest - h2ps://www.facebook.com/hashtag/ skyscannerbp Skyscanner - h2p://www.skyscanner.net/jobs/