test.yaml 14 KB

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