feat: real Path A implementation, cert extraction, mock test harness

Converts the Path A pipeline from stubs to a working implementation for
A5-A11 devices, adds real certificate extraction via lockdownd and ECDSA
P-256 signing, and introduces a comprehensive mock-based test suite.

Activation
- cert_extract: lockdownd-backed DeviceCertificate,
  AccountTokenCertificate, UniqueDeviceCertificate with base64 decode
  and CertificateChain array fallback.
- signer: OpenSSL EVP ECDSA P-256 sign/verify with DER-encoded
  signatures plus a test keypair generator.
- record.c: 3 of 6 cert fields now populate from live device data.
  AccountTokenSignature signs when TR4MPASS_SIGNING_KEY is set.
  FairPlayKeyData, RKCData, SepNonce remain documented stubs
  (SEP-blocked per 26.3-vulnerability.md).

Path A bypass
- path_a_ramdisk: iBSS/iBEC/devicetree/trustcache/ramdisk upload via
  libirecovery, reconnect handling between DFU and recovery stages,
  iBoot go/bootx commands, SSH readiness poll.
- path_a_ssh: libssh2 + SCP. Mounts rootfs rw (apfs then hfs), runs
  AMFI patcher, backs up and replaces /usr/libexec/mobileactivationd
  with a user-supplied patched binary, verifies mode.

Build, CLI, config
- Makefile: add libssh2 dependency, split test targets into
  hw-independent (make test) and full mock (make test-mocks).
- env_config + cli_flags: 12 new --flags for ramdisk, SSH, signing
  paths; flags map to TR4MPASS_* env vars. --verbose prints summary.
- start.sh slimmed; helpers extracted to start-helpers.sh.

Test suite (20 to 248 tests)
- Mock framework under tests/mocks/ intercepts libimobiledevice,
  libirecovery, libusb, libssh2, libcurl, plus mobileactivation and
  afc. Staging API in mock_control.h.
- Unit tests per module: cert_extract, signer, ramdisk, SSH jailbreak,
  env_config.
- Integration tests in tests/integration/: 6 Path A E2E scenarios
  (happy path, exploit fail, reconnect timeout, SSH auth fail, SCP
  fail, missing env) and 3 Path B scenarios (identity manipulation
  works, nonce barrier rejection is handled cleanly, signal
  detection). Path B tests verify the tool reports the SEP barrier
  failure rather than misclaiming success.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
tr4m0ryp 2026-04-18 17:43:30 +02:00
parent 5b758931ac
commit 30807c8ef6
58 changed files with 7967 additions and 1210 deletions

View file

@ -0,0 +1,296 @@
/*
* mock_afc_mobileactivation.c -- Mocks for libimobiledevice's AFC and
* mobileactivation service APIs. Staging + reset lives in the sibling
* file mock_afc_mobileactivation_ctl.c; only the interposed symbols
* remain here.
*/
#include "mock_internal.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <libimobiledevice/libimobiledevice.h>
#include <libimobiledevice/lockdown.h>
#include <libimobiledevice/mobileactivation.h>
#include <libimobiledevice/afc.h>
#include <plist/plist.h>
/* Shared staging state (see mock_afc_mobileactivation_ctl.c). */
extern int g_ma_activate_error;
extern int g_ma_create_info_error;
extern char g_ma_state[64];
/* ------------------------------------------------------------------ */
/* mobileactivation_* */
/* ------------------------------------------------------------------ */
struct mobileactivation_client_private {
uint32_t magic;
};
#define MA_MAGIC 0x4D414354u /* "MACT" */
mobileactivation_error_t
mobileactivation_client_start_service(idevice_t device,
mobileactivation_client_t *client,
const char *label)
{
struct mobileactivation_client_private *c;
(void)device; (void)label;
mock_log_append("mobileactivation_client_start_service");
if (!client) return MOBILEACTIVATION_E_INVALID_ARG;
c = calloc(1, sizeof(*c));
if (!c) return MOBILEACTIVATION_E_UNKNOWN_ERROR;
c->magic = MA_MAGIC;
*client = c;
return MOBILEACTIVATION_E_SUCCESS;
}
mobileactivation_error_t
mobileactivation_client_new(idevice_t device,
lockdownd_service_descriptor_t service,
mobileactivation_client_t *client)
{
(void)service;
return mobileactivation_client_start_service(device, client, "mock");
}
mobileactivation_error_t
mobileactivation_client_free(mobileactivation_client_t client)
{
mock_log_append("mobileactivation_client_free");
if (!client) return MOBILEACTIVATION_E_SUCCESS;
client->magic = 0;
free(client);
return MOBILEACTIVATION_E_SUCCESS;
}
mobileactivation_error_t
mobileactivation_get_activation_state(mobileactivation_client_t client,
plist_t *state)
{
(void)client;
mock_log_append("mobileactivation_get_activation_state");
if (!state) return MOBILEACTIVATION_E_INVALID_ARG;
*state = plist_new_string(g_ma_state);
return MOBILEACTIVATION_E_SUCCESS;
}
mobileactivation_error_t
mobileactivation_create_activation_session_info(mobileactivation_client_t client,
plist_t *blob)
{
(void)client;
mock_log_append("mobileactivation_create_activation_session_info");
if (!blob) return MOBILEACTIVATION_E_INVALID_ARG;
/* Return an empty dict: tests can replace via Phase 2 helpers. */
*blob = plist_new_dict();
return MOBILEACTIVATION_E_SUCCESS;
}
mobileactivation_error_t
mobileactivation_create_activation_info(mobileactivation_client_t client,
plist_t *info)
{
(void)client;
mock_log_append("mobileactivation_create_activation_info");
if (!info) return MOBILEACTIVATION_E_INVALID_ARG;
if (g_ma_create_info_error != 0) {
*info = NULL;
return (mobileactivation_error_t)g_ma_create_info_error;
}
*info = plist_new_dict();
return MOBILEACTIVATION_E_SUCCESS;
}
mobileactivation_error_t
mobileactivation_create_activation_info_with_session(mobileactivation_client_t client,
plist_t handshake_response,
plist_t *info)
{
(void)client; (void)handshake_response;
mock_log_append("mobileactivation_create_activation_info_with_session");
if (!info) return MOBILEACTIVATION_E_INVALID_ARG;
if (g_ma_create_info_error != 0) {
*info = NULL;
return (mobileactivation_error_t)g_ma_create_info_error;
}
*info = plist_new_dict();
return MOBILEACTIVATION_E_SUCCESS;
}
mobileactivation_error_t
mobileactivation_activate(mobileactivation_client_t client,
plist_t activation_record)
{
(void)client; (void)activation_record;
mock_log_append("mobileactivation_activate");
if (g_ma_activate_error != 0)
return (mobileactivation_error_t)g_ma_activate_error;
return MOBILEACTIVATION_E_SUCCESS;
}
mobileactivation_error_t
mobileactivation_activate_with_session(mobileactivation_client_t client,
plist_t activation_record,
plist_t headers)
{
(void)client; (void)activation_record; (void)headers;
mock_log_append("mobileactivation_activate_with_session");
if (g_ma_activate_error != 0)
return (mobileactivation_error_t)g_ma_activate_error;
return MOBILEACTIVATION_E_SUCCESS;
}
mobileactivation_error_t
mobileactivation_deactivate(mobileactivation_client_t client)
{
(void)client;
mock_log_append("mobileactivation_deactivate");
return MOBILEACTIVATION_E_SUCCESS;
}
/* ------------------------------------------------------------------ */
/* afc_* */
/* ------------------------------------------------------------------ */
struct afc_client_private {
uint32_t magic;
uint64_t next_handle;
};
#define AFC_MAGIC 0x41464343u /* "AFCC" */
afc_error_t afc_client_new(idevice_t device,
lockdownd_service_descriptor_t service,
afc_client_t *client)
{
struct afc_client_private *c;
(void)device; (void)service;
mock_log_append("afc_client_new");
if (!client) return AFC_E_INVALID_ARG;
c = calloc(1, sizeof(*c));
if (!c) return AFC_E_NO_MEM;
c->magic = AFC_MAGIC;
c->next_handle = 0x1000;
*client = c;
return AFC_E_SUCCESS;
}
afc_error_t afc_client_start_service(idevice_t device, afc_client_t *client,
const char *label)
{
(void)label;
return afc_client_new(device, NULL, client);
}
afc_error_t afc_client_free(afc_client_t client)
{
mock_log_append("afc_client_free");
if (!client) return AFC_E_SUCCESS;
client->magic = 0;
free(client);
return AFC_E_SUCCESS;
}
afc_error_t afc_file_open(afc_client_t client, const char *filename,
afc_file_mode_t mode, uint64_t *handle)
{
(void)mode;
mock_log_append("afc_file_open(%s)", filename ? filename : "");
if (!client || !handle) return AFC_E_INVALID_ARG;
*handle = ++client->next_handle;
return AFC_E_SUCCESS;
}
afc_error_t afc_file_close(afc_client_t client, uint64_t handle)
{
(void)client; (void)handle;
mock_log_append("afc_file_close");
return AFC_E_SUCCESS;
}
afc_error_t afc_file_read(afc_client_t client, uint64_t handle,
char *data, uint32_t length, uint32_t *bytes_read)
{
(void)client; (void)handle; (void)data; (void)length;
if (bytes_read) *bytes_read = 0;
return AFC_E_SUCCESS;
}
afc_error_t afc_file_write(afc_client_t client, uint64_t handle,
const char *data, uint32_t length,
uint32_t *bytes_written)
{
(void)client; (void)handle; (void)data;
mock_log_append("afc_file_write(%u)", length);
if (bytes_written) *bytes_written = length;
return AFC_E_SUCCESS;
}
afc_error_t afc_remove_path(afc_client_t client, const char *path)
{
(void)client;
mock_log_append("afc_remove_path(%s)", path ? path : "");
return AFC_E_SUCCESS;
}
afc_error_t afc_make_directory(afc_client_t client, const char *path)
{
(void)client;
mock_log_append("afc_make_directory(%s)", path ? path : "");
return AFC_E_SUCCESS;
}
afc_error_t afc_read_directory(afc_client_t client, const char *path,
char ***directory_information)
{
(void)client; (void)path;
if (directory_information) {
char **arr = calloc(1, sizeof(char *));
if (arr) *directory_information = arr;
else return AFC_E_NO_MEM;
}
return AFC_E_SUCCESS;
}
afc_error_t afc_get_file_info(afc_client_t client, const char *path,
char ***file_information)
{
(void)client; (void)path;
if (file_information) {
char **arr = calloc(1, sizeof(char *));
if (arr) *file_information = arr;
else return AFC_E_NO_MEM;
}
return AFC_E_SUCCESS;
}
afc_error_t afc_rename_path(afc_client_t client, const char *from,
const char *to)
{
(void)client;
mock_log_append("afc_rename_path(%s -> %s)",
from ? from : "", to ? to : "");
return AFC_E_SUCCESS;
}
afc_error_t afc_dictionary_free(char **dictionary)
{
int i;
if (!dictionary) return AFC_E_SUCCESS;
for (i = 0; dictionary[i]; i++)
free(dictionary[i]);
free(dictionary);
return AFC_E_SUCCESS;
}
const char *afc_strerror(afc_error_t err)
{
switch ((int)err) {
case 0: return "Success";
default: return "Mock AFC error";
}
}

View file

@ -0,0 +1,42 @@
/*
* mock_afc_mobileactivation_ctl.c -- Staging + reset for the AFC /
* mobileactivation mock. Kept separate from _sym.c so each TU stays
* under the 300-line project cap.
*/
#include "mock_internal.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Shared with the sym file via extern declarations in the header below. */
int g_ma_activate_error;
int g_ma_create_info_error;
char g_ma_state[64] = "Activated";
void mock_mobileactivation_set_activate_error(int error_code)
{
g_ma_activate_error = error_code;
}
void mock_mobileactivation_set_create_info_error(int error_code)
{
g_ma_create_info_error = error_code;
}
void mock_mobileactivation_set_state(const char *state)
{
if (!state)
snprintf(g_ma_state, sizeof(g_ma_state), "%s", "Activated");
else
snprintf(g_ma_state, sizeof(g_ma_state), "%s", state);
}
void mock_afc_mobileactivation_reset(void)
{
g_ma_activate_error = 0;
g_ma_create_info_error = 0;
snprintf(g_ma_state, sizeof(g_ma_state), "%s", "Activated");
}

263
tests/mocks/mock_control.h Normal file
View file

@ -0,0 +1,263 @@
/*
* mock_control.h -- Public staging API for the tr4mpass mock library.
*
* Tests call these functions in their setup to:
* - stage canned responses for the next library call,
* - register expectations that the next call must match,
* - inspect the recorded call log after the SUT runs,
* - and reset all state between tests.
*
* The mocks themselves live in mock_*.c files in this directory. They
* replace the symbols normally provided by libimobiledevice, libirecovery,
* libusb-1.0, libssh2, and libcurl when the test binary is linked without
* those real libraries.
*
* NONE of this API is thread-safe. Tests must run sequentially and call
* mock_reset_all() in their setup hook.
*/
#ifndef TR4MPASS_MOCK_CONTROL_H
#define TR4MPASS_MOCK_CONTROL_H
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ------------------------------------------------------------------ */
/* Global reset */
/* ------------------------------------------------------------------ */
/* Clear every staged value, expectation, captured call, and scratch
* buffer in every mock. MUST be called at the start of every test. */
void mock_reset_all(void);
/* ------------------------------------------------------------------ */
/* Call log (shared across all mocks) */
/* ------------------------------------------------------------------ */
#define MOCK_LOG_MAX 512
/* Append an entry. Used internally by mocks; exposed for ad-hoc test
* helpers that want to record synthetic events. */
void mock_log_append(const char *fmt, ...);
/* Number of entries currently in the log. */
size_t mock_get_call_count(void);
/* Read-only pointer to entry at index; NULL if out of range. */
const char *mock_get_call_log(size_t index);
/* Return 1 if any entry contains the given substring, else 0. */
int mock_call_log_contains(const char *needle);
/* ------------------------------------------------------------------ */
/* lockdownd / idevice staging */
/* ------------------------------------------------------------------ */
/*
* Stage a value to be returned by the next matching lockdownd_get_value
* call where (domain matches OR domain==NULL) AND key matches.
*
* data is duplicated; caller retains ownership of its original buffer.
* If data_type is "string", data is interpreted as a C string.
* If data_type is "uint", data is interpreted as a pointer to uint64_t.
* If data_type is "data", data is a raw byte blob of len bytes.
*
* Multiple stagings with the same (domain,key) are allowed; mocks
* return them in FIFO order.
*/
void mock_lockdownd_set_value(const char *domain, const char *key,
const char *data_type,
const void *data, size_t len);
/* Configure whether idevice_get_device_list returns a list with
* `count` fake UDID entries, or zero if count==0. The UDIDs returned
* are synthetic ("MOCKUDID0", "MOCKUDID1", ...). */
void mock_idevice_set_device_list(int count);
/* Force the next lockdownd_client_new_with_handshake call to fail
* with the given error code (default 0 = LOCKDOWN_E_SUCCESS). */
void mock_lockdownd_set_handshake_error(int error_code);
/* ------------------------------------------------------------------ */
/* libirecovery staging */
/* ------------------------------------------------------------------ */
/*
* Assert that the NEXT irecv_send_command call receives a string
* matching cmd_substring (substring match). If it does not, the
* call is recorded as a failure and the mock returns an error code.
* Only one expectation may be active at a time.
*/
void mock_irecv_expect_command(const char *cmd_substring);
/*
* Assert that the NEXT irecv_send_file call uploads a file. On the
* call, *bytes_sent_out is set to the number of bytes the SUT attempted
* to transfer. bytes_sent_out may be NULL if the test does not care.
*/
void mock_irecv_expect_file_upload(size_t *bytes_sent_out);
/*
* Configure the return value of irecv_setenv for a specific variable
* name (exact match). Call with var=NULL to set the default.
*/
void mock_irecv_set_setenv_result(const char *var, int error_code);
/*
* Configure the synthetic irecv_device_info returned by
* irecv_get_device_info. pid=0x1281 simulates recovery mode,
* pid=0x1227 simulates DFU mode, etc.
*/
void mock_irecv_set_device_info(uint32_t pid, uint64_t ecid,
const char *serial_string);
/*
* Force every subsequent irecv_open_* call to fail with the given error
* code. This is a sticky flag kept for backwards compatibility; new code
* should prefer mock_irecv_queue_open_error() for FIFO semantics.
*/
void mock_irecv_set_open_error(int error_code);
/*
* Push one more staged open error onto an 8-slot FIFO. Each call to
* irecv_open_with_ecid consumes one slot (or returns success if the FIFO
* is empty and no sticky flag is set).
*/
void mock_irecv_queue_open_error(int error_code);
/*
* Force the next irecv_send_file() call whose path contains
* path_needle (strstr match) to return error_code. Up to 8 independent
* stagings may be queued; each fires at most once.
*/
void mock_irecv_set_send_file_error(const char *path_needle, int error_code);
/* ------------------------------------------------------------------ */
/* libusb staging */
/* ------------------------------------------------------------------ */
/*
* Stage a response for the next libusb_control_transfer call.
* If data_len>0 and data!=NULL, the data is copied into the caller's
* buffer (for IN transfers). Return code is the value libusb returns.
*/
void mock_libusb_stage_control_transfer(int return_code,
const void *data, size_t data_len);
/*
* Configure libusb_get_device_list to return `count` synthetic devices
* with the given (vid,pid) pair.
*/
void mock_libusb_set_device_list(int count, uint16_t vid, uint16_t pid);
/* ------------------------------------------------------------------ */
/* libssh2 staging */
/* ------------------------------------------------------------------ */
/*
* Stage a canned response for the next libssh2_channel_exec call that
* contains command_substring. exit_code is what the channel reports,
* stdout_data is fed to libssh2_channel_read when the SUT reads it.
*
* stdout_data is duplicated; caller retains ownership.
*/
void mock_ssh_set_exec_response(const char *command_substring,
int exit_code,
const void *stdout_data, size_t stdout_len);
/*
* Capture the destination path and payload of the next SCP upload
* (libssh2_scp_send*). On the call:
* *path_out is set to a newly allocated string that the TEST must free
* *bytes_out is set to the number of bytes the SUT wrote
* Either argument may be NULL if the test does not need it.
*/
void mock_ssh_expect_scp(char **path_out, size_t *bytes_out);
/*
* Force the next libssh2_userauth_password call to fail (0=success,
* non-zero=error).
*/
void mock_ssh_set_auth_result(int error_code);
/*
* Force the next libssh2_scp_send*() call to return a NULL channel. The
* SUT's ssh_scp_upload helper treats that as a transport failure and
* returns -1. The flag is self-clearing after the next SCP attempt so
* tests that want to fail only one SCP out of several stay predictable.
*/
void mock_ssh_force_scp_error(int err);
/* ------------------------------------------------------------------ */
/* hardware stub staging (mock_hw_stubs.c) */
/* ------------------------------------------------------------------ */
/*
* Force the checkm8 stub to return the given value. Default is 0 (success).
* Pass -1 to simulate an exploit-stage failure.
*/
void mock_checkm8_set_exploit_result(int result);
/*
* Force the checkm8_verify_pwned stub to return the given value. Default
* is 1 (pwned). Pass 0 to simulate a non-pwned device after exploit; -1
* for a query error.
*/
void mock_checkm8_set_verify_pwned_result(int result);
/* ------------------------------------------------------------------ */
/* mobileactivation staging (mock_afc_mobileactivation.c) */
/* ------------------------------------------------------------------ */
/*
* Stage an error code to be returned by mobileactivation_activate() /
* mobileactivation_activate_with_session() on the next call. Used by
* Path B tests to simulate the SEP nonce barrier documented in
* 26.3-vulnerability.md. Pass 0 to clear.
*/
void mock_mobileactivation_set_activate_error(int error_code);
/*
* Stage an error code for mobileactivation_create_activation_info() /
* mobileactivation_create_activation_info_with_session(). Used to
* simulate server-side rejection of session handshake.
*/
void mock_mobileactivation_set_create_info_error(int error_code);
/*
* Stage the next activation state returned by
* mobileactivation_get_activation_state(). Default is "Activated".
* Pass NULL to reset to default.
*/
void mock_mobileactivation_set_state(const char *state);
/* ------------------------------------------------------------------ */
/* libcurl staging */
/* ------------------------------------------------------------------ */
/*
* Stage a response for any curl_easy_perform whose URL (set via
* CURLOPT_URL) contains url_substring. The SUT's write callback will
* be invoked with the staged body; CURLINFO_RESPONSE_CODE reports
* http_code. Body is duplicated.
*
* Multiple stagings may coexist; the first matching substring wins.
*/
void mock_curl_set_response(const char *url_substring, long http_code,
const void *body, size_t body_len);
/* Return number of times curl_easy_perform has been called. */
int mock_curl_get_perform_count(void);
/* Return the last URL passed to curl_easy_setopt(CURLOPT_URL). */
const char *mock_curl_get_last_url(void);
#ifdef __cplusplus
}
#endif
#endif /* TR4MPASS_MOCK_CONTROL_H */

101
tests/mocks/mock_core.c Normal file
View file

@ -0,0 +1,101 @@
/*
* mock_core.c -- Cross-cutting mock machinery: call log, reset, helpers.
*/
#include "mock_internal.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
mock_log_t g_mock_log;
/* ------------------------------------------------------------------ */
/* Call log */
/* ------------------------------------------------------------------ */
void mock_log_reset(void)
{
g_mock_log.count = 0;
/* No need to clear individual entries; count gates access. */
}
void mock_log_append(const char *fmt, ...)
{
va_list ap;
if (g_mock_log.count >= MOCK_LOG_MAX)
return;
va_start(ap, fmt);
vsnprintf(g_mock_log.entries[g_mock_log.count],
MOCK_LOG_ENTRY_LEN, fmt, ap);
va_end(ap);
g_mock_log.count++;
}
size_t mock_get_call_count(void)
{
return g_mock_log.count;
}
const char *mock_get_call_log(size_t index)
{
if (index >= g_mock_log.count)
return NULL;
return g_mock_log.entries[index];
}
int mock_call_log_contains(const char *needle)
{
size_t i;
if (!needle)
return 0;
for (i = 0; i < g_mock_log.count; i++) {
if (strstr(g_mock_log.entries[i], needle))
return 1;
}
return 0;
}
/* ------------------------------------------------------------------ */
/* Global reset */
/* ------------------------------------------------------------------ */
void mock_reset_all(void)
{
mock_log_reset();
mock_lockdownd_reset();
mock_irecv_reset();
mock_libusb_reset();
mock_ssh_reset();
mock_curl_reset();
mock_checkm8_reset();
mock_afc_mobileactivation_reset();
}
/* ------------------------------------------------------------------ */
/* Small helpers */
/* ------------------------------------------------------------------ */
void *mock_memdup(const void *src, size_t len)
{
void *copy;
if (!src || len == 0)
return NULL;
copy = malloc(len);
if (!copy)
return NULL;
memcpy(copy, src, len);
return copy;
}
char *mock_strdup_safe(const char *src)
{
if (!src)
return NULL;
return strdup(src);
}

162
tests/mocks/mock_hw_stubs.c Normal file
View file

@ -0,0 +1,162 @@
/*
* mock_hw_stubs.c -- Stubs for hardware-specific routines that the test
* binary does not include (everything under src/exploit and
* src/device/usb_dfu.c).
*
* The mock test build excludes those real implementations because they
* call directly into libusb with chip-specific offsets. Tests that
* drive Path A / Path B orchestration need *some* symbol to link
* against, so by default the stubs succeed quickly.
*/
#include "mock_internal.h"
#include "device/device.h"
#include "device/usb_dfu.h"
#include "exploit/checkm8.h"
#include "exploit/dfu_proto.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
/* Phase 3: let tests force checkm8_exploit / checkm8_verify_pwned to
* return specific values without faulting the rest of the pipeline. */
static int g_checkm8_exploit_result = 0; /* 0 = success */
static int g_checkm8_exploit_result_set = 0;
static int g_checkm8_verify_pwned_result = 1; /* 1 = pwned */
static int g_checkm8_verify_pwned_set = 0;
void mock_checkm8_set_exploit_result(int result)
{
g_checkm8_exploit_result = result;
g_checkm8_exploit_result_set = 1;
}
void mock_checkm8_set_verify_pwned_result(int result)
{
g_checkm8_verify_pwned_result = result;
g_checkm8_verify_pwned_set = 1;
}
void mock_checkm8_reset(void)
{
g_checkm8_exploit_result = 0;
g_checkm8_exploit_result_set = 0;
g_checkm8_verify_pwned_result = 1;
g_checkm8_verify_pwned_set = 0;
}
/* ------------------------------------------------------------------ */
/* src/exploit/checkm8.c */
/* ------------------------------------------------------------------ */
int checkm8_exploit(device_info_t *dev)
{
mock_log_append("checkm8_exploit");
if (!dev)
return -1;
if (g_checkm8_exploit_result_set)
return g_checkm8_exploit_result;
return 0;
}
int checkm8_verify_pwned(device_info_t *dev)
{
mock_log_append("checkm8_verify_pwned");
if (!dev)
return -1;
if (g_checkm8_verify_pwned_set)
return g_checkm8_verify_pwned_result;
return 1;
}
int checkm8_stage_reset(exploit_ctx_t *ctx) { (void)ctx; return 0; }
int checkm8_stage_setup(exploit_ctx_t *ctx) { (void)ctx; return 0; }
int checkm8_stage_spray(exploit_ctx_t *ctx) { (void)ctx; return 0; }
int checkm8_stage_patch(exploit_ctx_t *ctx) { (void)ctx; return 0; }
/* ------------------------------------------------------------------ */
/* src/device/usb_dfu.c */
/* ------------------------------------------------------------------ */
int usb_dfu_init(void) { return 0; }
void usb_dfu_cleanup(void) {}
int usb_dfu_find(libusb_device_handle **handle)
{
if (handle) *handle = NULL;
mock_log_append("usb_dfu_find");
return 0;
}
int usb_dfu_read_info(libusb_device_handle *handle, uint32_t *cpid,
uint64_t *ecid, char *serial, size_t serial_len)
{
(void)handle;
if (cpid) *cpid = 0x8015;
if (ecid) *ecid = 0x001A2B3C4D5E6F70ULL;
if (serial && serial_len > 0)
snprintf(serial, serial_len,
"CPID:8015 CPRV:11 BDID:0C ECID:001A2B3C4D5E6F70");
return 0;
}
int usb_dfu_send(libusb_device_handle *handle, const void *data, size_t len)
{
(void)handle; (void)data; (void)len;
return 0;
}
int usb_dfu_recv(libusb_device_handle *handle, void *buf, size_t len,
size_t *actual)
{
(void)handle; (void)buf; (void)len;
if (actual) *actual = 0;
return 0;
}
void usb_dfu_close(libusb_device_handle *handle)
{
(void)handle;
mock_log_append("usb_dfu_close");
}
/* ------------------------------------------------------------------ */
/* src/exploit/dfu_proto.c */
/* ------------------------------------------------------------------ */
int dfu_get_status(libusb_device_handle *dev, dfu_status_t *status)
{
(void)dev;
if (status) memset(status, 0, sizeof(*status));
return 0;
}
int dfu_clr_status(libusb_device_handle *dev)
{ (void)dev; return 0; }
int dfu_get_state(libusb_device_handle *dev, uint8_t *state)
{ (void)dev; if (state) *state = 0x02; return 0; }
int dfu_dnload(libusb_device_handle *dev, uint16_t block_num,
const void *data, size_t len)
{ (void)dev; (void)block_num; (void)data; (void)len; return 0; }
int dfu_upload(libusb_device_handle *dev, uint16_t block_num,
void *buf, size_t len, size_t *actual)
{
(void)dev; (void)block_num; (void)buf; (void)len;
if (actual) *actual = 0;
return 0;
}
int dfu_abort(libusb_device_handle *dev)
{ (void)dev; return 0; }
int dfu_send_data(libusb_device_handle *dev, const void *data, size_t len)
{ (void)dev; (void)data; (void)len; return 0; }
int dfu_reset_to_idle(libusb_device_handle *dev)
{ (void)dev; return 0; }

View file

@ -0,0 +1,57 @@
/*
* mock_internal.h -- Private state and helpers shared between mock_*.c files.
*
* Not part of the public staging API. Only mock_*.c should include this.
*/
#ifndef TR4MPASS_MOCK_INTERNAL_H
#define TR4MPASS_MOCK_INTERNAL_H
#include "mock_control.h"
#include <stddef.h>
#include <stdint.h>
/* Number of entries in each staging table. Tests typically stage only a
* handful of values; 32 is a comfortable upper bound. */
#define MOCK_STAGE_SLOTS 32
/* Maximum length of a single call-log entry after formatting. */
#define MOCK_LOG_ENTRY_LEN 256
/* ------------------------------------------------------------------ */
/* Shared call log */
/* ------------------------------------------------------------------ */
typedef struct {
char entries[MOCK_LOG_MAX][MOCK_LOG_ENTRY_LEN];
size_t count;
} mock_log_t;
extern mock_log_t g_mock_log;
void mock_log_reset(void);
/* ------------------------------------------------------------------ */
/* Per-subsystem reset hooks. Each mock_*.c implements one of these. */
/* ------------------------------------------------------------------ */
void mock_lockdownd_reset(void);
void mock_irecv_reset(void);
void mock_libusb_reset(void);
void mock_ssh_reset(void);
void mock_curl_reset(void);
void mock_checkm8_reset(void);
void mock_afc_mobileactivation_reset(void);
/* ------------------------------------------------------------------ */
/* Tiny helpers */
/* ------------------------------------------------------------------ */
/* Duplicate a blob; caller frees. Returns NULL on OOM or if src==NULL. */
void *mock_memdup(const void *src, size_t len);
/* Safe strdup wrapper returning NULL if src is NULL. */
char *mock_strdup_safe(const char *src);
#endif /* TR4MPASS_MOCK_INTERNAL_H */

View file

@ -0,0 +1,155 @@
/*
* mock_irecv_ctl.c -- Staging + reset for the libirecovery mock.
*/
#include "mock_irecv_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char g_irecv_cmd_expect[256];
int g_irecv_cmd_expect_active;
size_t *g_irecv_file_upload_bytes_out;
int g_irecv_file_upload_active;
mock_irecv_setenv_result_t g_irecv_setenv_results[MOCK_SETENV_SLOTS];
int g_irecv_setenv_default_error;
int g_irecv_open_error;
irecv_open_error_slot_t g_irecv_open_error_slots[IRECV_OPEN_ERROR_SLOTS];
irecv_send_file_stage_t g_irecv_send_file_stages[IRECV_SEND_FILE_STAGES];
uint32_t g_irecv_info_pid = 0x1281;
uint64_t g_irecv_info_ecid;
char g_irecv_info_serial[128] =
"CPID:8020 CPRV:11 ECID:0012345678ABCDEF";
void mock_irecv_reset(void)
{
int i;
g_irecv_cmd_expect[0] = '\0';
g_irecv_cmd_expect_active = 0;
g_irecv_file_upload_bytes_out = NULL;
g_irecv_file_upload_active = 0;
for (i = 0; i < MOCK_SETENV_SLOTS; i++)
memset(&g_irecv_setenv_results[i], 0,
sizeof(g_irecv_setenv_results[i]));
g_irecv_setenv_default_error = 0;
g_irecv_open_error = 0;
for (i = 0; i < IRECV_OPEN_ERROR_SLOTS; i++)
memset(&g_irecv_open_error_slots[i], 0,
sizeof(g_irecv_open_error_slots[i]));
for (i = 0; i < IRECV_SEND_FILE_STAGES; i++)
memset(&g_irecv_send_file_stages[i], 0,
sizeof(g_irecv_send_file_stages[i]));
g_irecv_info_pid = 0x1281;
g_irecv_info_ecid = 0;
snprintf(g_irecv_info_serial, sizeof(g_irecv_info_serial), "%s",
"CPID:8020 CPRV:11 ECID:0012345678ABCDEF");
}
void mock_irecv_expect_command(const char *cmd_substring)
{
if (!cmd_substring) {
g_irecv_cmd_expect_active = 0;
return;
}
snprintf(g_irecv_cmd_expect, sizeof(g_irecv_cmd_expect), "%s",
cmd_substring);
g_irecv_cmd_expect_active = 1;
}
void mock_irecv_expect_file_upload(size_t *bytes_sent_out)
{
g_irecv_file_upload_bytes_out = bytes_sent_out;
g_irecv_file_upload_active = 1;
}
void mock_irecv_set_setenv_result(const char *var, int error_code)
{
int i;
if (!var) {
g_irecv_setenv_default_error = error_code;
return;
}
for (i = 0; i < MOCK_SETENV_SLOTS; i++) {
if (!g_irecv_setenv_results[i].in_use) {
snprintf(g_irecv_setenv_results[i].var,
sizeof(g_irecv_setenv_results[i].var), "%s", var);
g_irecv_setenv_results[i].error = error_code;
g_irecv_setenv_results[i].in_use = 1;
return;
}
}
}
void mock_irecv_set_device_info(uint32_t pid, uint64_t ecid,
const char *serial_string)
{
g_irecv_info_pid = pid;
g_irecv_info_ecid = ecid;
snprintf(g_irecv_info_serial, sizeof(g_irecv_info_serial), "%s",
serial_string ? serial_string : "");
}
/*
* mock_irecv_set_open_error -- backwards-compatible sticky open error.
* Also queues a single entry in the FIFO so new-style callers still see
* an error on the very first call.
*/
void mock_irecv_set_open_error(int error_code)
{
g_irecv_open_error = error_code;
if (error_code != 0) {
int i;
for (i = 0; i < IRECV_OPEN_ERROR_SLOTS; i++) {
if (!g_irecv_open_error_slots[i].in_use) {
g_irecv_open_error_slots[i].error_code = error_code;
g_irecv_open_error_slots[i].in_use = 1;
g_irecv_open_error_slots[i].consumed = 0;
return;
}
}
}
}
/*
* mock_irecv_queue_open_error -- push one more staged open error onto
* the FIFO. Each irecv_open_with_ecid call consumes one slot; once the
* FIFO is empty the call succeeds normally.
*/
void mock_irecv_queue_open_error(int error_code)
{
int i;
if (error_code == 0)
return;
for (i = 0; i < IRECV_OPEN_ERROR_SLOTS; i++) {
if (!g_irecv_open_error_slots[i].in_use) {
g_irecv_open_error_slots[i].error_code = error_code;
g_irecv_open_error_slots[i].in_use = 1;
g_irecv_open_error_slots[i].consumed = 0;
return;
}
}
}
/*
* mock_irecv_set_send_file_error -- queue an error for the next unconsumed
* irecv_send_file() call whose path contains path_needle. Matches are
* FIFO by slot order; each slot fires at most once.
*/
void mock_irecv_set_send_file_error(const char *path_needle, int error_code)
{
int i;
if (!path_needle || error_code == 0)
return;
for (i = 0; i < IRECV_SEND_FILE_STAGES; i++) {
if (!g_irecv_send_file_stages[i].in_use) {
snprintf(g_irecv_send_file_stages[i].path_needle,
sizeof(g_irecv_send_file_stages[i].path_needle),
"%s", path_needle);
g_irecv_send_file_stages[i].error_code = error_code;
g_irecv_send_file_stages[i].in_use = 1;
g_irecv_send_file_stages[i].consumed = 0;
return;
}
}
}

View file

@ -0,0 +1,66 @@
/*
* mock_irecv_internal.h -- Shared state for libirecovery mock.
*/
#ifndef TR4MPASS_MOCK_IRECV_INTERNAL_H
#define TR4MPASS_MOCK_IRECV_INTERNAL_H
#include "mock_internal.h"
#include <stdint.h>
#include <stddef.h>
#include <libirecovery.h>
struct irecv_client_private {
uint32_t magic;
struct irecv_device_info info;
char serial_buf[128];
};
#define IRECV_MAGIC 0x49525256u /* "IRRV" */
#define MOCK_SETENV_SLOTS 16
typedef struct {
char var[64];
int error;
int in_use;
} mock_irecv_setenv_result_t;
/* Phase 3: per-path staged errors for irecv_send_file().
* Each slot matches via strstr() on the path argument; the first unconsumed
* match wins and the slot is marked consumed. */
#define IRECV_SEND_FILE_STAGES 8
typedef struct {
char path_needle[128]; /* match via strstr() against the filename */
int error_code;
int in_use;
int consumed;
} irecv_send_file_stage_t;
/* Phase 3: FIFO of staged irecv_open errors. Each call to
* irecv_open_with_ecid consumes one slot (or returns success if empty). */
#define IRECV_OPEN_ERROR_SLOTS 8
typedef struct {
int error_code;
int in_use;
int consumed;
} irecv_open_error_slot_t;
extern char g_irecv_cmd_expect[256];
extern int g_irecv_cmd_expect_active;
extern size_t *g_irecv_file_upload_bytes_out;
extern int g_irecv_file_upload_active;
extern mock_irecv_setenv_result_t g_irecv_setenv_results[MOCK_SETENV_SLOTS];
extern int g_irecv_setenv_default_error;
extern int g_irecv_open_error; /* backwards-compat sticky flag */
extern irecv_open_error_slot_t g_irecv_open_error_slots[IRECV_OPEN_ERROR_SLOTS];
extern irecv_send_file_stage_t g_irecv_send_file_stages[IRECV_SEND_FILE_STAGES];
extern uint32_t g_irecv_info_pid;
extern uint64_t g_irecv_info_ecid;
extern char g_irecv_info_serial[128];
#endif /* TR4MPASS_MOCK_IRECV_INTERNAL_H */

View file

@ -0,0 +1,227 @@
/*
* mock_irecv_sym.c -- Interposed libirecovery entry points.
*/
#include "mock_irecv_internal.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
const char *irecv_strerror(irecv_error_t error)
{
switch ((int)error) {
case 0: return "Success";
case -1: return "No device found";
case -2: return "Out of memory";
case -3: return "Unable to connect";
default: return "Mock irecovery error";
}
}
const char *irecv_version(void)
{
return "mock-1.3.1";
}
void irecv_set_debug_level(int level)
{
(void)level;
}
irecv_error_t irecv_open_with_ecid(irecv_client_t *client, uint64_t ecid)
{
struct irecv_client_private *c;
int i;
mock_log_append("irecv_open_with_ecid(ecid=0x%llx)",
(unsigned long long)ecid);
if (!client)
return (irecv_error_t)-1;
/* FIFO of staged open errors -- consume one per call. */
for (i = 0; i < IRECV_OPEN_ERROR_SLOTS; i++) {
irecv_open_error_slot_t *s = &g_irecv_open_error_slots[i];
if (s->in_use && !s->consumed) {
s->consumed = 1;
*client = NULL;
return (irecv_error_t)s->error_code;
}
}
/* Sticky flag (backwards compat) still takes effect when FIFO is
* empty. Tests that called mock_irecv_set_open_error() continue to
* see errors on every subsequent call. */
if (g_irecv_open_error != 0) {
*client = NULL;
return (irecv_error_t)g_irecv_open_error;
}
c = calloc(1, sizeof(*c));
if (!c)
return (irecv_error_t)-2;
c->magic = IRECV_MAGIC;
c->info.pid = (uint16_t)g_irecv_info_pid;
c->info.ecid = ecid != 0 ? ecid : g_irecv_info_ecid;
c->info.cpid = 0x8020;
c->info.have_ecid = 1;
c->info.have_cpid = 1;
snprintf(c->serial_buf, sizeof(c->serial_buf), "%s",
g_irecv_info_serial);
c->info.serial_string = c->serial_buf;
*client = c;
return (irecv_error_t)0;
}
irecv_error_t irecv_open_with_ecid_and_attempts(irecv_client_t *client,
uint64_t ecid, int attempts)
{
(void)attempts;
return irecv_open_with_ecid(client, ecid);
}
irecv_error_t irecv_close(irecv_client_t client)
{
mock_log_append("irecv_close");
if (!client)
return (irecv_error_t)0;
client->magic = 0;
free(client);
return (irecv_error_t)0;
}
irecv_error_t irecv_reset(irecv_client_t client)
{
(void)client;
mock_log_append("irecv_reset");
return (irecv_error_t)0;
}
irecv_client_t irecv_reconnect(irecv_client_t client, int initial_pause)
{
(void)initial_pause;
mock_log_append("irecv_reconnect");
return client;
}
const struct irecv_device_info *irecv_get_device_info(irecv_client_t client)
{
mock_log_append("irecv_get_device_info");
if (!client)
return NULL;
return &client->info;
}
irecv_error_t irecv_send_command(irecv_client_t client, const char *command)
{
(void)client;
mock_log_append("irecv_send_command(%s)",
command ? command : "(null)");
if (g_irecv_cmd_expect_active) {
g_irecv_cmd_expect_active = 0;
if (!command || !strstr(command, g_irecv_cmd_expect)) {
mock_log_append("EXPECT-FAIL: expected command containing '%s'",
g_irecv_cmd_expect);
return (irecv_error_t)-1;
}
}
return (irecv_error_t)0;
}
irecv_error_t irecv_send_command_breq(irecv_client_t client,
const char *command, uint8_t b_request)
{
(void)b_request;
return irecv_send_command(client, command);
}
irecv_error_t irecv_send_file(irecv_client_t client, const char *filename,
unsigned int options)
{
struct stat st;
size_t size = 0;
int i;
(void)client; (void)options;
if (filename && stat(filename, &st) == 0)
size = (size_t)st.st_size;
mock_log_append("irecv_send_file(%s,%zu)",
filename ? filename : "(null)", size);
/* Phase 3: per-path staged errors take priority. Each slot fires at
* most once; path_needle is matched via strstr() on the filename. */
if (filename) {
for (i = 0; i < IRECV_SEND_FILE_STAGES; i++) {
irecv_send_file_stage_t *s = &g_irecv_send_file_stages[i];
if (!s->in_use || s->consumed)
continue;
if (s->path_needle[0] == '\0')
continue;
if (strstr(filename, s->path_needle) != NULL) {
s->consumed = 1;
return (irecv_error_t)s->error_code;
}
}
}
if (g_irecv_file_upload_active) {
g_irecv_file_upload_active = 0;
if (g_irecv_file_upload_bytes_out)
*g_irecv_file_upload_bytes_out = size;
}
return (irecv_error_t)0;
}
irecv_error_t irecv_setenv(irecv_client_t client, const char *variable,
const char *value)
{
int i;
(void)client;
mock_log_append("irecv_setenv(%s=%s)",
variable ? variable : "(null)",
value ? value : "(null)");
if (variable) {
for (i = 0; i < MOCK_SETENV_SLOTS; i++) {
if (g_irecv_setenv_results[i].in_use &&
strcmp(g_irecv_setenv_results[i].var, variable) == 0) {
return (irecv_error_t)g_irecv_setenv_results[i].error;
}
}
}
return (irecv_error_t)g_irecv_setenv_default_error;
}
irecv_error_t irecv_setenv_np(irecv_client_t client, const char *variable,
const char *value)
{
return irecv_setenv(client, variable, value);
}
irecv_error_t irecv_getenv(irecv_client_t client, const char *variable,
char **value)
{
(void)client;
mock_log_append("irecv_getenv(%s)", variable ? variable : "(null)");
if (value)
*value = strdup("mockvalue");
return (irecv_error_t)0;
}
irecv_error_t irecv_saveenv(irecv_client_t client)
{
(void)client;
mock_log_append("irecv_saveenv");
return (irecv_error_t)0;
}
irecv_error_t irecv_reboot(irecv_client_t client)
{
(void)client;
mock_log_append("irecv_reboot");
return (irecv_error_t)0;
}

300
tests/mocks/mock_libcurl.c Normal file
View file

@ -0,0 +1,300 @@
/*
* mock_libcurl.c -- Link-time interposition of libcurl easy interface.
*
* The SUT uses curl for Albert activation server requests. This mock
* captures the URL + body passed via curl_easy_setopt, then on
* curl_easy_perform it invokes the write callback with any staged
* response body for matching URLs. CURLINFO_RESPONSE_CODE reports the
* staged HTTP status.
*/
#include "mock_internal.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <curl/curl.h>
/* Type alias for the write callback signature used by curl. */
typedef size_t (*curl_write_cb_t)(void *ptr, size_t size, size_t nmemb,
void *userdata);
/* ------------------------------------------------------------------ */
/* Curl handle */
/* ------------------------------------------------------------------ */
struct mock_curl_easy {
uint32_t magic;
char url[1024];
curl_write_cb_t write_cb;
void *write_data;
long last_http_code;
};
#define CURL_MAGIC 0x43524C45u /* "CRLE" */
/* Note: the public type is `CURL` which on the real libcurl is a void
* alias. We redeclare via typedef so our internal struct is legal
* without conflicting. The caller never dereferences it. */
/* ------------------------------------------------------------------ */
/* Staging */
/* ------------------------------------------------------------------ */
#define CURL_STAGE_SLOTS 16
typedef struct {
int in_use;
char substr[512];
long http_code;
void *body;
size_t body_len;
} curl_stage_t;
static curl_stage_t g_curl_stages[CURL_STAGE_SLOTS];
static int g_perform_count;
static char g_last_url[1024];
void mock_curl_reset(void)
{
int i;
for (i = 0; i < CURL_STAGE_SLOTS; i++) {
free(g_curl_stages[i].body);
memset(&g_curl_stages[i], 0, sizeof(g_curl_stages[i]));
}
g_perform_count = 0;
g_last_url[0] = '\0';
}
void mock_curl_set_response(const char *url_substring, long http_code,
const void *body, size_t body_len)
{
int i;
if (!url_substring)
return;
for (i = 0; i < CURL_STAGE_SLOTS; i++) {
if (!g_curl_stages[i].in_use) {
curl_stage_t *s = &g_curl_stages[i];
snprintf(s->substr, sizeof(s->substr), "%s", url_substring);
s->http_code = http_code;
s->body = mock_memdup(body, body_len);
s->body_len = body_len;
s->in_use = 1;
return;
}
}
}
int mock_curl_get_perform_count(void)
{
return g_perform_count;
}
const char *mock_curl_get_last_url(void)
{
return g_last_url;
}
/* ------------------------------------------------------------------ */
/* curl_easy_* */
/* ------------------------------------------------------------------ */
CURLcode curl_global_init(long flags)
{
(void)flags;
return CURLE_OK;
}
CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
curl_free_callback f,
curl_realloc_callback r,
curl_strdup_callback s,
curl_calloc_callback c)
{
(void)flags; (void)m; (void)f; (void)r; (void)s; (void)c;
return CURLE_OK;
}
void curl_global_cleanup(void) {}
CURL *curl_easy_init(void)
{
struct mock_curl_easy *e;
mock_log_append("curl_easy_init");
e = calloc(1, sizeof(*e));
if (!e) return NULL;
e->magic = CURL_MAGIC;
return (CURL *)e;
}
void curl_easy_cleanup(CURL *handle)
{
struct mock_curl_easy *e = (struct mock_curl_easy *)handle;
mock_log_append("curl_easy_cleanup");
if (!e) return;
e->magic = 0;
free(e);
}
CURLcode curl_easy_setopt(CURL *handle, CURLoption option, ...)
{
struct mock_curl_easy *e = (struct mock_curl_easy *)handle;
va_list ap;
if (!e)
return CURLE_FAILED_INIT;
va_start(ap, option);
switch (option) {
case CURLOPT_URL: {
const char *url = va_arg(ap, const char *);
if (url) {
snprintf(e->url, sizeof(e->url), "%s", url);
snprintf(g_last_url, sizeof(g_last_url), "%s", url);
mock_log_append("curl_easy_setopt(URL=%s)", url);
}
break;
}
case CURLOPT_WRITEFUNCTION: {
e->write_cb = va_arg(ap, curl_write_cb_t);
break;
}
case CURLOPT_WRITEDATA: {
e->write_data = va_arg(ap, void *);
break;
}
default:
/* All other options are discarded; we don't need them. */
break;
}
va_end(ap);
return CURLE_OK;
}
static curl_stage_t *find_curl_stage(const char *url)
{
int i;
if (!url)
return NULL;
for (i = 0; i < CURL_STAGE_SLOTS; i++) {
if (g_curl_stages[i].in_use &&
strstr(url, g_curl_stages[i].substr))
return &g_curl_stages[i];
}
return NULL;
}
CURLcode curl_easy_perform(CURL *handle)
{
struct mock_curl_easy *e = (struct mock_curl_easy *)handle;
curl_stage_t *stage;
if (!e)
return CURLE_FAILED_INIT;
g_perform_count++;
mock_log_append("curl_easy_perform(url=%s)", e->url);
stage = find_curl_stage(e->url);
if (!stage) {
/* No staging: respond with empty 200. */
e->last_http_code = 200;
return CURLE_OK;
}
e->last_http_code = stage->http_code;
if (e->write_cb && stage->body && stage->body_len > 0) {
e->write_cb(stage->body, 1, stage->body_len, e->write_data);
}
return CURLE_OK;
}
CURLcode curl_easy_getinfo(CURL *handle, CURLINFO info, ...)
{
struct mock_curl_easy *e = (struct mock_curl_easy *)handle;
va_list ap;
va_start(ap, info);
if (e && info == CURLINFO_RESPONSE_CODE) {
long *p = va_arg(ap, long *);
if (p) *p = e->last_http_code;
}
va_end(ap);
return CURLE_OK;
}
const char *curl_easy_strerror(CURLcode code)
{
switch (code) {
case CURLE_OK: return "No error";
case CURLE_FAILED_INIT: return "Failed initialization";
default: return "Mock curl error";
}
}
char *curl_easy_escape(CURL *handle, const char *string, int length)
{
size_t n;
char *buf;
(void)handle;
if (!string)
return NULL;
n = length > 0 ? (size_t)length : strlen(string);
buf = malloc(n * 3 + 1); /* worst case: %HH */
if (!buf) return NULL;
/* Trivial percent-encoding of non-alphanumerics. Adequate for tests. */
{
size_t i, o = 0;
static const char hex[] = "0123456789ABCDEF";
for (i = 0; i < n; i++) {
unsigned char c = (unsigned char)string[i];
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') || c == '-' || c == '_' ||
c == '.' || c == '~') {
buf[o++] = (char)c;
} else {
buf[o++] = '%';
buf[o++] = hex[c >> 4];
buf[o++] = hex[c & 0xF];
}
}
buf[o] = '\0';
}
return buf;
}
void curl_free(void *p) { free(p); }
/* ------------------------------------------------------------------ */
/* curl_slist_* */
/* ------------------------------------------------------------------ */
struct curl_slist *curl_slist_append(struct curl_slist *list,
const char *value)
{
struct curl_slist *node = calloc(1, sizeof(*node));
if (!node) return list;
node->data = mock_strdup_safe(value);
node->next = NULL;
if (!list) return node;
{
struct curl_slist *p = list;
while (p->next) p = p->next;
p->next = node;
}
return list;
}
void curl_slist_free_all(struct curl_slist *list)
{
while (list) {
struct curl_slist *next = list->next;
free(list->data);
free(list);
list = next;
}
}

View file

@ -0,0 +1,75 @@
/*
* mock_libssh2_ctl.c -- Staging / reset / state for the libssh2 mock.
* The mocked libssh2 entry points live in mock_libssh2_sym.c.
*/
#include "mock_libssh2_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
mock_ssh_exec_stage_t g_ssh_exec_stages[SSH_EXEC_SLOTS];
char **g_ssh_scp_path_out;
size_t *g_ssh_scp_bytes_out;
int g_ssh_scp_expect_active;
int g_ssh_auth_result;
int g_ssh_scp_force_error;
void mock_ssh_reset(void)
{
int i;
for (i = 0; i < SSH_EXEC_SLOTS; i++) {
free(g_ssh_exec_stages[i].stdout_data);
memset(&g_ssh_exec_stages[i], 0, sizeof(g_ssh_exec_stages[i]));
}
g_ssh_scp_path_out = NULL;
g_ssh_scp_bytes_out = NULL;
g_ssh_scp_expect_active = 0;
g_ssh_auth_result = 0;
g_ssh_scp_force_error = 0;
}
void mock_ssh_set_exec_response(const char *command_substring,
int exit_code,
const void *stdout_data, size_t stdout_len)
{
int i;
if (!command_substring)
return;
for (i = 0; i < SSH_EXEC_SLOTS; i++) {
if (!g_ssh_exec_stages[i].in_use) {
mock_ssh_exec_stage_t *s = &g_ssh_exec_stages[i];
snprintf(s->substr, sizeof(s->substr), "%s", command_substring);
s->exit_code = exit_code;
s->stdout_data = mock_memdup(stdout_data, stdout_len);
s->stdout_len = stdout_len;
s->in_use = 1;
s->consumed = 0;
return;
}
}
}
void mock_ssh_expect_scp(char **path_out, size_t *bytes_out)
{
g_ssh_scp_path_out = path_out;
g_ssh_scp_bytes_out = bytes_out;
g_ssh_scp_expect_active = 1;
}
void mock_ssh_set_auth_result(int error_code)
{
g_ssh_auth_result = error_code;
}
/*
* mock_ssh_force_scp_error -- on the next SCP send call, the mock returns
* a NULL channel simulating a remote-side refusal. The SUT's
* ssh_scp_upload helper translates that into a -1 return. The flag is
* cleared after the first SCP call; tests can re-arm for multiple fails.
*/
void mock_ssh_force_scp_error(int err)
{
g_ssh_scp_force_error = (err != 0) ? 1 : 0;
}

View file

@ -0,0 +1,68 @@
/*
* mock_libssh2_internal.h -- Private types shared between mock_libssh2_*.c
* files. Split out to keep each source file under 300 lines.
*/
#ifndef TR4MPASS_MOCK_LIBSSH2_INTERNAL_H
#define TR4MPASS_MOCK_LIBSSH2_INTERNAL_H
#include "mock_internal.h"
#include <stdint.h>
#include <stddef.h>
#include <libssh2.h>
#define SSH_SESSION_MAGIC 0x53534853u /* "SSHS" */
#define SSH_CHANNEL_MAGIC 0x53534843u /* "SSHC" */
struct _LIBSSH2_SESSION {
uint32_t magic;
int last_error;
int scp_mode;
char scp_path[512];
size_t scp_expected;
};
struct _LIBSSH2_CHANNEL {
uint32_t magic;
LIBSSH2_SESSION *session;
/* Canned exec response */
int exec_pending;
char command[512];
int exit_code;
void *stdout_data;
size_t stdout_len;
size_t stdout_pos;
/* SCP state */
int scp_mode;
char scp_path[512];
size_t scp_bytes_written;
};
#define SSH_EXEC_SLOTS 16
typedef struct {
int in_use;
int consumed;
char substr[256];
int exit_code;
void *stdout_data;
size_t stdout_len;
} mock_ssh_exec_stage_t;
/* Shared state: defined in mock_libssh2_ctl.c, referenced from _sym.c. */
extern mock_ssh_exec_stage_t g_ssh_exec_stages[SSH_EXEC_SLOTS];
extern char **g_ssh_scp_path_out;
extern size_t *g_ssh_scp_bytes_out;
extern int g_ssh_scp_expect_active;
extern int g_ssh_auth_result;
/* Phase 3: when non-zero, the next libssh2_scp_send*() call returns NULL
* (simulating a mid-stream SCP failure). Consumed by the first call so
* subsequent SCP attempts succeed unless re-armed. */
extern int g_ssh_scp_force_error;
#endif /* TR4MPASS_MOCK_LIBSSH2_INTERNAL_H */

View file

@ -0,0 +1,297 @@
/*
* mock_libssh2_sym.c -- Interposed libssh2 entry points.
*
* libssh2_channel_exec and libssh2_scp_send are preprocessor macros
* that expand to libssh2_channel_process_startup / libssh2_scp_send_ex
* respectively; those are what we actually define here.
*/
#include "mock_libssh2_internal.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
/* ------------------------------------------------------------------ */
/* Session */
/* ------------------------------------------------------------------ */
int libssh2_init(int flags)
{
(void)flags;
mock_log_append("libssh2_init");
return 0;
}
void libssh2_exit(void)
{
mock_log_append("libssh2_exit");
}
LIBSSH2_SESSION *libssh2_session_init_ex(LIBSSH2_ALLOC_FUNC((*my_alloc)),
LIBSSH2_FREE_FUNC((*my_free)),
LIBSSH2_REALLOC_FUNC((*my_realloc)),
void *abstract)
{
LIBSSH2_SESSION *s;
(void)my_alloc; (void)my_free; (void)my_realloc; (void)abstract;
mock_log_append("libssh2_session_init");
s = calloc(1, sizeof(*s));
if (s)
s->magic = SSH_SESSION_MAGIC;
return s;
}
int libssh2_session_handshake(LIBSSH2_SESSION *session, libssh2_socket_t sock)
{
(void)session; (void)sock;
mock_log_append("libssh2_session_handshake");
return 0;
}
void libssh2_session_set_blocking(LIBSSH2_SESSION *session, int blocking)
{
(void)session; (void)blocking;
}
int libssh2_session_disconnect_ex(LIBSSH2_SESSION *session, int reason,
const char *description, const char *lang)
{
(void)session; (void)reason; (void)lang;
mock_log_append("libssh2_session_disconnect(%s)",
description ? description : "");
return 0;
}
int libssh2_session_free(LIBSSH2_SESSION *session)
{
mock_log_append("libssh2_session_free");
if (!session)
return 0;
session->magic = 0;
free(session);
return 0;
}
int libssh2_session_last_error(LIBSSH2_SESSION *session, char **errmsg,
int *errmsg_len, int want_buf)
{
(void)want_buf;
if (errmsg)
*errmsg = (char *)"mock-last-error";
if (errmsg_len)
*errmsg_len = (int)strlen("mock-last-error");
return session ? session->last_error : 0;
}
/* ------------------------------------------------------------------ */
/* Authentication */
/* ------------------------------------------------------------------ */
int libssh2_userauth_password_ex(LIBSSH2_SESSION *session,
const char *username, unsigned int ulen,
const char *password, unsigned int plen,
LIBSSH2_PASSWD_CHANGEREQ_FUNC((*cb)))
{
(void)session; (void)ulen; (void)plen; (void)cb;
mock_log_append("libssh2_userauth_password(%s:%.4s...)",
username ? username : "",
password ? password : "");
return g_ssh_auth_result;
}
int libssh2_userauth_publickey_fromfile_ex(LIBSSH2_SESSION *session,
const char *username,
unsigned int ulen,
const char *publickey,
const char *privatekey,
const char *passphrase)
{
(void)session; (void)ulen; (void)publickey; (void)privatekey;
(void)passphrase;
mock_log_append("libssh2_userauth_publickey_fromfile(%s)",
username ? username : "");
return g_ssh_auth_result;
}
int libssh2_userauth_authenticated(LIBSSH2_SESSION *session)
{
(void)session;
return g_ssh_auth_result == 0 ? 1 : 0;
}
/* ------------------------------------------------------------------ */
/* Channels */
/* ------------------------------------------------------------------ */
LIBSSH2_CHANNEL *libssh2_channel_open_ex(LIBSSH2_SESSION *session,
const char *channel_type,
unsigned int ct_len,
unsigned int window_size,
unsigned int packet_size,
const char *message,
unsigned int msg_len)
{
LIBSSH2_CHANNEL *c;
(void)ct_len; (void)window_size; (void)packet_size;
(void)message; (void)msg_len;
mock_log_append("libssh2_channel_open(%s)",
channel_type ? channel_type : "");
c = calloc(1, sizeof(*c));
if (!c)
return NULL;
c->magic = SSH_CHANNEL_MAGIC;
c->session = session;
return c;
}
int libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
const char *request,
unsigned int rlen,
const char *message,
unsigned int mlen)
{
int i;
(void)rlen;
mock_log_append("libssh2_channel_process_startup(%s,msg=%.*s)",
request ? request : "",
(int)(mlen < 128 ? mlen : 128),
message ? message : "");
if (!channel || !request || strcmp(request, "exec") != 0 || !message)
return 0;
if (mlen >= sizeof(channel->command))
mlen = sizeof(channel->command) - 1;
memcpy(channel->command, message, mlen);
channel->command[mlen] = '\0';
channel->exec_pending = 1;
channel->exit_code = 0;
for (i = 0; i < SSH_EXEC_SLOTS; i++) {
mock_ssh_exec_stage_t *s = &g_ssh_exec_stages[i];
if (!s->in_use || s->consumed)
continue;
if (strstr(channel->command, s->substr)) {
s->consumed = 1;
channel->exit_code = s->exit_code;
channel->stdout_data = s->stdout_data;
channel->stdout_len = s->stdout_len;
channel->stdout_pos = 0;
s->stdout_data = NULL; /* ownership transferred */
s->stdout_len = 0;
break;
}
}
return 0;
}
ssize_t libssh2_channel_read_ex(LIBSSH2_CHANNEL *channel, int stream_id,
char *buf, size_t buflen)
{
size_t remaining;
size_t copy;
(void)stream_id;
if (!channel || !buf)
return -1;
if (!channel->stdout_data || channel->stdout_pos >= channel->stdout_len)
return 0;
remaining = channel->stdout_len - channel->stdout_pos;
copy = remaining < buflen ? remaining : buflen;
memcpy(buf, (uint8_t *)channel->stdout_data + channel->stdout_pos, copy);
channel->stdout_pos += copy;
return (ssize_t)copy;
}
ssize_t libssh2_channel_write_ex(LIBSSH2_CHANNEL *channel, int stream_id,
const char *buf, size_t buflen)
{
(void)stream_id; (void)buf;
if (!channel)
return -1;
if (channel->scp_mode)
channel->scp_bytes_written += buflen;
return (ssize_t)buflen;
}
int libssh2_channel_send_eof(LIBSSH2_CHANNEL *c) { (void)c; return 0; }
int libssh2_channel_wait_eof(LIBSSH2_CHANNEL *c) { (void)c; return 0; }
int libssh2_channel_close(LIBSSH2_CHANNEL *c) { (void)c; return 0; }
int libssh2_channel_wait_closed(LIBSSH2_CHANNEL *c) { (void)c; return 0; }
int libssh2_channel_get_exit_status(LIBSSH2_CHANNEL *c)
{
return c ? c->exit_code : -1;
}
int libssh2_channel_free(LIBSSH2_CHANNEL *c)
{
mock_log_append("libssh2_channel_free");
if (!c)
return 0;
if (c->scp_mode && g_ssh_scp_expect_active) {
g_ssh_scp_expect_active = 0;
if (g_ssh_scp_path_out)
*g_ssh_scp_path_out = strdup(c->scp_path);
if (g_ssh_scp_bytes_out)
*g_ssh_scp_bytes_out = c->scp_bytes_written;
}
free(c->stdout_data);
c->magic = 0;
free(c);
return 0;
}
int libssh2_channel_eof(LIBSSH2_CHANNEL *c)
{
if (!c) return 1;
if (!c->stdout_data) return 1;
return c->stdout_pos >= c->stdout_len ? 1 : 0;
}
/* ------------------------------------------------------------------ */
/* SCP upload */
/* ------------------------------------------------------------------ */
LIBSSH2_CHANNEL *libssh2_scp_send_ex(LIBSSH2_SESSION *session,
const char *path, int mode,
size_t size, long mtime, long atime)
{
LIBSSH2_CHANNEL *c;
(void)mode; (void)mtime; (void)atime;
mock_log_append("libssh2_scp_send(path=%s,size=%zu)",
path ? path : "", size);
/* Phase 3: tests can force SCP to fail at channel open. */
if (g_ssh_scp_force_error) {
g_ssh_scp_force_error = 0;
mock_log_append("libssh2_scp_send: forced error (NULL channel)");
return NULL;
}
c = calloc(1, sizeof(*c));
if (!c) return NULL;
c->magic = SSH_CHANNEL_MAGIC;
c->session = session;
c->scp_mode = 1;
if (path)
snprintf(c->scp_path, sizeof(c->scp_path), "%s", path);
return c;
}
LIBSSH2_CHANNEL *libssh2_scp_send64(LIBSSH2_SESSION *session, const char *path,
int mode, libssh2_int64_t size,
time_t mtime, time_t atime)
{
return libssh2_scp_send_ex(session, path, mode, (size_t)size,
(long)mtime, (long)atime);
}

View file

@ -0,0 +1,50 @@
/*
* mock_libusb_ctl.c -- Staging + reset for the libusb mock.
*/
#include "mock_libusb_internal.h"
#include <stdlib.h>
#include <string.h>
mock_libusb_ctrl_stage_t g_libusb_ctrl_stages[MOCK_CTRL_SLOTS];
int g_libusb_list_count = 1;
uint16_t g_libusb_list_vid = 0x05AC;
uint16_t g_libusb_list_pid = 0x1227;
void mock_libusb_reset(void)
{
int i;
for (i = 0; i < MOCK_CTRL_SLOTS; i++) {
free(g_libusb_ctrl_stages[i].data);
memset(&g_libusb_ctrl_stages[i], 0,
sizeof(g_libusb_ctrl_stages[i]));
}
g_libusb_list_count = 1;
g_libusb_list_vid = 0x05AC;
g_libusb_list_pid = 0x1227;
}
void mock_libusb_stage_control_transfer(int return_code,
const void *data, size_t data_len)
{
int i;
for (i = 0; i < MOCK_CTRL_SLOTS; i++) {
if (!g_libusb_ctrl_stages[i].in_use) {
mock_libusb_ctrl_stage_t *s = &g_libusb_ctrl_stages[i];
s->return_code = return_code;
s->data = mock_memdup(data, data_len);
s->data_len = data_len;
s->in_use = 1;
s->consumed = 0;
return;
}
}
}
void mock_libusb_set_device_list(int count, uint16_t vid, uint16_t pid)
{
g_libusb_list_count = count;
g_libusb_list_vid = vid;
g_libusb_list_pid = pid;
}

View file

@ -0,0 +1,48 @@
/*
* mock_libusb_internal.h -- Shared state for the libusb mock.
*/
#ifndef TR4MPASS_MOCK_LIBUSB_INTERNAL_H
#define TR4MPASS_MOCK_LIBUSB_INTERNAL_H
#include "mock_internal.h"
#include <stdint.h>
#include <stddef.h>
#include <libusb-1.0/libusb.h>
struct libusb_context {
uint32_t magic;
};
struct libusb_device {
uint16_t vid;
uint16_t pid;
uint8_t bus;
uint8_t addr;
};
struct libusb_device_handle {
struct libusb_device *dev;
int claimed;
};
#define CTX_MAGIC 0x5553425Bu /* "USB[" */
#define MOCK_CTRL_SLOTS 16
typedef struct {
int in_use;
int consumed;
int return_code;
void *data;
size_t data_len;
} mock_libusb_ctrl_stage_t;
extern mock_libusb_ctrl_stage_t g_libusb_ctrl_stages[MOCK_CTRL_SLOTS];
extern int g_libusb_list_count;
extern uint16_t g_libusb_list_vid;
extern uint16_t g_libusb_list_pid;
#endif /* TR4MPASS_MOCK_LIBUSB_INTERNAL_H */

View file

@ -0,0 +1,236 @@
/*
* mock_libusb_sym.c -- Interposed libusb-1.0 entry points.
*/
#include "mock_libusb_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int libusb_init(libusb_context **ctx)
{
mock_log_append("libusb_init");
if (ctx) {
struct libusb_context *c = calloc(1, sizeof(*c));
if (!c) return LIBUSB_ERROR_NO_MEM;
c->magic = CTX_MAGIC;
*ctx = c;
}
return 0;
}
int libusb_init_context(libusb_context **ctx,
const struct libusb_init_option options[],
int num_options)
{
(void)options; (void)num_options;
return libusb_init(ctx);
}
void libusb_exit(libusb_context *ctx)
{
mock_log_append("libusb_exit");
if (ctx) {
ctx->magic = 0;
free(ctx);
}
}
void libusb_set_debug(libusb_context *ctx, int level)
{
(void)ctx; (void)level;
}
int libusb_set_option(libusb_context *ctx, enum libusb_option option, ...)
{
(void)ctx; (void)option;
return 0;
}
const char *libusb_error_name(int error_code)
{
switch (error_code) {
case 0: return "LIBUSB_SUCCESS";
case LIBUSB_ERROR_IO: return "LIBUSB_ERROR_IO";
case LIBUSB_ERROR_NO_DEVICE: return "LIBUSB_ERROR_NO_DEVICE";
case LIBUSB_ERROR_NOT_FOUND: return "LIBUSB_ERROR_NOT_FOUND";
case LIBUSB_ERROR_PIPE: return "LIBUSB_ERROR_PIPE";
case LIBUSB_ERROR_TIMEOUT: return "LIBUSB_ERROR_TIMEOUT";
default: return "LIBUSB_ERROR_OTHER";
}
}
const char *libusb_strerror(int errcode)
{
return libusb_error_name(errcode);
}
/* ------------------------------------------------------------------ */
/* Device enumeration */
/* ------------------------------------------------------------------ */
ssize_t libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
{
struct libusb_device **arr;
int i;
(void)ctx;
mock_log_append("libusb_get_device_list(count=%d)", g_libusb_list_count);
if (!list)
return LIBUSB_ERROR_INVALID_PARAM;
if (g_libusb_list_count <= 0) {
*list = NULL;
return 0;
}
arr = calloc((size_t)g_libusb_list_count + 1,
sizeof(struct libusb_device *));
if (!arr) return LIBUSB_ERROR_NO_MEM;
for (i = 0; i < g_libusb_list_count; i++) {
arr[i] = calloc(1, sizeof(struct libusb_device));
if (arr[i]) {
arr[i]->vid = g_libusb_list_vid;
arr[i]->pid = g_libusb_list_pid;
arr[i]->bus = 1;
arr[i]->addr = (uint8_t)(i + 1);
}
}
arr[g_libusb_list_count] = NULL;
*list = arr;
return g_libusb_list_count;
}
void libusb_free_device_list(libusb_device **list, int unref)
{
int i;
(void)unref;
if (!list) return;
for (i = 0; list[i]; i++)
free(list[i]);
free(list);
}
int libusb_get_device_descriptor(libusb_device *dev,
struct libusb_device_descriptor *desc)
{
if (!dev || !desc) return LIBUSB_ERROR_INVALID_PARAM;
memset(desc, 0, sizeof(*desc));
desc->bLength = 18;
desc->bDescriptorType = 0x01;
desc->idVendor = dev->vid;
desc->idProduct = dev->pid;
desc->iSerialNumber = 3;
return 0;
}
uint8_t libusb_get_bus_number(libusb_device *dev)
{
return dev ? dev->bus : 0;
}
uint8_t libusb_get_device_address(libusb_device *dev)
{
return dev ? dev->addr : 0;
}
int libusb_open(libusb_device *dev, libusb_device_handle **handle)
{
struct libusb_device_handle *h;
mock_log_append("libusb_open");
if (!dev || !handle) return LIBUSB_ERROR_INVALID_PARAM;
h = calloc(1, sizeof(*h));
if (!h) return LIBUSB_ERROR_NO_MEM;
h->dev = dev;
*handle = h;
return 0;
}
void libusb_close(libusb_device_handle *handle)
{
mock_log_append("libusb_close");
free(handle);
}
libusb_device *libusb_get_device(libusb_device_handle *h)
{
return h ? h->dev : NULL;
}
int libusb_claim_interface(libusb_device_handle *h, int iface)
{
(void)h;
mock_log_append("libusb_claim_interface(%d)", iface);
return 0;
}
int libusb_release_interface(libusb_device_handle *h, int iface)
{
(void)h;
mock_log_append("libusb_release_interface(%d)", iface);
return 0;
}
int libusb_detach_kernel_driver(libusb_device_handle *h, int iface)
{
(void)h; (void)iface;
return 0;
}
int libusb_reset_device(libusb_device_handle *h)
{
(void)h;
mock_log_append("libusb_reset_device");
return 0;
}
int libusb_set_configuration(libusb_device_handle *h, int configuration)
{
(void)h; (void)configuration;
return 0;
}
/* ------------------------------------------------------------------ */
/* Transfers */
/* ------------------------------------------------------------------ */
int libusb_control_transfer(libusb_device_handle *h,
uint8_t bmRequestType, uint8_t bRequest,
uint16_t wValue, uint16_t wIndex,
unsigned char *data, uint16_t wLength,
unsigned int timeout)
{
int i;
(void)h; (void)timeout;
mock_log_append("libusb_control_transfer(rt=0x%02x,req=0x%02x,val=0x%04x,idx=0x%04x,len=%u)",
bmRequestType, bRequest, wValue, wIndex, wLength);
for (i = 0; i < MOCK_CTRL_SLOTS; i++) {
mock_libusb_ctrl_stage_t *s = &g_libusb_ctrl_stages[i];
if (!s->in_use || s->consumed)
continue;
s->consumed = 1;
if (data && s->data && s->data_len > 0) {
size_t copy = s->data_len;
if (copy > (size_t)wLength)
copy = (size_t)wLength;
memcpy(data, s->data, copy);
}
return s->return_code;
}
/* No stage set: return wLength (success, all bytes transferred). */
return (int)wLength;
}
int libusb_bulk_transfer(libusb_device_handle *h, unsigned char endpoint,
unsigned char *data, int length,
int *transferred, unsigned int timeout)
{
(void)h; (void)data; (void)timeout;
mock_log_append("libusb_bulk_transfer(ep=0x%02x,len=%d)",
endpoint, length);
if (transferred)
*transferred = length;
return 0;
}

View file

@ -0,0 +1,76 @@
/*
* mock_lockdownd_ctl.c -- Staging + reset for lockdownd/idevice mocks.
*/
#include "mock_lockdownd_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
mock_lockdownd_stage_t g_lockdownd_stage[MOCK_STAGE_SLOTS];
int g_idevice_list_count = 1;
int g_handshake_error = 0;
void mock_lockdownd_reset(void)
{
int i;
for (i = 0; i < MOCK_STAGE_SLOTS; i++) {
free(g_lockdownd_stage[i].data);
memset(&g_lockdownd_stage[i], 0, sizeof(g_lockdownd_stage[i]));
}
g_idevice_list_count = 1;
g_handshake_error = 0;
}
void mock_lockdownd_set_value(const char *domain, const char *key,
const char *data_type,
const void *data, size_t len)
{
int i;
if (!key || !data_type)
return;
for (i = 0; i < MOCK_STAGE_SLOTS; i++) {
if (!g_lockdownd_stage[i].in_use) {
mock_lockdownd_stage_t *s = &g_lockdownd_stage[i];
snprintf(s->domain, sizeof(s->domain), "%s",
domain ? domain : "");
snprintf(s->key, sizeof(s->key), "%s", key);
snprintf(s->type, sizeof(s->type), "%s", data_type);
s->data = mock_memdup(data, len);
s->len = len;
s->in_use = 1;
s->consumed = 0;
return;
}
}
}
void mock_idevice_set_device_list(int count)
{
g_idevice_list_count = count;
}
void mock_lockdownd_set_handshake_error(int error_code)
{
g_handshake_error = error_code;
}
mock_lockdownd_stage_t *find_lockdownd_stage(const char *domain,
const char *key)
{
int i;
for (i = 0; i < MOCK_STAGE_SLOTS; i++) {
mock_lockdownd_stage_t *s = &g_lockdownd_stage[i];
if (!s->in_use || s->consumed)
continue;
if (strcmp(s->key, key ? key : "") != 0)
continue;
/* domain "" in stage means wildcard */
if (s->domain[0] != '\0' && domain &&
strcmp(s->domain, domain) != 0)
continue;
return s;
}
return NULL;
}

View file

@ -0,0 +1,48 @@
/*
* mock_lockdownd_internal.h -- Shared state for lockdownd + idevice mock.
*/
#ifndef TR4MPASS_MOCK_LOCKDOWND_INTERNAL_H
#define TR4MPASS_MOCK_LOCKDOWND_INTERNAL_H
#include "mock_internal.h"
#include <stddef.h>
#include <stdint.h>
#include <libimobiledevice/libimobiledevice.h>
#include <libimobiledevice/lockdown.h>
#include <plist/plist.h>
struct idevice_private {
char udid[96];
uint32_t magic;
};
struct lockdownd_client_private {
char label[64];
uint32_t magic;
};
#define IDEV_MAGIC 0x49444556u /* "IDEV" */
#define LCKD_MAGIC 0x4C434B44u /* "LCKD" */
typedef struct {
int in_use;
int consumed;
char domain[64]; /* "" means match-any */
char key[64];
char type[16]; /* "string","uint","data","bool" */
void *data;
size_t len;
} mock_lockdownd_stage_t;
/* State shared between _ctl.c and _sym.c. */
extern mock_lockdownd_stage_t g_lockdownd_stage[MOCK_STAGE_SLOTS];
extern int g_idevice_list_count;
extern int g_handshake_error;
mock_lockdownd_stage_t *find_lockdownd_stage(const char *domain,
const char *key);
#endif /* TR4MPASS_MOCK_LOCKDOWND_INTERNAL_H */

View file

@ -0,0 +1,238 @@
/*
* mock_lockdownd_sym.c -- Interposed idevice_* and lockdownd_* entry points.
*/
#include "mock_lockdownd_internal.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ------------------------------------------------------------------ */
/* idevice_* */
/* ------------------------------------------------------------------ */
idevice_error_t idevice_get_device_list(char ***devices, int *count)
{
int i;
char **arr;
mock_log_append("idevice_get_device_list(count=%d)",
g_idevice_list_count);
if (!devices || !count)
return IDEVICE_E_INVALID_ARG;
*count = g_idevice_list_count;
if (g_idevice_list_count <= 0) {
*devices = NULL;
return IDEVICE_E_NO_DEVICE;
}
arr = calloc((size_t)g_idevice_list_count + 1, sizeof(char *));
if (!arr)
return IDEVICE_E_UNKNOWN_ERROR;
for (i = 0; i < g_idevice_list_count; i++) {
char buf[32];
snprintf(buf, sizeof(buf), "MOCKUDID%d", i);
arr[i] = strdup(buf);
}
arr[g_idevice_list_count] = NULL;
*devices = arr;
return IDEVICE_E_SUCCESS;
}
idevice_error_t idevice_device_list_free(char **devices)
{
int i;
mock_log_append("idevice_device_list_free");
if (!devices)
return IDEVICE_E_SUCCESS;
for (i = 0; devices[i]; i++)
free(devices[i]);
free(devices);
return IDEVICE_E_SUCCESS;
}
idevice_error_t idevice_new(idevice_t *device, const char *udid)
{
struct idevice_private *dev;
mock_log_append("idevice_new(udid=%s)", udid ? udid : "(null)");
if (!device)
return IDEVICE_E_INVALID_ARG;
dev = calloc(1, sizeof(*dev));
if (!dev)
return IDEVICE_E_UNKNOWN_ERROR;
dev->magic = IDEV_MAGIC;
if (udid)
snprintf(dev->udid, sizeof(dev->udid), "%s", udid);
*device = (idevice_t)dev;
return IDEVICE_E_SUCCESS;
}
idevice_error_t idevice_new_with_options(idevice_t *device,
const char *udid,
enum idevice_options options)
{
(void)options;
return idevice_new(device, udid);
}
idevice_error_t idevice_free(idevice_t device)
{
struct idevice_private *dev = (struct idevice_private *)device;
mock_log_append("idevice_free");
if (!dev)
return IDEVICE_E_INVALID_ARG;
if (dev->magic != IDEV_MAGIC)
return IDEVICE_E_INVALID_ARG;
dev->magic = 0;
free(dev);
return IDEVICE_E_SUCCESS;
}
/* ------------------------------------------------------------------ */
/* lockdownd_* */
/* ------------------------------------------------------------------ */
lockdownd_error_t
lockdownd_client_new_with_handshake(idevice_t device,
lockdownd_client_t *client,
const char *label)
{
struct lockdownd_client_private *c;
mock_log_append("lockdownd_client_new_with_handshake(label=%s)",
label ? label : "(null)");
if (!client)
return LOCKDOWN_E_INVALID_ARG;
if (g_handshake_error != 0)
return (lockdownd_error_t)g_handshake_error;
(void)device;
c = calloc(1, sizeof(*c));
if (!c)
return LOCKDOWN_E_UNKNOWN_ERROR;
c->magic = LCKD_MAGIC;
if (label)
snprintf(c->label, sizeof(c->label), "%s", label);
*client = (lockdownd_client_t)c;
return LOCKDOWN_E_SUCCESS;
}
lockdownd_error_t lockdownd_client_new(idevice_t device,
lockdownd_client_t *client,
const char *label)
{
return lockdownd_client_new_with_handshake(device, client, label);
}
lockdownd_error_t lockdownd_client_free(lockdownd_client_t client)
{
struct lockdownd_client_private *c = (struct lockdownd_client_private *)client;
mock_log_append("lockdownd_client_free");
if (!c)
return LOCKDOWN_E_INVALID_ARG;
c->magic = 0;
free(c);
return LOCKDOWN_E_SUCCESS;
}
lockdownd_error_t lockdownd_goodbye(lockdownd_client_t client)
{
(void)client;
mock_log_append("lockdownd_goodbye");
return LOCKDOWN_E_SUCCESS;
}
lockdownd_error_t lockdownd_get_value(lockdownd_client_t client,
const char *domain, const char *key,
plist_t *value)
{
mock_lockdownd_stage_t *stage;
(void)client;
mock_log_append("lockdownd_get_value(domain=%s,key=%s)",
domain ? domain : "(null)",
key ? key : "(null)");
if (!value)
return LOCKDOWN_E_INVALID_ARG;
*value = NULL;
stage = find_lockdownd_stage(domain, key);
if (!stage)
return LOCKDOWN_E_UNKNOWN_ERROR;
if (strcmp(stage->type, "string") == 0) {
*value = plist_new_string((const char *)stage->data);
} else if (strcmp(stage->type, "uint") == 0) {
uint64_t v = 0;
if (stage->len == sizeof(uint64_t))
memcpy(&v, stage->data, sizeof(v));
*value = plist_new_uint(v);
} else if (strcmp(stage->type, "bool") == 0) {
uint8_t b = stage->data ? *(uint8_t *)stage->data : 0;
*value = plist_new_bool(b);
} else if (strcmp(stage->type, "data") == 0) {
*value = plist_new_data((const char *)stage->data,
(uint64_t)stage->len);
} else {
return LOCKDOWN_E_UNKNOWN_ERROR;
}
stage->consumed = 1;
return *value ? LOCKDOWN_E_SUCCESS : LOCKDOWN_E_UNKNOWN_ERROR;
}
lockdownd_error_t lockdownd_set_value(lockdownd_client_t client,
const char *domain, const char *key,
plist_t value)
{
(void)client; (void)value;
mock_log_append("lockdownd_set_value(domain=%s,key=%s)",
domain ? domain : "(null)",
key ? key : "(null)");
return LOCKDOWN_E_SUCCESS;
}
lockdownd_error_t lockdownd_start_service(lockdownd_client_t client,
const char *identifier,
lockdownd_service_descriptor_t *service)
{
lockdownd_service_descriptor_t svc;
(void)client;
mock_log_append("lockdownd_start_service(identifier=%s)",
identifier ? identifier : "(null)");
if (!service)
return LOCKDOWN_E_INVALID_ARG;
svc = calloc(1, sizeof(*svc));
if (!svc)
return LOCKDOWN_E_UNKNOWN_ERROR;
svc->port = 0xB00B;
svc->ssl_enabled = 0;
if (identifier)
svc->identifier = strdup(identifier);
*service = svc;
return LOCKDOWN_E_SUCCESS;
}
lockdownd_error_t
lockdownd_service_descriptor_free(lockdownd_service_descriptor_t service)
{
mock_log_append("lockdownd_service_descriptor_free");
if (!service)
return LOCKDOWN_E_SUCCESS;
free(service->identifier);
free(service);
return LOCKDOWN_E_SUCCESS;
}
const char *lockdownd_strerror(lockdownd_error_t err)
{
switch ((int)err) {
case 0: return "Success";
case -1: return "Invalid argument";
default: return "Mock lockdownd error";
}
}