func TestApplicationAPI(t *testing.T) { conf := common.GetTestConfig() Convey("Given a clean database and api instance", t, func() { db, err := storage.OpenDatabase(conf.PostgresDSN) So(err, ShouldBeNil) common.MustResetDB(db) ctx := context.Background() lsCtx := loraserver.Context{DB: db} api := NewApplicationAPI(lsCtx) Convey("When creating an application", func() { _, err := api.Create(ctx, &pb.CreateApplicationRequest{AppEUI: "0102030405060708", Name: "test app"}) So(err, ShouldBeNil) Convey("Then we can get it", func() { resp, err := api.Get(ctx, &pb.GetApplicationRequest{AppEUI: "0102030405060708"}) So(err, ShouldBeNil) So(resp, ShouldResemble, &pb.GetApplicationResponse{AppEUI: "0102030405060708", Name: "test app"}) }) Convey("Then listing the applications returns a single item", func() { resp, err := api.List(ctx, &pb.ListApplicationRequest{Limit: 10}) So(err, ShouldBeNil) So(resp.Result, ShouldHaveLength, 1) So(resp.TotalCount, ShouldEqual, 1) So(resp.Result[0], ShouldResemble, &pb.GetApplicationResponse{AppEUI: "0102030405060708", Name: "test app"}) }) Convey("When updating the application", func() { _, err := api.Update(ctx, &pb.UpdateApplicationRequest{AppEUI: "0102030405060708", Name: "test app 2"}) So(err, ShouldBeNil) Convey("Then the application has been updated", func() { resp, err := api.Get(ctx, &pb.GetApplicationRequest{AppEUI: "0102030405060708"}) So(err, ShouldBeNil) So(resp.Name, ShouldEqual, "test app 2") }) }) Convey("After deleting the application", func() { _, err := api.Delete(ctx, &pb.DeleteApplicationRequest{AppEUI: "0102030405060708"}) So(err, ShouldBeNil) Convey("Then listing the applications resturns zero items", func() { resp, err := api.List(ctx, &pb.ListApplicationRequest{Limit: 10}) So(err, ShouldBeNil) So(resp.Result, ShouldHaveLength, 0) }) }) }) }) }
func TestNodeSessionAPI(t *testing.T) { conf := common.GetTestConfig() Convey("Given a clean database and api instance", t, func() { db, err := storage.OpenDatabase(conf.PostgresDSN) So(err, ShouldBeNil) common.MustResetDB(db) p := storage.NewRedisPool(conf.RedisURL) common.MustFlushRedis(p) lsCtx := loraserver.Context{DB: db, RedisPool: p, NetID: [3]byte{1, 2, 3}} ctx := context.Background() api := NewNodeSessionAPI(lsCtx) Convey("Given an application and node are created (fk constraints)", func() { app := models.Application{ AppEUI: [8]byte{1, 2, 3, 4, 5, 6, 7, 8}, Name: "test app", } So(storage.CreateApplication(db, app), ShouldBeNil) node := models.Node{ DevEUI: [8]byte{8, 7, 6, 5, 4, 3, 2, 1}, AppEUI: [8]byte{1, 2, 3, 4, 5, 6, 7, 8}, AppKey: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, UsedDevNonces: [][2]byte{}, } So(storage.CreateNode(db, node), ShouldBeNil) Convey("When creating a node-session", func() { _, err := api.Create(ctx, &pb.CreateNodeSessionRequest{ DevAddr: "06020304", DevEUI: node.DevEUI.String(), AppEUI: node.AppEUI.String(), AppSKey: node.AppKey.String(), NwkSKey: node.AppKey.String(), FCntUp: 10, FCntDown: 11, RxDelay: 1, Rx1DROffset: 2, CFList: []uint32{ 868700000, }, }) So(err, ShouldBeNil) Convey("Then it can be retrieved by DevAddr", func() { resp, err := api.Get(ctx, &pb.GetNodeSessionRequest{DevAddr: "06020304"}) So(err, ShouldBeNil) So(resp, ShouldResemble, &pb.GetNodeSessionResponse{ DevAddr: "06020304", DevEUI: node.DevEUI.String(), AppEUI: node.AppEUI.String(), AppSKey: node.AppKey.String(), NwkSKey: node.AppKey.String(), FCntUp: 10, FCntDown: 11, RxDelay: 1, Rx1DROffset: 2, CFList: []uint32{ 868700000, 0, 0, 0, 0, }, }) }) Convey("Then it can be retrieved by DevEUI", func() { resp, err := api.GetByDevEUI(ctx, &pb.GetNodeSessionByDevEUIRequest{DevEUI: node.DevEUI.String()}) So(err, ShouldBeNil) So(resp, ShouldResemble, &pb.GetNodeSessionResponse{ DevAddr: "06020304", DevEUI: node.DevEUI.String(), AppEUI: node.AppEUI.String(), AppSKey: node.AppKey.String(), NwkSKey: node.AppKey.String(), FCntUp: 10, FCntDown: 11, RxDelay: 1, Rx1DROffset: 2, CFList: []uint32{ 868700000, 0, 0, 0, 0, }, }) }) Convey("When updating the node-session", func() { _, err := api.Update(ctx, &pb.UpdateNodeSessionRequest{ DevAddr: "06020304", DevEUI: node.DevEUI.String(), AppEUI: node.AppEUI.String(), AppSKey: node.AppKey.String(), NwkSKey: node.AppKey.String(), FCntUp: 20, FCntDown: 22, RxDelay: 10, Rx1DROffset: 20, CFList: []uint32{ 868700000, 868800000, }, }) So(err, ShouldBeNil) Convey("Then the node-session has been updated", func() { resp, err := api.Get(ctx, &pb.GetNodeSessionRequest{DevAddr: "06020304"}) So(err, ShouldBeNil) So(resp, ShouldResemble, &pb.GetNodeSessionResponse{ DevAddr: "06020304", DevEUI: node.DevEUI.String(), AppEUI: node.AppEUI.String(), AppSKey: node.AppKey.String(), NwkSKey: node.AppKey.String(), FCntUp: 20, FCntDown: 22, RxDelay: 10, Rx1DROffset: 20, CFList: []uint32{ 868700000, 868800000, 0, 0, 0, }, }) }) }) Convey("When deleting the node-session", func() { _, err := api.Delete(ctx, &pb.DeleteNodeSessionRequest{DevAddr: "06020304"}) So(err, ShouldBeNil) Convey("Then the node-session has been deleted", func() { _, err := api.Get(ctx, &pb.GetNodeSessionRequest{DevAddr: "06020304"}) So(err, ShouldNotBeNil) }) }) }) }) }) }
func TestHandleJoinRequestPackets(t *testing.T) { conf := common.GetTestConfig() Convey("Given a dummy gateway and application backend and a clean Postgres and Redis database", t, func() { a := &testApplicationBackend{ rxPayloadChan: make(chan models.RXPayload, 1), notificationPayloadChan: make(chan interface{}, 10), } g := &testGatewayBackend{ rxPacketChan: make(chan models.RXPacket, 1), txPacketChan: make(chan models.TXPacket, 1), } p := storage.NewRedisPool(conf.RedisURL) common.MustFlushRedis(p) db, err := storage.OpenDatabase(conf.PostgresDSN) So(err, ShouldBeNil) common.MustResetDB(db) ctx := Context{ RedisPool: p, Gateway: g, Application: a, DB: db, } Convey("Given a node and application in the database", func() { app := models.Application{ AppEUI: [8]byte{1, 2, 3, 4, 5, 6, 7, 8}, Name: "test app", } So(storage.CreateApplication(ctx.DB, app), ShouldBeNil) node := models.Node{ DevEUI: [8]byte{8, 7, 6, 5, 4, 3, 2, 1}, AppEUI: app.AppEUI, AppKey: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, RXDelay: 3, RX1DROffset: 2, } So(storage.CreateNode(ctx.DB, node), ShouldBeNil) Convey("Given a JoinRequest with correct DevEUI but incorrect AppEUI", func() { phy := lorawan.PHYPayload{ MHDR: lorawan.MHDR{ MType: lorawan.JoinRequest, Major: lorawan.LoRaWANR1, }, MACPayload: &lorawan.JoinRequestPayload{ AppEUI: [8]byte{1, 2, 3, 4, 5, 6, 7, 9}, DevEUI: node.DevEUI, DevNonce: [2]byte{1, 2}, }, } So(phy.SetMIC(node.AppKey), ShouldBeNil) rxPacket := models.RXPacket{ PHYPayload: phy, RXInfo: models.RXInfo{ Frequency: common.Band.UplinkChannels[0].Frequency, DataRate: common.Band.DataRates[common.Band.UplinkChannels[0].DataRates[0]], }, } Convey("then handleRXPacket returns an error", func() { So(handleRXPacket(ctx, rxPacket), ShouldResemble, errors.New("node 0807060504030201 belongs to application 0102030405060708, 0102030405060709 was given")) }) }) Convey("Given a JoinRequest packet", func() { phy := lorawan.PHYPayload{ MHDR: lorawan.MHDR{ MType: lorawan.JoinRequest, Major: lorawan.LoRaWANR1, }, MACPayload: &lorawan.JoinRequestPayload{ AppEUI: app.AppEUI, DevEUI: node.DevEUI, DevNonce: [2]byte{1, 2}, }, } So(phy.SetMIC(node.AppKey), ShouldBeNil) rxPacket := models.RXPacket{ PHYPayload: phy, RXInfo: models.RXInfo{ Frequency: common.Band.UplinkChannels[0].Frequency, DataRate: common.Band.DataRates[common.Band.UplinkChannels[0].DataRates[0]], }, } Convey("When calling handleRXPacket", func() { So(handleRXPacket(ctx, rxPacket), ShouldBeNil) Convey("Then a JoinAccept was sent to the node", func() { txPacket := <-g.txPacketChan phy := txPacket.PHYPayload So(phy.DecryptJoinAcceptPayload(node.AppKey), ShouldBeNil) So(phy.MHDR.MType, ShouldEqual, lorawan.JoinAccept) Convey("Then it was sent after 5s", func() { So(txPacket.TXInfo.Timestamp, ShouldEqual, rxPacket.RXInfo.Timestamp+uint32(5*time.Second/time.Microsecond)) }) Convey("Then the RXDelay is set to 3s", func() { jaPL := phy.MACPayload.(*lorawan.JoinAcceptPayload) So(jaPL.RXDelay, ShouldEqual, 3) }) Convey("Then the DLSettings are set correctly", func() { jaPL := phy.MACPayload.(*lorawan.JoinAcceptPayload) So(jaPL.DLSettings.RX2DataRate, ShouldEqual, uint8(common.Band.RX2DataRate)) So(jaPL.DLSettings.RX1DROffset, ShouldEqual, node.RX1DROffset) }) Convey("Then a node-session was created", func() { jaPL := phy.MACPayload.(*lorawan.JoinAcceptPayload) _, err := storage.GetNodeSession(ctx.RedisPool, jaPL.DevAddr) So(err, ShouldBeNil) }) Convey("Then the dev-nonce was added to the used dev-nonces", func() { node, err := storage.GetNode(ctx.DB, node.DevEUI) So(err, ShouldBeNil) So([2]byte{1, 2}, ShouldBeIn, node.UsedDevNonces) }) Convey("Then a join notification was sent to the application", func() { notification := <-a.notificationPayloadChan join, ok := notification.(models.JoinNotification) So(ok, ShouldBeTrue) So(join.DevEUI, ShouldResemble, node.DevEUI) }) }) }) }) }) }) }
func TestNodeAPI(t *testing.T) { conf := common.GetTestConfig() Convey("Given a clean database with an application and api instance", t, func() { db, err := storage.OpenDatabase(conf.PostgresDSN) So(err, ShouldBeNil) common.MustResetDB(db) p := storage.NewRedisPool(conf.RedisURL) common.MustFlushRedis(p) ctx := context.Background() lsCtx := loraserver.Context{DB: db, RedisPool: p} api := NewNodeAPI(lsCtx) app := models.Application{ AppEUI: [8]byte{1, 2, 3, 4, 5, 6, 7, 8}, Name: "test app", } So(storage.CreateApplication(db, app), ShouldBeNil) Convey("When creating a node", func() { _, err := api.Create(ctx, &pb.CreateNodeRequest{ DevEUI: "0807060504030201", AppEUI: "0102030405060708", AppKey: "01020304050607080102030405060708", RxDelay: 1, Rx1DROffset: 3, }) So(err, ShouldBeNil) Convey("The node has been created", func() { node, err := api.Get(ctx, &pb.GetNodeRequest{DevEUI: "0807060504030201"}) So(err, ShouldBeNil) So(node, ShouldResemble, &pb.GetNodeResponse{ DevEUI: "0807060504030201", AppEUI: "0102030405060708", AppKey: "01020304050607080102030405060708", RxDelay: 1, Rx1DROffset: 3, }) }) Convey("Then listing the nodes returns a single items", func() { nodes, err := api.List(ctx, &pb.ListNodeRequest{ Limit: 10, }) So(err, ShouldBeNil) So(nodes.Result, ShouldHaveLength, 1) So(nodes.TotalCount, ShouldEqual, 1) So(nodes.Result[0], ShouldResemble, &pb.GetNodeResponse{ DevEUI: "0807060504030201", AppEUI: "0102030405060708", AppKey: "01020304050607080102030405060708", RxDelay: 1, Rx1DROffset: 3, }) }) Convey("Then listing the nodes for a given AppEUI returns a single item", func() { nodes, err := api.ListByAppEUI(ctx, &pb.ListNodeByAppEUIRequest{ Limit: 10, AppEUI: "0102030405060708", }) So(err, ShouldBeNil) So(nodes.Result, ShouldHaveLength, 1) So(nodes.TotalCount, ShouldEqual, 1) So(nodes.Result[0], ShouldResemble, &pb.GetNodeResponse{ DevEUI: "0807060504030201", AppEUI: "0102030405060708", AppKey: "01020304050607080102030405060708", RxDelay: 1, Rx1DROffset: 3, }) }) Convey("When updating the node", func() { _, err := api.Update(ctx, &pb.UpdateNodeRequest{ DevEUI: "0807060504030201", AppEUI: "0102030405060708", AppKey: "08070605040302010807060504030201", RxDelay: 3, Rx1DROffset: 1, }) So(err, ShouldBeNil) Convey("Then the node has been updated", func() { node, err := api.Get(ctx, &pb.GetNodeRequest{DevEUI: "0807060504030201"}) So(err, ShouldBeNil) So(node, ShouldResemble, &pb.GetNodeResponse{ DevEUI: "0807060504030201", AppEUI: "0102030405060708", AppKey: "08070605040302010807060504030201", RxDelay: 3, Rx1DROffset: 1, }) }) }) Convey("After deleting the node", func() { _, err := api.Delete(ctx, &pb.DeleteNodeRequest{DevEUI: "0807060504030201"}) So(err, ShouldBeNil) Convey("Then listing the nodes returns zero nodes", func() { nodes, err := api.List(ctx, &pb.ListNodeRequest{Limit: 10}) So(err, ShouldBeNil) So(nodes.TotalCount, ShouldEqual, 0) So(nodes.Result, ShouldHaveLength, 0) }) }) Convey("Given a tx payload in the the queue", func() { So(storage.AddTXPayloadToQueue(p, models.TXPayload{ DevEUI: [8]byte{8, 7, 6, 5, 4, 3, 2, 1}, Data: []byte("hello!"), }), ShouldBeNil) count, err := storage.GetTXPayloadQueueSize(p, [8]byte{8, 7, 6, 5, 4, 3, 2, 1}) So(err, ShouldBeNil) So(count, ShouldEqual, 1) Convey("When flushing the tx-payload queue", func() { _, err := api.FlushTXPayloadQueue(ctx, &pb.FlushTXPayloadQueueRequest{DevEUI: "0807060504030201"}) So(err, ShouldBeNil) Convey("Then the queue is empty", func() { count, err := storage.GetTXPayloadQueueSize(p, [8]byte{8, 7, 6, 5, 4, 3, 2, 1}) So(err, ShouldBeNil) So(count, ShouldEqual, 0) }) }) }) }) }) }
func run(c *cli.Context) error { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() // parse the NetID var netID lorawan.NetID if err := netID.UnmarshalText([]byte(c.String("net-id"))); err != nil { log.Fatalf("NetID parse error: %s", err) } // get the band config if c.String("band") == "" { log.Fatalf("--band is undefined, valid options are: %s", strings.Join(bands, ", ")) } bandConfig, err := band.GetConfig(band.Name(c.String("band"))) if err != nil { log.Fatal(err) } common.Band = bandConfig log.WithFields(log.Fields{ "version": version, "net_id": netID.String(), "band": c.String("band"), "docs": "https://docs.loraserver.io/", }).Info("starting LoRa Server") // connect to the database log.Info("connecting to postgresql") db, err := storage.OpenDatabase(c.String("postgres-dsn")) if err != nil { log.Fatalf("database connection error: %s", err) } // setup redis pool log.Info("setup redis connection pool") rp := storage.NewRedisPool(c.String("redis-url")) // setup gateway backend gw, err := gateway.NewBackend(c.String("gw-mqtt-server"), c.String("gw-mqtt-username"), c.String("gw-mqtt-password")) if err != nil { log.Fatalf("gateway-backend setup failed: %s", err) } // setup application backend app, err := application.NewBackend(rp, c.String("app-mqtt-server"), c.String("app-mqtt-username"), c.String("app-mqtt-password")) if err != nil { log.Fatalf("application-backend setup failed: %s", err) } // setup controller backend ctrl, err := controller.NewBackend(rp, c.String("controller-mqtt-server"), c.String("controller-mqtt-username"), c.String("controller-mqtt-password")) if err != nil { log.Fatalf("controller-backend setup failed: %s", err) } // auto-migrate the database if c.Bool("db-automigrate") { log.Info("applying database migrations") m := &migrate.AssetMigrationSource{ Asset: migrations.Asset, AssetDir: migrations.AssetDir, Dir: "", } n, err := migrate.Exec(db.DB, "postgres", m, migrate.Up) if err != nil { log.Fatalf("applying migrations failed: %s", err) } log.WithField("count", n).Info("migrations applied") } lsCtx := loraserver.Context{ DB: db, RedisPool: rp, Gateway: gw, Application: app, Controller: ctrl, NetID: netID, } // start the loraserver server := loraserver.NewServer(lsCtx) if err := server.Start(); err != nil { log.Fatal(err) } // setup the grpc api go func() { server := api.GetGRPCServer(ctx, lsCtx) list, err := net.Listen("tcp", c.String("grpc-bind")) if err != nil { log.Fatalf("error creating gRPC listener: %s", err) } log.WithField("bind", c.String("grpc-bind")).Info("starting gRPC server") log.Fatal(server.Serve(list)) }() // setup the http server r := mux.NewRouter() // setup json api jsonHandler, err := api.GetJSONGateway(ctx, lsCtx, c.String("grpc-bind")) if err != nil { log.Fatalf("get json gateway error: %s", err) } log.WithField("path", "/api/v1").Info("registering api handler and documentation endpoint") r.HandleFunc("/api/v1", api.SwaggerHandlerFunc).Methods("get") r.PathPrefix("/api/v1/").Handler(jsonHandler) // setup static file server (for the gui) log.WithField("path", "/").Info("registering gui handler") r.PathPrefix("/").Handler(http.FileServer(&assetfs.AssetFS{ Asset: static.Asset, AssetDir: static.AssetDir, AssetInfo: static.AssetInfo, Prefix: "", })) // start the http server go func() { log.WithField("bind", c.String("http-bind")).Info("starting rest api / gui server") log.Fatal(http.ListenAndServe(c.String("http-bind"), r)) }() sigChan := make(chan os.Signal) exitChan := make(chan struct{}) signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) log.WithField("signal", <-sigChan).Info("signal received") go func() { log.Warning("stopping loraserver") if err := server.Stop(); err != nil { log.Fatal(err) } exitChan <- struct{}{} }() select { case <-exitChan: case s := <-sigChan: log.WithField("signal", s).Info("signal received, stopping immediately") } return nil }
func TestChannelListAndChannelAPI(t *testing.T) { conf := common.GetTestConfig() Convey("Given a clean database and api instances", t, func() { db, err := storage.OpenDatabase(conf.PostgresDSN) So(err, ShouldBeNil) common.MustResetDB(db) ctx := context.Background() lsCtx := loraserver.Context{DB: db} cAPI := NewChannelAPI(lsCtx) clAPI := NewChannelListAPI(lsCtx) Convey("When creating a channel-list", func() { resp, err := clAPI.Create(ctx, &pb.CreateChannelListRequest{Name: "test channel-list"}) So(err, ShouldBeNil) clID := resp.Id Convey("Then the channel-list has been created", func() { cl, err := clAPI.Get(ctx, &pb.GetChannelListRequest{Id: clID}) So(err, ShouldBeNil) So(cl, ShouldResemble, &pb.GetChannelListResponse{Id: clID, Name: "test channel-list"}) }) Convey("When updating the channel-list", func() { _, err := clAPI.Update(ctx, &pb.UpdateChannelListRequest{Id: clID, Name: "test channel-list changed"}) So(err, ShouldBeNil) Convey("Then the channel-list has been updated", func() { cl, err := clAPI.Get(ctx, &pb.GetChannelListRequest{Id: clID}) So(err, ShouldBeNil) So(cl, ShouldResemble, &pb.GetChannelListResponse{Id: clID, Name: "test channel-list changed"}) }) }) Convey("Then listing the channel-lists returns 1 result", func() { resp, err := clAPI.List(ctx, &pb.ListChannelListRequest{Limit: 10, Offset: 0}) So(err, ShouldBeNil) So(resp.TotalCount, ShouldEqual, 1) So(resp.Result, ShouldHaveLength, 1) So(resp.Result[0], ShouldResemble, &pb.GetChannelListResponse{Id: clID, Name: "test channel-list"}) }) Convey("When deleting the channel-list", func() { _, err := clAPI.Delete(ctx, &pb.DeleteChannelListRequest{Id: clID}) So(err, ShouldBeNil) Convey("Then the channel-list has been deleted", func() { resp, err := clAPI.List(ctx, &pb.ListChannelListRequest{Limit: 10, Offset: 0}) So(err, ShouldBeNil) So(resp.TotalCount, ShouldEqual, 0) }) }) Convey("When creating a channel", func() { resp, err := cAPI.Create(ctx, &pb.CreateChannelRequest{ ChannelListID: clID, Channel: 4, Frequency: 868700000, }) So(err, ShouldBeNil) cID := resp.Id Convey("Then the channel has been created", func() { resp, err := cAPI.Get(ctx, &pb.GetChannelRequest{Id: cID}) So(err, ShouldBeNil) So(resp, ShouldResemble, &pb.GetChannelResponse{ Id: cID, ChannelListID: clID, Channel: 4, Frequency: 868700000, }) Convey("When updating the channel", func() { _, err := cAPI.Update(ctx, &pb.UpdateChannelRequest{ Id: cID, ChannelListID: clID, Channel: 5, Frequency: 868700000, }) So(err, ShouldBeNil) Convey("Then the channel has been updated", func() { resp, err := cAPI.Get(ctx, &pb.GetChannelRequest{Id: cID}) So(err, ShouldBeNil) So(resp, ShouldResemble, &pb.GetChannelResponse{ Id: cID, ChannelListID: clID, Channel: 5, Frequency: 868700000, }) }) }) Convey("When deleting the channel", func() { _, err := cAPI.Delete(ctx, &pb.DeleteChannelRequest{Id: cID}) So(err, ShouldBeNil) Convey("Then the channel has been deleted", func() { _, err := cAPI.Get(ctx, &pb.GetChannelRequest{Id: cID}) So(err, ShouldNotBeNil) }) }) Convey("When getting all channels for the channel-list", func() { resp, err := cAPI.ListByChannelList(ctx, &pb.ListChannelsByChannelListRequest{Id: clID}) So(err, ShouldBeNil) Convey("Then it contains the channel", func() { So(resp.Result, ShouldHaveLength, 1) So(resp.Result[0], ShouldResemble, &pb.GetChannelResponse{ Id: cID, ChannelListID: clID, Channel: 4, Frequency: 868700000, }) }) }) }) }) }) }) }