Beispiel #1
0
// TestRetainJustPublish tests
// 1. connect gateway to local broker
// 2. send data with retaind flag dummy device normaly
func TestRetainJustPublish(t *testing.T) {
	assert := assert.New(t)
	iniStr := `
	[gateway]
	
	    name = retainham
	
	[broker "local/1"]
	
	    host = localhost
	    port = 1883
	
	[device "doraretain/dummy"]
	
	    broker = local
	    qos = 0
	
	    interval = 10
	    payload = Hello world retain true.
	
	    type = EnOcean
	    retain = true
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	assert.Nil(err)
	commandChannel := make(chan string)
	go fuji.StartByFileWithChannel(conf, commandChannel)

	time.Sleep(2 * time.Second)
}
Beispiel #2
0
// TestWillJustPublish tests
// 1. connect localhost broker with will message
// 2. send data from a dummy device
// 3. disconnect
func TestWillJustPublish(t *testing.T) {
	assert := assert.New(t)

	iniStr := `
	[gateway]
	    name = willjustpublishham
	[broker "local/1"]
	    host = localhost
	    port = 1883
	    will_message = no letter is good letter.
	[device "dora/dummy"]
	    broker = local
	    qos = 0
	    interval = 10
	    payload = Hello will just publish world.
	    type = EnOcean
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	assert.Nil(err)
	commandChannel := make(chan string)
	go fuji.StartByFileWithChannel(conf, commandChannel)
	time.Sleep(5 * time.Second)

	//	fuji.Stop()
}
Beispiel #3
0
func TestStatus(t *testing.T) {
	assert := assert.New(t)

	iniStr := `
[broker "sango"]
  host = 192.168.1.20
  port = 1033
[status "cpu"]
  cpu_times = user, system, idle, nice, iowait, irq, softirq, guest
[status "memory"]
  virtual_memory = total, available, percent, used, free
[status]
  broker = sango
  interval = 10
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	assert.Nil(err)
	tt, err := NewStatus(conf)
	assert.Nil(err)
	st, ok := tt.(Status)
	assert.True(ok)

	assert.Equal(st.BrokerName, "sango")
	assert.Equal(st.Interval, 10)

	assert.Equal(8, len(st.CPU.CpuTimes))
	assert.Equal(5, len(st.Memory.VirtualMemory))
}
Beispiel #4
0
func TestNewGatewayMaxRetryCount(t *testing.T) {
	assert := assert.New(t)

	{ // default
		iniStr := `
[gateway]
name = sango
`
		conf, err := inidef.LoadConfigByte([]byte(iniStr))
		gw, err := NewGateway(conf)
		assert.Nil(err)
		assert.Equal(3, gw.MaxRetryCount)
	}
	{ // specified
		iniStr := `
[gateway]
name = sango
max_retry_count = 10
`
		conf, err := inidef.LoadConfigByte([]byte(iniStr))
		gw, err := NewGateway(conf)
		assert.Nil(err)
		assert.Equal(10, gw.MaxRetryCount)
	}
	{ // minus fail validation
		iniStr := `
[gateway]
name = sango
max_retry_count = -10
`
		conf, err := inidef.LoadConfigByte([]byte(iniStr))
		_, err = NewGateway(conf)
		assert.NotNil(err)
	}
	{ // invalid int
		iniStr := `
[gateway]
name = sango
max_retry_count = aabbcc
`
		conf, err := inidef.LoadConfigByte([]byte(iniStr))
		_, err = NewGateway(conf)
		assert.NotNil(err)
	}
}
Beispiel #5
0
func generalIniWillTest(test iniWillTestCase, t *testing.T) {
	assert := assert.New(t)

	conf, err := inidef.LoadConfigByte([]byte(test.iniStr))
	assert.Nil(err)

	brokers, err := broker.NewBrokers(conf, make(chan message.Message))
	assert.Nil(err)
	assert.NotEqual(0, len(brokers))
}
Beispiel #6
0
func TestBrokerValidationPort(t *testing.T) {
	assert := assert.New(t)
	iniStr := `
[broker "sango/2"]
    host = 192.168.1.22
    port = 65536
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	_, err = NewBrokers(conf, make(chan message.Message))
	assert.NotNil(err)
}
Beispiel #7
0
func TestBrokerValidationWill(t *testing.T) {
	assert := assert.New(t)
	iniStr := `
	[broker "sango/1"]
    host = 192.168.1.22
    port = 1883
    will_message = will
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	b, err := NewBrokers(conf, make(chan message.Message))
	assert.Nil(err)
	assert.Equal(1, len(b))
	assert.Equal([]byte("will"), b[0].WillMessage)

	iniStr = `
	[broker "sango/1"]
    host = 192.168.1.22
    port = 1883
    will_message = \x01\x0f
`
	conf, err = inidef.LoadConfigByte([]byte(iniStr))
	b, err = NewBrokers(conf, make(chan message.Message))
	assert.Nil(err)
	assert.Equal(1, len(b))
	assert.Equal([]byte{1, 15}, b[0].WillMessage)

	// either will message has invalid binary, not error, just warn
	iniStr = `
	[broker "sango/1"]
    host = 192.168.1.22
    port = 1883
    will_message = \x01\x0fffff
`
	conf, err = inidef.LoadConfigByte([]byte(iniStr))
	b, err = NewBrokers(conf, make(chan message.Message))
	assert.Nil(err)
	assert.Equal(1, len(b))
	assert.Equal([]byte{1, 15}, b[0].WillMessage)
}
Beispiel #8
0
func TestNewGatewayInvalidName(t *testing.T) {
	assert := assert.New(t)

	{ // includes plus
		iniStr := `
[gateway]
name = bone+lessham
`
		conf, err := inidef.LoadConfigByte([]byte(iniStr))
		_, err = NewGateway(conf)
		assert.NotNil(err)
	}
	{ // includes sharp
		iniStr := `
[gateway]
name = ` + "`" + `bone\#lessham`
		conf, err := inidef.LoadConfigByte([]byte(iniStr))
		_, err = NewGateway(conf)
		assert.NotNil(err)
	}
	{ // too long
		iniStr := `
[gateway]
name = bonelesshaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
`
		conf, err := inidef.LoadConfigByte([]byte(iniStr))
		_, err = NewGateway(conf)
		assert.NotNil(err)
	}
	{ // \\U0000 string
		iniStr := fmt.Sprintf(`
[gateway]
name = 	na%cme
`, '\u0000')
		conf, err := inidef.LoadConfigByte([]byte(iniStr))
		_, err = NewGateway(conf)
		assert.NotNil(err)
	}
}
Beispiel #9
0
func TestBrokerValidationHost(t *testing.T) {
	assert := assert.New(t)

	// invalid host, too long
	iniStr := `
[broker "sango/2"]
    host = 192.168.1.22aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    port = 1883
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	_, err = NewBrokers(conf, make(chan message.Message))
	assert.NotNil(err)
}
Beispiel #10
0
// generalIniRetainSerialDeviceTest checks retain function with serial device
func generalIniRetainSerialDeviceTest(test iniRetainTestCase, t *testing.T) {
	assert := assert.New(t)

	conf, err := inidef.LoadConfigByte([]byte(test.iniStr))
	assert.Nil(err)

	brokers, err := broker.NewBrokers(conf, make(chan message.Message))
	assert.Nil(err)

	devices, _, err := device.NewDevices(conf, brokers)
	assert.Nil(err)
	assert.Equal(1, len(devices))
}
Beispiel #11
0
func TestNewSerialDeviceInvalidQoS(t *testing.T) {
	assert := assert.New(t)

	iniStr := `
[device "dora/serial"]
    broker = sango
    qos = -1
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	b1 := &broker.Broker{Name: "sango"}
	brokers := []*broker.Broker{b1}
	_, err = NewSerialDevice(conf.Sections[1], brokers, make(chan message.Message))
	assert.NotNil(err)
}
Beispiel #12
0
func TestNewSerialDeviceInvalidBroker(t *testing.T) {
	assert := assert.New(t)

	iniStr := `
[device "dora/serial"]
    broker = doesNotExist
    qos = 1
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	b1 := &broker.Broker{Name: "sango"}
	brokers := []*broker.Broker{b1}
	_, err = NewSerialDevice(conf.Sections[1], brokers, NewDeviceChannel())
	assert.NotNil(err)
}
Beispiel #13
0
func TestNewDummyDeviceInvalidBroker(t *testing.T) {
	assert := assert.New(t)

	iniStr := `
[device "dora/dummy"]
    broker = doesNotExist
    interval = 10
    qos = 1
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	b1 := &broker.Broker{Name: "sango"}
	brokers := []*broker.Broker{b1}
	_, err = NewDummyDevice(conf.Sections[1], brokers, make(chan message.Message))
	assert.NotNil(err)
}
Beispiel #14
0
func TestNewDummyDeviceInvalidQoS(t *testing.T) {
	assert := assert.New(t)

	iniStr := `
[device "dora/dummy"]
    broker = sango
    interval = -1
    qos = -1
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	b1 := &broker.Broker{Name: "sango"}
	brokers := []*broker.Broker{b1}
	_, err = NewDummyDevice(conf.Sections[1], brokers, NewDeviceChannel())
	assert.NotNil(err)
}
Beispiel #15
0
func TestNewBrokersSingle(t *testing.T) {
	assert := assert.New(t)

	iniStr := `
[broker "sango/2"]
    host = 192.168.1.22
    port = 1883
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	b, err := NewBrokers(conf, make(chan message.Message))
	assert.Nil(err)
	assert.Equal(1, len(b))
	assert.Equal("sango", b[0].Name)
	assert.Equal(2, b[0].Priority)
	assert.Equal("", b[0].TopicPrefix)
	assert.Equal([]byte{}, b[0].WillMessage)
}
Beispiel #16
0
// generalIniRetainDummyDeviceTest checks retain function with dummy device
func generalIniRetainDummyDeviceTest(test iniRetainTestCase, t *testing.T) {
	assert := assert.New(t)

	conf, err := inidef.LoadConfigByte([]byte(test.iniStr))
	assert.Nil(err)

	brokers, err := broker.NewBrokers(conf, make(chan message.Message))
	assert.Nil(err)

	dummy, err := device.NewDummyDevice(conf.Sections[2], brokers, device.NewDeviceChannel())
	if test.expectedError == nil {
		assert.Nil(err)
		assert.NotNil(dummy)
	} else {
		assert.NotNil(err)
	}
}
Beispiel #17
0
func TestNewBrokersMulti(t *testing.T) {
	assert := assert.New(t)

	iniStr := `
[broker "sango/1"]
    host = 192.168.1.22
    port = 1883
[broker "sango/2"]
    host = 192.168.1.22
    port = 1883
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	b, err := NewBrokers(conf, make(chan message.Message))
	assert.Nil(err)
	assert.Equal(2, len(b))
	assert.Equal("sango", b[0].Name)
	assert.Equal(1, b[0].Priority)
	assert.Equal(2, b[1].Priority)
}
Beispiel #18
0
func TestNewSerialDeviceNotSetSize(t *testing.T) {
	assert := assert.New(t)

	iniStr := `
[device "dora/serial"]
    broker = sango
    qos = 1
    serial = /dev/tty.ble
    baud = 9600
    type = BLE
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	b1 := &broker.Broker{Name: "sango"}
	brokers := []*broker.Broker{b1}
	b, err := NewSerialDevice(conf.Sections[1], brokers, make(chan message.Message))
	assert.Nil(err)
	assert.NotNil(b.Broker)
	assert.Equal("dora", b.Name)
	assert.Equal(0, b.Size)
	assert.Equal("BLE", b.Type)
}
Beispiel #19
0
func TestNewBrokersSettings(t *testing.T) {
	assert := assert.New(t)

	iniStr := `
[broker "sango/2"]
    host = 192.168.1.22
    port = 1883
    username = usr
    password = pass
    topic_prefix = pre
    will_message = will
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	b, err := NewBrokers(conf, make(chan message.Message))
	assert.Nil(err)
	assert.Equal(1, len(b))
	assert.Equal("usr", b[0].Username)
	assert.Equal("pass", b[0].Password)
	assert.Equal("pre", b[0].TopicPrefix)
	assert.Equal([]byte("will"), b[0].WillMessage)
}
Beispiel #20
0
func genericWillTestDriver(t *testing.T, iniStr string, expectedTopic string, expectedPayload []byte) (ok bool) {
	assert := assert.New(t)

	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	assert.Nil(err)
	commandChannel := make(chan string)
	go fuji.StartByFileWithChannel(conf, commandChannel)

	gw, err := gateway.NewGateway(conf)
	assert.Nil(err)

	brokers, err := broker.NewBrokers(conf, gw.BrokerChan)
	assert.Nil(err)

	go func() {
		time.Sleep(1 * time.Second)

		subscriberChannel, err := setupWillSubscriber(gw, brokers[0])
		if err != inidef.Error("") {
			t.Error(err)
		}

		time.Sleep(1 * time.Second)

		// kill publisher
		brokers[0].FourceClose()
		fmt.Println("broker killed for getting will message")

		// check will message
		willMsg := <-subscriberChannel

		assert.Equal(expectedTopic, willMsg.Topic())
		assert.Equal(expectedPayload, willMsg.Payload())
		assert.Equal(byte(0), willMsg.Qos())
	}()
	time.Sleep(3 * time.Second)
	ok = true
	return ok
}
Beispiel #21
0
func TestNewDummyDevice(t *testing.T) {
	assert := assert.New(t)

	iniStr := `
[device "dora/dummy"]
    broker = sango
    qos = 1
    dummy = true
    interval = 10
    payload = Hello world.
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	b1 := &broker.Broker{Name: "sango"}
	brokers := []*broker.Broker{b1}
	b, err := NewDummyDevice(conf.Sections[1], brokers, NewDeviceChannel())
	assert.Nil(err)
	assert.NotNil(b.Broker)
	assert.Equal("dora", b.Name)
	assert.Equal(byte(1), b.QoS)
	assert.Equal(10, b.Interval)
	assert.Equal([]byte("Hello world."), b.Payload)
}
Beispiel #22
0
// TestRetainSubscribePublishClose
// 1. connect gateway to local broker
// 2. send data with retaind flag from dummy device
// 3. disconnect
// 4. reconnect
// 5. subscirbe and receive data
func TestRetainSubscribePublishClose(t *testing.T) {
	assert := assert.New(t)
	iniStr := `
	[gateway]
	
	    name = testRetainafterclose
	
	[broker "local/1"]
	
	    host = localhost
	    port = 1883
	
	[device "dora/dummy"]
	
	    broker = local
	    qos = 0
	
	    interval = 10
	    payload = Hello retained world to subscriber after close.
	
	    type = EnOcean
	    retain = true
`
	commandChannel := make(chan string)
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	assert.Nil(err)
	go fuji.StartByFileWithChannel(conf, commandChannel)

	gw, err := gateway.NewGateway(conf)
	if err != nil {
		t.Error("Cannot make Gateway")
	}

	brokerList, err := broker.NewBrokers(conf, gw.BrokerChan)
	if err != nil {
		t.Error("Cannot make BrokerList")
	}

	dummyDevice, err := device.NewDummyDevice(conf.Sections[3], brokerList, gw.DeviceChan)
	if err != nil {
		t.Error("Cannot make DummyDeviceList")
	}

	go func() {
		time.Sleep(2 * time.Second)

		// kill publisher
		gw.Stop()

		time.Sleep(2 * time.Second)

		subscriberChannel, err := setupRetainSubscriber(gw, brokerList[0], &dummyDevice)
		if err != inidef.Error("") {
			t.Error(err)
		}
		// check Retained message
		retainedMessage := <-subscriberChannel
		retainedTopic := retainedMessage[0]
		retainedPayload := retainedMessage[1]

		expectedTopic := fmt.Sprintf("%s/%s/%s/%s", brokerList[0].TopicPrefix, gw.Name, dummyDevice.Name, dummyDevice.Type)
		expectedPayload := dummyDevice.Payload

		assert.Equal(expectedTopic, retainedTopic)
		assert.Equal(expectedPayload, retainedPayload)
	}()
	time.Sleep(5 * time.Second)
}