2015-09-28 12:30:21 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-07 03:00:03 -04:00
|
|
|
"bytes"
|
2015-09-28 16:02:03 -04:00
|
|
|
"encoding/json"
|
2015-10-07 03:00:03 -04:00
|
|
|
"mime/multipart"
|
2015-09-28 12:30:21 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-10-07 03:00:03 -04:00
|
|
|
"net/url"
|
2015-09-28 12:30:21 -04:00
|
|
|
"os"
|
2015-09-28 12:35:11 -04:00
|
|
|
"path"
|
2015-09-28 16:02:03 -04:00
|
|
|
"strconv"
|
2015-09-28 12:30:21 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-10-07 03:00:03 -04:00
|
|
|
"time"
|
2015-09-28 12:30:21 -04:00
|
|
|
)
|
|
|
|
|
2015-10-07 03:00:03 -04:00
|
|
|
type RespOkJSON struct {
|
|
|
|
Filename string
|
|
|
|
Url string
|
|
|
|
Delete_Key string
|
|
|
|
Expiry string
|
|
|
|
Size string
|
|
|
|
}
|
|
|
|
|
|
|
|
type RespErrJSON struct {
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
2015-09-28 12:30:21 -04:00
|
|
|
func TestSetup(t *testing.T) {
|
|
|
|
Config.siteURL = "http://linx.example.org/"
|
2015-10-07 03:00:03 -04:00
|
|
|
Config.filesDir = path.Join(os.TempDir(), generateBarename())
|
2015-09-28 12:30:21 -04:00
|
|
|
Config.metaDir = Config.filesDir + "_meta"
|
2015-10-08 01:38:50 -04:00
|
|
|
Config.maxSize = 1024 * 1024 * 1024
|
2015-09-28 12:30:21 -04:00
|
|
|
Config.noLogs = true
|
|
|
|
Config.siteName = "linx"
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIndex(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-09-28 12:30:21 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-28 12:30:21 -04:00
|
|
|
|
2015-09-30 23:37:00 -04:00
|
|
|
if !strings.Contains(w.Body.String(), "Click or Drop file") {
|
|
|
|
t.Fatal("String 'Click or Drop file' not found in index response")
|
2015-09-28 12:30:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-10 15:26:47 -04:00
|
|
|
func TestIndexAuthKeys(t *testing.T) {
|
|
|
|
Config.authFile = "/dev/null"
|
|
|
|
|
|
|
|
mux := setup()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mux.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Code != 303 {
|
|
|
|
t.Fatalf("Status code is not 301, but %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
Config.authFile = ""
|
|
|
|
}
|
|
|
|
|
2015-09-28 12:30:21 -04:00
|
|
|
func TestNotFound(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-09-28 12:30:21 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/url/should/not/exist", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-28 12:30:21 -04:00
|
|
|
|
|
|
|
if w.Code != 404 {
|
|
|
|
t.Fatalf("Expected 404, got %d", w.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileNotFound(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-09-28 12:30:21 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2015-10-07 03:00:03 -04:00
|
|
|
filename := generateBarename()
|
2015-09-28 12:30:21 -04:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/selif/"+filename, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-28 12:30:21 -04:00
|
|
|
|
|
|
|
if w.Code != 404 {
|
|
|
|
t.Fatalf("Expected 404, got %d", w.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDisplayNotFound(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-09-28 12:30:21 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2015-10-07 03:00:03 -04:00
|
|
|
filename := generateBarename()
|
2015-09-28 12:30:21 -04:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/"+filename, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-28 12:30:21 -04:00
|
|
|
|
|
|
|
if w.Code != 404 {
|
|
|
|
t.Fatalf("Expected 404, got %d", w.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-07 03:00:03 -04:00
|
|
|
func TestPostCodeUpload(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-10-07 03:00:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
filename := generateBarename()
|
|
|
|
extension := "txt"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Add("content", "File content")
|
|
|
|
form.Add("filename", filename)
|
|
|
|
form.Add("extension", extension)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/upload/", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
req.PostForm = form
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
2015-10-08 23:27:04 -04:00
|
|
|
req.Header.Set("Referer", Config.siteURL)
|
2015-10-07 03:00:03 -04:00
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-07 03:00:03 -04:00
|
|
|
|
2015-10-10 16:10:06 -04:00
|
|
|
if w.Code != 303 {
|
|
|
|
t.Fatalf("Status code is not 303, but %d", w.Code)
|
2015-10-07 03:00:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if w.Header().Get("Location") != "/"+filename+"."+extension {
|
|
|
|
t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:27:04 -04:00
|
|
|
func TestPostCodeUploadWhitelistedHeader(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-10-08 23:27:04 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
filename := generateBarename()
|
|
|
|
extension := "txt"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Add("content", "File content")
|
|
|
|
form.Add("filename", filename)
|
|
|
|
form.Add("extension", extension)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/upload/", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
req.PostForm = form
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Set("Linx-Expiry", "0")
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-08 23:27:04 -04:00
|
|
|
|
2015-10-10 16:10:06 -04:00
|
|
|
if w.Code != 303 {
|
|
|
|
t.Fatalf("Status code is not 303, but %d", w.Code)
|
2015-10-08 23:27:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostCodeUploadNoReferrer(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-10-08 23:27:04 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
filename := generateBarename()
|
|
|
|
extension := "txt"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Add("content", "File content")
|
|
|
|
form.Add("filename", filename)
|
|
|
|
form.Add("extension", extension)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/upload/", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
req.PostForm = form
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-08 23:27:04 -04:00
|
|
|
|
|
|
|
if w.Code != 400 {
|
|
|
|
t.Fatalf("Status code is not 400, but %d", w.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostCodeUploadBadOrigin(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-10-08 23:27:04 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
filename := generateBarename()
|
|
|
|
extension := "txt"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Add("content", "File content")
|
|
|
|
form.Add("filename", filename)
|
|
|
|
form.Add("extension", extension)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/upload/", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
req.PostForm = form
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Set("Referer", Config.siteURL)
|
|
|
|
req.Header.Set("Origin", "http://example.com/")
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-08 23:27:04 -04:00
|
|
|
|
|
|
|
if w.Code != 400 {
|
|
|
|
t.Fatalf("Status code is not 400, but %d", w.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-07 03:00:03 -04:00
|
|
|
func TestPostCodeExpiryJSONUpload(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-10-07 03:00:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Add("content", "File content")
|
|
|
|
form.Add("filename", "")
|
|
|
|
form.Add("expires", "60")
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/upload/", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
req.PostForm = form
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2015-10-08 23:27:04 -04:00
|
|
|
req.Header.Set("Referer", Config.siteURL)
|
2015-10-07 03:00:03 -04:00
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-07 03:00:03 -04:00
|
|
|
|
|
|
|
if w.Code != 200 {
|
|
|
|
t.Log(w.Body.String())
|
|
|
|
t.Fatalf("Status code is not 200, but %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
var myjson RespOkJSON
|
|
|
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
curTime := time.Now().Unix()
|
|
|
|
|
|
|
|
if myExp < curTime {
|
|
|
|
t.Fatalf("File expiry (%d) is smaller than current time (%d)", myExp, curTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
if myjson.Size != "12" {
|
|
|
|
t.Fatalf("File size was not 12 but %s", myjson.Size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostUpload(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-10-07 03:00:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
filename := generateBarename() + ".txt"
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
mw := multipart.NewWriter(&b)
|
|
|
|
fw, err := mw.CreateFormFile("file", filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fw.Write([]byte("File content"))
|
|
|
|
mw.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/upload/", &b)
|
|
|
|
req.Header.Set("Content-Type", mw.FormDataContentType())
|
2015-10-08 23:27:04 -04:00
|
|
|
req.Header.Set("Referer", Config.siteURL)
|
2015-10-07 03:00:03 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-07 03:00:03 -04:00
|
|
|
|
2015-10-10 16:10:06 -04:00
|
|
|
if w.Code != 303 {
|
|
|
|
t.Fatalf("Status code is not 303, but %d", w.Code)
|
2015-10-07 03:00:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if w.Header().Get("Location") != "/"+filename {
|
|
|
|
t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostJSONUpload(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-10-07 03:00:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
filename := generateBarename() + ".txt"
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
mw := multipart.NewWriter(&b)
|
|
|
|
fw, err := mw.CreateFormFile("file", filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fw.Write([]byte("File content"))
|
|
|
|
mw.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/upload/", &b)
|
|
|
|
req.Header.Set("Content-Type", mw.FormDataContentType())
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2015-10-08 23:27:04 -04:00
|
|
|
req.Header.Set("Referer", Config.siteURL)
|
2015-10-07 03:00:03 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-07 03:00:03 -04:00
|
|
|
|
|
|
|
if w.Code != 200 {
|
|
|
|
t.Log(w.Body.String())
|
|
|
|
t.Fatalf("Status code is not 200, but %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
var myjson RespOkJSON
|
|
|
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if myjson.Filename != filename {
|
|
|
|
t.Fatalf("Filename is not '%s' but '%s' ", filename, myjson.Filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
if myjson.Expiry != "0" {
|
|
|
|
t.Fatalf("File expiry is not 0 but %s", myjson.Expiry)
|
|
|
|
}
|
|
|
|
|
|
|
|
if myjson.Size != "12" {
|
|
|
|
t.Fatalf("File size was not 12 but %s", myjson.Size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostExpiresJSONUpload(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-10-07 03:00:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
filename := generateBarename() + ".txt"
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
mw := multipart.NewWriter(&b)
|
|
|
|
fw, err := mw.CreateFormFile("file", filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fw.Write([]byte("File content"))
|
|
|
|
|
|
|
|
exp, err := mw.CreateFormField("expires")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
exp.Write([]byte("60"))
|
|
|
|
|
|
|
|
mw.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/upload/", &b)
|
|
|
|
req.Header.Set("Content-Type", mw.FormDataContentType())
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2015-10-08 23:27:04 -04:00
|
|
|
req.Header.Set("Referer", Config.siteURL)
|
2015-10-07 03:00:03 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-07 03:00:03 -04:00
|
|
|
|
|
|
|
if w.Code != 200 {
|
|
|
|
t.Log(w.Body.String())
|
|
|
|
t.Fatalf("Status code is not 200, but %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
var myjson RespOkJSON
|
|
|
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if myjson.Filename != filename {
|
|
|
|
t.Fatalf("Filename is not '%s' but '%s' ", filename, myjson.Filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
curTime := time.Now().Unix()
|
|
|
|
|
|
|
|
if myExp < curTime {
|
|
|
|
t.Fatalf("File expiry (%d) is smaller than current time (%d)", myExp, curTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
if myjson.Size != "12" {
|
|
|
|
t.Fatalf("File size was not 12 but %s", myjson.Size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostRandomizeJSONUpload(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-10-07 03:00:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
filename := generateBarename() + ".txt"
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
mw := multipart.NewWriter(&b)
|
|
|
|
fw, err := mw.CreateFormFile("file", filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fw.Write([]byte("File content"))
|
|
|
|
|
|
|
|
rnd, err := mw.CreateFormField("randomize")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
rnd.Write([]byte("true"))
|
|
|
|
|
|
|
|
mw.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/upload/", &b)
|
|
|
|
req.Header.Set("Content-Type", mw.FormDataContentType())
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2015-10-08 23:27:04 -04:00
|
|
|
req.Header.Set("Referer", Config.siteURL)
|
2015-10-07 03:00:03 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-07 03:00:03 -04:00
|
|
|
|
|
|
|
if w.Code != 200 {
|
|
|
|
t.Log(w.Body.String())
|
|
|
|
t.Fatalf("Status code is not 200, but %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
var myjson RespOkJSON
|
|
|
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if myjson.Filename == filename {
|
2015-10-07 03:02:07 -04:00
|
|
|
t.Fatalf("Filename (%s) is not random (%s)", filename, myjson.Filename)
|
2015-10-07 03:00:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if myjson.Size != "12" {
|
|
|
|
t.Fatalf("File size was not 12 but %s", myjson.Size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostEmptyUpload(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-10-07 03:00:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
filename := generateBarename() + ".txt"
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
mw := multipart.NewWriter(&b)
|
|
|
|
fw, err := mw.CreateFormFile("file", filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fw.Write([]byte(""))
|
|
|
|
mw.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/upload/", &b)
|
|
|
|
req.Header.Set("Content-Type", mw.FormDataContentType())
|
2015-10-08 23:27:04 -04:00
|
|
|
req.Header.Set("Referer", Config.siteURL)
|
2015-10-07 03:00:03 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-07 03:00:03 -04:00
|
|
|
|
|
|
|
if w.Code != 500 {
|
|
|
|
t.Log(w.Body.String())
|
|
|
|
t.Fatalf("Status code is not 500, but %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(w.Body.String(), "Empty file") {
|
|
|
|
t.Fatal("Response did not contain 'Empty file'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostEmptyJSONUpload(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-10-07 03:00:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
filename := generateBarename() + ".txt"
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
mw := multipart.NewWriter(&b)
|
|
|
|
fw, err := mw.CreateFormFile("file", filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fw.Write([]byte(""))
|
|
|
|
mw.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/upload/", &b)
|
|
|
|
req.Header.Set("Content-Type", mw.FormDataContentType())
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2015-10-08 23:27:04 -04:00
|
|
|
req.Header.Set("Referer", Config.siteURL)
|
2015-10-07 03:00:03 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-07 03:00:03 -04:00
|
|
|
|
|
|
|
if w.Code != 500 {
|
|
|
|
t.Log(w.Body.String())
|
|
|
|
t.Fatalf("Status code is not 500, but %d", w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
var myjson RespErrJSON
|
|
|
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if myjson.Error != "Could not upload file: Empty file" {
|
|
|
|
t.Fatal("Json 'error' was not 'Empty file' but " + myjson.Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 16:02:03 -04:00
|
|
|
func TestPutUpload(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-09-28 16:02:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2015-10-07 03:00:03 -04:00
|
|
|
filename := generateBarename() + ".ext"
|
2015-09-28 16:02:03 -04:00
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-28 16:02:03 -04:00
|
|
|
|
|
|
|
if w.Body.String() != Config.siteURL+filename {
|
|
|
|
t.Fatal("Response was not expected URL")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutRandomizedUpload(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-09-28 16:02:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2015-10-07 03:00:03 -04:00
|
|
|
filename := generateBarename() + ".ext"
|
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Linx-Randomize", "yes")
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-07 03:00:03 -04:00
|
|
|
|
|
|
|
if w.Body.String() == Config.siteURL+filename {
|
|
|
|
t.Fatal("Filename was not random")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutNoExtensionUpload(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-10-07 03:00:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
filename := generateBarename()
|
2015-09-28 16:02:03 -04:00
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-06 00:31:09 -04:00
|
|
|
req.Header.Set("Linx-Randomize", "yes")
|
2015-09-28 16:02:03 -04:00
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-28 16:02:03 -04:00
|
|
|
|
|
|
|
if w.Body.String() == Config.siteURL+filename {
|
|
|
|
t.Fatal("Filename was not random")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutEmptyUpload(t *testing.T) {
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-09-28 16:02:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2015-10-07 03:00:03 -04:00
|
|
|
filename := generateBarename() + ".ext"
|
2015-09-28 16:02:03 -04:00
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader(""))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-06 00:31:09 -04:00
|
|
|
req.Header.Set("Linx-Randomize", "yes")
|
2015-09-28 16:02:03 -04:00
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-28 16:02:03 -04:00
|
|
|
|
2015-10-04 12:47:20 -04:00
|
|
|
if !strings.Contains(w.Body.String(), "Empty file") {
|
|
|
|
t.Fatal("Response doesn't contain'Empty file'")
|
2015-09-28 16:02:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutJSONUpload(t *testing.T) {
|
2015-10-07 03:00:03 -04:00
|
|
|
var myjson RespOkJSON
|
2015-09-28 16:02:03 -04:00
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-09-28 16:02:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2015-10-07 03:00:03 -04:00
|
|
|
filename := generateBarename() + ".ext"
|
2015-09-28 16:02:03 -04:00
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-28 16:02:03 -04:00
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if myjson.Filename != filename {
|
|
|
|
t.Fatal("Filename was not provided one but " + myjson.Filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutRandomizedJSONUpload(t *testing.T) {
|
2015-10-07 03:00:03 -04:00
|
|
|
var myjson RespOkJSON
|
2015-09-28 16:02:03 -04:00
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-09-28 16:02:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2015-10-07 03:00:03 -04:00
|
|
|
filename := generateBarename() + ".ext"
|
2015-09-28 16:02:03 -04:00
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2015-10-06 00:31:09 -04:00
|
|
|
req.Header.Set("Linx-Randomize", "yes")
|
2015-09-28 16:02:03 -04:00
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-28 16:02:03 -04:00
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if myjson.Filename == filename {
|
|
|
|
t.Fatal("Filename was not random ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutExpireJSONUpload(t *testing.T) {
|
2015-10-07 03:00:03 -04:00
|
|
|
var myjson RespOkJSON
|
2015-09-28 16:02:03 -04:00
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-09-28 16:02:03 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2015-10-07 03:00:03 -04:00
|
|
|
filename := generateBarename() + ".ext"
|
2015-09-28 16:02:03 -04:00
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2015-10-06 00:31:09 -04:00
|
|
|
req.Header.Set("Linx-Expiry", "600")
|
2015-09-28 16:02:03 -04:00
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-28 16:02:03 -04:00
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expiry, err := strconv.Atoi(myjson.Expiry)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Expiry was not an integer")
|
|
|
|
}
|
|
|
|
if expiry < 1 {
|
|
|
|
t.Fatal("Expiry was not set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 00:32:16 -04:00
|
|
|
func TestPutAndDelete(t *testing.T) {
|
2015-10-07 03:00:03 -04:00
|
|
|
var myjson RespOkJSON
|
2015-09-29 00:32:16 -04:00
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-09-29 00:32:16 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-29 00:32:16 -04:00
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete it
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
|
2015-10-06 00:31:09 -04:00
|
|
|
req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-29 00:32:16 -04:00
|
|
|
|
2015-10-04 02:25:00 -04:00
|
|
|
if w.Code != 200 {
|
|
|
|
t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
|
2015-09-29 00:32:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it's actually gone
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-29 00:32:16 -04:00
|
|
|
|
|
|
|
if w.Code != 404 {
|
|
|
|
t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
|
|
|
|
}
|
2015-09-29 01:58:14 -04:00
|
|
|
|
|
|
|
// Make sure torrent is also gone
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-29 01:58:14 -04:00
|
|
|
|
|
|
|
if w.Code != 404 {
|
|
|
|
t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
|
|
|
|
}
|
2015-09-29 00:32:16 -04:00
|
|
|
}
|
|
|
|
|
2015-10-08 12:49:29 -04:00
|
|
|
func TestPutAndOverwrite(t *testing.T) {
|
|
|
|
var myjson RespOkJSON
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-10-08 12:49:29 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-08 12:49:29 -04:00
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite it
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("PUT", "/upload/"+myjson.Filename, strings.NewReader("New file content"))
|
|
|
|
req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-08 12:49:29 -04:00
|
|
|
|
|
|
|
if w.Code != 200 {
|
|
|
|
t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it's the new file
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("GET", "/selif/"+myjson.Filename, nil)
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-10-08 12:49:29 -04:00
|
|
|
|
|
|
|
if w.Code == 404 {
|
|
|
|
t.Fatal("Status code was 404")
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.Body.String() != "New file content" {
|
|
|
|
t.Fatal("File did not contain 'New file content")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 00:32:16 -04:00
|
|
|
func TestPutAndSpecificDelete(t *testing.T) {
|
2015-10-07 03:00:03 -04:00
|
|
|
var myjson RespOkJSON
|
2015-09-29 00:32:16 -04:00
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux := setup()
|
2015-09-29 00:32:16 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2015-10-06 00:31:09 -04:00
|
|
|
req.Header.Set("Linx-Delete-Key", "supersecret")
|
2015-09-29 00:32:16 -04:00
|
|
|
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-29 00:32:16 -04:00
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete it
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
|
2015-10-06 00:31:09 -04:00
|
|
|
req.Header.Set("Linx-Delete-Key", "supersecret")
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-29 00:32:16 -04:00
|
|
|
|
2015-10-04 02:25:00 -04:00
|
|
|
if w.Code != 200 {
|
|
|
|
t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
|
2015-09-29 00:32:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it's actually gone
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-29 00:32:16 -04:00
|
|
|
|
|
|
|
if w.Code != 404 {
|
|
|
|
t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
|
|
|
|
}
|
2015-09-29 01:58:14 -04:00
|
|
|
|
|
|
|
// Make sure torrent is gone too
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
|
2015-10-10 02:04:08 -04:00
|
|
|
mux.ServeHTTP(w, req)
|
2015-09-29 01:58:14 -04:00
|
|
|
|
|
|
|
if w.Code != 404 {
|
|
|
|
t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
|
|
|
|
}
|
2015-09-29 00:32:16 -04:00
|
|
|
}
|
|
|
|
|
2015-09-28 12:30:21 -04:00
|
|
|
func TestShutdown(t *testing.T) {
|
|
|
|
os.RemoveAll(Config.filesDir)
|
|
|
|
os.RemoveAll(Config.metaDir)
|
|
|
|
}
|