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.

1305 lines
27 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
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 TestPutForceRandomUpload(t *testing.T) {
  593. mux := setup()
  594. w := httptest.NewRecorder()
  595. oldFRF := Config.forceRandomFilename
  596. Config.forceRandomFilename = true
  597. filename := "randomizeme.file"
  598. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  599. if err != nil {
  600. t.Fatal(err)
  601. }
  602. // while this should also work without this header, let's try to force
  603. // the randomized filename off to be sure
  604. req.Header.Set("Linx-Randomize", "no")
  605. mux.ServeHTTP(w, req)
  606. if w.Body.String() == Config.siteURL+filename {
  607. t.Fatal("Filename was not random")
  608. }
  609. Config.forceRandomFilename = oldFRF
  610. }
  611. func TestPutNoExtensionUpload(t *testing.T) {
  612. mux := setup()
  613. w := httptest.NewRecorder()
  614. filename := generateBarename()
  615. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  616. if err != nil {
  617. t.Fatal(err)
  618. }
  619. req.Header.Set("Linx-Randomize", "yes")
  620. mux.ServeHTTP(w, req)
  621. if w.Body.String() == Config.siteURL+filename {
  622. t.Fatal("Filename was not random")
  623. }
  624. }
  625. func TestPutEmptyUpload(t *testing.T) {
  626. mux := setup()
  627. w := httptest.NewRecorder()
  628. filename := generateBarename() + ".file"
  629. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader(""))
  630. if err != nil {
  631. t.Fatal(err)
  632. }
  633. req.Header.Set("Linx-Randomize", "yes")
  634. mux.ServeHTTP(w, req)
  635. if w.Code != 400 {
  636. t.Fatalf("Status code is not 400, but %d", w.Code)
  637. }
  638. }
  639. func TestPutTooLargeUpload(t *testing.T) {
  640. mux := setup()
  641. oldMaxSize := Config.maxSize
  642. Config.maxSize = 2
  643. w := httptest.NewRecorder()
  644. filename := generateBarename() + ".file"
  645. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File too big"))
  646. if err != nil {
  647. t.Fatal(err)
  648. }
  649. req.Header.Set("Linx-Randomize", "yes")
  650. mux.ServeHTTP(w, req)
  651. if w.Code != 500 {
  652. t.Log(w.Body.String())
  653. t.Fatalf("Status code is not 500, but %d", w.Code)
  654. }
  655. if !strings.Contains(w.Body.String(), "request body too large") {
  656. t.Fatal("Response did not contain 'request body too large'")
  657. }
  658. Config.maxSize = oldMaxSize
  659. }
  660. func TestPutJSONUpload(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. mux.ServeHTTP(w, req)
  671. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  672. if err != nil {
  673. t.Fatal(err)
  674. }
  675. if myjson.Filename != filename {
  676. t.Fatal("Filename was not provided one but " + myjson.Filename)
  677. }
  678. }
  679. func TestPutRandomizedJSONUpload(t *testing.T) {
  680. var myjson RespOkJSON
  681. mux := setup()
  682. w := httptest.NewRecorder()
  683. filename := generateBarename() + ".file"
  684. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  685. if err != nil {
  686. t.Fatal(err)
  687. }
  688. req.Header.Set("Accept", "application/json")
  689. req.Header.Set("Linx-Randomize", "yes")
  690. mux.ServeHTTP(w, req)
  691. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  692. if err != nil {
  693. t.Fatal(err)
  694. }
  695. if myjson.Filename == filename {
  696. t.Fatal("Filename was not random ")
  697. }
  698. }
  699. func TestPutExpireJSONUpload(t *testing.T) {
  700. var myjson RespOkJSON
  701. mux := setup()
  702. w := httptest.NewRecorder()
  703. filename := generateBarename() + ".file"
  704. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  705. if err != nil {
  706. t.Fatal(err)
  707. }
  708. req.Header.Set("Accept", "application/json")
  709. req.Header.Set("Linx-Expiry", "600")
  710. mux.ServeHTTP(w, req)
  711. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  712. if err != nil {
  713. t.Fatal(err)
  714. }
  715. expiry, err := strconv.Atoi(myjson.Expiry)
  716. if err != nil {
  717. t.Fatal("Expiry was not an integer")
  718. }
  719. if expiry < 1 {
  720. t.Fatal("Expiry was not set")
  721. }
  722. }
  723. func TestPutAndDelete(t *testing.T) {
  724. var myjson RespOkJSON
  725. mux := setup()
  726. w := httptest.NewRecorder()
  727. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  728. if err != nil {
  729. t.Fatal(err)
  730. }
  731. req.Header.Set("Accept", "application/json")
  732. mux.ServeHTTP(w, req)
  733. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  734. if err != nil {
  735. t.Fatal(err)
  736. }
  737. // Delete it
  738. w = httptest.NewRecorder()
  739. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  740. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  741. mux.ServeHTTP(w, req)
  742. if w.Code != 200 {
  743. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  744. }
  745. // Make sure it's actually gone
  746. w = httptest.NewRecorder()
  747. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  748. mux.ServeHTTP(w, req)
  749. if w.Code != 404 {
  750. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  751. }
  752. // Make sure torrent is also gone
  753. w = httptest.NewRecorder()
  754. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  755. mux.ServeHTTP(w, req)
  756. if w.Code != 404 {
  757. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  758. }
  759. }
  760. func TestPutAndOverwrite(t *testing.T) {
  761. var myjson RespOkJSON
  762. mux := setup()
  763. w := httptest.NewRecorder()
  764. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  765. if err != nil {
  766. t.Fatal(err)
  767. }
  768. req.Header.Set("Accept", "application/json")
  769. mux.ServeHTTP(w, req)
  770. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  771. if err != nil {
  772. t.Fatal(err)
  773. }
  774. // Overwrite it
  775. w = httptest.NewRecorder()
  776. req, err = http.NewRequest("PUT", "/upload/"+myjson.Filename, strings.NewReader("New file content"))
  777. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  778. mux.ServeHTTP(w, req)
  779. if w.Code != 200 {
  780. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  781. }
  782. // Make sure it's the new file
  783. w = httptest.NewRecorder()
  784. req, err = http.NewRequest("GET", "/"+Config.selifPath+myjson.Filename, nil)
  785. mux.ServeHTTP(w, req)
  786. if w.Code == 404 {
  787. t.Fatal("Status code was 404")
  788. }
  789. if w.Body.String() != "New file content" {
  790. t.Fatal("File did not contain 'New file content")
  791. }
  792. }
  793. func TestPutAndOverwriteForceRandom(t *testing.T) {
  794. var myjson RespOkJSON
  795. mux := setup()
  796. w := httptest.NewRecorder()
  797. oldFRF := Config.forceRandomFilename
  798. Config.forceRandomFilename = true
  799. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  800. if err != nil {
  801. t.Fatal(err)
  802. }
  803. req.Header.Set("Accept", "application/json")
  804. mux.ServeHTTP(w, req)
  805. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  806. if err != nil {
  807. t.Fatal(err)
  808. }
  809. // Overwrite it
  810. w = httptest.NewRecorder()
  811. req, err = http.NewRequest("PUT", "/upload/"+myjson.Filename, strings.NewReader("New file content"))
  812. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  813. mux.ServeHTTP(w, req)
  814. if w.Code != 200 {
  815. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  816. }
  817. // Make sure it's the new file
  818. w = httptest.NewRecorder()
  819. req, err = http.NewRequest("GET", "/"+Config.selifPath+myjson.Filename, nil)
  820. mux.ServeHTTP(w, req)
  821. if w.Code == 404 {
  822. t.Fatal("Status code was 404")
  823. }
  824. if w.Body.String() != "New file content" {
  825. t.Fatal("File did not contain 'New file content")
  826. }
  827. Config.forceRandomFilename = oldFRF
  828. }
  829. func TestPutAndSpecificDelete(t *testing.T) {
  830. var myjson RespOkJSON
  831. mux := setup()
  832. w := httptest.NewRecorder()
  833. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  834. if err != nil {
  835. t.Fatal(err)
  836. }
  837. req.Header.Set("Accept", "application/json")
  838. req.Header.Set("Linx-Delete-Key", "supersecret")
  839. mux.ServeHTTP(w, req)
  840. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  841. if err != nil {
  842. t.Fatal(err)
  843. }
  844. // Delete it
  845. w = httptest.NewRecorder()
  846. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  847. req.Header.Set("Linx-Delete-Key", "supersecret")
  848. mux.ServeHTTP(w, req)
  849. if w.Code != 200 {
  850. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  851. }
  852. // Make sure it's actually gone
  853. w = httptest.NewRecorder()
  854. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  855. mux.ServeHTTP(w, req)
  856. if w.Code != 404 {
  857. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  858. }
  859. // Make sure torrent is gone too
  860. w = httptest.NewRecorder()
  861. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  862. mux.ServeHTTP(w, req)
  863. if w.Code != 404 {
  864. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  865. }
  866. }
  867. func TestExtension(t *testing.T) {
  868. barename, extension := barePlusExt("test.jpg.gz")
  869. if barename != "testjpg" {
  870. t.Fatal("Barename was not testjpg, but " + barename)
  871. }
  872. if extension != "gz" {
  873. t.Fatal("Extension was not gz, but " + extension)
  874. }
  875. barename, extension = barePlusExt("test.tar.gz")
  876. if barename != "test" {
  877. t.Fatal("Barename was not test, but " + barename)
  878. }
  879. if extension != "tar.gz" {
  880. t.Fatal("Extension was not tar.gz, but " + extension)
  881. }
  882. }
  883. func TestInferSiteURL(t *testing.T) {
  884. oldSiteURL := Config.siteURL
  885. oldSitePath := Config.sitePath
  886. Config.siteURL = ""
  887. Config.sitePath = "/linxtest/"
  888. mux := setup()
  889. w := httptest.NewRecorder()
  890. req, err := http.NewRequest("GET", "/API/", nil)
  891. req.Host = "example.com:8080"
  892. if err != nil {
  893. t.Fatal(err)
  894. }
  895. mux.ServeHTTP(w, req)
  896. if !strings.Contains(w.Body.String(), "http://example.com:8080/upload/") {
  897. t.Fatal("Site URL not found properly embedded in response")
  898. }
  899. Config.siteURL = oldSiteURL
  900. Config.sitePath = oldSitePath
  901. }
  902. func TestInferSiteURLProxied(t *testing.T) {
  903. oldSiteURL := Config.siteURL
  904. Config.siteURL = ""
  905. mux := setup()
  906. w := httptest.NewRecorder()
  907. req, err := http.NewRequest("GET", "/API/", nil)
  908. req.Header.Add("X-Forwarded-Proto", "https")
  909. req.Host = "example.com:8080"
  910. if err != nil {
  911. t.Fatal(err)
  912. }
  913. mux.ServeHTTP(w, req)
  914. if !strings.Contains(w.Body.String(), "https://example.com:8080/upload/") {
  915. t.Fatal("Site URL not found properly embedded in response")
  916. }
  917. Config.siteURL = oldSiteURL
  918. }
  919. func TestInferSiteURLHTTPS(t *testing.T) {
  920. oldSiteURL := Config.siteURL
  921. oldCertFile := Config.certFile
  922. Config.siteURL = ""
  923. Config.certFile = "/dev/null"
  924. mux := setup()
  925. w := httptest.NewRecorder()
  926. req, err := http.NewRequest("GET", "/API/", nil)
  927. req.Host = "example.com"
  928. if err != nil {
  929. t.Fatal(err)
  930. }
  931. mux.ServeHTTP(w, req)
  932. if !strings.Contains(w.Body.String(), "https://example.com/upload/") {
  933. t.Fatal("Site URL not found properly embedded in response")
  934. }
  935. Config.siteURL = oldSiteURL
  936. Config.certFile = oldCertFile
  937. }
  938. func TestInferSiteURLHTTPSFastCGI(t *testing.T) {
  939. oldSiteURL := Config.siteURL
  940. Config.siteURL = ""
  941. mux := setup()
  942. w := httptest.NewRecorder()
  943. req, err := http.NewRequest("GET", "/API/", nil)
  944. req.Host = "example.com"
  945. req.TLS = &tls.ConnectionState{HandshakeComplete: true}
  946. if err != nil {
  947. t.Fatal(err)
  948. }
  949. mux.ServeHTTP(w, req)
  950. if !strings.Contains(w.Body.String(), "https://example.com/upload/") {
  951. t.Fatal("Site URL not found properly embedded in response")
  952. }
  953. Config.siteURL = oldSiteURL
  954. }
  955. func TestShutdown(t *testing.T) {
  956. os.RemoveAll(Config.filesDir)
  957. os.RemoveAll(Config.metaDir)
  958. }
  959. func TestPutAndGetCLI(t *testing.T) {
  960. var myjson RespOkJSON
  961. mux := setup()
  962. // upload file
  963. w := httptest.NewRecorder()
  964. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  965. if err != nil {
  966. t.Fatal(err)
  967. }
  968. req.Header.Set("Accept", "application/json")
  969. mux.ServeHTTP(w, req)
  970. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  971. if err != nil {
  972. t.Fatal(err)
  973. }
  974. // request file without wget user agent
  975. w = httptest.NewRecorder()
  976. req, err = http.NewRequest("GET", myjson.Url, nil)
  977. if err != nil {
  978. t.Fatal(err)
  979. }
  980. mux.ServeHTTP(w, req)
  981. contentType := w.Header().Get("Content-Type")
  982. if strings.HasPrefix(contentType, "text/plain") {
  983. t.Fatalf("Didn't receive file display page but %s", contentType)
  984. }
  985. // request file with wget user agent
  986. w = httptest.NewRecorder()
  987. req, err = http.NewRequest("GET", myjson.Url, nil)
  988. req.Header.Set("User-Agent", "wget")
  989. if err != nil {
  990. t.Fatal(err)
  991. }
  992. mux.ServeHTTP(w, req)
  993. contentType = w.Header().Get("Content-Type")
  994. if !strings.HasPrefix(contentType, "text/plain") {
  995. t.Fatalf("Didn't receive file directly but %s", contentType)
  996. }
  997. }