// 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") } }
func TestVersionUpdate(t *testing.T) { storage, err := util.NewTempFileStorage() if err != nil { t.Fatal(err) } storage.Set("configHash", []byte("AB")) storage.Set("version", []byte("1")) config.load(storage) config.updateConfigHash([]byte("ABC")) config.save(storage) if x := config.version; x != 2 { t.Fatal(x) } if x, _ := storage.Get("configHash"); reflect.DeepEqual(x, config.configHash) == false { t.Fatal(string(x)) } if x, _ := storage.Get("version"); reflect.DeepEqual(x, []byte("2")) == false { t.Fatal(string(x)) } }
// 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) } }
// 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) } }
// NewTempDatabase returns a temp database func NewTempDatabase() (Database, error) { storage, err := util.NewTempFileStorage() return NewDatabaseWithStorage(storage), err }