registry_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package main
  2. //
  3. //import (
  4. // "encoding/json"
  5. // "fmt"
  6. // "sparrow/pkg/models"
  7. // "sparrow/pkg/mysql"
  8. // "sparrow/pkg/rpcs"
  9. // "testing"
  10. //)
  11. //
  12. //func testVendor(t *testing.T, r *Registry) {
  13. // vendor := &models.Vendor{
  14. // VendorName: "testvendor",
  15. // VendorDescription: "this is a test vendor",
  16. // }
  17. // err := r.SaveVendor(vendor, vendor)
  18. // if err != nil {
  19. // t.Fatal(err)
  20. // }
  21. // err = r.FindVendor(int32(vendor.ID), vendor)
  22. // if err != nil {
  23. // t.Fatal(err)
  24. // }
  25. //
  26. // updateVendor := &models.Vendor{
  27. // VendorName: "testvendorupdate",
  28. // VendorDescription: "this is a test vendor",
  29. // }
  30. // updateVendor.ID = vendor.ID
  31. //
  32. // err = r.SaveVendor(updateVendor, updateVendor)
  33. // if err != nil {
  34. // t.Fatal(err)
  35. // }
  36. // vendorRow := &models.Vendor{}
  37. // err = r.FindVendor(int32(updateVendor.ID), vendorRow)
  38. // if err != nil {
  39. // t.Fatal(err)
  40. // }
  41. //
  42. // if vendorRow.VendorName != updateVendor.VendorName {
  43. // t.Errorf("expect vendorName:%v, got:%v", updateVendor.VendorName, vendorRow.VendorName)
  44. // }
  45. //
  46. // t.Log(vendor)
  47. //}
  48. //
  49. //var (
  50. // testProductKey = ""
  51. //)
  52. //
  53. //func testProduct(t *testing.T, r *Registry) {
  54. // product := &models.Product{
  55. // VendorID: 1,
  56. // ProductName: "test product.",
  57. // ProductDescription: "this is a test product",
  58. // ProductConfig: "{}",
  59. // }
  60. // err := r.SaveProduct(product, product)
  61. // if err != nil {
  62. // t.Fatal(err)
  63. // }
  64. // t.Log(product)
  65. //
  66. // productRow := &models.Product{}
  67. // err = r.FindProduct(int32(product.ID), productRow)
  68. // if err != nil {
  69. // t.Fatal(err)
  70. // }
  71. //
  72. // if product.ProductKey != productRow.ProductKey {
  73. // t.Fatalf("expected %v, got %v", product.ProductKey, productRow.ProductKey)
  74. // }
  75. //
  76. // product.ProductName = "test for update."
  77. // err = r.SaveProduct(product, product)
  78. // if err != nil {
  79. // t.Fatal(err)
  80. // }
  81. // t.Log(product)
  82. //
  83. // reply := &models.Product{}
  84. // err = r.ValidateProduct(product.ProductKey, reply)
  85. // if err != nil {
  86. // t.Error(err)
  87. // }
  88. // t.Log(reply)
  89. //
  90. // if reply.ProductName != product.ProductName {
  91. // t.Errorf("expected %v, got %v", product.ProductName, reply.ProductName)
  92. // }
  93. //
  94. // testProductKey = product.ProductKey
  95. //
  96. // err = r.ValidateProduct("this is a wrong key , you know", reply)
  97. // if err == nil {
  98. // t.Error("wrong key should fail product key validation.")
  99. // }
  100. //}
  101. //
  102. //func testApplication(t *testing.T, r *Registry) {
  103. // app := &models.Application{
  104. // AppToken: "test-token",
  105. // ReportUrl: "http://localhost://6060",
  106. // AppName: "test",
  107. // AppDescription: "this is a test app",
  108. // AppDomain: "/*",
  109. // }
  110. // err := r.SaveApplication(app, app)
  111. // if err != nil {
  112. // t.Fatal(err)
  113. // }
  114. // t.Log(app)
  115. //
  116. // appRow := &models.Application{}
  117. // err = r.FindApplication(int32(app.ID), appRow)
  118. // if err != nil {
  119. // t.Fatal(err)
  120. // }
  121. //
  122. // app.AppName = "another desc."
  123. // err = r.SaveApplication(app, app)
  124. // if err != nil {
  125. // t.Fatal(err)
  126. // }
  127. // t.Log(app)
  128. // err = r.FindApplication(int32(app.ID), appRow)
  129. // if err != nil {
  130. // t.Fatal(err)
  131. // }
  132. //
  133. // if appRow.AppName != app.AppName {
  134. // t.Errorf("expected %v, got %v", app.AppName, appRow.AppName)
  135. // }
  136. //
  137. // reply := &models.Application{}
  138. // err = r.ValidateApplication(app.AppKey, reply)
  139. // if err != nil {
  140. // t.Error(err)
  141. // }
  142. // t.Log(reply)
  143. //
  144. // err = r.ValidateApplication("this is a wrong key , you know", reply)
  145. // if err == nil {
  146. // t.Error("wrong key should fail product key validation.")
  147. // }
  148. //}
  149. //
  150. //func testDevice(t *testing.T, r *Registry) {
  151. // args := &rpcs.ArgsDeviceRegister{
  152. // ProductKey: testProductKey,
  153. // DeviceCode: "ffffaaeeccbb",
  154. // DeviceVersion: "android-gprs-v1",
  155. // }
  156. // device := &models.Device{}
  157. // err := r.RegisterDevice(args, device)
  158. // if err != nil {
  159. // t.Fatal(err)
  160. // }
  161. // t.Log(device)
  162. //
  163. // founddev := &models.Device{}
  164. // arg := &rpcs.ArgsDeviceAuth{
  165. // DeviceID: int64(int(device.ID)),
  166. // }
  167. // err = r.FindDeviceById(arg, founddev)
  168. // if err != nil {
  169. // t.Error(err)
  170. // }
  171. // if device.DeviceIdentifier != founddev.DeviceIdentifier {
  172. // t.Errorf("FindDeviceById not match, want %v, got %v", device, founddev)
  173. // }
  174. //
  175. // err = r.FindDeviceByIdentifier(device.DeviceIdentifier, founddev)
  176. // if err != nil {
  177. // t.Error(err)
  178. // }
  179. // if device.ID != founddev.ID {
  180. // t.Errorf("FindDeviceByIdentifier not match, want %v, got %v", device, founddev)
  181. // }
  182. //
  183. // device.DeviceDescription = "test change device info."
  184. //
  185. // args2 := &rpcs.ArgsDeviceUpdate{
  186. // DeviceIdentifier: device.DeviceIdentifier,
  187. // DeviceName: "testupdatename",
  188. // DeviceDescription: "test change device info.",
  189. // }
  190. // err = r.UpdateDeviceInfo(args2, device)
  191. // if err != nil {
  192. // t.Error(err)
  193. // }
  194. //
  195. // devRow := &models.Device{}
  196. // err = r.FindDeviceByIdentifier(device.DeviceIdentifier, devRow)
  197. // if err != nil {
  198. // t.Error(err)
  199. // }
  200. // if devRow.DeviceName != device.DeviceName {
  201. // t.Errorf(" want %v, got %v", device.DeviceName, devRow.DeviceName)
  202. // }
  203. // t.Log(device)
  204. //}
  205. //func TestRegistry(t *testing.T) {
  206. // err := mysql.MigrateDatabase(defaultDBHost, defaultDBPort, defaultDBName, defaultDBUser, "123456")
  207. // if err != nil {
  208. // t.Fatal(err)
  209. // }
  210. //
  211. // *confAESKey = "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
  212. //
  213. // *confDBPass = "123456"
  214. // r, err := NewRegistry()
  215. // if err != nil {
  216. // t.Fatal(err)
  217. // }
  218. //
  219. // testAppAuth(t, r)
  220. //
  221. // //testVendor(t, r)
  222. // //testProduct(t, r)
  223. // //testApplication(t, r)
  224. // //testDevice(t, r)
  225. //}
  226. //
  227. //func testAppAuth(t *testing.T, r *Registry) {
  228. //
  229. // arg := &rpcs.ArgsAppAuth{
  230. // AppKey: "aab1f679672380cf8cc79816bac4256809d0820473bc958e4c4eac91fd97e27bss",
  231. // Secretkey: "S0gyxgdve3n7LY3FWuysSLqLft65NauC",
  232. // }
  233. // a := &models.Application{}
  234. // err := r.FindApplicationByAppKey(arg, a)
  235. // if err != nil {
  236. // t.Error(err)
  237. // }
  238. //
  239. // if a.ID > 0 {
  240. // buf, _ := json.Marshal(a)
  241. //
  242. // fmt.Println("==============", string(buf))
  243. // }
  244. //
  245. // fmt.Println("===============>>>>>>>>>>>>>")
  246. //
  247. //}