Просмотр исходного кода

compare two file's md5 should be correct.

master
Patrick Peng Sun 8 лет назад
Родитель
Сommit
5510623972
2 измененных файлов: 35 добавлений и 6 удалений
  1. +13
    -6
      crmAttachment_test.go
  2. +22
    -0
      fileinfo.go

+ 13
- 6
crmAttachment_test.go Просмотреть файл

@@ -1,21 +1,28 @@
package main

import "testing"

import "encoding/json"
import (
"encoding/json"
"os"
"testing"
"time"
)

func TestCRMAttachFile(t *testing.T) {

path1 := "media_for_test/200x200.png"
path2 := "/tmp/wechat_hitxy_测试_" + time.Now().Format("2006-Jan-02-05-04-05")
r, e := crmCreateAttachment("media_for_test/200x200.png")

AssertEqual(t, e, nil, "should be no error when create attachment")
AssertEqual(t, r.ID != "", true, "ID should not be empty ")
//
crmDownloadAttachmentAs(r.ID, "/tmp/wechat_hitxy_测试")
crmDownloadAttachmentAs(r.ID, path2)

AssertEqual(t, md5CompFile(path1, path2), true, "attachment should be downloade as same file ")

//TODO: two file should be equal
delete, e := crmDeleteEntity("Attachment", r.ID)
AssertEqual(t, delete, true, "temp attachment should have been deleted")

os.Remove(path2)
}

func TestDecodeJsonResponse(t *testing.T) {

+ 22
- 0
fileinfo.go Просмотреть файл

@@ -1,7 +1,9 @@
package main

import (
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"log"
"os"
@@ -120,3 +122,23 @@ func ensurePathExist(path string) {
os.MkdirAll(d, 0700)
}
}

func md5File(path string) (md5str string) {
f, err := os.Open(path)
if err != nil {
log.Println(err)
return
}
defer f.Close()

h := md5.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}

return fmt.Sprintf("%x", h.Sum(nil))
}

func md5CompFile(path1, path2 string) (same bool) {
return md5File(path1) == md5File(path2)
}

Загрузка…
Отмена
Сохранить