demo_test.go 8.2 KB

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