Skip to content

Commit

Permalink
Merge pull request #30 from seqsense/fix_options_field_name
Browse files Browse the repository at this point in the history
Fix options field name
  • Loading branch information
t-asaka authored Dec 18, 2018
2 parents 8a50f24 + 020715a commit c64f0bd
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 17 deletions.
4 changes: 2 additions & 2 deletions awsiotprotocol/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ type Config struct {
KeyPath string
CertPath string
CaPath string
ClientId string
Url string
ClientID string
URL string
}
4 changes: 2 additions & 2 deletions awsiotprotocol/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func init() {
registerProtocol(MockProtocol{})
}

// ByUrl selects MQTT connection type by URL string.
func ByUrl(u string) (Protocol, error) {
// ByURL selects MQTT connection type by URL string.
func ByURL(u string) (Protocol, error) {
url, err := url.Parse(u)
if err != nil {
return nil, err
Expand Down
35 changes: 35 additions & 0 deletions awsiotprotocol/loader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2018 SEQSENSE, Inc.
//
// 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 awsiotprotocol

import (
"testing"
)

func TestByURL(t *testing.T) {
testcase := []struct {
input string
expected Protocol
}{
{input: "mqtts://example.com", expected: Mqtts{}},
{input: "wss://example.com", expected: Wss{}},
}
for _, v := range testcase {
actual, _ := ByURL(v.input)
if actual != v.expected {
t.Errorf("awsiotprotocol.ByURL failed.\ninput: %#v\nactual: %#v\nexpected: %#v\n", v.input, actual, v.expected)
}
}
}
4 changes: 2 additions & 2 deletions awsiotprotocol/mqtts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s Mqtts) Name() string {

// NewClientOptions returns MQTT connection options.
func (s Mqtts) NewClientOptions(opt *Config) (*mqtt.ClientOptions, error) {
url, err := url.Parse(opt.Url)
url, err := url.Parse(opt.URL)
if err != nil {
return nil, err
}
Expand All @@ -63,7 +63,7 @@ func (s Mqtts) NewClientOptions(opt *Config) (*mqtt.ClientOptions, error) {
opts := mqtt.NewClientOptions()
opts.AddBroker(
fmt.Sprintf("ssl://%s:%d", host, port))
opts.SetClientID(opt.ClientId)
opts.SetClientID(opt.ClientID)
opts.SetTLSConfig(tlsconfig)

return opts, nil
Expand Down
48 changes: 48 additions & 0 deletions awsiotprotocol/mqtts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2018 SEQSENSE, Inc.
//
// 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 awsiotprotocol

import (
"testing"
)

func TestMqttsName(t *testing.T) {
name := Mqtts{}.Name()

if name != "mqtts" {
t.Fatalf("MQTTS protocol name is bad")
}
}

func TestMqttsNewClientOptions(t *testing.T) {
opt := &Config{
URL: "mqtts://example.com:8882",
ClientID: "mqttsclientid",
CaPath: "../sample/samplecerts/cafile.pem",
CertPath: "../sample/samplecerts/client-crt.pem",
KeyPath: "../sample/samplecerts/client-key.pem",
}
mqttOpts, _ := Mqtts{}.NewClientOptions(opt)

if mqttOpts == nil {
t.Fatalf("MQTT.ClientOptions is nil")
}
if mqttOpts.Servers[0].Scheme != "ssl" || mqttOpts.Servers[0].Host != "example.com:8882" {
t.Fatalf("Broker is not added")
}
if mqttOpts.ClientID != "mqttsclientid" {
t.Fatalf("ClientID is not set")
}
}
4 changes: 2 additions & 2 deletions awsiotprotocol/wss.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (s Wss) Name() string {
// NewClientOptions returns MQTT connection options.
func (s Wss) NewClientOptions(opt *Config) (*mqtt.ClientOptions, error) {
opts := mqtt.NewClientOptions()
opts.AddBroker(opt.Url)
opts.SetClientID(opt.ClientId)
opts.AddBroker(opt.URL)
opts.SetClientID(opt.ClientID)
opts.SetAutoReconnect(false) // use custom reconnection algorithm with offline queueing

return opts, nil
Expand Down
48 changes: 48 additions & 0 deletions awsiotprotocol/wss_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2018 SEQSENSE, Inc.
//
// 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 awsiotprotocol

import (
"testing"
)

func TestWssName(t *testing.T) {
name := Wss{}.Name()

if name != "wss" {
t.Fatalf("Wss protocol name is bad")
}
}

func TestWssNewClientOptions(t *testing.T) {
opt := &Config{
URL: "wss://example.com:443",
ClientID: "wssclientid",
}
mqttOpts, _ := Wss{}.NewClientOptions(opt)

if mqttOpts == nil {
t.Fatalf("MQTT.ClientOptions is nil")
}
if mqttOpts.Servers[0].Scheme != "wss" || mqttOpts.Servers[0].Host != "example.com:443" {
t.Fatalf("Broker is not added")
}
if mqttOpts.ClientID != "wssclientid" {
t.Fatalf("ClientID is not set")
}
if mqttOpts.AutoReconnect != false {
t.Fatalf("AutoReconnect flag is not set")
}
}
4 changes: 2 additions & 2 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestDeviceClient(t *testing.T) {
MaximumReconnectTime: time.Millisecond * 50,
MinimumConnectionTime: time.Millisecond * 50,
Keepalive: time.Second * 2,
Url: "mock://",
URL: "mock://",
OfflineQueueing: true,
OfflineQueueMaxSize: 100,
OfflineQueueDropBehavior: "oldest",
Expand Down Expand Up @@ -178,7 +178,7 @@ func TestConnectionLostHandler(t *testing.T) {
MaximumReconnectTime: time.Millisecond * 50,
MinimumConnectionTime: time.Millisecond * 50,
Keepalive: time.Second * 2,
Url: "mock://",
URL: "mock://",
OfflineQueueing: true,
OfflineQueueMaxSize: 100,
OfflineQueueDropBehavior: "oldest",
Expand Down
6 changes: 3 additions & 3 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type DeviceClient struct {
// New returns new MQTT client with offline queueing and reconnecting.
// Returned client is not connected to the broaker until calling Connect().
func New(opt *Options) *DeviceClient {
p, err := awsiotprotocol.ByUrl(opt.Url)
p, err := awsiotprotocol.ByURL(opt.URL)
if err != nil {
panic(err)
}
Expand All @@ -61,8 +61,8 @@ func New(opt *Options) *DeviceClient {
KeyPath: opt.KeyPath,
CertPath: opt.CertPath,
CaPath: opt.CaPath,
ClientId: opt.ClientId,
Url: opt.Url,
ClientID: opt.ClientID,
URL: opt.URL,
},
)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ type Options struct {
KeyPath string
CertPath string
CaPath string
ClientId string
ClientID string
Region string
BaseReconnectTime time.Duration
MaximumReconnectTime time.Duration
MinimumConnectionTime time.Duration
Keepalive time.Duration
Url string
URL string
Debug bool
Qos byte
Retain bool
Expand Down
4 changes: 2 additions & 2 deletions sample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ func main() {
KeyPath: *privatePath,
CertPath: *certPath,
CaPath: *caPath,
ClientId: *thingName,
ClientID: *thingName,
Region: *region,
BaseReconnectTime: time.Millisecond * 50,
MaximumReconnectTime: time.Second * 2,
MinimumConnectionTime: time.Second * 2,
Keepalive: time.Second * 2,
Url: *url,
URL: *url,
Debug: true,
Qos: 1,
Retain: false,
Expand Down
21 changes: 21 additions & 0 deletions sample/samplecerts/cafile.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIDXTCCAkWgAwIBAgIJALYnx/0t6Jk3MA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
BAYTAkpQMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTgxMjE3MDQwMjIwWhcNMjExMDA2MDQwMjIwWjBF
MQswCQYDVQQGEwJKUDETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
CgKCAQEAthAKd0PmlpNTCFGsCPb4iZsa2FpFbiSCKp3mKa3Rn0I4YvaD6Xj+r5zb
P0qjoyyAOr4lxKBxBZYeAYEwopncRRCzfGi67Ei6r3AXrRdyGjzkIb3r9QFl+v9k
y66iuVwy0/7vLxekpBfheezWTQWrREgza6MSdBjAT+9Iy3yg0IThHbm9MhThUU24
ctvcDlMjFLXqJfmK3ZRPp/aYNGyJ5zBchdiYlIPD9kSp6mhJdDdisDZs21whWY4p
2LeRF4rTZuESuMzruFI83vEddZsZhfwgjOZbDPv7yVKNDam+BJc/XIZtD5Lg6IFn
WsZLL0VoHbu1TUciPHDGzZq2y9U16wIDAQABo1AwTjAdBgNVHQ4EFgQUyRliehkq
AfXoEHbhCkx2qaLTk6cwHwYDVR0jBBgwFoAUyRliehkqAfXoEHbhCkx2qaLTk6cw
DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEASNXZpGT3sJw07EHrjdme
GuyHmd9JJcajIfb0fgcqY3SnXNfQEFR0y1aYhjK+K5I7pKdFwLddvZjNCqZzlCeZ
6KUrwPpdnbwQqAngLrv9NW4OpQzcVlZUiJTpW4kvXQilqY7Cw18VJiNRc5WI+tKQ
e14iQJ5QNCcfPzruS/xZgYsbJlWGAGIr++v0MLb/6nDRu3fKGbgGddnsXQpy5Iwq
5LLHYyhEQIGVeDvAmLap72ALHpga0A66iZc819sJnJD+oubi/zjKBmYNFnGqN3VM
wKghyH8PGxIOcsFYb+sg6u8vaDINoBNryAvOFGR00OnCmte1LwzrtIUBQ90THjAD
gA==
-----END CERTIFICATE-----
19 changes: 19 additions & 0 deletions sample/samplecerts/client-crt.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDBjCCAe4CCQD6x2uvM9t8JjANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJK
UDETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0
cyBQdHkgTHRkMB4XDTE4MTIxNzA0MDMwN1oXDTIwMDQzMDA0MDMwN1owRTELMAkG
A1UEBhMCSlAxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0
IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
ANelzglDH82UizLklwhZp4f3r6uAIdplarCZLeBHAHmZrDKOvyKzVJLSvxWTY6VW
b54btFF+EQKCEqJ1FriY5fwDPn/AVQ+cZ0KznUbOtdZGk0Hn4Vdd6zlCxLD3uJyM
cgSjLUsm/oAme961CAiUi24mC4GdImcxMKDtOW++auHhZchU/z+i0QPtgv256JwU
bxd812bVfCXIMu92siYaahwYG5GCy6/J4fJ4OKyo1wDabzfOWmlF3ef+XZcyTQ9+
lYFd6jxDDK7vqS1czkEbK/YiXXWXvn8mP9gdq0sJ3GveYqusYE+GnoDTdgckwaJ/
pKkoadgxCTupYb0hmunTfBkCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAJdONlGfF
oKl+8qdPv8+30nLufwGk1UCLBvTufGUEB9k36lpvXrpp6G7oUZC78fvdqDZ5nCIJ
PIVcrmAjxbCLp/4vVBCrmir/uYsB6buI5RvackJaafwB8NHKRiJxlXFnk+wEeQCf
8ODlzinCdTjI90MnqlNBe8zPJtlPMncdi6k6dO3uoSG1lBjkUkG0ag+aglJQVf9r
t8/uk4gF684KeOJkWMq3H6O/H6CiCCOd3/GmE1Q+9GtW59hkecizdfJju7otzDwo
k0x2mKKUOD9rOoUUc+fqoq/YdPSYITeGHaRfX4EijsS63I1Q49QvASbxETRGgaF3
ZmmCTX+2SfSJWg==
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions sample/samplecerts/client-key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA16XOCUMfzZSLMuSXCFmnh/evq4Ah2mVqsJkt4EcAeZmsMo6/
IrNUktK/FZNjpVZvnhu0UX4RAoISonUWuJjl/AM+f8BVD5xnQrOdRs611kaTQefh
V13rOULEsPe4nIxyBKMtSyb+gCZ73rUICJSLbiYLgZ0iZzEwoO05b75q4eFlyFT/
P6LRA+2C/bnonBRvF3zXZtV8Jcgy73ayJhpqHBgbkYLLr8nh8ng4rKjXANpvN85a
aUXd5/5dlzJND36VgV3qPEMMru+pLVzOQRsr9iJddZe+fyY/2B2rSwnca95iq6xg
T4aegNN2ByTBon+kqShp2DEJO6lhvSGa6dN8GQIDAQABAoIBAQCdOoehmy5J7r8b
CS3RacTLTtznVxHXsU/7mIOpXTtMba3uYsObIlNMhZnKul7Rwta42Yw8SoVOZdX+
Ww6I4vn0J81eybV7H1bufBT09mv/4g8l41IaKCED69mLCeKxSXksRmkUvUab2vM/
qpBD6UJLujNzekUGJghFnRDel2BIdX4oBSsTfTGEC5nNp/g30mymaNd4FhkmMccc
xry0FWc2DNbMI9HxZyb304p5myUSsBbPT+vzgRlsPw3zFQl7em5+Ljcnm2uKDMjw
TKeh8AZF9k6HI2ufs5xaJyPDTO6d6vJ9TPTInEZERPy47u1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