demo_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package test
  2. import (
  3. "fmt"
  4. "gfast/library/adapterUtils"
  5. "github.com/casbin/casbin/v2"
  6. "github.com/casbin/casbin/v2/util"
  7. "github.com/goflyfox/gtoken/gtoken"
  8. "github.com/gogf/gf/crypto/gaes"
  9. "github.com/gogf/gf/encoding/gbase64"
  10. "github.com/gogf/gf/frame/g"
  11. "github.com/gogf/gf/net/ghttp"
  12. "github.com/gogf/gf/os/glog"
  13. "github.com/mojocn/base64Captcha"
  14. "testing"
  15. )
  16. func TestDemo(t *testing.T) {
  17. //t.Run("demo1" ,Demo1)
  18. //t.Run("Adapters_test", Adapters)
  19. //t.Run("CaptchaDemo", CaptchaDemo)
  20. //t.Run("CaptchaVerify", CaptchaVerify)
  21. //t.Run("GTokenTest", GTokenTest)
  22. t.Run("CbcEncrypt", CbcEncrypt)
  23. }
  24. func CbcEncrypt(t *testing.T) {
  25. b, e := gaes.EncryptCBC([]byte("yxh123456"), []byte("HqmP1KLMuz09Q0Bu"), []byte("HqmP1KLMuz09Q0Bu"))
  26. if e != nil {
  27. panic(e)
  28. }
  29. fmt.Println(gbase64.EncodeToString(b))
  30. b, _ = gaes.DecryptCBC(b, []byte("HqmP1KLMuz09Q0Bu"), []byte("HqmP1KLMuz09Q0Bu"))
  31. fmt.Println(string(b))
  32. }
  33. func Demo1(t *testing.T) {
  34. e, err := casbin.NewEnforcer("casbin_conf/model.conf", "casbin_conf/policy.csv")
  35. if err != nil {
  36. panic(err)
  37. }
  38. sub := "alice" // the user that wants to access a resource. 对象
  39. obj := "data1" // the resource that is going to be accessed. 资源
  40. act := "write" // the operation that the user performs on the resource. 操作
  41. ok, err := e.Enforce(sub, obj, act)
  42. if err != nil {
  43. fmt.Println("验证失败", err)
  44. }
  45. if ok == true {
  46. fmt.Println("权限通过")
  47. } else {
  48. fmt.Println("没有权限")
  49. }
  50. }
  51. func GTokenTest(t *testing.T) {
  52. // 启动gtoken
  53. gtoken := &gtoken.GfToken{
  54. LoginPath: "/login",
  55. LoginBeforeFunc: loginFunc,
  56. LogoutPath: "/user/logout",
  57. AuthPaths: g.SliceStr{"/system/*"},
  58. LogoutBeforeFunc: loginOutFunc,
  59. }
  60. gtoken.Start()
  61. s := g.Server()
  62. s.BindHandler("/system/admin", func(r *ghttp.Request) {
  63. r.Response.Write("hello admin")
  64. })
  65. s.SetPort(8080)
  66. s.Run()
  67. }
  68. func loginFunc(r *ghttp.Request) (string, interface{}) {
  69. return "yixiaohu", []g.MapStrStr{{"name": "张三", "age": "18"}, {"name": "李四", "age": "32"}}
  70. }
  71. func loginOutFunc(r *ghttp.Request) bool {
  72. return true
  73. }
  74. func demoCodeCaptchaCreate() {
  75. //config struct for digits
  76. //数字验证码配置
  77. /*var configD = base64Captcha.ConfigDigit{
  78. Height: 80,
  79. Width: 240,
  80. MaxSkew: 0.7,
  81. DotCount: 80,
  82. CaptchaLen: 5,
  83. }*/
  84. //config struct for audio
  85. //声音验证码配置
  86. /*var configA = base64Captcha.ConfigAudio{
  87. CaptchaLen: 6,
  88. Language: "zh",
  89. }*/
  90. //config struct for Character
  91. //字符,公式,验证码配置
  92. var configC = base64Captcha.ConfigCharacter{
  93. Height: 60,
  94. Width: 240,
  95. //const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合.
  96. Mode: base64Captcha.CaptchaModeNumber,
  97. ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
  98. ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower,
  99. IsShowHollowLine: false,
  100. IsShowNoiseDot: false,
  101. IsShowNoiseText: false,
  102. IsShowSlimeLine: false,
  103. IsShowSineLine: false,
  104. CaptchaLen: 4,
  105. }
  106. //创建字符公式验证码.
  107. //GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid.
  108. idKeyC, capC := base64Captcha.GenerateCaptcha("8nM77YhE2xOvU6GMQ33A", configC)
  109. //以base64编码
  110. base64stringC := base64Captcha.CaptchaWriteToBase64Encoding(capC)
  111. fmt.Println(idKeyC, "\n", base64stringC)
  112. }
  113. func CaptchaDemo(t *testing.T) {
  114. demoCodeCaptchaCreate()
  115. }
  116. func CaptchaVerify(t *testing.T) {
  117. if base64Captcha.VerifyCaptchaAndIsClear("8nM77YhE2xOvU6GMQ33A", "0870", false) {
  118. fmt.Println("验证成功")
  119. } else {
  120. fmt.Println("验证失败")
  121. }
  122. }
  123. func Adapters(t *testing.T) {
  124. a := initAdapter(t, "mysql", "root:123456@tcp(127.0.0.1:3306)/test2")
  125. testAutoSave(t, a)
  126. testSaveLoad(t, a)
  127. a = initAdapterFormOptions(t, &adapterUtils.Adapter{
  128. DriverName: "mysql",
  129. DataSourceName: "root:123456@tcp(127.0.0.1:3306)/test2",
  130. })
  131. testAutoSave(t, a)
  132. testSaveLoad(t, a)
  133. }
  134. func initAdapterFormOptions(t *testing.T, adapter *adapterUtils.Adapter) *adapterUtils.Adapter {
  135. // Create an adapter
  136. a, _ := adapterUtils.NewAdapterFromOptions(adapter)
  137. // Initialize some policy in DB.
  138. initPolicy(t, a)
  139. // Now the DB has policy, so we can provide a normal use case.
  140. // Note: you don't need to look at the above code
  141. // if you already have a working DB with policy inside.
  142. return a
  143. }
  144. func initPolicy(t *testing.T, a *adapterUtils.Adapter) {
  145. // Because the DB is empty at first,
  146. // so we need to load the policy from the file adapter (.CSV) first.
  147. e, err := casbin.NewEnforcer("casbin_conf/rbac_model.conf", "casbin_conf/rbac_policy.csv")
  148. if err != nil {
  149. panic(err)
  150. }
  151. // This is a trick to save the current policy to the DB.
  152. // We can't call e.SavePolicy() because the adapter in the enforcer is still the file adapter.
  153. // The current policy means the policy in the Casbin enforcer (aka in memory).
  154. err = a.SavePolicy(e.GetModel())
  155. if err != nil {
  156. panic(err)
  157. }
  158. // Clear the current policy.
  159. e.ClearPolicy()
  160. testGetPolicy(t, e, [][]string{})
  161. // Load the policy from DB.
  162. err = a.LoadPolicy(e.GetModel())
  163. if err != nil {
  164. panic(err)
  165. }
  166. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
  167. }
  168. func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
  169. myRes := e.GetPolicy()
  170. glog.Info("Policy: ", myRes)
  171. if !util.Array2DEquals(res, myRes) {
  172. t.Error("Policy: ", myRes, ", supposed to be ", res)
  173. }
  174. }
  175. func initAdapter(t *testing.T, driverName string, dataSourceName string) *adapterUtils.Adapter {
  176. // Create an adapter
  177. a, err := adapterUtils.NewAdapter(driverName, dataSourceName)
  178. if err != nil {
  179. panic(err)
  180. }
  181. // Initialize some policy in DB.
  182. initPolicy(t, a)
  183. // Now the DB has policy, so we can provide a normal use case.
  184. // Note: you don't need to look at the above code
  185. // if you already have a working DB with policy inside.
  186. return a
  187. }
  188. func testAutoSave(t *testing.T, a *adapterUtils.Adapter) {
  189. // NewEnforcer() will load the policy automatically.
  190. e, err := casbin.NewEnforcer("casbin_conf/rbac_model.conf", a)
  191. if err != nil {
  192. panic(err)
  193. }
  194. // AutoSave is enabled by default.
  195. // Now we disable it.
  196. e.EnableAutoSave(false)
  197. // Because AutoSave is disabled, the policy change only affects the policy in Casbin enforcer,
  198. // it doesn't affect the policy in the storage.
  199. e.AddPolicy("alice", "data1", "write")
  200. // Reload the policy from the storage to see the effect.
  201. e.LoadPolicy()
  202. // This is still the original policy.
  203. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
  204. // Now we enable the AutoSave.
  205. e.EnableAutoSave(true)
  206. // Because AutoSave is enabled, the policy change not only affects the policy in Casbin enforcer,
  207. // but also affects the policy in the storage.
  208. e.AddPolicy("alice", "data1", "write")
  209. // Reload the policy from the storage to see the effect.
  210. e.LoadPolicy()
  211. // The policy has a new rule: {"alice", "data1", "write"}.
  212. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"alice", "data1", "write"}})
  213. // Remove the added rule.
  214. e.RemovePolicy("alice", "data1", "write")
  215. e.LoadPolicy()
  216. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
  217. // Remove "data2_admin" related policy rules via a filter.
  218. // Two rules: {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"} are deleted.
  219. e.RemoveFilteredPolicy(0, "data2_admin")
  220. e.LoadPolicy()
  221. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}})
  222. }
  223. func testSaveLoad(t *testing.T, a *adapterUtils.Adapter) {
  224. // Initialize some policy in DB.
  225. initPolicy(t, a)
  226. // Note: you don't need to look at the above code
  227. // if you already have a working DB with policy inside.
  228. // Now the DB has policy, so we can provide a normal use case.
  229. // Create an adapter and an enforcer.
  230. // NewEnforcer() will load the policy automatically.
  231. e, _ := casbin.NewEnforcer("casbin_conf/rbac_model.conf", a)
  232. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
  233. }