frankenphp_test.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. // In all tests, headers added to requests are copied on the heap using strings.Clone.
  2. // This was originally a workaround for https://github.com/golang/go/issues/65286#issuecomment-1920087884 (fixed in Go 1.22),
  3. // but this allows to catch panics occuring in real life but not when the string is in the internal binary memory.
  4. package frankenphp_test
  5. import (
  6. "bytes"
  7. "context"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "log"
  12. "mime/multipart"
  13. "net/http"
  14. "net/http/cookiejar"
  15. "net/http/httptest"
  16. "net/http/httptrace"
  17. "net/textproto"
  18. "net/url"
  19. "os"
  20. "os/exec"
  21. "path/filepath"
  22. "strconv"
  23. "strings"
  24. "sync"
  25. "testing"
  26. "github.com/dunglas/frankenphp"
  27. "github.com/stretchr/testify/assert"
  28. "github.com/stretchr/testify/require"
  29. "go.uber.org/zap"
  30. "go.uber.org/zap/zapcore"
  31. "go.uber.org/zap/zaptest"
  32. "go.uber.org/zap/zaptest/observer"
  33. )
  34. type testOptions struct {
  35. workerScript string
  36. watch []string
  37. nbWorkers int
  38. env map[string]string
  39. nbParrallelRequests int
  40. realServer bool
  41. logger *zap.Logger
  42. initOpts []frankenphp.Option
  43. }
  44. func runTest(t *testing.T, test func(func(http.ResponseWriter, *http.Request), *httptest.Server, int), opts *testOptions) {
  45. if opts == nil {
  46. opts = &testOptions{}
  47. }
  48. if opts.nbParrallelRequests == 0 {
  49. opts.nbParrallelRequests = 100
  50. }
  51. cwd, _ := os.Getwd()
  52. testDataDir := cwd + "/testdata/"
  53. if opts.logger == nil {
  54. opts.logger = zaptest.NewLogger(t)
  55. }
  56. initOpts := []frankenphp.Option{frankenphp.WithLogger(opts.logger)}
  57. if opts.workerScript != "" {
  58. initOpts = append(initOpts, frankenphp.WithWorkers(testDataDir+opts.workerScript, opts.nbWorkers, opts.env, opts.watch))
  59. }
  60. initOpts = append(initOpts, opts.initOpts...)
  61. err := frankenphp.Init(initOpts...)
  62. require.Nil(t, err)
  63. defer frankenphp.Shutdown()
  64. handler := func(w http.ResponseWriter, r *http.Request) {
  65. req, err := frankenphp.NewRequestWithContext(r, frankenphp.WithRequestDocumentRoot(testDataDir, false))
  66. assert.NoError(t, err)
  67. err = frankenphp.ServeHTTP(w, req)
  68. assert.NoError(t, err)
  69. }
  70. var ts *httptest.Server
  71. if opts.realServer {
  72. ts = httptest.NewServer(http.HandlerFunc(handler))
  73. defer ts.Close()
  74. }
  75. var wg sync.WaitGroup
  76. wg.Add(opts.nbParrallelRequests)
  77. for i := 0; i < opts.nbParrallelRequests; i++ {
  78. go func(i int) {
  79. test(handler, ts, i)
  80. wg.Done()
  81. }(i)
  82. }
  83. wg.Wait()
  84. }
  85. func TestHelloWorld_module(t *testing.T) { testHelloWorld(t, nil) }
  86. func TestHelloWorld_worker(t *testing.T) {
  87. testHelloWorld(t, &testOptions{workerScript: "index.php"})
  88. }
  89. func testHelloWorld(t *testing.T, opts *testOptions) {
  90. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  91. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/index.php?i=%d", i), nil)
  92. w := httptest.NewRecorder()
  93. handler(w, req)
  94. resp := w.Result()
  95. body, _ := io.ReadAll(resp.Body)
  96. assert.Equal(t, fmt.Sprintf("I am by birth a Genevese (%d)", i), string(body))
  97. }, opts)
  98. }
  99. func TestFinishRequest_module(t *testing.T) { testFinishRequest(t, nil) }
  100. func TestFinishRequest_worker(t *testing.T) {
  101. testFinishRequest(t, &testOptions{workerScript: "finish-request.php"})
  102. }
  103. func testFinishRequest(t *testing.T, opts *testOptions) {
  104. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  105. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/finish-request.php?i=%d", i), nil)
  106. w := httptest.NewRecorder()
  107. handler(w, req)
  108. resp := w.Result()
  109. body, _ := io.ReadAll(resp.Body)
  110. assert.Equal(t, fmt.Sprintf("This is output %d\n", i), string(body))
  111. }, opts)
  112. }
  113. func TestServerVariable_module(t *testing.T) {
  114. testServerVariable(t, nil)
  115. }
  116. func TestServerVariable_worker(t *testing.T) {
  117. testServerVariable(t, &testOptions{workerScript: "server-variable.php"})
  118. }
  119. func testServerVariable(t *testing.T, opts *testOptions) {
  120. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  121. req := httptest.NewRequest("POST", fmt.Sprintf("http://example.com/server-variable.php/baz/bat?foo=a&bar=b&i=%d#hash", i), strings.NewReader("foo"))
  122. req.SetBasicAuth(strings.Clone("kevin"), strings.Clone("password"))
  123. req.Header.Add(strings.Clone("Content-Type"), strings.Clone("text/plain"))
  124. w := httptest.NewRecorder()
  125. handler(w, req)
  126. resp := w.Result()
  127. body, _ := io.ReadAll(resp.Body)
  128. strBody := string(body)
  129. assert.Contains(t, strBody, "[REMOTE_HOST]")
  130. assert.Contains(t, strBody, "[REMOTE_USER] => kevin")
  131. assert.Contains(t, strBody, "[PHP_AUTH_USER] => kevin")
  132. assert.Contains(t, strBody, "[PHP_AUTH_PW] => password")
  133. assert.Contains(t, strBody, "[HTTP_AUTHORIZATION] => Basic a2V2aW46cGFzc3dvcmQ=")
  134. assert.Contains(t, strBody, "[DOCUMENT_ROOT]")
  135. assert.Contains(t, strBody, "[PHP_SELF] => /server-variable.php/baz/bat")
  136. assert.Contains(t, strBody, "[CONTENT_TYPE] => text/plain")
  137. assert.Contains(t, strBody, fmt.Sprintf("[QUERY_STRING] => foo=a&bar=b&i=%d#hash", i))
  138. assert.Contains(t, strBody, fmt.Sprintf("[REQUEST_URI] => /server-variable.php/baz/bat?foo=a&bar=b&i=%d#hash", i))
  139. assert.Contains(t, strBody, "[CONTENT_LENGTH]")
  140. assert.Contains(t, strBody, "[REMOTE_ADDR]")
  141. assert.Contains(t, strBody, "[REMOTE_PORT]")
  142. assert.Contains(t, strBody, "[REQUEST_SCHEME] => http")
  143. assert.Contains(t, strBody, "[DOCUMENT_URI]")
  144. assert.Contains(t, strBody, "[AUTH_TYPE]")
  145. assert.Contains(t, strBody, "[REMOTE_IDENT]")
  146. assert.Contains(t, strBody, "[REQUEST_METHOD] => POST")
  147. assert.Contains(t, strBody, "[SERVER_NAME] => example.com")
  148. assert.Contains(t, strBody, "[SERVER_PROTOCOL] => HTTP/1.1")
  149. assert.Contains(t, strBody, "[SCRIPT_FILENAME]")
  150. assert.Contains(t, strBody, "[SERVER_SOFTWARE] => FrankenPHP")
  151. assert.Contains(t, strBody, "[REQUEST_TIME_FLOAT]")
  152. assert.Contains(t, strBody, "[REQUEST_TIME]")
  153. assert.Contains(t, strBody, "[SERVER_PORT] => 80")
  154. }, opts)
  155. }
  156. func TestPathInfo_module(t *testing.T) { testPathInfo(t, nil) }
  157. func TestPathInfo_worker(t *testing.T) {
  158. testPathInfo(t, &testOptions{workerScript: "server-variable.php"})
  159. }
  160. func testPathInfo(t *testing.T, opts *testOptions) {
  161. cwd, _ := os.Getwd()
  162. testDataDir := cwd + strings.Clone("/testdata/")
  163. path := strings.Clone("/server-variable.php/pathinfo")
  164. runTest(t, func(_ func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  165. handler := func(w http.ResponseWriter, r *http.Request) {
  166. requestURI := r.URL.RequestURI()
  167. r.URL.Path = path
  168. rewriteRequest, err := frankenphp.NewRequestWithContext(r,
  169. frankenphp.WithRequestDocumentRoot(testDataDir, false),
  170. frankenphp.WithRequestEnv(map[string]string{"REQUEST_URI": requestURI}),
  171. )
  172. assert.NoError(t, err)
  173. err = frankenphp.ServeHTTP(w, rewriteRequest)
  174. assert.NoError(t, err)
  175. }
  176. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/pathinfo/%d", i), nil)
  177. w := httptest.NewRecorder()
  178. handler(w, req)
  179. resp := w.Result()
  180. body, _ := io.ReadAll(resp.Body)
  181. strBody := string(body)
  182. assert.Contains(t, strBody, "[PATH_INFO] => /pathinfo")
  183. assert.Contains(t, strBody, fmt.Sprintf("[REQUEST_URI] => /pathinfo/%d", i))
  184. assert.Contains(t, strBody, "[PATH_TRANSLATED] =>")
  185. assert.Contains(t, strBody, "[SCRIPT_NAME] => /server-variable.php")
  186. }, opts)
  187. }
  188. func TestHeaders_module(t *testing.T) { testHeaders(t, nil) }
  189. func TestHeaders_worker(t *testing.T) { testHeaders(t, &testOptions{workerScript: "headers.php"}) }
  190. func testHeaders(t *testing.T, opts *testOptions) {
  191. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  192. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/headers.php?i=%d", i), nil)
  193. w := httptest.NewRecorder()
  194. handler(w, req)
  195. resp := w.Result()
  196. body, _ := io.ReadAll(resp.Body)
  197. assert.Equal(t, "Hello", string(body))
  198. assert.Equal(t, 201, resp.StatusCode)
  199. assert.Equal(t, "bar", resp.Header.Get("Foo"))
  200. assert.Equal(t, "bar2", resp.Header.Get("Foo2"))
  201. assert.Empty(t, resp.Header.Get("Invalid"))
  202. assert.Equal(t, fmt.Sprintf("%d", i), resp.Header.Get("I"))
  203. }, opts)
  204. }
  205. func TestResponseHeaders_module(t *testing.T) { testResponseHeaders(t, nil) }
  206. func TestResponseHeaders_worker(t *testing.T) {
  207. testResponseHeaders(t, &testOptions{workerScript: "response-headers.php"})
  208. }
  209. func testResponseHeaders(t *testing.T, opts *testOptions) {
  210. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  211. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/response-headers.php?i=%d", i), nil)
  212. w := httptest.NewRecorder()
  213. handler(w, req)
  214. resp := w.Result()
  215. body, _ := io.ReadAll(resp.Body)
  216. if i%3 != 0 {
  217. assert.Equal(t, i+100, resp.StatusCode)
  218. } else {
  219. assert.Equal(t, 200, resp.StatusCode)
  220. }
  221. assert.Contains(t, string(body), "'X-Powered-By' => 'PH")
  222. assert.Contains(t, string(body), "'Foo' => 'bar',")
  223. assert.Contains(t, string(body), "'Foo2' => 'bar2',")
  224. assert.Contains(t, string(body), fmt.Sprintf("'I' => '%d',", i))
  225. assert.NotContains(t, string(body), "Invalid")
  226. }, opts)
  227. }
  228. func TestInput_module(t *testing.T) { testInput(t, nil) }
  229. func TestInput_worker(t *testing.T) { testInput(t, &testOptions{workerScript: "input.php"}) }
  230. func testInput(t *testing.T, opts *testOptions) {
  231. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  232. req := httptest.NewRequest("POST", "http://example.com/input.php", strings.NewReader(fmt.Sprintf("post data %d", i)))
  233. w := httptest.NewRecorder()
  234. handler(w, req)
  235. resp := w.Result()
  236. body, _ := io.ReadAll(resp.Body)
  237. assert.Equal(t, fmt.Sprintf("post data %d", i), string(body))
  238. assert.Equal(t, "bar", resp.Header.Get("Foo"))
  239. }, opts)
  240. }
  241. func TestPostSuperGlobals_module(t *testing.T) { testPostSuperGlobals(t, nil) }
  242. func TestPostSuperGlobals_worker(t *testing.T) {
  243. testPostSuperGlobals(t, &testOptions{workerScript: "super-globals.php"})
  244. }
  245. func testPostSuperGlobals(t *testing.T, opts *testOptions) {
  246. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  247. formData := url.Values{"baz": {"bat"}, "i": {fmt.Sprintf("%d", i)}}
  248. req := httptest.NewRequest("POST", fmt.Sprintf("http://example.com/super-globals.php?foo=bar&iG=%d", i), strings.NewReader(formData.Encode()))
  249. req.Header.Set("Content-Type", strings.Clone("application/x-www-form-urlencoded"))
  250. w := httptest.NewRecorder()
  251. handler(w, req)
  252. resp := w.Result()
  253. body, _ := io.ReadAll(resp.Body)
  254. assert.Contains(t, string(body), "'foo' => 'bar'")
  255. assert.Contains(t, string(body), fmt.Sprintf("'i' => '%d'", i))
  256. assert.Contains(t, string(body), "'baz' => 'bat'")
  257. assert.Contains(t, string(body), fmt.Sprintf("'iG' => '%d'", i))
  258. }, opts)
  259. }
  260. func TestCookies_module(t *testing.T) { testCookies(t, nil) }
  261. func TestCookies_worker(t *testing.T) { testCookies(t, &testOptions{workerScript: "cookies.php"}) }
  262. func testCookies(t *testing.T, opts *testOptions) {
  263. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  264. req := httptest.NewRequest("GET", "http://example.com/cookies.php", nil)
  265. req.AddCookie(&http.Cookie{Name: "foo", Value: "bar"})
  266. req.AddCookie(&http.Cookie{Name: "i", Value: fmt.Sprintf("%d", i)})
  267. w := httptest.NewRecorder()
  268. handler(w, req)
  269. resp := w.Result()
  270. body, _ := io.ReadAll(resp.Body)
  271. assert.Contains(t, string(body), "'foo' => 'bar'")
  272. assert.Contains(t, string(body), fmt.Sprintf("'i' => '%d'", i))
  273. }, opts)
  274. }
  275. func TestSession_module(t *testing.T) { testSession(t, nil) }
  276. func TestSession_worker(t *testing.T) {
  277. testSession(t, &testOptions{workerScript: "session.php"})
  278. }
  279. func testSession(t *testing.T, opts *testOptions) {
  280. if opts == nil {
  281. opts = &testOptions{}
  282. }
  283. opts.realServer = true
  284. runTest(t, func(_ func(http.ResponseWriter, *http.Request), ts *httptest.Server, i int) {
  285. jar, err := cookiejar.New(&cookiejar.Options{})
  286. assert.NoError(t, err)
  287. client := &http.Client{Jar: jar}
  288. resp1, err := client.Get(ts.URL + "/session.php")
  289. assert.NoError(t, err)
  290. body1, _ := io.ReadAll(resp1.Body)
  291. assert.Equal(t, "Count: 0\n", string(body1))
  292. resp2, err := client.Get(ts.URL + "/session.php")
  293. assert.NoError(t, err)
  294. body2, _ := io.ReadAll(resp2.Body)
  295. assert.Equal(t, "Count: 1\n", string(body2))
  296. }, opts)
  297. }
  298. func TestPhpInfo_module(t *testing.T) { testPhpInfo(t, nil) }
  299. func TestPhpInfo_worker(t *testing.T) { testPhpInfo(t, &testOptions{workerScript: "phpinfo.php"}) }
  300. func testPhpInfo(t *testing.T, opts *testOptions) {
  301. var logOnce sync.Once
  302. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  303. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/phpinfo.php?i=%d", i), nil)
  304. w := httptest.NewRecorder()
  305. handler(w, req)
  306. resp := w.Result()
  307. body, _ := io.ReadAll(resp.Body)
  308. logOnce.Do(func() {
  309. t.Log(string(body))
  310. })
  311. assert.Contains(t, string(body), "frankenphp")
  312. assert.Contains(t, string(body), fmt.Sprintf("i=%d", i))
  313. }, opts)
  314. }
  315. func TestPersistentObject_module(t *testing.T) { testPersistentObject(t, nil) }
  316. func TestPersistentObject_worker(t *testing.T) {
  317. testPersistentObject(t, &testOptions{workerScript: "persistent-object.php"})
  318. }
  319. func testPersistentObject(t *testing.T, opts *testOptions) {
  320. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  321. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/persistent-object.php?i=%d", i), nil)
  322. w := httptest.NewRecorder()
  323. handler(w, req)
  324. resp := w.Result()
  325. body, _ := io.ReadAll(resp.Body)
  326. assert.Equal(t, fmt.Sprintf(`request: %d
  327. class exists: 1
  328. id: obj1
  329. object id: 1`, i), string(body))
  330. }, opts)
  331. }
  332. func TestAutoloader_module(t *testing.T) { testAutoloader(t, nil) }
  333. func TestAutoloader_worker(t *testing.T) {
  334. testAutoloader(t, &testOptions{workerScript: "autoloader.php"})
  335. }
  336. func testAutoloader(t *testing.T, opts *testOptions) {
  337. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  338. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/autoloader.php?i=%d", i), nil)
  339. w := httptest.NewRecorder()
  340. handler(w, req)
  341. resp := w.Result()
  342. body, _ := io.ReadAll(resp.Body)
  343. assert.Equal(t, fmt.Sprintf(`request %d
  344. my_autoloader`, i), string(body))
  345. }, opts)
  346. }
  347. func TestLog_module(t *testing.T) { testLog(t, &testOptions{}) }
  348. func TestLog_worker(t *testing.T) {
  349. testLog(t, &testOptions{workerScript: "log.php"})
  350. }
  351. func testLog(t *testing.T, opts *testOptions) {
  352. logger, logs := observer.New(zapcore.InfoLevel)
  353. opts.logger = zap.New(logger)
  354. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  355. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/log.php?i=%d", i), nil)
  356. w := httptest.NewRecorder()
  357. handler(w, req)
  358. for logs.FilterMessage(fmt.Sprintf("request %d", i)).Len() <= 0 {
  359. }
  360. }, opts)
  361. }
  362. func TestConnectionAbort_module(t *testing.T) { testConnectionAbort(t, &testOptions{}) }
  363. func TestConnectionAbort_worker(t *testing.T) {
  364. testConnectionAbort(t, &testOptions{workerScript: "connectionStatusLog.php"})
  365. }
  366. func testConnectionAbort(t *testing.T, opts *testOptions) {
  367. testFinish := func(finish string) {
  368. t.Run(fmt.Sprintf("finish=%s", finish), func(t *testing.T) {
  369. logger, logs := observer.New(zapcore.InfoLevel)
  370. opts.logger = zap.New(logger)
  371. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  372. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/connectionStatusLog.php?i=%d&finish=%s", i, finish), nil)
  373. w := httptest.NewRecorder()
  374. ctx, cancel := context.WithCancel(req.Context())
  375. req = req.WithContext(ctx)
  376. cancel()
  377. handler(w, req)
  378. for logs.FilterMessage(fmt.Sprintf("request %d: 1", i)).Len() <= 0 {
  379. }
  380. }, opts)
  381. })
  382. }
  383. testFinish("0")
  384. testFinish("1")
  385. }
  386. func TestException_module(t *testing.T) { testException(t, &testOptions{}) }
  387. func TestException_worker(t *testing.T) {
  388. testException(t, &testOptions{workerScript: "exception.php"})
  389. }
  390. func testException(t *testing.T, opts *testOptions) {
  391. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  392. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/exception.php?i=%d", i), nil)
  393. w := httptest.NewRecorder()
  394. handler(w, req)
  395. resp := w.Result()
  396. body, _ := io.ReadAll(resp.Body)
  397. assert.Contains(t, string(body), "hello")
  398. assert.Contains(t, string(body), fmt.Sprintf(`Uncaught Exception: request %d`, i))
  399. }, opts)
  400. }
  401. func TestEarlyHints_module(t *testing.T) { testEarlyHints(t, &testOptions{}) }
  402. func TestEarlyHints_worker(t *testing.T) {
  403. testEarlyHints(t, &testOptions{workerScript: "early-hints.php"})
  404. }
  405. func testEarlyHints(t *testing.T, opts *testOptions) {
  406. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  407. var earlyHintReceived bool
  408. trace := &httptrace.ClientTrace{
  409. Got1xxResponse: func(code int, header textproto.MIMEHeader) error {
  410. switch code {
  411. case http.StatusEarlyHints:
  412. assert.Equal(t, "</style.css>; rel=preload; as=style", header.Get("Link"))
  413. assert.Equal(t, strconv.Itoa(i), header.Get("Request"))
  414. earlyHintReceived = true
  415. }
  416. return nil
  417. },
  418. }
  419. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/early-hints.php?i=%d", i), nil)
  420. w := NewRecorder()
  421. w.ClientTrace = trace
  422. handler(w, req)
  423. assert.Equal(t, strconv.Itoa(i), w.Header().Get("Request"))
  424. assert.Equal(t, "", w.Header().Get("Link"))
  425. assert.True(t, earlyHintReceived)
  426. }, opts)
  427. }
  428. type streamResponseRecorder struct {
  429. *httptest.ResponseRecorder
  430. writeCallback func(buf []byte)
  431. }
  432. func (srr *streamResponseRecorder) Write(buf []byte) (int, error) {
  433. srr.writeCallback(buf)
  434. return srr.ResponseRecorder.Write(buf)
  435. }
  436. func TestFlush_module(t *testing.T) { testFlush(t, &testOptions{}) }
  437. func TestFlush_worker(t *testing.T) {
  438. testFlush(t, &testOptions{workerScript: "flush.php"})
  439. }
  440. func testFlush(t *testing.T, opts *testOptions) {
  441. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  442. var j int
  443. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/flush.php?i=%d", i), nil)
  444. w := &streamResponseRecorder{httptest.NewRecorder(), func(buf []byte) {
  445. if j == 0 {
  446. assert.Equal(t, []byte("He"), buf)
  447. } else {
  448. assert.Equal(t, []byte(fmt.Sprintf("llo %d", i)), buf)
  449. }
  450. j++
  451. }}
  452. handler(w, req)
  453. assert.Equal(t, 2, j)
  454. }, opts)
  455. }
  456. func TestLargeRequest_module(t *testing.T) {
  457. testLargeRequest(t, &testOptions{})
  458. }
  459. func TestLargeRequest_worker(t *testing.T) {
  460. testLargeRequest(t, &testOptions{workerScript: "large-request.php"})
  461. }
  462. func testLargeRequest(t *testing.T, opts *testOptions) {
  463. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  464. req := httptest.NewRequest(
  465. "POST",
  466. fmt.Sprintf("http://example.com/large-request.php?i=%d", i),
  467. strings.NewReader(strings.Repeat("f", 6_048_576)),
  468. )
  469. w := httptest.NewRecorder()
  470. handler(w, req)
  471. resp := w.Result()
  472. body, _ := io.ReadAll(resp.Body)
  473. assert.Contains(t, string(body), fmt.Sprintf("Request body size: 6048576 (%d)", i))
  474. }, opts)
  475. }
  476. func TestVersion(t *testing.T) {
  477. v := frankenphp.Version()
  478. assert.GreaterOrEqual(t, v.MajorVersion, 8)
  479. assert.GreaterOrEqual(t, v.MinorVersion, 0)
  480. assert.GreaterOrEqual(t, v.ReleaseVersion, 0)
  481. assert.GreaterOrEqual(t, v.VersionID, 0)
  482. assert.NotEmpty(t, v.Version, 0)
  483. }
  484. func TestFiberNoCgo_module(t *testing.T) { testFiberNoCgo(t, &testOptions{}) }
  485. func TestFiberNonCgo_worker(t *testing.T) {
  486. testFiberNoCgo(t, &testOptions{workerScript: "fiber-no-cgo.php"})
  487. }
  488. func testFiberNoCgo(t *testing.T, opts *testOptions) {
  489. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  490. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/fiber-no-cgo.php?i=%d", i), nil)
  491. w := httptest.NewRecorder()
  492. handler(w, req)
  493. resp := w.Result()
  494. body, _ := io.ReadAll(resp.Body)
  495. assert.Equal(t, string(body), fmt.Sprintf("Fiber %d", i))
  496. }, opts)
  497. }
  498. func TestRequestHeaders_module(t *testing.T) { testRequestHeaders(t, &testOptions{}) }
  499. func TestRequestHeaders_worker(t *testing.T) {
  500. testRequestHeaders(t, &testOptions{workerScript: "request-headers.php"})
  501. }
  502. func testRequestHeaders(t *testing.T, opts *testOptions) {
  503. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  504. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/request-headers.php?i=%d", i), nil)
  505. req.Header.Add(strings.Clone("Content-Type"), strings.Clone("text/plain"))
  506. req.Header.Add(strings.Clone("Frankenphp-I"), strings.Clone(strconv.Itoa(i)))
  507. w := httptest.NewRecorder()
  508. handler(w, req)
  509. resp := w.Result()
  510. body, _ := io.ReadAll(resp.Body)
  511. assert.Contains(t, string(body), "[Content-Type] => text/plain")
  512. assert.Contains(t, string(body), fmt.Sprintf("[Frankenphp-I] => %d", i))
  513. }, opts)
  514. }
  515. func TestFailingWorker(t *testing.T) {
  516. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  517. req := httptest.NewRequest("GET", "http://example.com/failing-worker.php", nil)
  518. w := httptest.NewRecorder()
  519. handler(w, req)
  520. resp := w.Result()
  521. body, _ := io.ReadAll(resp.Body)
  522. assert.Contains(t, string(body), "ok")
  523. }, &testOptions{workerScript: "failing-worker.php"})
  524. }
  525. func TestEnv(t *testing.T) {
  526. testEnv(t, &testOptions{})
  527. }
  528. func TestEnvWorker(t *testing.T) {
  529. testEnv(t, &testOptions{workerScript: "test-env.php"})
  530. }
  531. func testEnv(t *testing.T, opts *testOptions) {
  532. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  533. req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/test-env.php?var=%d", i), nil)
  534. w := httptest.NewRecorder()
  535. handler(w, req)
  536. resp := w.Result()
  537. body, _ := io.ReadAll(resp.Body)
  538. // execute the script as regular php script
  539. cmd := exec.Command("php", "testdata/test-env.php", strconv.Itoa(i))
  540. stdoutStderr, err := cmd.CombinedOutput()
  541. if err != nil {
  542. // php is not installed or other issue, use the hardcoded output below:
  543. stdoutStderr = []byte("Set MY_VAR successfully.\nMY_VAR = HelloWorld\nUnset MY_VAR successfully.\nMY_VAR is unset.\nMY_VAR set to empty successfully.\nMY_VAR = \nUnset NON_EXISTING_VAR successfully.\n")
  544. }
  545. assert.Equal(t, string(stdoutStderr), string(body))
  546. }, opts)
  547. }
  548. func TestFileUpload_module(t *testing.T) { testFileUpload(t, &testOptions{}) }
  549. func TestFileUpload_worker(t *testing.T) {
  550. testFileUpload(t, &testOptions{workerScript: "file-upload.php"})
  551. }
  552. func testFileUpload(t *testing.T, opts *testOptions) {
  553. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
  554. requestBody := &bytes.Buffer{}
  555. writer := multipart.NewWriter(requestBody)
  556. part, _ := writer.CreateFormFile("file", "foo.txt")
  557. _, err := part.Write([]byte("bar"))
  558. require.NoError(t, err)
  559. writer.Close()
  560. req := httptest.NewRequest("POST", "http://example.com/file-upload.php", requestBody)
  561. req.Header.Add("Content-Type", writer.FormDataContentType())
  562. w := httptest.NewRecorder()
  563. handler(w, req)
  564. resp := w.Result()
  565. body, _ := io.ReadAll(resp.Body)
  566. assert.Contains(t, string(body), "Upload OK")
  567. }, opts)
  568. }
  569. func TestExecuteScriptCLI(t *testing.T) {
  570. if _, err := os.Stat("internal/testcli/testcli"); err != nil {
  571. t.Skip("internal/testcli/testcli has not been compiled, run `cd internal/testcli/ && go build`")
  572. }
  573. cmd := exec.Command("internal/testcli/testcli", "testdata/command.php", "foo", "bar")
  574. stdoutStderr, err := cmd.CombinedOutput()
  575. assert.Error(t, err)
  576. var exitError *exec.ExitError
  577. if errors.As(err, &exitError) {
  578. assert.Equal(t, 3, exitError.ExitCode())
  579. }
  580. stdoutStderrStr := string(stdoutStderr)
  581. assert.Contains(t, stdoutStderrStr, `"foo"`)
  582. assert.Contains(t, stdoutStderrStr, `"bar"`)
  583. assert.Contains(t, stdoutStderrStr, "From the CLI")
  584. }
  585. func ExampleServeHTTP() {
  586. if err := frankenphp.Init(); err != nil {
  587. panic(err)
  588. }
  589. defer frankenphp.Shutdown()
  590. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  591. req, err := frankenphp.NewRequestWithContext(r, frankenphp.WithRequestDocumentRoot("/path/to/document/root", false))
  592. if err != nil {
  593. panic(err)
  594. }
  595. if err := frankenphp.ServeHTTP(w, req); err != nil {
  596. panic(err)
  597. }
  598. })
  599. log.Fatal(http.ListenAndServe(":8080", nil))
  600. }
  601. func ExampleExecuteScriptCLI() {
  602. if len(os.Args) <= 1 {
  603. log.Println("Usage: my-program script.php")
  604. os.Exit(1)
  605. }
  606. os.Exit(frankenphp.ExecuteScriptCLI(os.Args[1], os.Args))
  607. }
  608. func BenchmarkHelloWorld(b *testing.B) {
  609. if err := frankenphp.Init(frankenphp.WithLogger(zap.NewNop())); err != nil {
  610. panic(err)
  611. }
  612. defer frankenphp.Shutdown()
  613. cwd, _ := os.Getwd()
  614. testDataDir := cwd + "/testdata/"
  615. handler := func(w http.ResponseWriter, r *http.Request) {
  616. req, err := frankenphp.NewRequestWithContext(r, frankenphp.WithRequestDocumentRoot(testDataDir, false))
  617. if err != nil {
  618. panic(err)
  619. }
  620. if err := frankenphp.ServeHTTP(w, req); err != nil {
  621. panic(err)
  622. }
  623. }
  624. req := httptest.NewRequest("GET", "http://example.com/index.php", nil)
  625. w := httptest.NewRecorder()
  626. b.ResetTimer()
  627. for i := 0; i < b.N; i++ {
  628. handler(w, req)
  629. }
  630. }
  631. func BenchmarkEcho(b *testing.B) {
  632. if err := frankenphp.Init(frankenphp.WithLogger(zap.NewNop())); err != nil {
  633. panic(err)
  634. }
  635. defer frankenphp.Shutdown()
  636. cwd, _ := os.Getwd()
  637. testDataDir := cwd + "/testdata/"
  638. handler := func(w http.ResponseWriter, r *http.Request) {
  639. req, err := frankenphp.NewRequestWithContext(r, frankenphp.WithRequestDocumentRoot(testDataDir, false))
  640. if err != nil {
  641. panic(err)
  642. }
  643. if err := frankenphp.ServeHTTP(w, req); err != nil {
  644. panic(err)
  645. }
  646. }
  647. const body = `{
  648. "squadName": "Super hero squad",
  649. "homeTown": "Metro City",
  650. "formed": 2016,
  651. "secretBase": "Super tower",
  652. "active": true,
  653. "members": [
  654. {
  655. "name": "Molecule Man",
  656. "age": 29,
  657. "secretIdentity": "Dan Jukes",
  658. "powers": ["Radiation resistance", "Turning tiny", "Radiation blast"]
  659. },
  660. {
  661. "name": "Madame Uppercut",
  662. "age": 39,
  663. "secretIdentity": "Jane Wilson",
  664. "powers": [
  665. "Million tonne punch",
  666. "Damage resistance",
  667. "Superhuman reflexes"
  668. ]
  669. },
  670. {
  671. "name": "Eternal Flame",
  672. "age": 1000000,
  673. "secretIdentity": "Unknown",
  674. "powers": [
  675. "Immortality",
  676. "Heat Immunity",
  677. "Inferno",
  678. "Teleportation",
  679. "Interdimensional travel"
  680. ]
  681. }
  682. ]
  683. }`
  684. r := strings.NewReader(body)
  685. req := httptest.NewRequest("POST", "http://example.com/echo.php", r)
  686. w := httptest.NewRecorder()
  687. b.ResetTimer()
  688. for i := 0; i < b.N; i++ {
  689. r.Reset(body)
  690. handler(w, req)
  691. }
  692. }
  693. func BenchmarkServerSuperGlobal(b *testing.B) {
  694. if err := frankenphp.Init(frankenphp.WithLogger(zap.NewNop())); err != nil {
  695. panic(err)
  696. }
  697. defer frankenphp.Shutdown()
  698. cwd, _ := os.Getwd()
  699. testDataDir := cwd + "/testdata/"
  700. // Mimicks headers of a request sent by Firefox to GitHub
  701. headers := http.Header{}
  702. headers.Add(strings.Clone("Accept"), strings.Clone("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"))
  703. headers.Add(strings.Clone("Accept-Encoding"), strings.Clone("gzip, deflate, br"))
  704. headers.Add(strings.Clone("Accept-Language"), strings.Clone("fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3"))
  705. headers.Add(strings.Clone("Cache-Control"), strings.Clone("no-cache"))
  706. headers.Add(strings.Clone("Connection"), strings.Clone("keep-alive"))
  707. headers.Add(strings.Clone("Cookie"), strings.Clone("user_session=myrandomuuid; __Host-user_session_same_site=myotherrandomuuid; dotcom_user=dunglas; logged_in=yes; _foo=barbarbarbarbarbar; _device_id=anotherrandomuuid; color_mode=foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar; preferred_color_mode=light; tz=Europe%2FParis; has_recent_activity=1"))
  708. headers.Add(strings.Clone("DNT"), strings.Clone("1"))
  709. headers.Add(strings.Clone("Host"), strings.Clone("example.com"))
  710. headers.Add(strings.Clone("Pragma"), strings.Clone("no-cache"))
  711. headers.Add(strings.Clone("Sec-Fetch-Dest"), strings.Clone("document"))
  712. headers.Add(strings.Clone("Sec-Fetch-Mode"), strings.Clone("navigate"))
  713. headers.Add(strings.Clone("Sec-Fetch-Site"), strings.Clone("cross-site"))
  714. headers.Add(strings.Clone("Sec-GPC"), strings.Clone("1"))
  715. headers.Add(strings.Clone("Upgrade-Insecure-Requests"), strings.Clone("1"))
  716. headers.Add(strings.Clone("User-Agent"), strings.Clone("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:122.0) Gecko/20100101 Firefox/122.0"))
  717. // Env vars available in a typical Docker container
  718. env := map[string]string{
  719. "HOSTNAME": "a88e81aa22e4",
  720. "PHP_INI_DIR": "/usr/local/etc/php",
  721. "HOME": "/root",
  722. "GODEBUG": "cgocheck=0",
  723. "PHP_LDFLAGS": "-Wl,-O1 -pie",
  724. "PHP_CFLAGS": "-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64",
  725. "PHP_VERSION": "8.3.2",
  726. "GPG_KEYS": "1198C0117593497A5EC5C199286AF1F9897469DC C28D937575603EB4ABB725861C0779DC5C0A9DE4 AFD8691FDAEDF03BDF6E460563F15A9B715376CA",
  727. "PHP_CPPFLAGS": "-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64",
  728. "PHP_ASC_URL": "https://www.php.net/distributions/php-8.3.2.tar.xz.asc",
  729. "PHP_URL": "https://www.php.net/distributions/php-8.3.2.tar.xz",
  730. "PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  731. "XDG_CONFIG_HOME": "/config",
  732. "XDG_DATA_HOME": "/data",
  733. "PHPIZE_DEPS": "autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c",
  734. "PWD": "/app",
  735. "PHP_SHA256": "4ffa3e44afc9c590e28dc0d2d31fc61f0139f8b335f11880a121b9f9b9f0634e",
  736. }
  737. preparedEnv := frankenphp.PrepareEnv(env)
  738. handler := func(w http.ResponseWriter, r *http.Request) {
  739. req, err := frankenphp.NewRequestWithContext(r, frankenphp.WithRequestDocumentRoot(testDataDir, false), frankenphp.WithRequestPreparedEnv(preparedEnv))
  740. if err != nil {
  741. panic(err)
  742. }
  743. r.Header = headers
  744. if err := frankenphp.ServeHTTP(w, req); err != nil {
  745. panic(err)
  746. }
  747. }
  748. req := httptest.NewRequest("GET", "http://example.com/server-variable.php", nil)
  749. w := httptest.NewRecorder()
  750. b.ResetTimer()
  751. for i := 0; i < b.N; i++ {
  752. handler(w, req)
  753. }
  754. }
  755. func TestRejectInvalidHeaders_module(t *testing.T) { testRejectInvalidHeaders(t, &testOptions{}) }
  756. func TestRejectInvalidHeaders_worker(t *testing.T) {
  757. testRejectInvalidHeaders(t, &testOptions{workerScript: "headers.php"})
  758. }
  759. func testRejectInvalidHeaders(t *testing.T, opts *testOptions) {
  760. invalidHeaders := [][]string{
  761. {"Content-Length", "-1"},
  762. {"Content-Length", "something"},
  763. }
  764. for _, header := range invalidHeaders {
  765. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) {
  766. req := httptest.NewRequest("GET", "http://example.com/headers.php", nil)
  767. req.Header.Add(header[0], header[1])
  768. w := httptest.NewRecorder()
  769. handler(w, req)
  770. resp := w.Result()
  771. body, _ := io.ReadAll(resp.Body)
  772. assert.Equal(t, 400, resp.StatusCode)
  773. assert.Contains(t, string(body), "invalid")
  774. }, opts)
  775. }
  776. }
  777. // To run this fuzzing test use: go test -fuzz FuzzRequest
  778. // TODO: Cover more potential cases
  779. func FuzzRequest(f *testing.F) {
  780. f.Add("hello world")
  781. f.Add("😀😅🙃🤩🥲🤪😘😇😉🐘🧟")
  782. f.Add("%00%11%%22%%33%%44%%55%%66%%77%%88%%99%%aa%%bb%%cc%%dd%%ee%%ff")
  783. f.Add("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f")
  784. f.Fuzz(func(t *testing.T, fuzzedString string) {
  785. absPath, _ := filepath.Abs("./testdata/")
  786. runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) {
  787. req := httptest.NewRequest("GET", "http://example.com/server-variable", nil)
  788. req.URL = &url.URL{RawQuery: "test=" + fuzzedString, Path: "/server-variable.php/" + fuzzedString}
  789. req.Header.Add(strings.Clone("Fuzzed"), strings.Clone(fuzzedString))
  790. req.Header.Add(strings.Clone("Content-Type"), fuzzedString)
  791. w := httptest.NewRecorder()
  792. handler(w, req)
  793. resp := w.Result()
  794. body, _ := io.ReadAll(resp.Body)
  795. // The response status must be 400 if the request path contains null bytes
  796. if strings.Contains(req.URL.Path, "\x00") {
  797. assert.Equal(t, 400, resp.StatusCode)
  798. assert.Contains(t, string(body), "Invalid request path")
  799. return
  800. }
  801. // The fuzzed string must be present in the path
  802. assert.Contains(t, string(body), fmt.Sprintf("[PATH_INFO] => /%s", fuzzedString))
  803. assert.Contains(t, string(body), fmt.Sprintf("[PATH_TRANSLATED] => %s", filepath.Join(absPath, fuzzedString)))
  804. // The header should only be present if the fuzzed string is not empty
  805. if len(fuzzedString) > 0 {
  806. assert.Contains(t, string(body), fmt.Sprintf("[CONTENT_TYPE] => %s", fuzzedString))
  807. assert.Contains(t, string(body), fmt.Sprintf("[HTTP_FUZZED] => %s", fuzzedString))
  808. } else {
  809. assert.NotContains(t, string(body), "[HTTP_FUZZED]")
  810. }
  811. }, &testOptions{workerScript: "request-headers.php"})
  812. })
  813. }