registry_test.go 4.8 KB

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