-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
qrg: Implement grpc methods for gateway qr code parser service
- Loading branch information
Vlad Vitan
committed
Aug 28, 2024
1 parent
6f8044d
commit 8ec181c
Showing
6 changed files
with
258 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright © 2024 The Things Network Foundation, The Things Industries B.V. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package qrcodegenerator | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"go.thethings.network/lorawan-stack/v3/pkg/qrcodegenerator/qrcode/gateways" | ||
"go.thethings.network/lorawan-stack/v3/pkg/rpcmetadata" | ||
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb" | ||
) | ||
|
||
var errCorruptedData = errors.New("corrupted data") | ||
|
||
type gatewayQRCodeGeneratorServer struct { | ||
ttnpb.UnimplementedGatewayQRCodeGeneratorServer | ||
|
||
QRG *QRCodeGenerator | ||
} | ||
|
||
// GetFormat implements EndDeviceQRCodeGenerator. | ||
func (s *gatewayQRCodeGeneratorServer) GetFormat(ctx context.Context, req *ttnpb.GetQRCodeFormatRequest) (*ttnpb.QRCodeFormat, error) { | ||
_, err := rpcmetadata.WithForwardedAuth(ctx, s.QRG.AllowInsecureForCredentials()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
format := s.QRG.gateways.GetGatewayFormat(req.FormatId) | ||
if format == nil { | ||
return nil, errFormatNotFound.New() | ||
} | ||
return format.Format(), nil | ||
} | ||
|
||
// Parse implements EndDeviceQRCodeGenerator. | ||
func (s *gatewayQRCodeGeneratorServer) Parse(ctx context.Context, req *ttnpb.ParseGatewayQRCodeRequest) (*ttnpb.ParseGatewayQRCodeResponse, error) { | ||
_, err := rpcmetadata.WithForwardedAuth(ctx, s.QRG.AllowInsecureForCredentials()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
data, err := s.QRG.gateways.Parse(req.FormatId, req.QrCode) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ttigpro1Data, ok := data.(*gateways.TTIgpro1) | ||
if !ok { | ||
return nil, errCorruptedData | ||
} | ||
|
||
return &ttnpb.ParseGatewayQRCodeResponse{ | ||
FormatId: data.FormatID(), | ||
ClaimGatewayRequest: &ttnpb.ClaimGatewayRequest{ | ||
SourceGateway: &ttnpb.ClaimGatewayRequest_AuthenticatedIdentifiers_{ | ||
AuthenticatedIdentifiers: &ttnpb.ClaimGatewayRequest_AuthenticatedIdentifiers{ | ||
GatewayEui: ttigpro1Data.GtwEUI[:], | ||
AuthenticationCode: ttigpro1Data.OwnerToken, | ||
}, | ||
}, | ||
}, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright © 2024 The Things Network Foundation, The Things Industries B.V. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package qrcodegenerator_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/smarty/assertions" | ||
"go.thethings.network/lorawan-stack/v3/pkg/component" | ||
componenttest "go.thethings.network/lorawan-stack/v3/pkg/component/test" | ||
"go.thethings.network/lorawan-stack/v3/pkg/errors" | ||
"go.thethings.network/lorawan-stack/v3/pkg/log" | ||
. "go.thethings.network/lorawan-stack/v3/pkg/qrcodegenerator" | ||
"go.thethings.network/lorawan-stack/v3/pkg/qrcodegenerator/qrcode/gateways" | ||
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb" | ||
"go.thethings.network/lorawan-stack/v3/pkg/util/test" | ||
"go.thethings.network/lorawan-stack/v3/pkg/util/test/assertions/should" | ||
) | ||
|
||
func TestGatewayQRCodeParsing(t *testing.T) { | ||
a := assertions.New(t) | ||
ctx := log.NewContext(test.Context(), test.GetLogger(t)) | ||
|
||
c := componenttest.NewComponent(t, &component.Config{}) | ||
ttigpro1 := new(gateways.TTIgpro1Format) | ||
qrg, err := New(c, &Config{}, WithGatewayFormat(ttigpro1.ID(), ttigpro1)) | ||
test.Must(qrg, err) | ||
|
||
componenttest.StartComponent(t, c) | ||
defer c.Close() | ||
|
||
mustHavePeer(ctx, c, ttnpb.ClusterRole_QR_CODE_GENERATOR) | ||
|
||
client := ttnpb.NewGatewayQRCodeGeneratorClient(c.LoopbackConn()) | ||
|
||
for _, tc := range []struct { | ||
Name string | ||
FormatID string | ||
GetQRData func() []byte | ||
Assertion func(*assertions.Assertion, *ttnpb.ParseGatewayQRCodeResponse, error) bool | ||
}{ | ||
{ | ||
Name: "EmptyData", | ||
GetQRData: func() []byte { | ||
return []byte{} | ||
}, | ||
Assertion: func(a *assertions.Assertion, resp *ttnpb.ParseGatewayQRCodeResponse, err error) bool { | ||
if !a.So(resp, should.BeNil) { | ||
return false | ||
} | ||
if !a.So(errors.IsInvalidArgument(err), should.BeTrue) { | ||
return false | ||
} | ||
return true | ||
}, | ||
}, | ||
{ | ||
Name: "UnknownFormat", | ||
FormatID: "unknown", | ||
GetQRData: func() []byte { | ||
return []byte(`https://ttig.pro/c/ec656efffe000128/abcdef123456`) | ||
}, | ||
Assertion: func(a *assertions.Assertion, resp *ttnpb.ParseGatewayQRCodeResponse, err error) bool { | ||
if !a.So(resp, should.BeNil) { | ||
return false | ||
} | ||
if !a.So(errors.IsInvalidArgument(err), should.BeTrue) { | ||
return false | ||
} | ||
return true | ||
}, | ||
}, | ||
{ | ||
Name: "InvalidFormat", | ||
FormatID: "tr005", | ||
GetQRData: func() []byte { | ||
return []byte(`https://ttig.pro/c/ec656efffe000128/abcdef123456`) | ||
}, | ||
Assertion: func(a *assertions.Assertion, resp *ttnpb.ParseGatewayQRCodeResponse, err error) bool { | ||
if !a.So(resp, should.BeNil) { | ||
return false | ||
} | ||
if !a.So(errors.IsInvalidArgument(err), should.BeTrue) { | ||
return false | ||
} | ||
return true | ||
}, | ||
}, | ||
{ | ||
Name: "ValidTTIgpro1", | ||
FormatID: ttigpro1.ID(), | ||
GetQRData: func() []byte { | ||
return []byte(`https://ttig.pro/c/ec656efffe000128/abcdef123456`) | ||
}, | ||
Assertion: func(a *assertions.Assertion, resp *ttnpb.ParseGatewayQRCodeResponse, err error) bool { | ||
if !a.So(resp, should.NotBeNil) { | ||
return false | ||
} | ||
if !a.So(err, should.BeNil) { | ||
return false | ||
} | ||
a.So(resp.FormatId, should.Equal, ttigpro1.ID()) | ||
|
||
return true | ||
}, | ||
}, | ||
} { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
resp, err := client.Parse(ctx, &ttnpb.ParseGatewayQRCodeRequest{ | ||
FormatId: tc.FormatID, | ||
QrCode: tc.GetQRData(), | ||
}, c.WithClusterAuth()) | ||
if !a.So(tc.Assertion(a, resp, err), should.BeTrue) { | ||
t.FailNow() | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters