mirror of https://github.com/schollz/croc.git
210 lines
5.0 KiB
Go
210 lines
5.0 KiB
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"math/rand"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/schollz/croc/v9/src/models"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
var bigFileSize = 75000000
|
|
|
|
func bigFile() {
|
|
ioutil.WriteFile("bigfile.test", bytes.Repeat([]byte("z"), bigFileSize), 0666)
|
|
}
|
|
|
|
func BenchmarkMD5(b *testing.B) {
|
|
bigFile()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
MD5HashFile("bigfile.test")
|
|
}
|
|
}
|
|
|
|
func BenchmarkXXHash(b *testing.B) {
|
|
bigFile()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
XXHashFile("bigfile.test")
|
|
}
|
|
}
|
|
func BenchmarkImoHash(b *testing.B) {
|
|
bigFile()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
IMOHashFile("bigfile.test")
|
|
}
|
|
}
|
|
|
|
func BenchmarkSha256(b *testing.B) {
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
SHA256("hello,world")
|
|
}
|
|
}
|
|
|
|
func BenchmarkMissingChunks(b *testing.B) {
|
|
bigFile()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
MissingChunks("bigfile.test", int64(bigFileSize), models.TCP_BUFFER_SIZE/2)
|
|
}
|
|
}
|
|
|
|
func TestExists(t *testing.T) {
|
|
bigFile()
|
|
defer os.Remove("bigfile.test")
|
|
fmt.Println(GetLocalIPs())
|
|
assert.True(t, Exists("bigfile.test"))
|
|
assert.False(t, Exists("doesnotexist"))
|
|
}
|
|
|
|
func TestMD5HashFile(t *testing.T) {
|
|
bigFile()
|
|
defer os.Remove("bigfile.test")
|
|
b, err := MD5HashFile("bigfile.test")
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, "8304ff018e02baad0e3555bade29a405", fmt.Sprintf("%x", b))
|
|
_, err = MD5HashFile("bigfile.test.nofile")
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
func TestIMOHashFile(t *testing.T) {
|
|
bigFile()
|
|
defer os.Remove("bigfile.test")
|
|
b, err := IMOHashFile("bigfile.test")
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, "c0d1e123ca94148ffea146137684ebb9", fmt.Sprintf("%x", b))
|
|
}
|
|
|
|
func TestXXHashFile(t *testing.T) {
|
|
bigFile()
|
|
defer os.Remove("bigfile.test")
|
|
b, err := XXHashFile("bigfile.test")
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, "4918740eb5ccb6f7", fmt.Sprintf("%x", b))
|
|
_, err = XXHashFile("nofile")
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
func TestSHA256(t *testing.T) {
|
|
assert.Equal(t, "09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b", SHA256("hello, world"))
|
|
}
|
|
|
|
func TestByteCountDecimal(t *testing.T) {
|
|
assert.Equal(t, "10.0 kB", ByteCountDecimal(10240))
|
|
assert.Equal(t, "50 B", ByteCountDecimal(50))
|
|
assert.Equal(t, "12.4 MB", ByteCountDecimal(13002343))
|
|
}
|
|
|
|
func TestMissingChunks(t *testing.T) {
|
|
fileSize := 100
|
|
chunkSize := 10
|
|
rand.Seed(1)
|
|
bigBuff := make([]byte, fileSize)
|
|
rand.Read(bigBuff)
|
|
ioutil.WriteFile("missing.test", bigBuff, 0644)
|
|
empty := make([]byte, chunkSize)
|
|
f, err := os.OpenFile("missing.test", os.O_RDWR, 0644)
|
|
assert.Nil(t, err)
|
|
for block := 0; block < fileSize/chunkSize; block++ {
|
|
if block == 0 || block == 4 || block == 5 || block >= 7 {
|
|
f.WriteAt(empty, int64(block*chunkSize))
|
|
}
|
|
}
|
|
f.Close()
|
|
|
|
chunkRanges := MissingChunks("missing.test", int64(fileSize), chunkSize)
|
|
assert.Equal(t, []int64{10, 0, 1, 40, 2, 70, 3}, chunkRanges)
|
|
|
|
chunks := ChunkRangesToChunks(chunkRanges)
|
|
assert.Equal(t, []int64{0, 40, 50, 70, 80, 90}, chunks)
|
|
|
|
os.Remove("missing.test")
|
|
|
|
content := []byte("temporary file's content")
|
|
tmpfile, err := ioutil.TempFile("", "example")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
if _, err := tmpfile.Write(content); err != nil {
|
|
panic(err)
|
|
}
|
|
if err := tmpfile.Close(); err != nil {
|
|
panic(err)
|
|
}
|
|
chunkRanges = MissingChunks(tmpfile.Name(), int64(len(content)), chunkSize)
|
|
assert.Empty(t, chunkRanges)
|
|
chunkRanges = MissingChunks(tmpfile.Name(), int64(len(content)+10), chunkSize)
|
|
assert.Empty(t, chunkRanges)
|
|
chunkRanges = MissingChunks(tmpfile.Name()+"ok", int64(len(content)), chunkSize)
|
|
assert.Empty(t, chunkRanges)
|
|
chunks = ChunkRangesToChunks(chunkRanges)
|
|
assert.Empty(t, chunks)
|
|
}
|
|
|
|
// func Test1(t *testing.T) {
|
|
// chunkRanges := MissingChunks("../../m/bigfile.test", int64(75000000), 1024*64/2)
|
|
// fmt.Println(chunkRanges)
|
|
// fmt.Println(ChunkRangesToChunks((chunkRanges)))
|
|
// assert.Nil(t, nil)
|
|
// }
|
|
|
|
func TestHashFile(t *testing.T) {
|
|
content := []byte("temporary file's content")
|
|
tmpfile, err := ioutil.TempFile("", "example")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
if _, err := tmpfile.Write(content); err != nil {
|
|
panic(err)
|
|
}
|
|
if err := tmpfile.Close(); err != nil {
|
|
panic(err)
|
|
}
|
|
hashed, err := HashFile(tmpfile.Name())
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, "18c9673a4bb8325d323e7f24fda9ae1e", fmt.Sprintf("%x", hashed))
|
|
}
|
|
|
|
func TestPublicIP(t *testing.T) {
|
|
ip, err := PublicIP()
|
|
fmt.Println(ip)
|
|
assert.True(t, strings.Contains(ip, ".") || strings.Contains(ip, ":"))
|
|
assert.Nil(t, err)
|
|
}
|
|
|
|
func TestLocalIP(t *testing.T) {
|
|
ip := LocalIP()
|
|
fmt.Println(ip)
|
|
assert.True(t, strings.Contains(ip, ".") || strings.Contains(ip, ":"))
|
|
}
|
|
|
|
func TestGetRandomName(t *testing.T) {
|
|
name := GetRandomName()
|
|
fmt.Println(name)
|
|
assert.NotEmpty(t, name)
|
|
}
|
|
|
|
func TestFindOpenPorts(t *testing.T) {
|
|
openPorts := FindOpenPorts("localhost", 9009, 4)
|
|
assert.Equal(t, []int{9009, 9010, 9011, 9012}, openPorts)
|
|
}
|
|
|
|
func TestIsLocalIP(t *testing.T) {
|
|
assert.True(t, IsLocalIP("192.168.0.14:9009"))
|
|
}
|