123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- package main
- import (
- "encoding/json"
- "fmt"
- "sparrow/pkg/models"
- "sparrow/pkg/mysql"
- "sparrow/pkg/rpcs"
- "testing"
- )
- func testVendor(t *testing.T, r *Registry) {
- vendor := &models.Vendor{
- VendorName: "testvendor",
- VendorDescription: "this is a test vendor",
- }
- err := r.SaveVendor(vendor, vendor)
- if err != nil {
- t.Fatal(err)
- }
- err = r.FindVendor(int32(vendor.ID), vendor)
- if err != nil {
- t.Fatal(err)
- }
- updateVendor := &models.Vendor{
- VendorName: "testvendorupdate",
- VendorDescription: "this is a test vendor",
- }
- updateVendor.ID = vendor.ID
- err = r.SaveVendor(updateVendor, updateVendor)
- if err != nil {
- t.Fatal(err)
- }
- vendorRow := &models.Vendor{}
- err = r.FindVendor(int32(updateVendor.ID), vendorRow)
- if err != nil {
- t.Fatal(err)
- }
- if vendorRow.VendorName != updateVendor.VendorName {
- t.Errorf("expect vendorName:%v, got:%v", updateVendor.VendorName, vendorRow.VendorName)
- }
- t.Log(vendor)
- }
- var (
- testProductKey = ""
- )
- func testProduct(t *testing.T, r *Registry) {
- product := &models.Product{
- VendorID: 1,
- ProductName: "test product.",
- ProductDescription: "this is a test product",
- ProductConfig: "{}",
- }
- err := r.SaveProduct(product, product)
- if err != nil {
- t.Fatal(err)
- }
- t.Log(product)
- productRow := &models.Product{}
- err = r.FindProduct(int32(product.ID), productRow)
- if err != nil {
- t.Fatal(err)
- }
- if product.ProductKey != productRow.ProductKey {
- t.Fatalf("expected %v, got %v", product.ProductKey, productRow.ProductKey)
- }
- product.ProductName = "test for update."
- err = r.SaveProduct(product, product)
- if err != nil {
- t.Fatal(err)
- }
- t.Log(product)
- reply := &models.Product{}
- err = r.ValidateProduct(product.ProductKey, reply)
- if err != nil {
- t.Error(err)
- }
- t.Log(reply)
- if reply.ProductName != product.ProductName {
- t.Errorf("expected %v, got %v", product.ProductName, reply.ProductName)
- }
- testProductKey = product.ProductKey
- err = r.ValidateProduct("this is a wrong key , you know", reply)
- if err == nil {
- t.Error("wrong key should fail product key validation.")
- }
- }
- func testApplication(t *testing.T, r *Registry) {
- app := &models.Application{
- AppToken: "test-token",
- ReportUrl: "http://localhost://6060",
- AppName: "test",
- AppDescription: "this is a test app",
- AppDomain: "/*",
- }
- err := r.SaveApplication(app, app)
- if err != nil {
- t.Fatal(err)
- }
- t.Log(app)
- appRow := &models.Application{}
- err = r.FindApplication(int32(app.ID), appRow)
- if err != nil {
- t.Fatal(err)
- }
- app.AppName = "another desc."
- err = r.SaveApplication(app, app)
- if err != nil {
- t.Fatal(err)
- }
- t.Log(app)
- err = r.FindApplication(int32(app.ID), appRow)
- if err != nil {
- t.Fatal(err)
- }
- if appRow.AppName != app.AppName {
- t.Errorf("expected %v, got %v", app.AppName, appRow.AppName)
- }
- reply := &models.Application{}
- err = r.ValidateApplication(app.AppKey, reply)
- if err != nil {
- t.Error(err)
- }
- t.Log(reply)
- err = r.ValidateApplication("this is a wrong key , you know", reply)
- if err == nil {
- t.Error("wrong key should fail product key validation.")
- }
- }
- func testDevice(t *testing.T, r *Registry) {
- args := &rpcs.ArgsDeviceRegister{
- ProductKey: testProductKey,
- DeviceCode: "ffffaaeeccbb",
- DeviceVersion: "android-gprs-v1",
- }
- device := &models.Device{}
- err := r.RegisterDevice(args, device)
- if err != nil {
- t.Fatal(err)
- }
- t.Log(device)
- founddev := &models.Device{}
- arg := &rpcs.ArgsDeviceAuth{
- DeviceID: int64(int(device.ID)),
- }
- err = r.FindDeviceById(arg, founddev)
- if err != nil {
- t.Error(err)
- }
- if device.DeviceIdentifier != founddev.DeviceIdentifier {
- t.Errorf("FindDeviceById not match, want %v, got %v", device, founddev)
- }
- err = r.FindDeviceByIdentifier(device.DeviceIdentifier, founddev)
- if err != nil {
- t.Error(err)
- }
- if device.ID != founddev.ID {
- t.Errorf("FindDeviceByIdentifier not match, want %v, got %v", device, founddev)
- }
- device.DeviceDescription = "test change device info."
- args2 := &rpcs.ArgsDeviceUpdate{
- DeviceIdentifier: device.DeviceIdentifier,
- DeviceName: "testupdatename",
- DeviceDescription: "test change device info.",
- }
- err = r.UpdateDeviceInfo(args2, device)
- if err != nil {
- t.Error(err)
- }
- devRow := &models.Device{}
- err = r.FindDeviceByIdentifier(device.DeviceIdentifier, devRow)
- if err != nil {
- t.Error(err)
- }
- if devRow.DeviceName != device.DeviceName {
- t.Errorf(" want %v, got %v", device.DeviceName, devRow.DeviceName)
- }
- t.Log(device)
- }
- func TestRegistry(t *testing.T) {
- err := mysql.MigrateDatabase(defaultDBHost, defaultDBPort, defaultDBName, defaultDBUser, "123456")
- if err != nil {
- t.Fatal(err)
- }
- *confAESKey = "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
- *confDBPass = "123456"
- r, err := NewRegistry()
- if err != nil {
- t.Fatal(err)
- }
- testAppAuth(t, r)
- //testVendor(t, r)
- //testProduct(t, r)
- //testApplication(t, r)
- //testDevice(t, r)
- }
- func testAppAuth(t *testing.T, r *Registry) {
- arg := &rpcs.ArgsAppAuth{
- AppKey: "aab1f679672380cf8cc79816bac4256809d0820473bc958e4c4eac91fd97e27bss",
- Secretkey: "S0gyxgdve3n7LY3FWuysSLqLft65NauC",
- }
- a := &models.Application{}
- err := r.FindApplicationByAppKey(arg, a)
- if err != nil {
- t.Error(err)
- }
- if a.ID > 0 {
- buf, _ := json.Marshal(a)
- fmt.Println("==============", string(buf))
- }
- fmt.Println("===============>>>>>>>>>>>>>")
- }
|