Contains the Concourse pipeline definition for building a line-server container
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1230 lines
26 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "encoding/json"
  6. "fmt"
  7. "mime/multipart"
  8. "net/http"
  9. "net/http/httptest"
  10. "net/url"
  11. "os"
  12. "path"
  13. "strconv"
  14. "strings"
  15. "testing"
  16. "time"
  17. )
  18. type RespOkJSON struct {
  19. Filename string
  20. Url string
  21. Delete_Key string
  22. Expiry string
  23. Size string
  24. }
  25. type RespErrJSON struct {
  26. Error string
  27. }
  28. func TestSetup(t *testing.T) {
  29. Config.siteURL = "http://linx.example.org/"
  30. Config.filesDir = path.Join(os.TempDir(), generateBarename())
  31. Config.metaDir = Config.filesDir + "_meta"
  32. Config.maxSize = 1024 * 1024 * 1024
  33. Config.noLogs = true
  34. Config.siteName = "linx"
  35. }
  36. func TestIndex(t *testing.T) {
  37. mux := setup()
  38. w := httptest.NewRecorder()
  39. req, err := http.NewRequest("GET", "/", nil)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. mux.ServeHTTP(w, req)
  44. if !strings.Contains(w.Body.String(), "Click or Drop file") {
  45. t.Fatal("String 'Click or Drop file' not found in index response")
  46. }
  47. }
  48. func TestIndexStandardMaxExpiry(t *testing.T) {
  49. mux := setup()
  50. Config.maxExpiry = 60
  51. w := httptest.NewRecorder()
  52. req, err := http.NewRequest("GET", "/", nil)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. mux.ServeHTTP(w, req)
  57. if strings.Contains(w.Body.String(), ">1 hour</object>") {
  58. t.Fatal("String '>1 hour</object>' found in index response")
  59. }
  60. Config.maxExpiry = 0
  61. }
  62. func TestIndexWeirdMaxExpiry(t *testing.T) {
  63. mux := setup()
  64. Config.maxExpiry = 1500
  65. w := httptest.NewRecorder()
  66. req, err := http.NewRequest("GET", "/", nil)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. mux.ServeHTTP(w, req)
  71. if strings.Contains(w.Body.String(), ">never</object>") {
  72. t.Fatal("String '>never</object>' found in index response")
  73. }
  74. Config.maxExpiry = 0
  75. }
  76. func TestAddHeader(t *testing.T) {
  77. Config.addHeaders = []string{"Linx-Test: It works!"}
  78. mux := setup()
  79. w := httptest.NewRecorder()
  80. req, err := http.NewRequest("GET", "/", nil)
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. mux.ServeHTTP(w, req)
  85. if w.Header().Get("Linx-Test") != "It works!" {
  86. t.Fatal("Header 'Linx-Test: It works!' not found in index response")
  87. }
  88. }
  89. func TestAuthKeys(t *testing.T) {
  90. Config.authFile = "/dev/null"
  91. redirects := []string{
  92. "/",
  93. "/paste/",
  94. }
  95. mux := setup()
  96. for _, v := range redirects {
  97. w := httptest.NewRecorder()
  98. req, err := http.NewRequest("GET", v, nil)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. mux.ServeHTTP(w, req)
  103. if w.Code != 303 {
  104. t.Fatalf("Status code is not 303, but %d", w.Code)
  105. }
  106. }
  107. w := httptest.NewRecorder()
  108. req, err := http.NewRequest("POST", "/paste/", nil)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. mux.ServeHTTP(w, req)
  113. if w.Code != 401 {
  114. t.Fatalf("Status code is not 401, but %d", w.Code)
  115. }
  116. Config.authFile = ""
  117. }
  118. func TestNotFound(t *testing.T) {
  119. mux := setup()
  120. w := httptest.NewRecorder()
  121. req, err := http.NewRequest("GET", "/url/should/not/exist", nil)
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. mux.ServeHTTP(w, req)
  126. if w.Code != 404 {
  127. t.Fatalf("Expected 404, got %d", w.Code)
  128. }
  129. }
  130. func TestFileNotFound(t *testing.T) {
  131. mux := setup()
  132. w := httptest.NewRecorder()
  133. filename := generateBarename()
  134. req, err := http.NewRequest("GET", "/"+Config.selifPath+filename, nil)
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. mux.ServeHTTP(w, req)
  139. if w.Code != 404 {
  140. t.Fatalf("Expected 404, got %d", w.Code)
  141. }
  142. }
  143. func TestDisplayNotFound(t *testing.T) {
  144. mux := setup()
  145. w := httptest.NewRecorder()
  146. filename := generateBarename()
  147. req, err := http.NewRequest("GET", "/"+filename, nil)
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. mux.ServeHTTP(w, req)
  152. if w.Code != 404 {
  153. t.Fatalf("Expected 404, got %d", w.Code)
  154. }
  155. }
  156. func TestPostCodeUpload(t *testing.T) {
  157. mux := setup()
  158. w := httptest.NewRecorder()
  159. filename := generateBarename()
  160. extension := "txt"
  161. form := url.Values{}
  162. form.Add("content", "File content")
  163. form.Add("filename", filename)
  164. form.Add("extension", extension)
  165. req, err := http.NewRequest("POST", "/upload/", nil)
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. req.PostForm = form
  170. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  171. req.Header.Set("Referer", Config.siteURL)
  172. mux.ServeHTTP(w, req)
  173. if w.Code != 303 {
  174. t.Fatalf("Status code is not 303, but %d", w.Code)
  175. }
  176. if w.Header().Get("Location") != "/"+filename+"."+extension {
  177. t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
  178. }
  179. }
  180. func TestPostCodeUploadWhitelistedHeader(t *testing.T) {
  181. mux := setup()
  182. w := httptest.NewRecorder()
  183. filename := generateBarename()
  184. extension := "txt"
  185. form := url.Values{}
  186. form.Add("content", "File content")
  187. form.Add("filename", filename)
  188. form.Add("extension", extension)
  189. req, err := http.NewRequest("POST", "/upload/", nil)
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. req.PostForm = form
  194. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  195. req.Header.Set("Linx-Expiry", "0")
  196. mux.ServeHTTP(w, req)
  197. if w.Code != 303 {
  198. t.Fatalf("Status code is not 303, but %d", w.Code)
  199. }
  200. }
  201. func TestPostCodeUploadNoReferrer(t *testing.T) {
  202. mux := setup()
  203. w := httptest.NewRecorder()
  204. filename := generateBarename()
  205. extension := "txt"
  206. form := url.Values{}
  207. form.Add("content", "File content")
  208. form.Add("filename", filename)
  209. form.Add("extension", extension)
  210. req, err := http.NewRequest("POST", "/upload/", nil)
  211. if err != nil {
  212. t.Fatal(err)
  213. }
  214. req.PostForm = form
  215. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  216. mux.ServeHTTP(w, req)
  217. if w.Code != 400 {
  218. t.Fatalf("Status code is not 400, but %d", w.Code)
  219. }
  220. }
  221. func TestPostCodeUploadBadOrigin(t *testing.T) {
  222. mux := setup()
  223. w := httptest.NewRecorder()
  224. filename := generateBarename()
  225. extension := "txt"
  226. form := url.Values{}
  227. form.Add("content", "File content")
  228. form.Add("filename", filename)
  229. form.Add("extension", extension)
  230. req, err := http.NewRequest("POST", "/upload/", nil)
  231. if err != nil {
  232. t.Fatal(err)
  233. }
  234. req.PostForm = form
  235. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  236. req.Header.Set("Referer", Config.siteURL)
  237. req.Header.Set("Origin", "http://example.com")
  238. mux.ServeHTTP(w, req)
  239. if w.Code != 400 {
  240. t.Fatalf("Status code is not 400, but %d", w.Code)
  241. }
  242. }
  243. func TestPostCodeExpiryJSONUpload(t *testing.T) {
  244. mux := setup()
  245. w := httptest.NewRecorder()
  246. form := url.Values{}
  247. form.Add("content", "File content")
  248. form.Add("filename", "")
  249. form.Add("expires", "60")
  250. req, err := http.NewRequest("POST", "/upload/", nil)
  251. if err != nil {
  252. t.Fatal(err)
  253. }
  254. req.PostForm = form
  255. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  256. req.Header.Set("Accept", "application/json")
  257. req.Header.Set("Referer", Config.siteURL)
  258. req.Header.Set("Origin", strings.TrimSuffix(Config.siteURL, "/"))
  259. mux.ServeHTTP(w, req)
  260. if w.Code != 200 {
  261. t.Log(w.Body.String())
  262. t.Fatalf("Status code is not 200, but %d", w.Code)
  263. }
  264. var myjson RespOkJSON
  265. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  266. if err != nil {
  267. t.Fatal(err)
  268. }
  269. myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. curTime := time.Now().Unix()
  274. if myExp < curTime {
  275. t.Fatalf("File expiry (%d) is smaller than current time (%d)", myExp, curTime)
  276. }
  277. if myjson.Size != "12" {
  278. t.Fatalf("File size was not 12 but %s", myjson.Size)
  279. }
  280. }
  281. func TestPostUpload(t *testing.T) {
  282. mux := setup()
  283. w := httptest.NewRecorder()
  284. filename := generateBarename() + ".txt"
  285. var b bytes.Buffer
  286. mw := multipart.NewWriter(&b)
  287. fw, err := mw.CreateFormFile("file", filename)
  288. if err != nil {
  289. t.Fatal(err)
  290. }
  291. fw.Write([]byte("File content"))
  292. mw.Close()
  293. req, err := http.NewRequest("POST", "/upload/", &b)
  294. req.Header.Set("Content-Type", mw.FormDataContentType())
  295. req.Header.Set("Referer", Config.siteURL)
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. mux.ServeHTTP(w, req)
  300. if w.Code != 303 {
  301. t.Fatalf("Status code is not 303, but %d", w.Code)
  302. }
  303. if w.Header().Get("Location") != "/"+filename {
  304. t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
  305. }
  306. }
  307. func TestPostJSONUpload(t *testing.T) {
  308. mux := setup()
  309. w := httptest.NewRecorder()
  310. filename := generateBarename() + ".txt"
  311. var b bytes.Buffer
  312. mw := multipart.NewWriter(&b)
  313. fw, err := mw.CreateFormFile("file", filename)
  314. if err != nil {
  315. t.Fatal(err)
  316. }
  317. fw.Write([]byte("File content"))
  318. mw.Close()
  319. req, err := http.NewRequest("POST", "/upload/", &b)
  320. req.Header.Set("Content-Type", mw.FormDataContentType())
  321. req.Header.Set("Accept", "application/json")
  322. req.Header.Set("Referer", Config.siteURL)
  323. if err != nil {
  324. t.Fatal(err)
  325. }
  326. mux.ServeHTTP(w, req)
  327. if w.Code != 200 {
  328. t.Log(w.Body.String())
  329. t.Fatalf("Status code is not 200, but %d", w.Code)
  330. }
  331. var myjson RespOkJSON
  332. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  333. if err != nil {
  334. t.Fatal(err)
  335. }
  336. if myjson.Filename != filename {
  337. t.Fatalf("Filename is not '%s' but '%s' ", filename, myjson.Filename)
  338. }
  339. if myjson.Expiry != "0" {
  340. t.Fatalf("File expiry is not 0 but %s", myjson.Expiry)
  341. }
  342. if myjson.Size != "12" {
  343. t.Fatalf("File size was not 12 but %s", myjson.Size)
  344. }
  345. }
  346. func TestPostJSONUploadMaxExpiry(t *testing.T) {
  347. mux := setup()
  348. Config.maxExpiry = 300
  349. // include 0 to test edge case
  350. // https://github.com/andreimarcu/linx-server/issues/111
  351. testExpiries := []string{"86400", "-150", "0"}
  352. for _, expiry := range testExpiries {
  353. w := httptest.NewRecorder()
  354. filename := generateBarename() + ".txt"
  355. var b bytes.Buffer
  356. mw := multipart.NewWriter(&b)
  357. fw, err := mw.CreateFormFile("file", filename)
  358. if err != nil {
  359. t.Fatal(err)
  360. }
  361. fw.Write([]byte("File content"))
  362. mw.Close()
  363. req, err := http.NewRequest("POST", "/upload/", &b)
  364. req.Header.Set("Content-Type", mw.FormDataContentType())
  365. req.Header.Set("Accept", "application/json")
  366. req.Header.Set("Linx-Expiry", expiry)
  367. if err != nil {
  368. t.Fatal(err)
  369. }
  370. mux.ServeHTTP(w, req)
  371. if w.Code != 200 {
  372. t.Log(w.Body.String())
  373. t.Fatalf("Status code is not 200, but %d", w.Code)
  374. }
  375. var myjson RespOkJSON
  376. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  377. if err != nil {
  378. t.Fatal(err)
  379. }
  380. myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
  381. if err != nil {
  382. t.Fatal(err)
  383. }
  384. expected := time.Now().Add(time.Duration(Config.maxExpiry) * time.Second).Unix()
  385. if myExp != expected {
  386. t.Fatalf("File expiry is not %d but %s", expected, myjson.Expiry)
  387. }
  388. }
  389. Config.maxExpiry = 0
  390. }
  391. func TestPostExpiresJSONUpload(t *testing.T) {
  392. mux := setup()
  393. w := httptest.NewRecorder()
  394. filename := generateBarename() + ".txt"
  395. var b bytes.Buffer
  396. mw := multipart.NewWriter(&b)
  397. fw, err := mw.CreateFormFile("file", filename)
  398. if err != nil {
  399. t.Fatal(err)
  400. }
  401. fw.Write([]byte("File content"))
  402. exp, err := mw.CreateFormField("expires")
  403. if err != nil {
  404. t.Fatal(err)
  405. }
  406. exp.Write([]byte("60"))
  407. mw.Close()
  408. req, err := http.NewRequest("POST", "/upload/", &b)
  409. req.Header.Set("Content-Type", mw.FormDataContentType())
  410. req.Header.Set("Accept", "application/json")
  411. req.Header.Set("Referer", Config.siteURL)
  412. if err != nil {
  413. t.Fatal(err)
  414. }
  415. mux.ServeHTTP(w, req)
  416. if w.Code != 200 {
  417. t.Log(w.Body.String())
  418. t.Fatalf("Status code is not 200, but %d", w.Code)
  419. }
  420. var myjson RespOkJSON
  421. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  422. if err != nil {
  423. t.Fatal(err)
  424. }
  425. if myjson.Filename != filename {
  426. t.Fatalf("Filename is not '%s' but '%s' ", filename, myjson.Filename)
  427. }
  428. myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
  429. if err != nil {
  430. t.Fatal(err)
  431. }
  432. curTime := time.Now().Unix()
  433. if myExp < curTime {
  434. t.Fatalf("File expiry (%d) is smaller than current time (%d)", myExp, curTime)
  435. }
  436. if myjson.Size != "12" {
  437. t.Fatalf("File size was not 12 but %s", myjson.Size)
  438. }
  439. }
  440. func TestPostRandomizeJSONUpload(t *testing.T) {
  441. mux := setup()
  442. w := httptest.NewRecorder()
  443. filename := generateBarename() + ".txt"
  444. var b bytes.Buffer
  445. mw := multipart.NewWriter(&b)
  446. fw, err := mw.CreateFormFile("file", filename)
  447. if err != nil {
  448. t.Fatal(err)
  449. }
  450. fw.Write([]byte("File content"))
  451. rnd, err := mw.CreateFormField("randomize")
  452. if err != nil {
  453. t.Fatal(err)
  454. }
  455. rnd.Write([]byte("true"))
  456. mw.Close()
  457. req, err := http.NewRequest("POST", "/upload/", &b)
  458. req.Header.Set("Content-Type", mw.FormDataContentType())
  459. req.Header.Set("Accept", "application/json")
  460. req.Header.Set("Referer", Config.siteURL)
  461. if err != nil {
  462. t.Fatal(err)
  463. }
  464. mux.ServeHTTP(w, req)
  465. if w.Code != 200 {
  466. t.Log(w.Body.String())
  467. t.Fatalf("Status code is not 200, but %d", w.Code)
  468. }
  469. var myjson RespOkJSON
  470. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  471. if err != nil {
  472. t.Fatal(err)
  473. }
  474. if myjson.Filename == filename {
  475. t.Fatalf("Filename (%s) is not random (%s)", filename, myjson.Filename)
  476. }
  477. if myjson.Size != "12" {
  478. t.Fatalf("File size was not 12 but %s", myjson.Size)
  479. }
  480. }
  481. func TestPostEmptyUpload(t *testing.T) {
  482. mux := setup()
  483. w := httptest.NewRecorder()
  484. filename := generateBarename() + ".txt"
  485. var b bytes.Buffer
  486. mw := multipart.NewWriter(&b)
  487. fw, err := mw.CreateFormFile("file", filename)
  488. if err != nil {
  489. t.Fatal(err)
  490. }
  491. fw.Write([]byte(""))
  492. mw.Close()
  493. req, err := http.NewRequest("POST", "/upload/", &b)
  494. req.Header.Set("Content-Type", mw.FormDataContentType())
  495. req.Header.Set("Referer", Config.siteURL)
  496. if err != nil {
  497. t.Fatal(err)
  498. }
  499. mux.ServeHTTP(w, req)
  500. if w.Code != 400 {
  501. t.Log(w.Body.String())
  502. t.Fatalf("Status code is not 400, but %d", w.Code)
  503. }
  504. }
  505. func TestPostTooLargeUpload(t *testing.T) {
  506. mux := setup()
  507. oldMaxSize := Config.maxSize
  508. Config.maxSize = 2
  509. w := httptest.NewRecorder()
  510. filename := generateBarename() + ".txt"
  511. var b bytes.Buffer
  512. mw := multipart.NewWriter(&b)
  513. fw, err := mw.CreateFormFile("file", filename)
  514. if err != nil {
  515. t.Fatal(err)
  516. }
  517. fw.Write([]byte("test content"))
  518. mw.Close()
  519. req, err := http.NewRequest("POST", "/upload/", &b)
  520. req.Header.Set("Content-Type", mw.FormDataContentType())
  521. req.Header.Set("Referer", Config.siteURL)
  522. if err != nil {
  523. t.Fatal(err)
  524. }
  525. mux.ServeHTTP(w, req)
  526. if w.Code != 400 {
  527. t.Log(w.Body.String())
  528. t.Fatalf("Status code is not 400, but %d", w.Code)
  529. }
  530. Config.maxSize = oldMaxSize
  531. }
  532. func TestPostEmptyJSONUpload(t *testing.T) {
  533. mux := setup()
  534. w := httptest.NewRecorder()
  535. filename := generateBarename() + ".txt"
  536. var b bytes.Buffer
  537. mw := multipart.NewWriter(&b)
  538. fw, err := mw.CreateFormFile("file", filename)
  539. if err != nil {
  540. t.Fatal(err)
  541. }
  542. fw.Write([]byte(""))
  543. mw.Close()
  544. req, err := http.NewRequest("POST", "/upload/", &b)
  545. req.Header.Set("Content-Type", mw.FormDataContentType())
  546. req.Header.Set("Accept", "application/json")
  547. req.Header.Set("Referer", Config.siteURL)
  548. if err != nil {
  549. t.Fatal(err)
  550. }
  551. mux.ServeHTTP(w, req)
  552. if w.Code != 400 {
  553. t.Log(w.Body.String())
  554. t.Fatalf("Status code is not 400, but %d", w.Code)
  555. }
  556. var myjson RespErrJSON
  557. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  558. if err != nil {
  559. t.Fatal(err)
  560. }
  561. if myjson.Error != "Empty file" {
  562. t.Fatal("Json 'error' was not 'Empty file' but " + myjson.Error)
  563. }
  564. }
  565. func TestPutUpload(t *testing.T) {
  566. mux := setup()
  567. w := httptest.NewRecorder()
  568. filename := generateBarename() + ".file"
  569. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  570. if err != nil {
  571. t.Fatal(err)
  572. }
  573. mux.ServeHTTP(w, req)
  574. if w.Body.String() != fmt.Sprintf("%s\n", Config.siteURL+filename) {
  575. t.Fatal("Response was not expected URL")
  576. }
  577. }
  578. func TestPutRandomizedUpload(t *testing.T) {
  579. mux := setup()
  580. w := httptest.NewRecorder()
  581. filename := generateBarename() + ".file"
  582. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  583. if err != nil {
  584. t.Fatal(err)
  585. }
  586. req.Header.Set("Linx-Randomize", "yes")
  587. mux.ServeHTTP(w, req)
  588. if w.Body.String() == Config.siteURL+filename {
  589. t.Fatal("Filename was not random")
  590. }
  591. }
  592. func TestPutNoExtensionUpload(t *testing.T) {
  593. mux := setup()
  594. w := httptest.NewRecorder()
  595. filename := generateBarename()
  596. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  597. if err != nil {
  598. t.Fatal(err)
  599. }
  600. req.Header.Set("Linx-Randomize", "yes")
  601. mux.ServeHTTP(w, req)
  602. if w.Body.String() == Config.siteURL+filename {
  603. t.Fatal("Filename was not random")
  604. }
  605. }
  606. func TestPutEmptyUpload(t *testing.T) {
  607. mux := setup()
  608. w := httptest.NewRecorder()
  609. filename := generateBarename() + ".file"
  610. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader(""))
  611. if err != nil {
  612. t.Fatal(err)
  613. }
  614. req.Header.Set("Linx-Randomize", "yes")
  615. mux.ServeHTTP(w, req)
  616. if w.Code != 400 {
  617. t.Fatalf("Status code is not 400, but %d", w.Code)
  618. }
  619. }
  620. func TestPutTooLargeUpload(t *testing.T) {
  621. mux := setup()
  622. oldMaxSize := Config.maxSize
  623. Config.maxSize = 2
  624. w := httptest.NewRecorder()
  625. filename := generateBarename() + ".file"
  626. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File too big"))
  627. if err != nil {
  628. t.Fatal(err)
  629. }
  630. req.Header.Set("Linx-Randomize", "yes")
  631. mux.ServeHTTP(w, req)
  632. if w.Code != 500 {
  633. t.Log(w.Body.String())
  634. t.Fatalf("Status code is not 500, but %d", w.Code)
  635. }
  636. if !strings.Contains(w.Body.String(), "request body too large") {
  637. t.Fatal("Response did not contain 'request body too large'")
  638. }
  639. Config.maxSize = oldMaxSize
  640. }
  641. func TestPutJSONUpload(t *testing.T) {
  642. var myjson RespOkJSON
  643. mux := setup()
  644. w := httptest.NewRecorder()
  645. filename := generateBarename() + ".file"
  646. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  647. if err != nil {
  648. t.Fatal(err)
  649. }
  650. req.Header.Set("Accept", "application/json")
  651. mux.ServeHTTP(w, req)
  652. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  653. if err != nil {
  654. t.Fatal(err)
  655. }
  656. if myjson.Filename != filename {
  657. t.Fatal("Filename was not provided one but " + myjson.Filename)
  658. }
  659. }
  660. func TestPutRandomizedJSONUpload(t *testing.T) {
  661. var myjson RespOkJSON
  662. mux := setup()
  663. w := httptest.NewRecorder()
  664. filename := generateBarename() + ".file"
  665. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  666. if err != nil {
  667. t.Fatal(err)
  668. }
  669. req.Header.Set("Accept", "application/json")
  670. req.Header.Set("Linx-Randomize", "yes")
  671. mux.ServeHTTP(w, req)
  672. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  673. if err != nil {
  674. t.Fatal(err)
  675. }
  676. if myjson.Filename == filename {
  677. t.Fatal("Filename was not random ")
  678. }
  679. }
  680. func TestPutExpireJSONUpload(t *testing.T) {
  681. var myjson RespOkJSON
  682. mux := setup()
  683. w := httptest.NewRecorder()
  684. filename := generateBarename() + ".file"
  685. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  686. if err != nil {
  687. t.Fatal(err)
  688. }
  689. req.Header.Set("Accept", "application/json")
  690. req.Header.Set("Linx-Expiry", "600")
  691. mux.ServeHTTP(w, req)
  692. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  693. if err != nil {
  694. t.Fatal(err)
  695. }
  696. expiry, err := strconv.Atoi(myjson.Expiry)
  697. if err != nil {
  698. t.Fatal("Expiry was not an integer")
  699. }
  700. if expiry < 1 {
  701. t.Fatal("Expiry was not set")
  702. }
  703. }
  704. func TestPutAndDelete(t *testing.T) {
  705. var myjson RespOkJSON
  706. mux := setup()
  707. w := httptest.NewRecorder()
  708. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  709. if err != nil {
  710. t.Fatal(err)
  711. }
  712. req.Header.Set("Accept", "application/json")
  713. mux.ServeHTTP(w, req)
  714. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  715. if err != nil {
  716. t.Fatal(err)
  717. }
  718. // Delete it
  719. w = httptest.NewRecorder()
  720. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  721. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  722. mux.ServeHTTP(w, req)
  723. if w.Code != 200 {
  724. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  725. }
  726. // Make sure it's actually gone
  727. w = httptest.NewRecorder()
  728. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  729. mux.ServeHTTP(w, req)
  730. if w.Code != 404 {
  731. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  732. }
  733. // Make sure torrent is also gone
  734. w = httptest.NewRecorder()
  735. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  736. mux.ServeHTTP(w, req)
  737. if w.Code != 404 {
  738. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  739. }
  740. }
  741. func TestPutAndOverwrite(t *testing.T) {
  742. var myjson RespOkJSON
  743. mux := setup()
  744. w := httptest.NewRecorder()
  745. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  746. if err != nil {
  747. t.Fatal(err)
  748. }
  749. req.Header.Set("Accept", "application/json")
  750. mux.ServeHTTP(w, req)
  751. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  752. if err != nil {
  753. t.Fatal(err)
  754. }
  755. // Overwrite it
  756. w = httptest.NewRecorder()
  757. req, err = http.NewRequest("PUT", "/upload/"+myjson.Filename, strings.NewReader("New file content"))
  758. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  759. mux.ServeHTTP(w, req)
  760. if w.Code != 200 {
  761. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  762. }
  763. // Make sure it's the new file
  764. w = httptest.NewRecorder()
  765. req, err = http.NewRequest("GET", "/"+Config.selifPath+myjson.Filename, nil)
  766. mux.ServeHTTP(w, req)
  767. if w.Code == 404 {
  768. t.Fatal("Status code was 404")
  769. }
  770. if w.Body.String() != "New file content" {
  771. t.Fatal("File did not contain 'New file content")
  772. }
  773. }
  774. func TestPutAndSpecificDelete(t *testing.T) {
  775. var myjson RespOkJSON
  776. mux := setup()
  777. w := httptest.NewRecorder()
  778. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  779. if err != nil {
  780. t.Fatal(err)
  781. }
  782. req.Header.Set("Accept", "application/json")
  783. req.Header.Set("Linx-Delete-Key", "supersecret")
  784. mux.ServeHTTP(w, req)
  785. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  786. if err != nil {
  787. t.Fatal(err)
  788. }
  789. // Delete it
  790. w = httptest.NewRecorder()
  791. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  792. req.Header.Set("Linx-Delete-Key", "supersecret")
  793. mux.ServeHTTP(w, req)
  794. if w.Code != 200 {
  795. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  796. }
  797. // Make sure it's actually gone
  798. w = httptest.NewRecorder()
  799. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  800. mux.ServeHTTP(w, req)
  801. if w.Code != 404 {
  802. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  803. }
  804. // Make sure torrent is gone too
  805. w = httptest.NewRecorder()
  806. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  807. mux.ServeHTTP(w, req)
  808. if w.Code != 404 {
  809. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  810. }
  811. }
  812. func TestExtension(t *testing.T) {
  813. barename, extension := barePlusExt("test.jpg.gz")
  814. if barename != "testjpg" {
  815. t.Fatal("Barename was not testjpg, but " + barename)
  816. }
  817. if extension != "gz" {
  818. t.Fatal("Extension was not gz, but " + extension)
  819. }
  820. barename, extension = barePlusExt("test.tar.gz")
  821. if barename != "test" {
  822. t.Fatal("Barename was not test, but " + barename)
  823. }
  824. if extension != "tar.gz" {
  825. t.Fatal("Extension was not tar.gz, but " + extension)
  826. }
  827. }
  828. func TestInferSiteURL(t *testing.T) {
  829. oldSiteURL := Config.siteURL
  830. oldSitePath := Config.sitePath
  831. Config.siteURL = ""
  832. Config.sitePath = "/linxtest/"
  833. mux := setup()
  834. w := httptest.NewRecorder()
  835. req, err := http.NewRequest("GET", "/API/", nil)
  836. req.Host = "example.com:8080"
  837. if err != nil {
  838. t.Fatal(err)
  839. }
  840. mux.ServeHTTP(w, req)
  841. if !strings.Contains(w.Body.String(), "http://example.com:8080/upload/") {
  842. t.Fatal("Site URL not found properly embedded in response")
  843. }
  844. Config.siteURL = oldSiteURL
  845. Config.sitePath = oldSitePath
  846. }
  847. func TestInferSiteURLProxied(t *testing.T) {
  848. oldSiteURL := Config.siteURL
  849. Config.siteURL = ""
  850. mux := setup()
  851. w := httptest.NewRecorder()
  852. req, err := http.NewRequest("GET", "/API/", nil)
  853. req.Header.Add("X-Forwarded-Proto", "https")
  854. req.Host = "example.com:8080"
  855. if err != nil {
  856. t.Fatal(err)
  857. }
  858. mux.ServeHTTP(w, req)
  859. if !strings.Contains(w.Body.String(), "https://example.com:8080/upload/") {
  860. t.Fatal("Site URL not found properly embedded in response")
  861. }
  862. Config.siteURL = oldSiteURL
  863. }
  864. func TestInferSiteURLHTTPS(t *testing.T) {
  865. oldSiteURL := Config.siteURL
  866. oldCertFile := Config.certFile
  867. Config.siteURL = ""
  868. Config.certFile = "/dev/null"
  869. mux := setup()
  870. w := httptest.NewRecorder()
  871. req, err := http.NewRequest("GET", "/API/", nil)
  872. req.Host = "example.com"
  873. if err != nil {
  874. t.Fatal(err)
  875. }
  876. mux.ServeHTTP(w, req)
  877. if !strings.Contains(w.Body.String(), "https://example.com/upload/") {
  878. t.Fatal("Site URL not found properly embedded in response")
  879. }
  880. Config.siteURL = oldSiteURL
  881. Config.certFile = oldCertFile
  882. }
  883. func TestInferSiteURLHTTPSFastCGI(t *testing.T) {
  884. oldSiteURL := Config.siteURL
  885. Config.siteURL = ""
  886. mux := setup()
  887. w := httptest.NewRecorder()
  888. req, err := http.NewRequest("GET", "/API/", nil)
  889. req.Host = "example.com"
  890. req.TLS = &tls.ConnectionState{HandshakeComplete: true}
  891. if err != nil {
  892. t.Fatal(err)
  893. }
  894. mux.ServeHTTP(w, req)
  895. if !strings.Contains(w.Body.String(), "https://example.com/upload/") {
  896. t.Fatal("Site URL not found properly embedded in response")
  897. }
  898. Config.siteURL = oldSiteURL
  899. }
  900. func TestShutdown(t *testing.T) {
  901. os.RemoveAll(Config.filesDir)
  902. os.RemoveAll(Config.metaDir)
  903. }
  904. func TestPutAndGetCLI(t *testing.T) {
  905. var myjson RespOkJSON
  906. mux := setup()
  907. // upload file
  908. w := httptest.NewRecorder()
  909. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  910. if err != nil {
  911. t.Fatal(err)
  912. }
  913. req.Header.Set("Accept", "application/json")
  914. mux.ServeHTTP(w, req)
  915. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  916. if err != nil {
  917. t.Fatal(err)
  918. }
  919. // request file without wget user agent
  920. w = httptest.NewRecorder()
  921. req, err = http.NewRequest("GET", myjson.Url, nil)
  922. if err != nil {
  923. t.Fatal(err)
  924. }
  925. mux.ServeHTTP(w, req)
  926. contentType := w.Header().Get("Content-Type")
  927. if strings.HasPrefix(contentType, "text/plain") {
  928. t.Fatalf("Didn't receive file display page but %s", contentType)
  929. }
  930. // request file with wget user agent
  931. w = httptest.NewRecorder()
  932. req, err = http.NewRequest("GET", myjson.Url, nil)
  933. req.Header.Set("User-Agent", "wget")
  934. if err != nil {
  935. t.Fatal(err)
  936. }
  937. mux.ServeHTTP(w, req)
  938. contentType = w.Header().Get("Content-Type")
  939. if !strings.HasPrefix(contentType, "text/plain") {
  940. t.Fatalf("Didn't receive file directly but %s", contentType)
  941. }
  942. }