Ejemplo n.º 1
0
func TestNewDevice(t *testing.T) {
	db, _ := db.NewDatabase(os.TempDir())
	client, err := NewDevice("Test Client", db)

	if err != nil {
		t.Fatal(err)
	}
	if x := len(client.PublicKey()); x == 0 {
		t.Fatal(x)
	}
	if x := len(client.PrivateKey()); x == 0 {
		t.Fatal(x)
	}

	entity := db.EntityWithName("Test Client")
	if is, want := entity.Name(), "Test Client"; is != want {
		t.Fatalf("is=%v want=%v", is, want)
	}

	if err != nil {
		t.Fatal(err)
	}
	if is, want := entity.PublicKey(), client.PublicKey(); reflect.DeepEqual(is, want) == false {
		t.Fatalf("is=%v want=%v", is, want)
	}
	if is, want := entity.PrivateKey(), client.PrivateKey(); reflect.DeepEqual(is, want) == false {
		t.Fatalf("is=%v want=%v", is, want)
	}
}
Ejemplo n.º 2
0
func TestDeletePairing(t *testing.T) {
	username := "******"
	entity := db.NewEntity(username, []byte{0x01, 0x02}, nil)
	database, _ := db.NewDatabase(os.TempDir())
	database.SaveEntity(entity)

	in := util.NewTLV8Container()
	in.SetByte(TagPairingMethod, PairingMethodDelete.Byte())
	in.SetByte(TagSequence, 0x01)
	in.SetString(TagUsername, username)

	controller := NewPairingController(database)

	out, err := controller.Handle(in)
	if err != nil {
		t.Fatal(err)
	}
	if out == nil {
		t.Fatal("no response")
	}

	if is, want := out.GetByte(TagSequence), byte(0x2); is != want {
		t.Fatalf("is=%v want=%v", is, want)
	}

	if _, err := database.EntityWithName(username); err == nil {
		t.Fatal("expected error")
	}
}
Ejemplo n.º 3
0
func TestUnknownPairingMethod(t *testing.T) {
	tlv8 := util.NewTLV8Container()
	tlv8.SetByte(TagPairingMethod, 0x09)

	database, _ := db.NewDatabase(os.TempDir())
	controller := NewPairingController(database)

	out, err := controller.Handle(tlv8)

	if err == nil {
		t.Fatal("expected error for unknown pairing method")
	}
	if out != nil {
		t.Fatal(out)
	}
}
Ejemplo n.º 4
0
func TestAddPairing(t *testing.T) {
	in := util.NewTLV8Container()
	in.SetByte(TagPairingMethod, PairingMethodAdd.Byte())
	in.SetByte(TagSequence, 0x01)
	in.SetString(TagUsername, "Unit Test")
	in.SetBytes(TagPublicKey, []byte{0x01, 0x02})

	database, _ := db.NewDatabase(os.TempDir())
	controller := NewPairingController(database)

	out, err := controller.Handle(in)
	if err != nil {
		t.Fatal(err)
	}
	if out == nil {
		t.Fatal("no response")
	}
	if is, want := out.GetByte(TagSequence), byte(0x2); is != want {
		t.Fatalf("is=%v want=%v", is, want)
	}
}
Ejemplo n.º 5
0
func TestNewDevice(t *testing.T) {
	var e db.Entity
	var client Device
	var err error

	database, _ := db.NewDatabase(os.TempDir())
	client, err = NewDevice("Test Client", database)

	if err != nil {
		t.Fatal(err)
	}
	if x := len(client.PublicKey()); x == 0 {
		t.Fatal(x)
	}
	if x := len(client.PrivateKey()); x == 0 {
		t.Fatal(x)
	}

	if e, err = database.EntityWithName("Test Client"); err != nil {
		t.Fatal(err)
	}

	if is, want := e.Name, "Test Client"; is != want {
		t.Fatalf("is=%v want=%v", is, want)
	}

	if err != nil {
		t.Fatal(err)
	}
	if is, want := e.PublicKey, client.PublicKey(); reflect.DeepEqual(is, want) == false {
		t.Fatalf("is=%v want=%v", is, want)
	}
	if is, want := e.PrivateKey, client.PrivateKey(); reflect.DeepEqual(is, want) == false {
		t.Fatalf("is=%v want=%v", is, want)
	}
}
Ejemplo n.º 6
0
func main() {
	database, _ := db.NewDatabase("./data")
	c, _ := netio.NewDevice("Golang Client", database)
	client := pair.NewSetupClientController("740-51-881", c, database)
	pairStartRequest := client.InitialPairingRequest()

	pairStartResponse, err := pairSetup(pairStartRequest)
	if err != nil {
		log.Fatal(err)
	}

	// 2) S -> C
	pairVerifyRequest, err := pair.HandleReaderForHandler(pairStartResponse, client)
	if err != nil {
		log.Fatal(err)
	}

	// 3) C -> S
	pairVerifyResponse, err := pairSetup(pairVerifyRequest)
	if err != nil {
		log.Fatal(err)
	}

	// 4) S -> C
	pairKeyRequest, err := pair.HandleReaderForHandler(pairVerifyResponse, client)
	if err != nil {
		log.Fatal(err)
	}

	// 5) C -> S
	pairKeyRespond, err := pairSetup(pairKeyRequest)
	if err != nil {
		log.Fatal(err)
	}

	// 6) S -> C
	request, err := pair.HandleReaderForHandler(pairKeyRespond, client)
	if err != nil {
		log.Fatal(err)
	}

	if request != nil {
		log.Println(request)
	}

	log.Println("*** Pairing done ***")

	verify := pair.NewVerifyClientController(c, database)

	verifyStartRequest := verify.InitialKeyVerifyRequest()
	// 1) C -> S
	verifyStartResponse, err := pairVerify(verifyStartRequest)
	if err != nil {
		log.Fatal(err)
	}

	// 2) S -> C
	verifyFinishRequest, err := pair.HandleReaderForHandler(verifyStartResponse, verify)
	if err != nil {
		log.Fatal(err)
	}

	// 3) C -> S
	verifyFinishResponse, err := pairVerify(verifyFinishRequest)
	if err != nil {
		log.Fatal(err)
	}

	// 4) S -> C
	last_request, err := pair.HandleReaderForHandler(verifyFinishResponse, verify)
	if err != nil {
		log.Fatal(err)
	}

	if last_request != nil {
		log.Println(last_request)
	}

	log.Println("*** Key Verification done ***")
}