diff --git a/crmAttachment_test.go b/crmAttachment_test.go index f18147a..c2998cc 100644 --- a/crmAttachment_test.go +++ b/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) { diff --git a/fileinfo.go b/fileinfo.go index 791acea..167da2c 100644 --- a/fileinfo.go +++ b/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) +}