1*8fb009dcSAndroid Build Coastguard Worker# Porting to Other Implementations 2*8fb009dcSAndroid Build Coastguard Worker 3*8fb009dcSAndroid Build Coastguard Worker## Introduction 4*8fb009dcSAndroid Build Coastguard Worker 5*8fb009dcSAndroid Build Coastguard WorkerThis document provides an overview of the test runner and how to 6*8fb009dcSAndroid Build Coastguard Workerintegrate it with other stacks. So far we have it working with 7*8fb009dcSAndroid Build Coastguard WorkerBoringSSL and some incomplete integrations with NSS and OpenSSL. 8*8fb009dcSAndroid Build Coastguard Worker 9*8fb009dcSAndroid Build Coastguard WorkerNote that supporting non-BoringSSL implementations is a work in 10*8fb009dcSAndroid Build Coastguard Workerprogress and interfaces may change in the future. Consumers should pin 11*8fb009dcSAndroid Build Coastguard Workerto a particular revision rather than using BoringSSL’s master branch 12*8fb009dcSAndroid Build Coastguard Workerdirectly. As we gain experience with other implementations, we hope to 13*8fb009dcSAndroid Build Coastguard Workermake further improvements to portability, so please contact 14*8fb009dcSAndroid Build Coastguard Worker[email protected] and [email protected] if implementing a new shim. 15*8fb009dcSAndroid Build Coastguard Worker 16*8fb009dcSAndroid Build Coastguard Worker 17*8fb009dcSAndroid Build Coastguard Worker## Integration Architecture 18*8fb009dcSAndroid Build Coastguard Worker 19*8fb009dcSAndroid Build Coastguard WorkerThe test runner integrates with the TLS stack under test through a 20*8fb009dcSAndroid Build Coastguard Worker“shim”: a command line program which encapsulates the stack. By 21*8fb009dcSAndroid Build Coastguard Workerdefault, the shim points to the BoringSSL shim in the same source 22*8fb009dcSAndroid Build Coastguard Workertree, but any program can be supplied via the `-shim-path` flag. The 23*8fb009dcSAndroid Build Coastguard Workerrunner opens up a server socket and provides the shim with `-port`, `-shim-id` 24*8fb009dcSAndroid Build Coastguard Workerand optional `-ipv6` arguments. 25*8fb009dcSAndroid Build Coastguard Worker 26*8fb009dcSAndroid Build Coastguard WorkerFor each connection, the shim should connect to loopback as a TCP client on 27*8fb009dcSAndroid Build Coastguard Workerthe specified port, using IPv6 if `-ipv6` is specified and IPv4 otherwise. 28*8fb009dcSAndroid Build Coastguard WorkerIt then sends the shim ID as a 64-bit, little-endian integer and proceeds with 29*8fb009dcSAndroid Build Coastguard Workerthe test. The shim is a TCP client even when testing DTLS or TLS server 30*8fb009dcSAndroid Build Coastguard Workerbehavior. For DTLS, there is a small framing layer that gives packet boundaries 31*8fb009dcSAndroid Build Coastguard Workerover TCP. The shim can also pass a variety of command line arguments 32*8fb009dcSAndroid Build Coastguard Workerwhich are used to configure the stack under test. These can be found at 33*8fb009dcSAndroid Build Coastguard Worker`test_config.cc`. 34*8fb009dcSAndroid Build Coastguard Worker 35*8fb009dcSAndroid Build Coastguard WorkerThe shim reports success by exiting with a `0` error code and failure by 36*8fb009dcSAndroid Build Coastguard Workerreporting a non-zero error code and generally sending a textual error 37*8fb009dcSAndroid Build Coastguard Workervalue to stderr. Many of the tests expect specific error string (such 38*8fb009dcSAndroid Build Coastguard Workeras `NO_SHARED_CIPHER`) that indicates what went wrong. 39*8fb009dcSAndroid Build Coastguard Worker 40*8fb009dcSAndroid Build Coastguard Worker 41*8fb009dcSAndroid Build Coastguard Worker## Compatibility Issues 42*8fb009dcSAndroid Build Coastguard Worker 43*8fb009dcSAndroid Build Coastguard WorkerThere are a number of situations in which the runner might succeed 44*8fb009dcSAndroid Build Coastguard Workerwith some tests and not others: 45*8fb009dcSAndroid Build Coastguard Worker 46*8fb009dcSAndroid Build Coastguard Worker* Defects in the stack under test 47*8fb009dcSAndroid Build Coastguard Worker* Features which haven’t yet been implemented 48*8fb009dcSAndroid Build Coastguard Worker* Failure to implement one or more of the command line flags the runner uses with the shim 49*8fb009dcSAndroid Build Coastguard Worker* Disagreement about the right behavior/interpretation of the spec 50*8fb009dcSAndroid Build Coastguard Worker 51*8fb009dcSAndroid Build Coastguard Worker 52*8fb009dcSAndroid Build Coastguard WorkerWe have implemented several features which allow implementations to ease these compatibility issues. 53*8fb009dcSAndroid Build Coastguard Worker 54*8fb009dcSAndroid Build Coastguard Worker### Configuration File 55*8fb009dcSAndroid Build Coastguard Worker 56*8fb009dcSAndroid Build Coastguard WorkerThe runner can be supplied with a JSON configuration file which is 57*8fb009dcSAndroid Build Coastguard Workerintended to allow for a per-stack mapping. This file currently takes 58*8fb009dcSAndroid Build Coastguard Workertwo directives: 59*8fb009dcSAndroid Build Coastguard Worker 60*8fb009dcSAndroid Build Coastguard Worker 61*8fb009dcSAndroid Build Coastguard Worker* `DisabledTests`: A JSON map consisting of the pattern matching the 62*8fb009dcSAndroid Build Coastguard Worker tests to be disabled as the key and some sort of reason why it was 63*8fb009dcSAndroid Build Coastguard Worker disabled as the value. The key is used as a match against the test 64*8fb009dcSAndroid Build Coastguard Worker name. The value is ignored and is just used for documentation 65*8fb009dcSAndroid Build Coastguard Worker purposes so you can remember why you disabled a 66*8fb009dcSAndroid Build Coastguard Worker test. `-include-disabled` overrides this filter. 67*8fb009dcSAndroid Build Coastguard Worker 68*8fb009dcSAndroid Build Coastguard Worker* `ErrorMap`: A JSON map from the internal errors the runner expects to 69*8fb009dcSAndroid Build Coastguard Worker the error strings that your implementation spits out. Generally 70*8fb009dcSAndroid Build Coastguard Worker you’ll need to map every error, but if you also provide the 71*8fb009dcSAndroid Build Coastguard Worker ` -loose-errors` flag, then every un-mapped error just gets mapped to 72*8fb009dcSAndroid Build Coastguard Worker the empty string and treated as if it matched every error the runner 73*8fb009dcSAndroid Build Coastguard Worker expects. 74*8fb009dcSAndroid Build Coastguard Worker 75*8fb009dcSAndroid Build Coastguard Worker 76*8fb009dcSAndroid Build Coastguard WorkerThe `-shim-config` flag is used to provide the config file. 77*8fb009dcSAndroid Build Coastguard Worker 78*8fb009dcSAndroid Build Coastguard Worker 79*8fb009dcSAndroid Build Coastguard Worker### Unimplemented Features 80*8fb009dcSAndroid Build Coastguard WorkerIf the shim encounters some request from the runner that it knows it 81*8fb009dcSAndroid Build Coastguard Workercan’t fulfill (e.g., a command line flag that it doesn’t recognize), 82*8fb009dcSAndroid Build Coastguard Workerthen it can exit with the special code `89`. Shims are recommended to 83*8fb009dcSAndroid Build Coastguard Workeruse this exit code on unknown command-line arguments. 84*8fb009dcSAndroid Build Coastguard Worker 85*8fb009dcSAndroid Build Coastguard WorkerThe test runner interprets this as “unimplemented” and skips the 86*8fb009dcSAndroid Build Coastguard Workertest. If run normally, this will cause the test runner to report that 87*8fb009dcSAndroid Build Coastguard Workerthe entire test suite failed. The `-allow-unimplemented` flag suppresses 88*8fb009dcSAndroid Build Coastguard Workerthis behavior and causes the test runner to ignore these tests for the 89*8fb009dcSAndroid Build Coastguard Workerpurpose of evaluating the success or failure of the test suite. 90*8fb009dcSAndroid Build Coastguard Worker 91*8fb009dcSAndroid Build Coastguard Worker 92*8fb009dcSAndroid Build Coastguard Worker### Malloc Tests 93*8fb009dcSAndroid Build Coastguard Worker 94*8fb009dcSAndroid Build Coastguard WorkerThe test runner can also be used to stress malloc failure 95*8fb009dcSAndroid Build Coastguard Workercodepaths. If passed `-malloc-test=0`, the runner will run each test 96*8fb009dcSAndroid Build Coastguard Workerrepeatedly with an incrementing `MALLOC_NUMBER_TO_FAIL` environment 97*8fb009dcSAndroid Build Coastguard Workervariable. The shim should then replace the malloc implementation with 98*8fb009dcSAndroid Build Coastguard Workerone which fails at the specified number of calls. If there are not 99*8fb009dcSAndroid Build Coastguard Workerenough calls to reach the number, the shim should fail with exit code 100*8fb009dcSAndroid Build Coastguard Worker`88`. This signals to the runner that the test has completed. 101*8fb009dcSAndroid Build Coastguard Worker 102*8fb009dcSAndroid Build Coastguard WorkerHistorically, BoringSSL did this by replacing the actual `malloc` 103*8fb009dcSAndroid Build Coastguard Workersymbol, but we have found hooking the library's `malloc` wrapper, under a 104*8fb009dcSAndroid Build Coastguard Workertest-only build configuration, to be more straightforward. See `crypto/mem.c` 105*8fb009dcSAndroid Build Coastguard Workerfor an example which handles the environment variables in `OPENSSL_malloc`. 106*8fb009dcSAndroid Build Coastguard Worker 107*8fb009dcSAndroid Build Coastguard WorkerNote these tests are slow and will hit Go's test timeout. Pass `-timeout 72h` to 108*8fb009dcSAndroid Build Coastguard Workeravoid crashing after 10 minutes. 109*8fb009dcSAndroid Build Coastguard Worker 110*8fb009dcSAndroid Build Coastguard Worker 111*8fb009dcSAndroid Build Coastguard Worker## Example: Running Against NSS 112*8fb009dcSAndroid Build Coastguard Worker 113*8fb009dcSAndroid Build Coastguard Worker``` 114*8fb009dcSAndroid Build Coastguard WorkerDYLD_LIBRARY_PATH=~/dev/nss-dev/nss-sandbox/dist/Darwin15.6.0_64_DBG.OBJ/lib go test -shim-path ~/dev/nss-dev/nss-sandbox/dist/Darwin15.6.0_64_DBG.OBJ/bin/nss_bogo_shim -loose-errors -allow-unimplemented -shim-config ~/dev/nss-dev/nss-sandbox/nss/external_tests/nss_bogo_shim/config.json 115*8fb009dcSAndroid Build Coastguard Worker``` 116