Skip to content

Commit

Permalink
add test and fix a decode bug
Browse files Browse the repository at this point in the history
  • Loading branch information
plucury committed Jun 2, 2012
1 parent f8e5eca commit 60f9667
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
2 changes: 0 additions & 2 deletions mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ func Decode(b []byte)*Mqtt{
}
mqtt.topics = topics
}
default:
mqtt = nil
}
return mqtt
}
Expand Down
64 changes: 64 additions & 0 deletions mqtt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package mqtt

import (
"testing"
"fmt"
)

func Test(t *testing.T){
mqtt := initTest()
fmt.Println("------ Origin MQTT Object")
printMqtt(mqtt)
fmt.Println("------ Encode To Binary")
bits := Encode(mqtt)
printBytes(bits)
fmt.Println("------ Decode To Object")
printMqtt(Decode(bits))
}

func initTest()*Mqtt{
mqtt := new(Mqtt)
mqtt.header = new(Header)
mqtt.header.messageType = MessageType(1)
mqtt.protocolName = "MQIsdp"
mqtt.protocolVersion = uint8(3)
mqtt.connectFlags = new(ConnectFlags)
mqtt.connectFlags.usernameFlag = true
mqtt.connectFlags.passwordFlag = true
mqtt.connectFlags.willRetain = false
mqtt.connectFlags.willQos = uint8(1)
mqtt.connectFlags.willFlag = true
mqtt.connectFlags.cleanSession = true
mqtt.keepAliveTimer = uint16(10)
mqtt.clientId = "xixihaha"
mqtt.willTopic = "topic"
mqtt.willMessage = "message"
mqtt.username = "name"
mqtt.password = "pwd"
return mqtt
}

func printByte(b byte){
out := make([]uint8, 8)
val := uint8(b)
for i := 1; val > 0;i += 1{
foo := val % 2
val = (val - foo) / 2
out[8 - i] = foo
}
fmt.Println(out)
}

func printBytes(b []byte){
for i := 0;i < len(b);i += 1{
printByte(b[i])
}
}

func printMqtt(mqtt *Mqtt){
fmt.Printf("MQTT = %+v\n", *mqtt)
fmt.Printf("Header = %+v\n", *mqtt.header)
if mqtt.connectFlags != nil{
fmt.Printf("ConnectFlags = %+v\n", *mqtt.connectFlags)
}
}

0 comments on commit 60f9667

Please sign in to comment.