From patchwork Fri Apr 5 19:06:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Schrock X-Patchwork-Id: 13619327 Received: from mx0a-003ede02.pphosted.com (mx0a-003ede02.pphosted.com [205.220.169.153]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E197917276C for ; Fri, 5 Apr 2024 19:19:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.169.153 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712344776; cv=none; b=fmPf7UDf2ZbYN2qA2rrDIC/axVWOABqPvMKhccQ8+n/zX2C//U0FWEqULuf4rl9mfFHHYfKfgl6kvVW6nPA+hkEy+NaPu4YWuQxfaPoOAauwgGmfho3C1htvmo5DugklkTteJgMst3HVSVREX+f0yhTOs2TQ8F0yQ/JTfs5dB04= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712344776; c=relaxed/simple; bh=llRvzofzgNHzqcG72DxZoD9fht4s5/fikVluwfJlNMw=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=EgdCeKAgEIR9UAf50IloYeK0vzFxyKHWMUjTVToQLqdnr/H5ykjm/jGi5jpJKeAN1hGan/DN/za8hcc4BAJ6mIrzYc7ZkloylsvBY0BcHzP1gdr+AqyPWgbng01hnJ7vUlOXpTeKWsTot1HgF2VwcJFMoT2IiQqv2XRXCp0/Ky4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=getcruise.com; spf=pass smtp.mailfrom=getcruise.com; dkim=pass (2048-bit key) header.d=getcruise.com header.i=@getcruise.com header.b=PEDToIdJ; dkim=pass (2048-bit key) header.d=getcruise.com header.i=@getcruise.com header.b=Gd2X2Cz7; arc=none smtp.client-ip=205.220.169.153 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=getcruise.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=getcruise.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=getcruise.com header.i=@getcruise.com header.b="PEDToIdJ"; dkim=pass (2048-bit key) header.d=getcruise.com header.i=@getcruise.com header.b="Gd2X2Cz7" Received: from pps.filterd (m0286615.ppops.net [127.0.0.1]) by mx0b-003ede02.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 435DYCjK026984 for ; Fri, 5 Apr 2024 12:08:10 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=getcruise.com; h=from:to:cc:subject:date:message-id:mime-version:content-type; s=ppemail; bh=Ce448ze2bpO5qRUS7xzRG02QNoXJHWb8zIFDKBfh5zE=; b=P EDToIdJWNf9VAJubQXqpB+/rEy68ZpVXtq01HkdwlZtvUb3TxLHMsNuseaPdXxYF nCATtVCFgpZz5tQXs6iD8p7bQmBO8mhNnc3sb4N+9dhY9RTlla5CxW80kkPPWorU fZo4CepsfemWjHJS7hoPZa9wIZNITCWWFV2frn7xQeFQzdK8Q2XDKrkdm/gdI8Ke Se1Q3nHCeAXVsximrmOARC4PP4twDKIqMQCTIDtCUQ3QV2NqcqiTpH/hASURgQQz ImI0wZtSw/piX8oWWgaKadNF3dWaPDZuae/P3N8cEC4vnrToMgu8akp/gT+RzyBh 0LwItrkb8lcsXTMegeKrA== Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by mx0b-003ede02.pphosted.com (PPS) with ESMTPS id 3xah3x0c8e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 05 Apr 2024 12:08:08 -0700 (PDT) Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-43168f0d394so30217941cf.3 for ; Fri, 05 Apr 2024 12:08:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=getcruise.com; s=google; t=1712344087; x=1712948887; darn=lists.linux.dev; h=mime-version:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=Ce448ze2bpO5qRUS7xzRG02QNoXJHWb8zIFDKBfh5zE=; b=Gd2X2Cz766/nGOTxeK7+vXN2LgMJkZ7W012TIlGySu3TT/1PKfLyA4QkHSkc9R3lpK K10+EnqlTluuOV83tIJB3edhc4kIe5AULsSSRjZPzyX8wuY0x8cOEVtmhxiBCZ5++zDJ I6V8UECkoS1LJi0d/Z4oN+OLIwNpFny36D4vUMmeGAbWzpbmLoyHHb3ZRIsCdsggQhfj BZs0+0BLqJfXXr+DKNFAlI/V6H1ebHGVB23Y1bEGHvWziLJRKAkKdPpLPSEqvP5RUUJd pRDupUVodmNdOMoemvJfP4v41ttF6H0efnZA202OLnfb0HgTm+/Gv3LP5GNw5w6PPNC4 IWUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712344087; x=1712948887; h=mime-version:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Ce448ze2bpO5qRUS7xzRG02QNoXJHWb8zIFDKBfh5zE=; b=LgnQY/CyVoMOh41Pn/p4dKt+5IzfnABZLhrvesnS/u9IFWHhBbStuS3p6s/AwoAaHb 8jmpcfAhU+AuRFwj1Tysd8WEvNOD9O/d/+QRbwG6WiNJqbHLH65LTDCea7Hky1Y5oXEi fTvNoQ3NHl4Le+ahMcE7TEQY0C2khXDHHF+KAaAktCaYl2ns+B+4YUjQKAXekHzpKa1g Ql5d7E/VXr/LoSnHbdv+/lzUkWkFkEvLytYHEjxoH9v1k/DT7L9tUbBDxnp6v8MU1Xvj zaXNldM9wBeMgbZOE78SuH0TPyH358MTZrpneHZiSvmabkltRnQ+3rC7OV0lBuHknFHr zU1g== X-Gm-Message-State: AOJu0YyYKrGxZ222ox7auHXyK7rtPGDfhBHsuzrjBrqqoSjp6tFVZlXV zoCvN9gWDPOxxaewXlrw6YOKTW7E4zJajYTxdBT/vDTqy0e5yor/aO04OrXNg+71e9+rI7V3lrR 3nGtPkIpbDCZJBaZFoPoqgb+k30uf2zU2j/700DqOcSbTd2njWAXCsAiFn1fkiP84cF/SJkjDMW lIf5T4IWxoV/nXBPzM6r6q8buqWNY/FLiCPWutHC5X+tAIb4Q= X-Received: by 2002:ac8:59c9:0:b0:431:5aaa:649b with SMTP id f9-20020ac859c9000000b004315aaa649bmr3010782qtf.3.1712344087226; Fri, 05 Apr 2024 12:08:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEgCn5GrbFQqhh2lffUKxXR4Bh70QImNHRB0S2QqrvSNTv9fN7t/rHO9PxmJxMDcmO1l+whPQ== X-Received: by 2002:ac8:59c9:0:b0:431:5aaa:649b with SMTP id f9-20020ac859c9000000b004315aaa649bmr3010752qtf.3.1712344086679; Fri, 05 Apr 2024 12:08:06 -0700 (PDT) Received: from localhost.localdomain ([140.141.139.15]) by smtp.gmail.com with ESMTPSA id hd20-20020a05622a299400b0043146d55f0csm1005414qtb.60.2024.04.05.12.08.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 12:08:06 -0700 (PDT) From: Steve Schrock To: ofono@lists.linux.dev Cc: Steve Schrock Subject: [PATCH v2 1/2] qmi: Create QRTR unit testing framework Date: Fri, 5 Apr 2024 19:06:20 +0000 Message-Id: <20240405190621.38200-1-steve.schrock@getcruise.com> X-Mailer: git-send-email 2.40.1 Precedence: bulk X-Mailing-List: ofono@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-GUID: j2XYTtlbOruTMqZHFyNfR8feJ9tyPo56 X-Proofpoint-ORIG-GUID: j2XYTtlbOruTMqZHFyNfR8feJ9tyPo56 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-04-05_21,2024-04-05_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1011 priorityscore=1501 mlxscore=0 lowpriorityscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 impostorscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2404010003 definitions=main-2404050135 Use actual AF_QIPCRTR sockets to create test QRTR services that may be utilized to validate the functionality of the QRTR implementation of qmi_device. These initial tests validate that the qmi code is performing service lookup and instantiation properly. --- Makefile.am | 7 + unit/test-qmimodem-qmi.c | 286 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 293 insertions(+) create mode 100644 unit/test-qmimodem-qmi.c diff --git a/Makefile.am b/Makefile.am index 85bebae97830..3e545e8f11de 100644 --- a/Makefile.am +++ b/Makefile.am @@ -858,6 +858,7 @@ unit_tests = unit/test-common unit/test-util \ unit/test-simutil unit/test-stkutil \ unit/test-sms \ unit/test-mbim \ + unit/test-qmimodem-qmi \ unit/test-rilmodem-cs \ unit/test-rilmodem-sms \ unit/test-rilmodem-cb \ @@ -953,6 +954,12 @@ unit_test_mbim_SOURCES = unit/test-mbim.c \ unit_test_mbim_LDADD = $(ell_ldadd) unit_objects += $(unit_test_mbim_OBJECTS) +unit_test_qmimodem_qmi_SOURCES = unit/test-qmimodem-qmi.c src/common.c \ + src/util.c src/log.c \ + drivers/qmimodem/qmi.c +unit_test_qmimodem_qmi_LDADD = @GLIB_LIBS@ $(ell_ldadd) +unit_objects += $(unit_test_qmimodem_qmi_OBJECTS) + unit/test-provision.db: unit/test-provision.json $(AM_V_GEN)$(srcdir)/tools/provisiontool generate \ --infile $< --outfile $@ diff --git a/unit/test-qmimodem-qmi.c b/unit/test-qmimodem-qmi.c new file mode 100644 index 000000000000..0bc2946a1a82 --- /dev/null +++ b/unit/test-qmimodem-qmi.c @@ -0,0 +1,286 @@ +/* + * oFono - Open Source Telephony + * Copyright (C) 2024 Cruise, LLC + * + * SPDX-License-Identifier: GPL-2.0-only + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "drivers/qmimodem/qmi.h" +#include + +#include +#include +#include +#include + +#include +#include + +#define TEST_SERVICE_COUNT 2 +#define TEST_TIMEOUT 5 + +struct test_info { + int service_fds[TEST_SERVICE_COUNT]; + struct qmi_device *device; + struct l_timeout *timeout; + struct l_queue *services; + bool discovery_callback_called : 1; +}; + +static uint32_t unique_service_type(uint32_t index) +{ + /* Try to use a value that will not conflict with any real services. */ + return index + 10000; +} + +static uint32_t unique_service_version(uint32_t index) +{ + return index + 10; +} + +static uint32_t unique_service_instance(uint32_t index) +{ + return index + 20; +} + +static int create_service(int i) +{ + int fd; + struct sockaddr_qrtr addr; + socklen_t addrlen; + struct qrtr_ctrl_pkt packet; + ssize_t bytes_sent; + + fd = socket(AF_QIPCRTR, SOCK_DGRAM, 0); + if (fd == -1) { + DBG("Could not create AF_QIPCRTR socket: %s", strerror(errno)); + return -errno; + } + + /* + * The control node is configured by the system. Use getsockname to + * get its node. + */ + addrlen = sizeof(addr); + if (getsockname(fd, (struct sockaddr *) &addr, &addrlen) == -1) { + DBG("getsockname failed: %s", strerror(errno)); + goto error; + } + + if (addr.sq_family != AF_QIPCRTR || addrlen != sizeof(addr)) { + DBG("Unexpected sockaddr from getsockname. family: %d size: %d", + addr.sq_family, addrlen); + goto error; + } + + /* Node and port are assigned automatically so no need to set them */ + memset(&packet, 0, sizeof(packet)); + packet.cmd = L_CPU_TO_LE32(QRTR_TYPE_NEW_SERVER); + packet.server.service = L_CPU_TO_LE32(unique_service_type(i)); + packet.server.instance = L_CPU_TO_LE32( + unique_service_instance(i) << 8 | + unique_service_version(i)); + + bytes_sent = sendto(fd, &packet, sizeof(packet), 0, + (struct sockaddr *) &addr, addrlen); + if (bytes_sent != sizeof(packet)) { + DBG("sendto to set up the qrtr service failed: %s", + strerror(errno)); + goto error; + } + + return fd; + +error: + close(fd); + + return -errno; +} + +static void setup_test_qrtr_services(struct test_info *info) +{ + int i; + + for (i = 0; i < TEST_SERVICE_COUNT; ++i) { + info->service_fds[i] = create_service(i); + assert(info->service_fds[i] >= 0); + } +} + +static void debug_log(const char *str, void *user_data) +{ + printf("%s\n", str); +} + +static void test_timeout_cb(struct l_timeout *timeout, void *user_data) +{ + DBG("Test timed out!"); + assert(false); +} + +static struct test_info *test_setup(void) +{ + struct test_info *info; + + l_main_init(); + + info = l_new(struct test_info, 1); + setup_test_qrtr_services(info); + info->device = qmi_device_new_qrtr(); + assert(info->device); + + /* Enable ofono logging */ + qmi_device_set_debug(info->device, debug_log, NULL); + + info->services = l_queue_new(); + info->timeout = l_timeout_create(TEST_TIMEOUT, test_timeout_cb, info, + NULL); + + return info; +} + +static void test_cleanup(struct test_info *info) +{ + int i; + + l_timeout_remove(info->timeout); + l_queue_destroy(info->services, + (l_queue_destroy_func_t) qmi_service_unref); + qmi_device_free(info->device); + + /* The qrtr services will be destroyed automatically. */ + for (i = 0; i < TEST_SERVICE_COUNT; ++i) + close(info->service_fds[i]); + + l_free(info); + + l_main_exit(); +} + +static void test_create_qrtr_device(const void *data) +{ + struct test_info *info = test_setup(); + + test_cleanup(info); +} + +static void discovery_complete_cb(void *user_data) +{ + struct test_info *info = user_data; + + info->discovery_callback_called = true; +} + +static void perform_discovery(struct test_info *info) +{ + qmi_device_discover(info->device, discovery_complete_cb, info, NULL); + + while (!info->discovery_callback_called) + l_main_iterate(-1); +} + +static void test_discovery(const void *data) +{ + struct test_info *info = test_setup(); + + perform_discovery(info); + + test_cleanup(info); +} + +static void create_service_cb(struct qmi_service *service, void *user_data) +{ + struct test_info *info = user_data; + + service = qmi_service_ref(service); + l_queue_push_tail(info->services, service); +} + +/* Callbacks could queue other callbacks so continue until there are no more. */ +static void perform_all_pending_work(void) +{ + l_main_iterate(0); + + while (l_main_prepare() != -1) + l_main_iterate(0); +} + +static void test_create_services(const void *data) +{ + struct test_info *info = test_setup(); + uint32_t service_type; + int i; + + perform_discovery(info); + + for (i = 0; i < TEST_SERVICE_COUNT; i++) { + struct qmi_service *service; + uint16_t major, minor; + + service_type = unique_service_type(i); + assert(qmi_service_create(info->device, service_type, + create_service_cb, info, NULL)); + perform_all_pending_work(); + + assert(l_queue_length(info->services) == 1); + service = l_queue_pop_head(info->services); + assert(service); + + assert(qmi_service_get_version(service, &major, &minor)); + assert(major == unique_service_version(i)); + assert(minor == 0); + + qmi_service_unref(service); + } + + /* + * Confirm that an unknown service cannot be created and does not + * call the callback. + */ + service_type = unique_service_type(TEST_SERVICE_COUNT); + assert(!qmi_service_create(info->device, service_type, + create_service_cb, info, NULL)); + perform_all_pending_work(); + assert(l_queue_isempty(info->services)); + + test_cleanup(info); +} + +static void exit_if_qrtr_not_supported() +{ + int fd; + + fd = socket(AF_QIPCRTR, SOCK_DGRAM, 0); + if (fd == -1) { + printf("Could not create AF_QIPCRTR socket: %s\n", + strerror(errno)); + printf("Skipping tests...\n"); + exit(0); + } + + close(fd); +} + +int main(int argc, char **argv) +{ + int result; + + exit_if_qrtr_not_supported(); + + /* Enable all DBG logging */ + __ofono_log_init(argv[0], "*", FALSE); + + l_test_init(&argc, &argv); + l_test_add("QRTR device creation", test_create_qrtr_device, NULL); + l_test_add("QRTR discovery", test_discovery, NULL); + l_test_add("QRTR services may be created", test_create_services, NULL); + result = l_test_run(); + + __ofono_log_cleanup(); + + return result; +} From patchwork Fri Apr 5 19:06:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Schrock X-Patchwork-Id: 13619325 Received: from mx0b-003ede02.pphosted.com (mx0b-003ede02.pphosted.com [205.220.181.153]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E79C716D328 for ; Fri, 5 Apr 2024 19:08:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.181.153 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712344094; cv=none; b=W4MNR7fgmI4eKRdqv+hHiW0ExdAhS/zUvAYQ7I1IKU+kujP6ncQCBV/zrRW32p/3au2H4Xmj+xxzhfyAGv2qlYWYvqhRVr59TV/Np6m9BFLqmxKlrBHqztk5mdvcg3nell9xG55VcawhkLHxQ+dE/tec09sDoWfC13IOgW4E/kg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712344094; c=relaxed/simple; bh=cDEjxl6Fpew3NViSn81/J/zZudxaCjghYFYxCokvnLc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=PYgbXrwSSFhfVoLhtgo7GwUiGUJPxXa7V5QM54G5cggwLTsnBB/93bN5hv1PVdQiDj1O4MN8D7AQIWdR2YPPQfor5v9tQxa6z9/Yp5rsVLrjM9qi6+bYFC4zZFnHaWbPepfLLEzlnpj8Ad/x9wOJZsrdWNzdNWR0qp9uE0tP0bA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=getcruise.com; spf=pass smtp.mailfrom=getcruise.com; dkim=pass (2048-bit key) header.d=getcruise.com header.i=@getcruise.com header.b=Wcs6dGbN; dkim=pass (2048-bit key) header.d=getcruise.com header.i=@getcruise.com header.b=fdc1MHTC; arc=none smtp.client-ip=205.220.181.153 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=getcruise.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=getcruise.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=getcruise.com header.i=@getcruise.com header.b="Wcs6dGbN"; dkim=pass (2048-bit key) header.d=getcruise.com header.i=@getcruise.com header.b="fdc1MHTC" Received: from pps.filterd (m0286621.ppops.net [127.0.0.1]) by mx0b-003ede02.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 435Dw81b006033 for ; Fri, 5 Apr 2024 12:08:09 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=getcruise.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type; s=ppemail; bh=XV58LAVi8reNHvUrE/X5dp exUYFcJu0ioZL7iQigth4=; b=Wcs6dGbNPSHrZtHqkL382X91ZskBY4SaVpcLDJ wiV6lt4pVLyGlMZn5Q2mu1KqQw0izvYUpbQO5wzuPl0MSO4WBnvXNgeqrQqCHPH9 xFhyRCS75irI5DVF5lz+3DEIyfkcQOAlc5cTM8NtgPa+VjX7mFUTOYoz1PsQQdjC rOhrugQ8KUIyT331rr9VqI0dZvYevX4DFfiyLLfjwhFeY5I9C9leY3jEXbzBOmZM T41K8N/qxNHNPCidSZQzmP7lBLfEHBkeeXl4FI7YEqjeDf/NVIAIkocBZHPHUbct Bz99aU1jZpYvxdw5GISyJJXMzRZpPNR3bKnHL9nLimjXwMCw== Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by mx0b-003ede02.pphosted.com (PPS) with ESMTPS id 3x9emut4a5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 05 Apr 2024 12:08:09 -0700 (PDT) Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-42f3069a45dso62849071cf.1 for ; Fri, 05 Apr 2024 12:08:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=getcruise.com; s=google; t=1712344088; x=1712948888; darn=lists.linux.dev; h=mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=XV58LAVi8reNHvUrE/X5dpexUYFcJu0ioZL7iQigth4=; b=fdc1MHTCxGy+Y4cMxvvXKNJO1Dvji837c7upp4f7ITMazwzZ28tlfebwPID9Nl0Dpq xmItwJpeV9HM+aA/ushyRiYMFp0R2j7vL2rKCIiHxLmlAUqyBoIlbIdWBsfR3+pCOHR7 Yxedft6513vwzpGVejv8slZTN1oLYZeE6AkSBv6mr6BZLJ2yxv/JZKeg6p8fcORfHxZw XOEZ4Jw08FGc5rqrGBHHlfYhjJh1RwyHgbRPjgrlcEovYJ3tD3p5s7Y34aKvJ75/iVzv ux/mjODn/mV28EgkrY7ot1823A5pfuH+Uj8ZNJJxAWqcA/oqc2A15lujMWYVxRDcFWB0 Ox0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712344088; x=1712948888; h=mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XV58LAVi8reNHvUrE/X5dpexUYFcJu0ioZL7iQigth4=; b=WPWpM9xM/hmtmKanzdFOjMwp6+3gT8520ktGSWd7XMgy1Cz1IjMSYc0e8PM6Lt1xol 5RncQZCrynwqQfEMXAlErKYyw5pE1JvOFqCk7ow7fevzOEIlOri0+vICHgvEhs+CyRnu LBYmg2+ucCYMYlJJ6nLXz5m6V3KpwegY/JNJzKR24/7LRjztlWzTjM+ijt0C5cw5mSJe XJVaxyqeuL/qP6x5x2ua1zRTsgPweGlCjTfPVYl09eEYSkuy3BMpB2yAgtkDckG/4zZh /J4b+j8DgMuaNmCM4XPyj/qmN96Nri22bEq3AE0ewTOn+W/6bllnbGlYWEXQ+VbLW2Nt MVqw== X-Gm-Message-State: AOJu0YzgmiX50sc8CNb7GRqpKvC+PaSv9oaMkAXgclqbs/Z8nHepYdVO uq+MWdksvWISvIK1evxz167UbxEWtg9EE3+4BAitsDGhqKlwIvrC/umjVowRv7H2UGzUXyBg2Ti ybCVsb+HuYGxuYN+HgzflFgHoN21xtv05oUXFfAHSFfEvvyGULRwBZOBg4CfclmSmOUo1w/UJpo nY8iqoFDQqIYbafVDjzKIdzQhd4KmLkorUj6hbaYXZxsFsxVc= X-Received: by 2002:a05:622a:14c7:b0:434:5894:768f with SMTP id u7-20020a05622a14c700b004345894768fmr5880923qtx.31.1712344087737; Fri, 05 Apr 2024 12:08:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH+mRrAFAICRP8T+RI0ghF2mfiReMlOSLft8PXx+nFbPDW3J3ANERw80ZDSYZ6VQESEJLyNdg== X-Received: by 2002:a05:622a:14c7:b0:434:5894:768f with SMTP id u7-20020a05622a14c700b004345894768fmr5880897qtx.31.1712344087309; Fri, 05 Apr 2024 12:08:07 -0700 (PDT) Received: from localhost.localdomain ([140.141.139.15]) by smtp.gmail.com with ESMTPSA id hd20-20020a05622a299400b0043146d55f0csm1005414qtb.60.2024.04.05.12.08.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 12:08:07 -0700 (PDT) From: Steve Schrock To: ofono@lists.linux.dev Cc: Steve Schrock Subject: [PATCH v2 2/2] qmi: Add unit test for QRTR qmi_service_send Date: Fri, 5 Apr 2024 19:06:21 +0000 Message-Id: <20240405190621.38200-2-steve.schrock@getcruise.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240405190621.38200-1-steve.schrock@getcruise.com> References: <20240405190621.38200-1-steve.schrock@getcruise.com> Precedence: bulk X-Mailing-List: ofono@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: xUOIruU_PamL8RRfxffVyZFu6AMEX6Hh X-Proofpoint-GUID: xUOIruU_PamL8RRfxffVyZFu6AMEX6Hh X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-04-05_21,2024-04-05_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 bulkscore=0 clxscore=1015 priorityscore=1501 spamscore=0 mlxscore=0 suspectscore=0 mlxlogscore=999 adultscore=0 malwarescore=0 phishscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2404010003 definitions=main-2404050135 The unit test causes a qmi request to be sent to the test QRTR service and echos the data back in its response. --- unit/test-qmimodem-qmi.c | 166 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 165 insertions(+), 1 deletion(-) diff --git a/unit/test-qmimodem-qmi.c b/unit/test-qmimodem-qmi.c index 0bc2946a1a82..79a9a1437aba 100644 --- a/unit/test-qmimodem-qmi.c +++ b/unit/test-qmimodem-qmi.c @@ -28,7 +28,14 @@ struct test_info { struct qmi_device *device; struct l_timeout *timeout; struct l_queue *services; - bool discovery_callback_called : 1; + + /* Data sent to our test service */ + struct sockaddr_qrtr sender; + size_t received_len; + void *received; + + bool discovery_callback_called : 1; + bool service_send_callback_called : 1; }; static uint32_t unique_service_type(uint32_t index) @@ -147,6 +154,7 @@ static void test_cleanup(struct test_info *info) { int i; + l_free(info->received); l_timeout_remove(info->timeout); l_queue_destroy(info->services, (l_queue_destroy_func_t) qmi_service_unref); @@ -250,6 +258,161 @@ static void test_create_services(const void *data) test_cleanup(info); } +static bool received_data(struct l_io *io, void *user_data) +{ + struct test_info *info = user_data; + struct sockaddr_qrtr addr; + unsigned char buf[2048]; + ssize_t bytes_read; + socklen_t addr_size; + + addr_size = sizeof(addr); + bytes_read = recvfrom(l_io_get_fd(io), buf, sizeof(buf), 0, + (struct sockaddr *) &addr, &addr_size); + memcpy(&info->sender, &addr, sizeof(addr)); + + assert(!info->received); /* Only expect one message */ + info->received_len = bytes_read; + info->received = l_memdup(buf, bytes_read); + + return true; +} + +#define TEST_TLV_TYPE 21 /* Its data value is 1 byte */ +#define TEST_DATA_VALUE 0x89 + +static void send_test_data_cb(struct qmi_result *result, void *user_data) +{ + struct test_info *info = user_data; + + uint8_t data; + + assert(!qmi_result_set_error(result, NULL)); + assert(qmi_result_get_uint8(result, TEST_TLV_TYPE, &data)); + assert(data == TEST_DATA_VALUE); + + info->service_send_callback_called = true; +} + +/* + * We know exactly how the qmi data should be packed so we can hard code the + * structure layout to simplify the tests. + */ + +#define TEST_REQ_MESSAGE_ID 42 +#define TEST_RESP_MESSAGE_ID 43 +#define QMI_HDR_SIZE 7 + +struct qmi_test_service_request { + uint8_t type; + uint16_t transaction; + uint16_t message; + uint16_t length; /* Message size without header */ + uint8_t data_type; + uint16_t data_length; + uint8_t data_value; +} __attribute__ ((packed)); + +struct qmi_test_service_response { + uint8_t type; + uint16_t transaction; + uint16_t message; + uint16_t length; /* Message size without header */ + uint8_t error_type; + uint16_t error_length; + uint16_t error_result; + uint16_t error_error; + uint8_t data_type; + uint16_t data_length; + uint8_t data_value; +} __attribute__ ((packed)); + +static void send_request_via_qmi(struct test_info *info, + struct qmi_service *service) +{ + struct qmi_param *param; + + param = qmi_param_new(); + qmi_param_append_uint8(param, TEST_TLV_TYPE, TEST_DATA_VALUE); + assert(qmi_service_send(service, TEST_REQ_MESSAGE_ID, param, + send_test_data_cb, info, NULL)); + + while (!info->received) + l_main_iterate(-1); +} + +static void send_response_via_socket(struct test_info *info, struct l_io *io) +{ + const struct qmi_test_service_request *request; + struct qmi_test_service_response response; + + /* First validate that the qrtr code sent the qmi request properly. */ + assert(info->received_len == sizeof(*request)); + request = info->received; + assert(request->type == 0x00); + assert(request->message == L_CPU_TO_LE16(TEST_REQ_MESSAGE_ID)); + assert(request->length == L_CPU_TO_LE16( + sizeof(*request) - QMI_HDR_SIZE)); + assert(request->data_type == TEST_TLV_TYPE); + assert(request->data_length == L_CPU_TO_LE16(1)); + assert(request->data_value == TEST_DATA_VALUE); + + /* + * Now echo it back to the qrtr client. The qmi_service send callback + * will validate that the client processed this response correctly. + */ + memset(&response, 0, sizeof(response)); + response.type = 0x02; + response.transaction = request->transaction; + response.message = L_CPU_TO_LE16(TEST_RESP_MESSAGE_ID); + response.length = L_CPU_TO_LE16(sizeof(response) - QMI_HDR_SIZE); + response.error_type = 2; + response.error_length = L_CPU_TO_LE16(4); + response.data_type = request->data_type; + response.data_length = request->data_length; + response.data_value = request->data_value; + + sendto(l_io_get_fd(io), &response, sizeof(response), 0, + (struct sockaddr *) &info->sender, + sizeof(info->sender)); + + while (!info->service_send_callback_called) + l_main_iterate(-1); +} + +/* + * Initiates a send of the TLV data payload to the test service. The test + * service will respond with the same data payload. + */ +static void test_send_data(const void *data) +{ + struct test_info *info = test_setup(); + struct l_io *io; + uint32_t service_type; + struct qmi_service *service; + + perform_discovery(info); + + service_type = unique_service_type(0); /* Use the first service */ + assert(qmi_service_create(info->device, service_type, + create_service_cb, info, NULL)); + perform_all_pending_work(); + service = l_queue_pop_head(info->services); + assert(service); + + io = l_io_new(info->service_fds[0]); + assert(io); + l_io_set_read_handler(io, received_data, info, NULL); + + send_request_via_qmi(info, service); + send_response_via_socket(info, io); + + l_io_destroy(io); + qmi_service_unref(service); + + test_cleanup(info); +} + static void exit_if_qrtr_not_supported() { int fd; @@ -278,6 +441,7 @@ int main(int argc, char **argv) l_test_add("QRTR device creation", test_create_qrtr_device, NULL); l_test_add("QRTR discovery", test_discovery, NULL); l_test_add("QRTR services may be created", test_create_services, NULL); + l_test_add("QRTR service sends/responses", test_send_data, NULL); result = l_test_run(); __ofono_log_cleanup();