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.

1244 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
  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", "/selif/"+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. fmt.Println(w.Body.String())
  379. t.Fatal(err)
  380. }
  381. myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
  382. if err != nil {
  383. t.Fatal(err)
  384. }
  385. expected := time.Now().Add(time.Duration(Config.maxExpiry) * time.Second).Unix()
  386. if myExp != expected {
  387. t.Fatalf("File expiry is not %d but %s", expected, myjson.Expiry)
  388. }
  389. }
  390. Config.maxExpiry = 0
  391. }
  392. func TestPostExpiresJSONUpload(t *testing.T) {
  393. mux := setup()
  394. w := httptest.NewRecorder()
  395. filename := generateBarename() + ".txt"
  396. var b bytes.Buffer
  397. mw := multipart.NewWriter(&b)
  398. fw, err := mw.CreateFormFile("file", filename)
  399. if err != nil {
  400. t.Fatal(err)
  401. }
  402. fw.Write([]byte("File content"))
  403. exp, err := mw.CreateFormField("expires")
  404. if err != nil {
  405. t.Fatal(err)
  406. }
  407. exp.Write([]byte("60"))
  408. mw.Close()
  409. req, err := http.NewRequest("POST", "/upload/", &b)
  410. req.Header.Set("Content-Type", mw.FormDataContentType())
  411. req.Header.Set("Accept", "application/json")
  412. req.Header.Set("Referer", Config.siteURL)
  413. if err != nil {
  414. t.Fatal(err)
  415. }
  416. mux.ServeHTTP(w, req)
  417. if w.Code != 200 {
  418. t.Log(w.Body.String())
  419. t.Fatalf("Status code is not 200, but %d", w.Code)
  420. }
  421. var myjson RespOkJSON
  422. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  423. if err != nil {
  424. t.Fatal(err)
  425. }
  426. if myjson.Filename != filename {
  427. t.Fatalf("Filename is not '%s' but '%s' ", filename, myjson.Filename)
  428. }
  429. myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
  430. if err != nil {
  431. t.Fatal(err)
  432. }
  433. curTime := time.Now().Unix()
  434. if myExp < curTime {
  435. t.Fatalf("File expiry (%d) is smaller than current time (%d)", myExp, curTime)
  436. }
  437. if myjson.Size != "12" {
  438. t.Fatalf("File size was not 12 but %s", myjson.Size)
  439. }
  440. }
  441. func TestPostRandomizeJSONUpload(t *testing.T) {
  442. mux := setup()
  443. w := httptest.NewRecorder()
  444. filename := generateBarename() + ".txt"
  445. var b bytes.Buffer
  446. mw := multipart.NewWriter(&b)
  447. fw, err := mw.CreateFormFile("file", filename)
  448. if err != nil {
  449. t.Fatal(err)
  450. }
  451. fw.Write([]byte("File content"))
  452. rnd, err := mw.CreateFormField("randomize")
  453. if err != nil {
  454. t.Fatal(err)
  455. }
  456. rnd.Write([]byte("true"))
  457. mw.Close()
  458. req, err := http.NewRequest("POST", "/upload/", &b)
  459. req.Header.Set("Content-Type", mw.FormDataContentType())
  460. req.Header.Set("Accept", "application/json")
  461. req.Header.Set("Referer", Config.siteURL)
  462. if err != nil {
  463. t.Fatal(err)
  464. }
  465. mux.ServeHTTP(w, req)
  466. if w.Code != 200 {
  467. t.Log(w.Body.String())
  468. t.Fatalf("Status code is not 200, but %d", w.Code)
  469. }
  470. var myjson RespOkJSON
  471. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  472. if err != nil {
  473. t.Fatal(err)
  474. }
  475. if myjson.Filename == filename {
  476. t.Fatalf("Filename (%s) is not random (%s)", filename, myjson.Filename)
  477. }
  478. if myjson.Size != "12" {
  479. t.Fatalf("File size was not 12 but %s", myjson.Size)
  480. }
  481. }
  482. func TestPostEmptyUpload(t *testing.T) {
  483. mux := setup()
  484. w := httptest.NewRecorder()
  485. filename := generateBarename() + ".txt"
  486. var b bytes.Buffer
  487. mw := multipart.NewWriter(&b)
  488. fw, err := mw.CreateFormFile("file", filename)
  489. if err != nil {
  490. t.Fatal(err)
  491. }
  492. fw.Write([]byte(""))
  493. mw.Close()
  494. req, err := http.NewRequest("POST", "/upload/", &b)
  495. req.Header.Set("Content-Type", mw.FormDataContentType())
  496. req.Header.Set("Referer", Config.siteURL)
  497. if err != nil {
  498. t.Fatal(err)
  499. }
  500. mux.ServeHTTP(w, req)
  501. if w.Code != 500 {
  502. t.Log(w.Body.String())
  503. t.Fatalf("Status code is not 500, but %d", w.Code)
  504. }
  505. if !strings.Contains(w.Body.String(), "Empty file") {
  506. t.Fatal("Response did not contain 'Empty file'")
  507. }
  508. }
  509. func TestPostTooLargeUpload(t *testing.T) {
  510. mux := setup()
  511. oldMaxSize := Config.maxSize
  512. Config.maxSize = 2
  513. w := httptest.NewRecorder()
  514. filename := generateBarename() + ".txt"
  515. var b bytes.Buffer
  516. mw := multipart.NewWriter(&b)
  517. fw, err := mw.CreateFormFile("file", filename)
  518. if err != nil {
  519. t.Fatal(err)
  520. }
  521. fw.Write([]byte("test content"))
  522. mw.Close()
  523. req, err := http.NewRequest("POST", "/upload/", &b)
  524. req.Header.Set("Content-Type", mw.FormDataContentType())
  525. req.Header.Set("Referer", Config.siteURL)
  526. if err != nil {
  527. t.Fatal(err)
  528. }
  529. mux.ServeHTTP(w, req)
  530. if w.Code != 500 {
  531. t.Log(w.Body.String())
  532. t.Fatalf("Status code is not 500, but %d", w.Code)
  533. }
  534. if !strings.Contains(w.Body.String(), "request body too large") {
  535. t.Fatal("Response did not contain 'request body too large'")
  536. }
  537. Config.maxSize = oldMaxSize
  538. }
  539. func TestPostEmptyJSONUpload(t *testing.T) {
  540. mux := setup()
  541. w := httptest.NewRecorder()
  542. filename := generateBarename() + ".txt"
  543. var b bytes.Buffer
  544. mw := multipart.NewWriter(&b)
  545. fw, err := mw.CreateFormFile("file", filename)
  546. if err != nil {
  547. t.Fatal(err)
  548. }
  549. fw.Write([]byte(""))
  550. mw.Close()
  551. req, err := http.NewRequest("POST", "/upload/", &b)
  552. req.Header.Set("Content-Type", mw.FormDataContentType())
  553. req.Header.Set("Accept", "application/json")
  554. req.Header.Set("Referer", Config.siteURL)
  555. if err != nil {
  556. t.Fatal(err)
  557. }
  558. mux.ServeHTTP(w, req)
  559. if w.Code != 500 {
  560. t.Log(w.Body.String())
  561. t.Fatalf("Status code is not 500, but %d", w.Code)
  562. }
  563. var myjson RespErrJSON
  564. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  565. if err != nil {
  566. t.Fatal(err)
  567. }
  568. if myjson.Error != "Could not upload file: Empty file" {
  569. t.Fatal("Json 'error' was not 'Empty file' but " + myjson.Error)
  570. }
  571. }
  572. func TestPutUpload(t *testing.T) {
  573. mux := setup()
  574. w := httptest.NewRecorder()
  575. filename := generateBarename() + ".file"
  576. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  577. if err != nil {
  578. t.Fatal(err)
  579. }
  580. mux.ServeHTTP(w, req)
  581. if w.Body.String() != fmt.Sprintf("%s\n", Config.siteURL+filename) {
  582. t.Fatal("Response was not expected URL")
  583. }
  584. }
  585. func TestPutRandomizedUpload(t *testing.T) {
  586. mux := setup()
  587. w := httptest.NewRecorder()
  588. filename := generateBarename() + ".file"
  589. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  590. if err != nil {
  591. t.Fatal(err)
  592. }
  593. req.Header.Set("Linx-Randomize", "yes")
  594. mux.ServeHTTP(w, req)
  595. if w.Body.String() == Config.siteURL+filename {
  596. t.Fatal("Filename was not random")
  597. }
  598. }
  599. func TestPutNoExtensionUpload(t *testing.T) {
  600. mux := setup()
  601. w := httptest.NewRecorder()
  602. filename := generateBarename()
  603. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  604. if err != nil {
  605. t.Fatal(err)
  606. }
  607. req.Header.Set("Linx-Randomize", "yes")
  608. mux.ServeHTTP(w, req)
  609. if w.Body.String() == Config.siteURL+filename {
  610. t.Fatal("Filename was not random")
  611. }
  612. }
  613. func TestPutEmptyUpload(t *testing.T) {
  614. mux := setup()
  615. w := httptest.NewRecorder()
  616. filename := generateBarename() + ".file"
  617. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader(""))
  618. if err != nil {
  619. t.Fatal(err)
  620. }
  621. req.Header.Set("Linx-Randomize", "yes")
  622. mux.ServeHTTP(w, req)
  623. if w.Code != 500 {
  624. t.Log(w.Body.String())
  625. t.Fatalf("Status code is not 500, but %d", w.Code)
  626. }
  627. if !strings.Contains(w.Body.String(), "Empty file") {
  628. t.Fatal("Response did not contain 'Empty file'")
  629. }
  630. }
  631. func TestPutTooLargeUpload(t *testing.T) {
  632. mux := setup()
  633. oldMaxSize := Config.maxSize
  634. Config.maxSize = 2
  635. w := httptest.NewRecorder()
  636. filename := generateBarename() + ".file"
  637. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File too big"))
  638. if err != nil {
  639. t.Fatal(err)
  640. }
  641. req.Header.Set("Linx-Randomize", "yes")
  642. mux.ServeHTTP(w, req)
  643. if w.Code != 500 {
  644. t.Log(w.Body.String())
  645. t.Fatalf("Status code is not 500, but %d", w.Code)
  646. }
  647. if !strings.Contains(w.Body.String(), "request body too large") {
  648. t.Fatal("Response did not contain 'request body too large'")
  649. }
  650. Config.maxSize = oldMaxSize
  651. }
  652. func TestPutJSONUpload(t *testing.T) {
  653. var myjson RespOkJSON
  654. mux := setup()
  655. w := httptest.NewRecorder()
  656. filename := generateBarename() + ".file"
  657. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  658. if err != nil {
  659. t.Fatal(err)
  660. }
  661. req.Header.Set("Accept", "application/json")
  662. mux.ServeHTTP(w, req)
  663. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  664. if err != nil {
  665. t.Fatal(err)
  666. }
  667. if myjson.Filename != filename {
  668. t.Fatal("Filename was not provided one but " + myjson.Filename)
  669. }
  670. }
  671. func TestPutRandomizedJSONUpload(t *testing.T) {
  672. var myjson RespOkJSON
  673. mux := setup()
  674. w := httptest.NewRecorder()
  675. filename := generateBarename() + ".file"
  676. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  677. if err != nil {
  678. t.Fatal(err)
  679. }
  680. req.Header.Set("Accept", "application/json")
  681. req.Header.Set("Linx-Randomize", "yes")
  682. mux.ServeHTTP(w, req)
  683. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  684. if err != nil {
  685. t.Fatal(err)
  686. }
  687. if myjson.Filename == filename {
  688. t.Fatal("Filename was not random ")
  689. }
  690. }
  691. func TestPutExpireJSONUpload(t *testing.T) {
  692. var myjson RespOkJSON
  693. mux := setup()
  694. w := httptest.NewRecorder()
  695. filename := generateBarename() + ".file"
  696. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  697. if err != nil {
  698. t.Fatal(err)
  699. }
  700. req.Header.Set("Accept", "application/json")
  701. req.Header.Set("Linx-Expiry", "600")
  702. mux.ServeHTTP(w, req)
  703. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  704. if err != nil {
  705. t.Fatal(err)
  706. }
  707. expiry, err := strconv.Atoi(myjson.Expiry)
  708. if err != nil {
  709. t.Fatal("Expiry was not an integer")
  710. }
  711. if expiry < 1 {
  712. t.Fatal("Expiry was not set")
  713. }
  714. }
  715. func TestPutAndDelete(t *testing.T) {
  716. var myjson RespOkJSON
  717. mux := setup()
  718. w := httptest.NewRecorder()
  719. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  720. if err != nil {
  721. t.Fatal(err)
  722. }
  723. req.Header.Set("Accept", "application/json")
  724. mux.ServeHTTP(w, req)
  725. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  726. if err != nil {
  727. t.Fatal(err)
  728. }
  729. // Delete it
  730. w = httptest.NewRecorder()
  731. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  732. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  733. mux.ServeHTTP(w, req)
  734. if w.Code != 200 {
  735. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  736. }
  737. // Make sure it's actually gone
  738. w = httptest.NewRecorder()
  739. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  740. mux.ServeHTTP(w, req)
  741. if w.Code != 404 {
  742. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  743. }
  744. // Make sure torrent is also gone
  745. w = httptest.NewRecorder()
  746. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  747. mux.ServeHTTP(w, req)
  748. if w.Code != 404 {
  749. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  750. }
  751. }
  752. func TestPutAndOverwrite(t *testing.T) {
  753. var myjson RespOkJSON
  754. mux := setup()
  755. w := httptest.NewRecorder()
  756. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  757. if err != nil {
  758. t.Fatal(err)
  759. }
  760. req.Header.Set("Accept", "application/json")
  761. mux.ServeHTTP(w, req)
  762. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  763. if err != nil {
  764. t.Fatal(err)
  765. }
  766. // Overwrite it
  767. w = httptest.NewRecorder()
  768. req, err = http.NewRequest("PUT", "/upload/"+myjson.Filename, strings.NewReader("New file content"))
  769. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  770. mux.ServeHTTP(w, req)
  771. if w.Code != 200 {
  772. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  773. }
  774. // Make sure it's the new file
  775. w = httptest.NewRecorder()
  776. req, err = http.NewRequest("GET", "/selif/"+myjson.Filename, nil)
  777. mux.ServeHTTP(w, req)
  778. if w.Code == 404 {
  779. t.Fatal("Status code was 404")
  780. }
  781. if w.Body.String() != "New file content" {
  782. t.Fatal("File did not contain 'New file content")
  783. }
  784. }
  785. func TestPutAndSpecificDelete(t *testing.T) {
  786. var myjson RespOkJSON
  787. mux := setup()
  788. w := httptest.NewRecorder()
  789. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  790. if err != nil {
  791. t.Fatal(err)
  792. }
  793. req.Header.Set("Accept", "application/json")
  794. req.Header.Set("Linx-Delete-Key", "supersecret")
  795. mux.ServeHTTP(w, req)
  796. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  797. if err != nil {
  798. t.Fatal(err)
  799. }
  800. // Delete it
  801. w = httptest.NewRecorder()
  802. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  803. req.Header.Set("Linx-Delete-Key", "supersecret")
  804. mux.ServeHTTP(w, req)
  805. if w.Code != 200 {
  806. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  807. }
  808. // Make sure it's actually gone
  809. w = httptest.NewRecorder()
  810. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  811. mux.ServeHTTP(w, req)
  812. if w.Code != 404 {
  813. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  814. }
  815. // Make sure torrent is gone too
  816. w = httptest.NewRecorder()
  817. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  818. mux.ServeHTTP(w, req)
  819. if w.Code != 404 {
  820. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  821. }
  822. }
  823. func TestExtension(t *testing.T) {
  824. barename, extension := barePlusExt("test.jpg.gz")
  825. if barename != "testjpg" {
  826. t.Fatal("Barename was not testjpg, but " + barename)
  827. }
  828. if extension != "gz" {
  829. t.Fatal("Extension was not gz, but " + extension)
  830. }
  831. barename, extension = barePlusExt("test.tar.gz")
  832. if barename != "test" {
  833. t.Fatal("Barename was not test, but " + barename)
  834. }
  835. if extension != "tar.gz" {
  836. t.Fatal("Extension was not tar.gz, but " + extension)
  837. }
  838. }
  839. func TestInferSiteURL(t *testing.T) {
  840. oldSiteURL := Config.siteURL
  841. oldSitePath := Config.sitePath
  842. Config.siteURL = ""
  843. Config.sitePath = "/linxtest/"
  844. mux := setup()
  845. w := httptest.NewRecorder()
  846. req, err := http.NewRequest("GET", "/API/", nil)
  847. req.Host = "example.com:8080"
  848. if err != nil {
  849. t.Fatal(err)
  850. }
  851. mux.ServeHTTP(w, req)
  852. if !strings.Contains(w.Body.String(), "http://example.com:8080/upload/") {
  853. t.Fatal("Site URL not found properly embedded in response")
  854. }
  855. Config.siteURL = oldSiteURL
  856. Config.sitePath = oldSitePath
  857. }
  858. func TestInferSiteURLProxied(t *testing.T) {
  859. oldSiteURL := Config.siteURL
  860. Config.siteURL = ""
  861. mux := setup()
  862. w := httptest.NewRecorder()
  863. req, err := http.NewRequest("GET", "/API/", nil)
  864. req.Header.Add("X-Forwarded-Proto", "https")
  865. req.Host = "example.com:8080"
  866. if err != nil {
  867. t.Fatal(err)
  868. }
  869. mux.ServeHTTP(w, req)
  870. if !strings.Contains(w.Body.String(), "https://example.com:8080/upload/") {
  871. t.Fatal("Site URL not found properly embedded in response")
  872. }
  873. Config.siteURL = oldSiteURL
  874. }
  875. func TestInferSiteURLHTTPS(t *testing.T) {
  876. oldSiteURL := Config.siteURL
  877. oldCertFile := Config.certFile
  878. Config.siteURL = ""
  879. Config.certFile = "/dev/null"
  880. mux := setup()
  881. w := httptest.NewRecorder()
  882. req, err := http.NewRequest("GET", "/API/", nil)
  883. req.Host = "example.com"
  884. if err != nil {
  885. t.Fatal(err)
  886. }
  887. mux.ServeHTTP(w, req)
  888. if !strings.Contains(w.Body.String(), "https://example.com/upload/") {
  889. t.Fatal("Site URL not found properly embedded in response")
  890. }
  891. Config.siteURL = oldSiteURL
  892. Config.certFile = oldCertFile
  893. }
  894. func TestInferSiteURLHTTPSFastCGI(t *testing.T) {
  895. oldSiteURL := Config.siteURL
  896. Config.siteURL = ""
  897. mux := setup()
  898. w := httptest.NewRecorder()
  899. req, err := http.NewRequest("GET", "/API/", nil)
  900. req.Host = "example.com"
  901. req.TLS = &tls.ConnectionState{HandshakeComplete: true}
  902. if err != nil {
  903. t.Fatal(err)
  904. }
  905. mux.ServeHTTP(w, req)
  906. if !strings.Contains(w.Body.String(), "https://example.com/upload/") {
  907. t.Fatal("Site URL not found properly embedded in response")
  908. }
  909. Config.siteURL = oldSiteURL
  910. }
  911. func TestShutdown(t *testing.T) {
  912. os.RemoveAll(Config.filesDir)
  913. os.RemoveAll(Config.metaDir)
  914. }
  915. func TestPutAndGetCLI(t *testing.T) {
  916. var myjson RespOkJSON
  917. mux := setup()
  918. // upload file
  919. w := httptest.NewRecorder()
  920. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  921. if err != nil {
  922. t.Fatal(err)
  923. }
  924. req.Header.Set("Accept", "application/json")
  925. mux.ServeHTTP(w, req)
  926. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  927. if err != nil {
  928. t.Fatal(err)
  929. }
  930. // request file without wget user agent
  931. w = httptest.NewRecorder()
  932. req, err = http.NewRequest("GET", myjson.Url, nil)
  933. if err != nil {
  934. t.Fatal(err)
  935. }
  936. mux.ServeHTTP(w, req)
  937. contentType := w.Header().Get("Content-Type")
  938. if strings.HasPrefix(contentType, "text/plain") {
  939. t.Fatalf("Didn't receive file display page but %s", contentType)
  940. }
  941. // request file with wget user agent
  942. w = httptest.NewRecorder()
  943. req, err = http.NewRequest("GET", myjson.Url, nil)
  944. req.Header.Set("User-Agent", "wget")
  945. if err != nil {
  946. t.Fatal(err)
  947. }
  948. mux.ServeHTTP(w, req)
  949. contentType = w.Header().Get("Content-Type")
  950. if !strings.HasPrefix(contentType, "text/plain") {
  951. t.Fatalf("Didn't receive file directly but %s", contentType)
  952. }
  953. }