1
0

test.yaml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. name: test
  2. on:
  3. push:
  4. pull_request:
  5. workflow_dispatch:
  6. inputs:
  7. gtest_filter:
  8. description: 'Google Test filter'
  9. test_linux:
  10. description: 'Test on Linux'
  11. type: boolean
  12. default: true
  13. test_macos:
  14. description: 'Test on MacOS'
  15. type: boolean
  16. default: true
  17. test_windows:
  18. description: 'Test on Windows'
  19. type: boolean
  20. default: true
  21. concurrency:
  22. group: ${{ github.workflow }}-${{ github.ref || github.run_id }}
  23. cancel-in-progress: true
  24. env:
  25. GTEST_FILTER: ${{ github.event.inputs.gtest_filter || '*' }}
  26. jobs:
  27. style-check:
  28. runs-on: ubuntu-latest
  29. if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
  30. continue-on-error: true
  31. steps:
  32. - name: checkout
  33. uses: actions/checkout@v4
  34. - name: run style check
  35. run: |
  36. clang-format --version
  37. cd test && make style_check
  38. build-and-test-on-32bit:
  39. runs-on: ubuntu-latest
  40. if: >
  41. (github.event_name == 'push') ||
  42. (github.event_name == 'pull_request' &&
  43. github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
  44. (github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true')
  45. strategy:
  46. matrix:
  47. config:
  48. - arch_flags: -m32
  49. arch_suffix: :i386
  50. name: (32-bit)
  51. steps:
  52. - name: checkout
  53. uses: actions/checkout@v4
  54. - name: install libraries
  55. run: |
  56. sudo dpkg --add-architecture i386
  57. sudo apt-get update
  58. sudo apt-get install -y libc6-dev${{ matrix.config.arch_suffix }} libstdc++-13-dev${{ matrix.config.arch_suffix }} \
  59. libssl-dev${{ matrix.config.arch_suffix }} libcurl4-openssl-dev${{ matrix.config.arch_suffix }} \
  60. zlib1g-dev${{ matrix.config.arch_suffix }} libbrotli-dev${{ matrix.config.arch_suffix }} \
  61. libzstd-dev${{ matrix.config.arch_suffix }}
  62. - name: build and run tests
  63. run: cd test && make test EXTRA_CXXFLAGS="${{ matrix.config.arch_flags }}"
  64. ubuntu:
  65. runs-on: ubuntu-latest
  66. if: >
  67. (github.event_name == 'push') ||
  68. (github.event_name == 'pull_request' &&
  69. github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
  70. (github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true')
  71. strategy:
  72. fail-fast: false
  73. matrix:
  74. tls_backend: [openssl, mbedtls, wolfssl]
  75. name: ubuntu (${{ matrix.tls_backend }})
  76. steps:
  77. - name: checkout
  78. uses: actions/checkout@v4
  79. - name: install common libraries
  80. run: |
  81. sudo apt-get update
  82. sudo apt-get install -y libcurl4-openssl-dev zlib1g-dev libbrotli-dev libzstd-dev
  83. - name: install OpenSSL
  84. if: matrix.tls_backend == 'openssl'
  85. run: sudo apt-get install -y libssl-dev
  86. - name: install Mbed TLS
  87. if: matrix.tls_backend == 'mbedtls'
  88. run: sudo apt-get install -y libmbedtls-dev
  89. - name: install wolfSSL
  90. if: matrix.tls_backend == 'wolfssl'
  91. run: sudo apt-get install -y libwolfssl-dev
  92. - name: build and run tests (OpenSSL)
  93. if: matrix.tls_backend == 'openssl'
  94. run: cd test && make test_split && make test_openssl_parallel
  95. env:
  96. LSAN_OPTIONS: suppressions=lsan_suppressions.txt
  97. - name: build and run tests (Mbed TLS)
  98. if: matrix.tls_backend == 'mbedtls'
  99. run: cd test && make test_split_mbedtls && make test_mbedtls_parallel
  100. - name: build and run tests (wolfSSL)
  101. if: matrix.tls_backend == 'wolfssl'
  102. run: cd test && make test_split_wolfssl && make test_wolfssl_parallel
  103. - name: run fuzz test target
  104. if: matrix.tls_backend == 'openssl'
  105. run: cd test && make fuzz_test
  106. - name: build and run WebSocket heartbeat test
  107. if: matrix.tls_backend == 'openssl'
  108. run: cd test && make test_websocket_heartbeat && ./test_websocket_heartbeat
  109. - name: build and run ThreadPool test
  110. run: cd test && make test_thread_pool && ./test_thread_pool
  111. # Reproducer for https://github.com/yhirose/cpp-httplib/issues/2431.
  112. # On Linux/glibc, getaddrinfo_with_timeout() schedules an asynchronous
  113. # DNS lookup with getaddrinfo_a(GAI_NOWAIT) using a stack-local gaicb.
  114. # When gai_suspend() hits the connection timeout, gai_cancel() is called
  115. # but does not block; the resolver worker can later write back into the
  116. # destroyed stack frame. To make the worker actually reach that write,
  117. # the test job runs a loopback UDP responder (test/dns_test_fixture.py)
  118. # that delays its reply past the test's 1s timeout, and uses an iptables
  119. # NAT rule so glibc's lookups land on that fixture instead of a real
  120. # nameserver. With ASAN's detect_stack_use_after_return enabled, the
  121. # late write-back is reported as a stack-use-after-return.
  122. issue-2431-repro:
  123. runs-on: ubuntu-latest
  124. if: >
  125. (github.event_name == 'push') ||
  126. (github.event_name == 'pull_request' &&
  127. github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
  128. (github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true')
  129. name: issue-2431 repro (Linux + ASAN)
  130. # Bound the whole job in case anything in the test harness hangs
  131. # unexpectedly. With the fixture in place a normal run is well under
  132. # a minute either way (ASAN abort on broken HEAD, clean pass on fix).
  133. timeout-minutes: 5
  134. env:
  135. DNS_FIXTURE_PORT: "15353"
  136. DNS_FIXTURE_DELAY: "3"
  137. steps:
  138. - name: checkout
  139. uses: actions/checkout@v4
  140. - name: install libraries
  141. run: |
  142. sudo apt-get update
  143. sudo apt-get install -y libssl-dev zlib1g-dev libbrotli-dev \
  144. libzstd-dev libcurl4-openssl-dev iptables util-linux iproute2
  145. - name: start loopback DNS test fixture
  146. run: |
  147. # Force glibc through its DNS code path: Ubuntu's default
  148. # nsswitch short-circuits to NOTFOUND through mdns4_minimal,
  149. # which would skip the buggy code entirely.
  150. sudo sed -i 's/^hosts:.*/hosts: dns/' /etc/nsswitch.conf
  151. # Run the loopback fixture (delayed UDP responder).
  152. python3 test/dns_test_fixture.py "$DNS_FIXTURE_PORT" "$DNS_FIXTURE_DELAY" \
  153. >/tmp/dns_fixture.log 2>&1 &
  154. echo $! | sudo tee /tmp/dns_fixture.pid >/dev/null
  155. # Wait for the fixture to start listening.
  156. for _ in $(seq 1 50); do
  157. if ss -lun "( sport = :$DNS_FIXTURE_PORT )" | grep -q ":$DNS_FIXTURE_PORT"; then
  158. break
  159. fi
  160. sleep 0.1
  161. done
  162. ss -lun "( sport = :$DNS_FIXTURE_PORT )" | grep -q ":$DNS_FIXTURE_PORT" \
  163. || { echo "fixture failed to start"; cat /tmp/dns_fixture.log; exit 1; }
  164. # Send the test process's DNS lookups to the loopback fixture.
  165. # NAT only the local OUTPUT chain; conntrack handles the reply path.
  166. sudo iptables -t nat -I OUTPUT -p udp --dport 53 \
  167. -j REDIRECT --to-port "$DNS_FIXTURE_PORT"
  168. # Sanity check: a query must take at least the fixture delay
  169. # and resolve to NXDOMAIN (proving traffic reaches the fixture).
  170. start=$(date +%s)
  171. getent hosts unresolvable-host.invalid >/dev/null 2>&1 || true
  172. elapsed=$(( $(date +%s) - start ))
  173. if [ "$elapsed" -lt 2 ]; then
  174. echo "ERROR: lookup returned in ${elapsed}s; fixture not in path" >&2
  175. exit 1
  176. fi
  177. echo "[ok] DNS lookups are routed to the test fixture (took ${elapsed}s)"
  178. - name: build test binary
  179. run: cd test && make test
  180. - name: run GetAddrInfoAsyncCancelTest
  181. run: |
  182. cd test
  183. ARCH=$(uname -m)
  184. CPPHTTPLIB_TEST_ISSUE_2431=1 \
  185. ASAN_OPTIONS=detect_stack_use_after_return=1 \
  186. LSAN_OPTIONS=suppressions=lsan_suppressions.txt \
  187. setarch "$ARCH" -R \
  188. ./test --gtest_filter='GetAddrInfoAsyncCancelTest.*'
  189. - name: tear down test fixture
  190. if: always()
  191. run: |
  192. sudo iptables -t nat -F OUTPUT || true
  193. if [ -f /tmp/dns_fixture.pid ]; then
  194. sudo kill "$(cat /tmp/dns_fixture.pid)" 2>/dev/null || true
  195. fi
  196. macos:
  197. runs-on: macos-latest
  198. if: >
  199. (github.event_name == 'push') ||
  200. (github.event_name == 'pull_request' &&
  201. github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
  202. (github.event_name == 'workflow_dispatch' && github.event.inputs.test_macos == 'true')
  203. strategy:
  204. fail-fast: false
  205. matrix:
  206. tls_backend: [openssl, mbedtls, wolfssl]
  207. name: macos (${{ matrix.tls_backend }})
  208. steps:
  209. - name: checkout
  210. uses: actions/checkout@v4
  211. - name: install Mbed TLS
  212. if: matrix.tls_backend == 'mbedtls'
  213. run: brew install mbedtls@3
  214. - name: install wolfSSL
  215. if: matrix.tls_backend == 'wolfssl'
  216. run: brew install wolfssl
  217. - name: build and run tests (OpenSSL)
  218. if: matrix.tls_backend == 'openssl'
  219. run: cd test && make test_split && make test_openssl_parallel
  220. env:
  221. LSAN_OPTIONS: suppressions=lsan_suppressions.txt
  222. - name: build and run tests (Mbed TLS)
  223. if: matrix.tls_backend == 'mbedtls'
  224. run: cd test && make test_split_mbedtls && make test_mbedtls_parallel
  225. - name: build and run tests (wolfSSL)
  226. if: matrix.tls_backend == 'wolfssl'
  227. run: cd test && make test_split_wolfssl && make test_wolfssl_parallel
  228. - name: run fuzz test target
  229. if: matrix.tls_backend == 'openssl'
  230. run: cd test && make fuzz_test
  231. - name: build and run WebSocket heartbeat test
  232. if: matrix.tls_backend == 'openssl'
  233. run: cd test && make test_websocket_heartbeat && ./test_websocket_heartbeat
  234. - name: build and run ThreadPool test
  235. run: cd test && make test_thread_pool && ./test_thread_pool
  236. windows:
  237. runs-on: windows-latest
  238. if: >
  239. (github.event_name == 'push') ||
  240. (github.event_name == 'pull_request' &&
  241. github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) ||
  242. (github.event_name == 'workflow_dispatch' && github.event.inputs.test_windows == 'true')
  243. strategy:
  244. fail-fast: false
  245. matrix:
  246. config:
  247. - with_ssl: false
  248. compiled: false
  249. run_tests: true
  250. name: without SSL
  251. - with_ssl: true
  252. compiled: false
  253. run_tests: true
  254. name: with SSL
  255. - with_ssl: false
  256. compiled: true
  257. run_tests: false
  258. name: compiled
  259. name: windows ${{ matrix.config.name }}
  260. steps:
  261. - name: Prepare Git for Checkout on Windows
  262. run: |
  263. git config --global core.autocrlf false
  264. git config --global core.eol lf
  265. - name: Checkout
  266. uses: actions/checkout@v4
  267. - name: Export GitHub Actions cache environment variables
  268. uses: actions/github-script@v7
  269. with:
  270. script: |
  271. core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
  272. core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');
  273. - name: Setup msbuild on windows
  274. uses: microsoft/setup-msbuild@v2
  275. - name: Cache vcpkg packages
  276. id: vcpkg-cache
  277. uses: actions/cache@v4
  278. with:
  279. path: C:/vcpkg/installed
  280. key: vcpkg-installed-windows-gtest-curl-zlib-brotli-zstd
  281. - name: Install vcpkg dependencies
  282. if: steps.vcpkg-cache.outputs.cache-hit != 'true'
  283. run: vcpkg install gtest curl zlib brotli zstd
  284. - name: Install OpenSSL
  285. if: ${{ matrix.config.with_ssl }}
  286. run: choco install openssl
  287. - name: Configure CMake ${{ matrix.config.name }}
  288. run: >
  289. cmake -B build -S .
  290. -DCMAKE_BUILD_TYPE=Release
  291. -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake
  292. -DHTTPLIB_TEST=ON
  293. -DHTTPLIB_COMPILE=${{ matrix.config.compiled && 'ON' || 'OFF' }}
  294. -DHTTPLIB_USE_OPENSSL_IF_AVAILABLE=${{ matrix.config.with_ssl && 'ON' || 'OFF' }}
  295. -DHTTPLIB_REQUIRE_ZLIB=ON
  296. -DHTTPLIB_REQUIRE_BROTLI=ON
  297. -DHTTPLIB_REQUIRE_ZSTD=ON
  298. -DHTTPLIB_REQUIRE_OPENSSL=${{ matrix.config.with_ssl && 'ON' || 'OFF' }}
  299. - name: Build ${{ matrix.config.name }}
  300. run: cmake --build build --config Release -- /v:m /clp:ShowCommandLine
  301. - name: Run tests ${{ matrix.config.name }}
  302. if: ${{ matrix.config.run_tests }}
  303. shell: pwsh
  304. working-directory: build/test
  305. run: |
  306. $shards = 4
  307. $procs = @()
  308. for ($i = 0; $i -lt $shards; $i++) {
  309. $log = "shard_${i}.log"
  310. $procs += Start-Process -FilePath ./Release/httplib-test.exe `
  311. -ArgumentList "--gtest_color=yes","--gtest_filter=${{ github.event.inputs.gtest_filter || '*' }}" `
  312. -NoNewWindow -PassThru -RedirectStandardOutput $log -RedirectStandardError "${log}.err" `
  313. -Environment @{ GTEST_TOTAL_SHARDS="$shards"; GTEST_SHARD_INDEX="$i" }
  314. }
  315. $procs | Wait-Process
  316. $failed = $false
  317. for ($i = 0; $i -lt $shards; $i++) {
  318. $log = "shard_${i}.log"
  319. $proc = $procs[$i]
  320. $hasPassed = Select-String -Path $log -Pattern "\[ PASSED \]" -Quiet
  321. $hasFailed = Select-String -Path $log -Pattern "\[ FAILED \]" -Quiet
  322. if ($hasPassed -and -not $hasFailed -and $proc.ExitCode -eq 0) {
  323. $passed = (Select-String -Path $log -Pattern "\[ PASSED \]").Line
  324. Write-Host "Shard ${i}: $passed"
  325. } else {
  326. Write-Host "=== Shard $i FAILED (exit=$($proc.ExitCode)) ==="
  327. Get-Content $log
  328. if (Test-Path "${log}.err") { Get-Content "${log}.err" }
  329. $failed = $true
  330. }
  331. }
  332. if ($failed) { exit 1 }
  333. Write-Host "All shards passed."
  334. env:
  335. VCPKG_ROOT: "C:/vcpkg"
  336. VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"