【p2p、分布式,区块链笔记 UPNP】: Libupnp test_init.c 01 初始化SDK

Make

test/CMakeLists.txt

# https://github1s.com/pupnp/pupnp/blob/branch-1.14.x/upnp/test/CMakeLists.txt#L1-L4
UPNP_addUnitTest (test-upnp-init test_init.c)
UPNP_addUnitTest (test-upnp-list test_list.c)
UPNP_addUnitTest (test-upnp-log test_log.c)
UPNP_addUnitTest (test-upnp-url test_url.c)

UPNP_addUnitTest

  • 函数 UPNP_addUnitTest,用于add_test添加单元测试
    • 参数:testName (测试名称),sourceFile (测试的源文件)
// https://github1s.com/pupnp/pupnp/blob/branch-1.14.x/cmake/test-functions.cmake#L90-L112
function (UPNP_addUnitTest testName sourceFile)
    
    # 调用自定义函数 UPNP_addTestExecutable 创建测试可执行文件,传入 testName 和 sourceFile,
    # 使用add_executable 生成测试的可执行文件, target_link_libraries为可执行文件链接 upnp 库。
    UPNP_addTestExecutable (${testName} ${sourceFile})

    # 检查是否启用了共享库构建(UPNP_BUILD_SHARED 变量是否为真)
    if (UPNP_BUILD_SHARED)
        add_test (NAME ${testName}  # CTest函数 : 如果启用了共享库构建,则为该测试添加一个普通测试(动态库版本的测试)
            COMMAND ${testName}  # 测试命令为生成的可执行文件 testName
        )
        if (MSVC OR MSYS OR MINGW OR CYGWIN) # 如果使用的是 MSVC、MSYS、MinGW 或 Cygwin 环境(这些是 Windows 平台上的编译工具)
            UPNP_findTestEnv (${testName} TEST_ENV) # 调用自定义函数 UPNP_findTestEnv 来查找该测试的环境变量, 结果存储在 TEST_ENV 变量中
            set_tests_properties (${testName} PROPERTIES # CTest函数 : 设置该测试的属性,特别是设置环境变量(TEST_ENV),# 这些环境变量将在测试运行时生效
                ENVIRONMENT "${TEST_ENV}"  # 设置环境变量
            )
        endif()  # 结束平台检查的条件语句
    endif()  

    # 检查是否启用了静态库构建(UPNP_BUILD_STATIC 变量是否为真)
    if (UPNP_BUILD_STATIC)
        # 如果启用了静态库构建,则为该测试添加一个静态库版本的测试
        # 这里测试命令为 testName-static,即静态库版本的可执行文件
        add_test (NAME ${testName}-static
            COMMAND ${testName}-static  # 静态库版本的测试命令
        )
    endif()  
endfunction()  # 函数定义结束

CODE

test_init.c

  • 这是一个用于初始化并测试 UPnP(Universal Plug and Play) 库的 C 程序。UPnP 是一种支持设备自动化发现和与网络服务交互的协议,常用于智能设备、网络媒体服务器等。

  • test_init.c的主要功能是:

    1. 检查 UPnP 版本 Check library version (and formats)
    2. 检查可选功能 Check library optional features
    3. 初始化 UPnP 库 Test library initialisation,并输出服务器的 IP 地址和端口。
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/**************************************************************************
 *
 * Copyright (c) 2006 Rémi Turboult <r3mi@users.sourceforge.net>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * * Neither name of Intel Corporation nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *************************************************************************/

#include "upnp.h"

#include <stdio.h>
#include <stdlib.h>

#if UPNP_HAVE_TOOLS // 根据条件编译 (#if UPNP_HAVE_TOOLS) 导入一些工具函数
	#include "upnptools.h"
#endif

#include "upnpdebug.h"
#include "posix_overwrites.h"

int main(int argc, char *argv[])
{
	int rc;
	int a, b, c;
	(void)argc;
	(void)argv;
	const char *l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值