test.yaml 15 KB

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