property-based testing - etsi · pdf file• each raised alarm is cleared after soft...

53
Property-Based Testing Quviq AB Thomas Arts

Upload: ngodiep

Post on 07-Feb-2018

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Property-Based Testing

Quviq AB Thomas Arts

Page 2: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Innovations

Functional Programming late 80ies

©

Haskell (Academia)

Erlang (Ericsson)

Page 3: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Innovations

90ies: Sweden strong in Functional Programming

©

Haskell (Academia) John Hughes professor in Göteborg

Erlang (Ericsson) Thomas Arts Ericsson Research lab

Page 4: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Innovations

2000 Property-based testing invented

©

Haskell (Academia) John Hughes professor in Göteborg QuickCheck: Generating test cases

Erlang (Ericsson) Thomas Arts Ericsson Research lab

experience in formal verification professor in Göteborg

Page 5: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Properties

We identify software properties

•  For all possible sequence of allowed database queries, business rule R is never violated

•  Under all circumstances, highest priority signals are submitted first

•  Each raised alarm is cleared after soft restart •  the legacy software behaves exactly the same

as the new product

©

Page 6: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

How does it work?

Requirements provide us software properties

QuickCheck is a tool that automatically generates test cases from these properties.

©

Properties Test case Test case Test case Test case Test case

Minimal Test case

Page 7: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Benefits

•  Less time spent writing test code –  One property replaces many tests

•  Better testing –  Lots of combinations you’d never test by hand

•  Less time spent on diagnosis –  Failures minimized automagically

Page 8: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Property-based testing

Based on a property, we test a program An example: SMS encode/decode GSM standard Technical Specification 03.38 Version 5.3.0, July 1996 Packing of 7 bit characters 6.1.2.1.1 (page 13-14)

©

Page 9: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Example

Interpretation 3 characters containing 7 bits packets 8 bits

a b c d e f g a b c d e f g a b c d e f g

10 December 2007 Thomas Arts 9

Page 10: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Example

0 0 0

a b c d e f a b c d e f f

a b c d e

pack

unpack

1 0 0 1 0 0 0 1 1 0 0 1 0

1

1 1 0 1 0 1 0

1 0 0 1 0 0 0 1 1 0 0 1 0 1 1 1 0 1 0 1 0

“Ȳ^Z” = [200,178,26] “Hej” = [72,101,106]

10 December 2007 Thomas Arts 10

Page 11: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Example

Two properties prop_compress() -> ?FORALL(Len, choose(8,160), ?FORALL(Msg, vector(Len,choose(0,127)),

size(sms:pack(Msg)) < length(Msg))).

©

An SMS of 8 characters or longer, is shorter than

8 bytes after packing

Page 12: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Example

Two properties prop_identity() -> ?FORALL(Msg, list(choose(0,127)), sms:unpack(sms:pack(Msg)) == Msg).

©

after packing, unpacking returns the original SMS

Page 13: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Example

DEMO

©

Page 14: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Benefits

•  Less time spent writing test code –  One property replaces many tests

•  Better testing –  Lots of combinations you’d never test by hand

•  Less time spent on diagnosis –  Failures minimized automagically

Page 15: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

QuickCheck

In real software, properties are more complex Real software has state... properties need to be stateful and generate a sequence of commands Shrinking to minimal failing test case saves a lot of time in analysis

©

Page 16: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

OK

3G Radio Base Station

Testing cell and channel setup/teardown and configuration

Setup

Setup

OK

Reject

©

Page 17: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

AUTOSAR

Property: highest priority message should be send first

©

Sample

send priority 1

send priority 2

send priority 3

tx_confirm

sending 1

sending 3 1 sent

queued

Cause: failure to mask a bit off an extended CAN-identifier

Page 18: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

QuickCheck

two examples

Financial sector: finding race conditions Automotive: highly configurable software

©

Page 19: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

What is it?

Application

Mnesia

Dets

File system

Invoicing services for web shops

Distributed database: transactions, distribution, replication

Tuple storage

500+ people in 5 years

Race conditions?

Page 20: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Financial sector

Bug in Klarna's financial database - Appearing in production once a month -  subtle, not found by six weeks of investigation

with conventional methods -  Thought to appear in databases of ~ 1GB.

©

Page 21: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Finding race-conditions

Only special circumstances trigger a specific race

Observation in fault-tolerant software difficult

Traditional approach: try to find race-condition related errors in integration testing… try to simulate extreme cases and see what happens. If you observe the error, you don’t know what really caused it!

©

Page 22: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Finding race-conditions

If you observe the error, you don’t know what really caused it!

Analysis is expensive. … we see it after 25000 function calls, 491 communicating processes, and with 1GB data loaded, …sometimes…

Analysis for traditional unit testing is cheaper… but we cannot write unit tests for concurrency!

©

Page 23: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Imagine Testing This…

dispenser:take_ticket() dispenser:reset()

©

Page 24: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

ok = 1 = 2 = 3 =

A Unit Test in Erlang

test_dispenser() ->

Expected results

BUT…

reset(), take_ticket(), take_ticket(), take_ticket().

©

Page 25: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

A Parallel Unit Test

•  Three possible correct outcomes!

reset

take_ticket

take_ticket

take_ticket

1 2

31 3

22 3

1

ok

1 2

1

©

Page 26: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

ok = 1 = 2 = 3 = ok = 1 =

A Unit Test in Erlang

test_dispenser() ->

Expected results

BUT…

reset(), take_ticket(), take_ticket(), take_ticket(), reset(), take_ticket().

©

Page 27: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Another Parallel Test

30 possible correct outcomes!

reset

take_ticket

take_ticket

take_ticket

take_ticket

reset

©

Page 28: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Finding race-conditions

Writing unit tests for concurrent events: Headache! Thus, people don’t! QuickCheck does it for you!

©

Page 29: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Modelling  the  dispenser  

reset   take   take   take  

*   0   1   2  

ok                                      1                                      2                                      3  

Page 30: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Parallel  Test  Cases  

resetèok  

takeè1  

takeè3  

takeè2  

*   0   1   2  

Page 31: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Concurrency errors

QuickCheck properties: Property specifies behaviour of any command sequence QuickCheck •  runs the sequences with different threads •  collect the results •  checks whether this can be explained from

sequential behaviour

©

Page 32: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Databases

Bug in Klarna's financial database

©

Before

•  Files over 1GB? •  Rehashing? •  > 6 weeks of effort!

After

•  Database with 1 record! •  5 calls to reproduce •  2 days model •  < 1 day to fix

Page 33: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Application areas

COTS components in automotive domain AUTOSAR is a standard for software architecture in the automotive industry. Volvo wants to be able to replace components from one vendor by components of another.

©

Page 34: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

AUTOSAR standard

3000 pages of pdf specifications

©

Page 35: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Testing AUTOSAR

What is difficult in writing test cases? •  Everything is configurable. Thousands of

parameters can be specified •  AUTOSAR is modular. Tests are designed

against a specification, but there is no specification for combinations of modules

©

Page 36: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Configurations are vendor specific

System template Configuration

tool

XML Config

Code generation

tool lib .h

A test is: A configuration and a set of API calls with their expected results.

©

Page 37: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Tests

A test is: A configuration and a set of API calls with their expected results. For testing configurations are kept small A number of API call sequences per configuration Vendor may need to change configuration a bit before code can be generated and tests can be run.

Page 38: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Tests

config - - -

- - -

- - -

config - - -

- - -

- - -

- - -

Doing the maths: 1 person 1 week

Page 39: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Tests

- - -

- - -

- - -

- - -

- - -

- - -

- - -

Doing the maths: 30 persons 1 week

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -- - -

- - -

- - -

- - -

- - -

- - -

- - --

- -

- - -

- - -

- - -

- - -

- - -

- - --

- -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - --

- -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - --

- -

- - -

- - -

- - -

- - -

- - -

- - --

- -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - --

- -

- - -

- - -

- - -

- - -

- - -

- - --

- -

- - -

- - -

- - -

- - -

- - -

- - -- - -

- - -

- - -

- - -

- - -

- - -

- - --

- -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

- - -

Page 40: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Tests

Doing the maths: 30 person years, 2-5 tests per week per developer, 10000 tests…. 3000 configurations Executing those tests is a nightmare, since one needs to adopt the configurations and generate code

- - -

- - -

- - -

- - -

- - - - - -

- - -

- - -

- - -

- - -

- - -

- - - - - -

- - -

- - -

- - -

- - -

- - -

- - - - - -

- - - - - -

- - -

- - -

- - -

- - - - - -

- - - -

- -

- - -

- - -

- - -

- - - - - -

- - - -

- -

- - -

- - -

- - -

- - - - - -

- - -

- - -

- - -

- - -

- - -

- - - - - -

- - - -

- -

- - -

- - -

- - -

- - - - - -

- - -

- - -

- - -

- - -

- - -

- - - - - -

- - -

- - -

- - -

- - -

- - -

- - - - - -

- - - -

- -

- - -

- - -

- - -

- - - - - -

- - - -

- -

- - -

- - -

- - -

- - - - - -

- - -

- - -

- - -

- - -

- - -

- - - - - -

- - -

- - -

- - -

- - -

- - -

- - - - - -

- - -

- - -

- - -

- - -

- - -

- - - - - -

- - - -

- -

- - -

- - -

- - -

- - - - - -

- - - -

- -

- - -

- - -

- - -

- - - - - -

- - - - - -

- - -

- - -

- - -

- - - - - -

- - - -

- -

- - -

- - -

- - -

- - - - - -

- - -

- - -

- - -

- - -

- - -

- - - - - -

- - -

- - -

- - -

- - -

- - -

- - - - - -

- - -

- - -

- - -

- - -

- - -

- - - - - -

- - -

- - -

- - -

- - -

- - -

- - - - - -

- - -

X 50

Needed to cover the specifications and configuration

features

Page 41: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Property-based testing

change your mind about testing

Page 42: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Model-based testing

•  Compositional models •  One large configuration supporting all test cases •  A huge number of test cases automatically

generated •  All features/requirements tested at the same

time –  Many API’s at once, all assertions always around and

random sequences no-one would consider testing

Page 43: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Highly configurable

©

Page 44: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Properties vs. Test Cases

TTCN-3 test suite for CAN modules: 245 test cases, 58KLOC

0

5000

10000

15000

20000

25000

CanIf CanSM CanNm CanTp

TTCN3 LOC QC LOC

The test code is 8x smaller!

©

Page 45: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

The Problem of Scale

0 2000 4000 6000 8000

10000 12000 14000 16000 18000 20000

CAN DEM FlexRay COM

Implementation Specification

considerably less work to create complete model

©

Page 46: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Advantages –  Over 100 errors found in 4 weeks of testing production

ready code –  Erroneous dependencies between features found

•  Mix of many features tested in same tests

–  Failures found in “obvious fault-free implementation” •  Everything is tested, even parts otherwise excluded by manual

tests

–  General higher coverage •  Many more tests executed •  All assertions always considered

–  Common human mistakes detected •  Common human errors for both developer and test designer are

found by model

©

Page 47: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Deliverables

Jan 6 – Jan 31 Confirmed errors in production code

–  Com: over 20 –  Can: over 30, mostly in CanSM and CanTp –  Lin: 5 errors, Requesting a schedule cannot be tested

due to incompatibility.

In some cases, model more precise than vendors want to be.

Page 48: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Starting a TP Gateway

!PduR_Init( ...);!PduR_FrTpStartOfReception( 'PduR_Path15_S', 30, ...);!!Vendor1: { 0,'BUFREQ_E_OVFL'}!Vendor2: {29,'BUFREQ_OK'} !Vendor3: { 0,'BUFREQ_E_OVFL'}!Vendor4: {29,'BUFREQ_OK'}!!Scope reduction: do not test Tp Gateway with messages larger than buffer size !!!

Page 49: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Starting a TP Gateway

PduR_Init(...);!PduR_FrTpStartOfReception( 'PduR_Path15_S', 8, ...);!!Vendor1: {29,'BUFREQ_OK'}!Vendor2: {29,'BUFREQ_OK'} !Vendor3: { 8,'BUFREQ_OK'}!Vendor4: { 8,'BUFREQ_OK'}!!Model Variant: return either max buffer size (i.e. 29) or maximum requested size (i.e. 8). !!!

Page 50: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Starting a TP Gateway

!PduR_Init(...);!PduR_FrTpStartOfReception( 'PduR_Path15_S', 0, ...);!!Vendor1: {29,'BUFREQ_OK'}!Vendor2: {29,'BUFREQ_OK'} !Vendor3: { 0,'BUFREQ_OK'}!Vendor4: { 0,'BUFREQ_OK'}!!Violation of PDUR507: The service shall provide the currently available maximum buffer size when invoked with TpSdulength equal to 0. !!!

Page 51: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

AUTOSAR

Comparing implementations •  reveals ambiguities in specifications •  reveals freedom in specifications

©

A test that succeeds for one vendor may fail for all others. Static tests are not re-usable!

Page 52: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

AUTOSAR

Comparing implementations •  reveals ambiguities in specifications •  reveals freedom in specifications

Dealing with "freedom" by defining model variations. Models configurable and instantiated by variants

©

Page 53: Property-Based Testing - ETSI · PDF file• Each raised alarm is cleared after soft restart ... QuickCheck is a tool that automatically ... Property-based testing Based on a property,

Property-based testing

change your mind about testing