tools_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package util
  2. import (
  3. "fmt"
  4. "gopkg.in/gographics/imagick.v3/imagick"
  5. "math/rand"
  6. "testing"
  7. )
  8. func TestEncrypt(t *testing.T) {
  9. str := Encrypt("password", "key")
  10. if str == "" {
  11. t.Error("加密失败")
  12. }
  13. fmt.Println("加密结果:", str)
  14. }
  15. func TestDecrypt(t *testing.T) {
  16. key := "oadsoas"
  17. str := "password"
  18. encode := Encrypt(str, key)
  19. decode := Decrypt(encode, key)
  20. if str != decode {
  21. t.Error("加解密失败")
  22. }
  23. fmt.Println("str :", str, "加密解密结果:", decode)
  24. }
  25. func TestRandomNumberStr(t *testing.T) {
  26. s := RandomNumberStr(8)
  27. fmt.Println("random number string", s)
  28. }
  29. func TestRandomStr(t *testing.T) {
  30. s := RandomStr(8)
  31. fmt.Println("random string", s)
  32. letterBytes := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  33. b := make([]byte, 8)
  34. for i := range b {
  35. b[i] = letterBytes[rand.Intn(len(letterBytes))]
  36. }
  37. fmt.Println(string(b))
  38. }
  39. func TestRandNum(t *testing.T) {
  40. fmt.Println("rand num", RandNum(1000))
  41. fmt.Println("rand num", RandNum(10000))
  42. letterBytes := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  43. b := make([]byte, 8)
  44. for i := range b {
  45. b[i] = letterBytes[rand.Intn(len(letterBytes))]
  46. }
  47. fmt.Println(string(b))
  48. }
  49. func TestGenerateKey(t *testing.T) {
  50. fmt.Println(GenerateKey([]byte("nadsnadsfads")))
  51. imagick.Initialize()
  52. defer imagick.Terminate()
  53. mw := imagick.NewMagickWand()
  54. pw := imagick.NewPixelWand()
  55. defer pw.Destroy()
  56. // Create the initial 640x480 transparent canvas
  57. pw.SetColor("none")
  58. if err := mw.NewImage(100, 100, pw); err != nil {
  59. fmt.Println("error")
  60. }
  61. mw.SetImageFormat("PNG")
  62. mw.WriteImage("go-test.png")
  63. fmt.Println(mw.GetImageBlob())
  64. }