示例#1
0
// Tests the pairing key verification
func TestInvalidPublicKey(t *testing.T) {
	storage, err := util.NewTempFileStorage()

	if err != nil {
		t.Fatal(err)
	}

	database := db.NewDatabaseWithStorage(storage)
	bridge, err := hap.NewSecuredDevice("Macbook Bridge", "001-02-003", database)

	if err != nil {
		t.Fatal(err)
	}

	context := hap.NewContextForSecuredDevice(bridge)

	controller := NewVerifyServerController(database, context)

	client, _ := hap.NewDevice("HomeKit Client", database)
	clientController := NewVerifyClientController(client, database)

	req := clientController.InitialKeyVerifyRequest()
	reqContainer, err := util.NewTLV8ContainerFromReader(req)

	if err != nil {
		t.Fatal(err)
	}

	reqContainer.SetByte(TagPublicKey, byte(0x01))
	// 1) C -> S
	if _, err = HandleReaderForHandler(reqContainer.BytesBuffer(), controller); err != errInvalidClientKeyLength {
		t.Fatal("expected invalid client key length error")
	}
}
示例#2
0
// Tests the pairing key verification
func TestPairVerifyIntegration(t *testing.T) {
	storage, err := util.NewTempFileStorage()

	if err != nil {
		t.Fatal(err)
	}

	database := db.NewDatabaseWithStorage(storage)
	bridge, err := hap.NewSecuredDevice("Macbook Bridge", "001-02-003", database)

	if err != nil {
		t.Fatal(err)
	}

	context := hap.NewContextForSecuredDevice(bridge)
	controller := NewVerifyServerController(database, context)

	clientDatabase, _ := db.NewTempDatabase()
	bridgeEntity := db.NewEntity(bridge.Name(), bridge.PublicKey(), nil)
	err = clientDatabase.SaveEntity(bridgeEntity)

	if err != nil {
		t.Fatal(err)
	}

	client, _ := hap.NewDevice("HomeKit Client", clientDatabase)
	clientEntity := db.NewEntity(client.Name(), client.PublicKey(), nil)
	err = database.SaveEntity(clientEntity)

	if err != nil {
		t.Fatal(err)
	}

	clientController := NewVerifyClientController(client, clientDatabase)

	tlvVerifyStepStartRequest := clientController.InitialKeyVerifyRequest()
	// 1) C -> S
	tlvVerifyStepStartResponse, err := HandleReaderForHandler(tlvVerifyStepStartRequest, controller)

	if err != nil {
		t.Fatal(err)
	}

	// 2) S -> C
	tlvFinishRequest, err := HandleReaderForHandler(tlvVerifyStepStartResponse, clientController)

	if err != nil {
		t.Fatal(err)
	}

	// 3) C -> S
	tlvFinishRespond, err := HandleReaderForHandler(tlvFinishRequest, controller)

	if err != nil {
		t.Fatal(err)
	}

	// 4) S -> C
	response, err := HandleReaderForHandler(tlvFinishRespond, clientController)

	if err != nil {
		t.Fatal(err)
	}
	if response != nil {
		t.Fatal(response)
	}
}
示例#3
0
// Tests the pairing setup
func TestPairingIntegration(t *testing.T) {
	storage, err := util.NewTempFileStorage()

	if err != nil {
		t.Fatal(err)
	}

	database := db.NewDatabaseWithStorage(storage)
	bridge, err := hap.NewSecuredDevice("Macbook Bridge", "001-02-003", database)

	if err != nil {
		t.Fatal(err)
	}

	controller, err := NewSetupServerController(bridge, database)

	if err != nil {
		t.Fatal(err)
	}

	clientDatabase, _ := db.NewTempDatabase()
	client, _ := hap.NewDevice("Client", clientDatabase)
	clientController := NewSetupClientController("001-02-003", client, clientDatabase)
	pairStartRequest := clientController.InitialPairingRequest()

	// 1) C -> S
	pairStartResponse, err := HandleReaderForHandler(pairStartRequest, controller)

	if err != nil {
		t.Fatal(err)
	}

	// 2) S -> C
	pairVerifyRequest, err := HandleReaderForHandler(pairStartResponse, clientController)

	if err != nil {
		t.Fatal(err)
	}

	// 3) C -> S
	pairVerifyResponse, err := HandleReaderForHandler(pairVerifyRequest, controller)

	if err != nil {
		t.Fatal(err)
	}

	// 4) S -> C
	pairKeyRequest, err := HandleReaderForHandler(pairVerifyResponse, clientController)

	if err != nil {
		t.Fatal(err)
	}

	// 5) C -> S
	pairKeyRespond, err := HandleReaderForHandler(pairKeyRequest, controller)

	if err != nil {
		t.Fatal(err)
	}

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

	if request != nil {
		t.Fatal(request)
	}
}
示例#4
0
文件: client.go 项目: brutella/hklifx
func main() {
	database, _ := db.NewDatabase("./data")
	c, _ := hap.NewDevice("Golang Client", database)
	client := pair.NewSetupClientController("336-02-620", c, database)
	pairStartRequest := client.InitialPairingRequest()

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

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

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

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

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

	// 6) S -> C
	request, err := pair.HandleReaderForHandler(pairKeyRespond, client)
	if err != nil {
		log.Info.Panic(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.Info.Panic(err)
	}

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

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

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

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

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