Exemplo n.º 1
0
func TestFetchWithContentLength(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		file, err := os.Open("./fixtures/test")
		assert.Ok(t, err)
		assert.Cond(t, file != nil, "Failed loading fixture file")
		defer file.Close()

		http.ServeContent(w, r, file.Name(), time.Time{}, file)
	}))
	defer ts.Close()

	progressCh := make(chan ProgressReport)
	done := make(chan bool)

	destDir, err := ioutil.TempDir(os.TempDir(), "content-length")
	assert.Ok(t, err)
	defer os.RemoveAll(destDir)

	gf := New(WithDestDir(destDir), WithConcurrency(50))
	go func() {
		_, err := gf.Fetch(ts.URL, progressCh)
		assert.Ok(t, err)
		done <- true
	}()

	var total int64
	for p := range progressCh {
		//fmt.Printf("\r%d of %d", p.WrittenBytes, p.Total)
		total += p.WrittenBytes
	}
	assert.Equals(t, int64(10485760), total)
	<-done
}
Exemplo n.º 2
0
func TestUnpacking(t *testing.T) {
	image, err := os.Open("./fixtures/test.iso")
	defer image.Close()
	reader, err := NewReader(image)
	assert.Ok(t, err)

	tests := []struct {
		name    string
		isDir   bool
		content string
	}{
		{"/dir1", true, ""},
		{"/dir2", true, ""},
		{"/file.txt", false, "hola amigo\n"},
		{"/dir1/hello.txt", false, "hello there!"},
		{"/dir2/dir3", true, ""},
		{"/dir2/dir3/blah.txt", false, "do you feel me?\n"},
	}

	count := 0
	for {
		fi, err := reader.Next()
		if err == io.EOF {
			break
		}
		assert.Ok(t, err)

		f := fi.(*File)
		//fmt.Printf("%s\n", tests[count].name)
		assert.Equals(t, tests[count].name, f.Name())
		assert.Equals(t, tests[count].isDir, f.IsDir())

		rawBytes := f.Sys()
		if !f.IsDir() {
			assert.Cond(t, rawBytes != nil, "when it is file, content should not be nil")
			content, err := ioutil.ReadAll(rawBytes.(io.Reader))
			assert.Ok(t, err)
			//fmt.Printf("%s -> %s\n", tests[count].name, content)
			assert.Equals(t, tests[count].content, string(content[:]))
		} else {
			assert.Equals(t, nil, rawBytes)
		}
		count++
	}
	assert.Equals(t, 6, count)
}
Exemplo n.º 3
0
func TestFetchWithoutContentLength(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			return
		}
		file, err := os.Open("./fixtures/test")
		assert.Ok(t, err)
		assert.Cond(t, file != nil, "Failed loading fixture file")
		defer file.Close()

		_, err = io.Copy(w, file)
		assert.Ok(t, err)
	}))
	defer ts.Close()

	progressCh := make(chan ProgressReport)
	done := make(chan bool)

	destDir, err := ioutil.TempDir(os.TempDir(), "no-content-length")
	assert.Ok(t, err)
	defer os.RemoveAll(destDir)

	gf := New(WithDestDir(destDir))
	go func() {
		_, err := gf.Fetch(ts.URL, progressCh)
		assert.Ok(t, err)
		done <- true
	}()

	var total int64
	for p := range progressCh {
		total += p.WrittenBytes
		assert.Equals(t, int64(-1), p.Total)
	}
	assert.Equals(t, int64(10485760), total)
	<-done
	// Now we can close the test server and let the deferred function to run.
}
Exemplo n.º 4
0
func TestEtagSupport(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		file, err := os.Open("./fixtures/test")
		assert.Ok(t, err)
		assert.Cond(t, file != nil, "Failed loading fixture file")
		defer file.Close()

		w.Header().Add("Etag", "7h153746154w350m3")
		http.ServeContent(w, r, file.Name(), time.Time{}, file)
	}))
	defer ts.Close()

	destDir, err := ioutil.TempDir(os.TempDir(), "etag")
	assert.Ok(t, err)
	defer os.RemoveAll(destDir)

	gf := New(WithDestDir(destDir), WithConcurrency(1), WithETag())

	// Fetches file for the first tim
	_, err = gf.Fetch(ts.URL+"/test", nil)
	assert.Ok(t, err)

	progressCh := make(chan ProgressReport)
	go func() {
		// Attempts to fetch file once again.
		_, err := gf.Fetch(ts.URL+"/test", progressCh)
		assert.Ok(t, err)
	}()

	var progressCount int
	for range progressCh {
		progressCount++
	}

	// Since the file was already downloaded, there shouldn't be any download
	// progress reported.
	assert.Equals(t, 0, progressCount)

	// Cleans up ~/.gofetch work dir
	dir, err := homedir.Dir()
	assert.Ok(t, err)
	err = os.RemoveAll(filepath.Join(dir, ".gofetch"))
	assert.Ok(t, err)
}
Exemplo n.º 5
0
func TestResume(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		file, err := os.Open("./fixtures/test")
		assert.Ok(t, err)
		assert.Cond(t, file != nil, "Failed loading fixture file")
		defer file.Close()

		http.ServeContent(w, r, file.Name(), time.Time{}, file)
	}))
	defer ts.Close()

	destDir, err := ioutil.TempDir(os.TempDir(), "resume")
	assert.Ok(t, err)
	defer os.RemoveAll(destDir)

	chunksDir := filepath.Join(destDir, path.Base(ts.URL)+".chunks")
	err = os.MkdirAll(chunksDir, 0760)
	assert.Ok(t, err)

	fixtureFile, err := os.Open("./fixtures/test-resume")
	assert.Ok(t, err)

	chunkFile, err := os.Create(filepath.Join(chunksDir, "0"))
	assert.Ok(t, err)

	_, err = io.Copy(chunkFile, fixtureFile)
	assert.Ok(t, err)

	fixtureFile.Close()
	chunkFile.Close()

	done := make(chan bool)
	progressCh := make(chan ProgressReport)
	var file *os.File
	gf := New(WithDestDir(destDir), WithConcurrency(1))
	go func() {
		var err error
		file, err = gf.Fetch(ts.URL, progressCh)
		assert.Ok(t, err)
		done <- true
	}()

	var total int64
	for p := range progressCh {
		//fmt.Printf("\r%d of %d", p.WrittenBytes, p.Total)
		total += p.WrittenBytes
	}
	// It should report the complete file size through the channel
	assert.Equals(t, int64(10485760), total)
	<-done
	// Fetch finished and we can now use the file without causing data races.

	// Checks that the downloaded file has the same size as the test fixture
	fi, err := file.Stat()
	assert.Ok(t, err)
	defer file.Close()
	assert.Equals(t, int64(10485760), fi.Size())

	// Checks file integrity
	hasher := sha512.New()
	_, err = io.Copy(hasher, file)
	assert.Ok(t, err)

	result := fmt.Sprintf("%x", hasher.Sum(nil))
	assert.Equals(t, "4ff6e159db38d46a665f26e9f82b98134238c0457cc82727a5258b7184773e4967068cc0eecf3928ecd079f3aea6e22aac024847c6d76c0329c4635c4b6ae327", result)
	file.Close()
}
Exemplo n.º 6
0
func TestNewReader(t *testing.T) {
	image, err := os.Open("./fixtures/test.iso")
	defer image.Close()
	r, err := NewReader(image)
	assert.Ok(t, err)
	// Test first half of primary volume descriptor
	assert.Equals(t, "CD001", string(r.pvd.StandardID[:]))
	assert.Equals(t, 1, int(r.pvd.Type))
	assert.Equals(t, 1, int(r.pvd.Version))
	assert.Equals(t, "Mac OS X", strings.TrimSpace(string(r.pvd.SystemID[:])))
	assert.Equals(t, "my-vol-id", strings.TrimSpace(string(r.pvd.ID[:])))
	assert.Equals(t, 181, int(r.pvd.VolumeSpaceSizeBE))
	assert.Equals(t, 1, int(r.pvd.VolumeSetSizeBE))
	assert.Equals(t, 1, int(r.pvd.VolumeSeqNumberBE))
	assert.Equals(t, 2048, int(r.pvd.LogicalBlkSizeBE))
	assert.Equals(t, 46, int(r.pvd.PathTableSizeBE))
	assert.Equals(t, 21, int(r.pvd.LocPathTableBE))
	assert.Equals(t, 0, int(r.pvd.LocOptPathTableBE))
	// Test root directory record values
	assert.Equals(t, 0, int(r.pvd.DirectoryRecord.ExtendedAttrLen))
	assert.Equals(t, 23, int(r.pvd.DirectoryRecord.ExtentLocationBE))
	assert.Equals(t, 2048, int(r.pvd.DirectoryRecord.ExtentLengthBE))
	assert.Equals(t, 2, int(r.pvd.DirectoryRecord.FileFlags))
	assert.Equals(t, 0, int(r.pvd.DirectoryRecord.FileUnitSize))
	assert.Equals(t, 0, int(r.pvd.DirectoryRecord.InterleaveGapSize))
	assert.Equals(t, 1, int(r.pvd.DirectoryRecord.VolumeSeqNumberBE))
	assert.Equals(t, 1, int(r.pvd.DirectoryRecord.FileIDLength))
	// Test second half of primary volume descriptor
	assert.Equals(t, "my-vol-id", strings.TrimSpace(string(r.pvd.ID[:])))
	assert.Equals(t, "test-volset-id", strings.TrimSpace(string(r.pvd.VolumeSetID[:])))
	assert.Equals(t, "hooklift", strings.TrimSpace(string(r.pvd.PublisherID[:])))
	assert.Equals(t, "hooklift", strings.TrimSpace(string(r.pvd.DataPreparerID[:])))
	assert.Equals(t, "MKISOFS ISO9660/HFS/UDF FILESYSTEM BUILDER & CDRECORD CD/DVD/BluRay CREATOR (C) 1993 E.YOUNGDALE (C) 1997 J.PEARSON/J.SCHILLING", strings.TrimSpace(string(r.pvd.AppID[:])))
	assert.Equals(t, 1, int(r.pvd.FileStructVersion))
}