registry_test.go 5.3 KB

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