示例#1
0
        public async Task GeneratePairings_RequiresPositiveId()
        {
            var service    = new Mock <IPairingService>(MockBehavior.Strict);
            var controller = new PairingController(service.Object, Mapper.Instance);

            var result = await controller.GeneratePairings(-1);

            Assert.IsTrue(result is BadRequestObjectResult);
        }
示例#2
0
        public async Task GeneratePairings_ReturnResult()
        {
            var pairings = CreatePairings();

            var service = new Mock <IPairingService>();

            service.Setup(x => x.GeneratePairings(It.IsAny <int>()))
            .ReturnsAsync(pairings)
            .Verifiable();

            var controller = new PairingController(service.Object, Mapper.Instance);
            var result     = await controller.GeneratePairings(1) as CreatedResult;

            var returnPairings = result?.Value as List <PairingViewModel>;

            Assert.IsNotNull(returnPairings);

            Assert.AreEqual(1, returnPairings[0].Id);
            Assert.AreEqual(1, returnPairings[0].SantaId);
            Assert.AreEqual(2, returnPairings[0].RecipientId);

            Assert.AreEqual(2, returnPairings[1].Id);
            Assert.AreEqual(2, returnPairings[1].SantaId);
            Assert.AreEqual(3, returnPairings[1].RecipientId);

            Assert.AreEqual(3, returnPairings[2].Id);
            Assert.AreEqual(3, returnPairings[2].SantaId);
            Assert.AreEqual(4, returnPairings[2].RecipientId);

            Assert.AreEqual(4, returnPairings[3].Id);
            Assert.AreEqual(4, returnPairings[3].SantaId);
            Assert.AreEqual(5, returnPairings[3].RecipientId);

            Assert.AreEqual(5, returnPairings[4].Id);
            Assert.AreEqual(5, returnPairings[4].SantaId);
            Assert.AreEqual(1, returnPairings[4].RecipientId);

            service.VerifyAll();
        }
示例#3
0
        public Tuple <string, byte[]> Invoke(string connectionId, string url, string method, byte[] inputData,
                                             ConnectionSession session)
        {
            if (!_sessions.ContainsKey(connectionId))
            {
                _sessions.TryAdd(connectionId, new HapSession());
            }


            _logger.LogDebug($"Working on request {url}");
            var queryString = new NameValueCollection();

            if (url.Contains("?"))
            {
                var parts = url.Split('?');

                queryString = HttpUtility.ParseQueryString(parts[1]);
                url         = parts[0];
            }

            try
            {
                if (inputData.Length > 0)
                {
                    var parts = TlvParser.Parse(inputData);

                    if (method == "POST")
                    {
                        var state = parts.GetTypeAsInt(Constants.State);

                        if (url.EndsWith("pair-setup"))
                        {
                            _logger.LogDebug($"Working on pair-setup request");
                            if (_pairController != null && state == 1)
                            {
                                return(ReturnError(state, ErrorCodes.Busy));
                            }

                            if (state == 1 && _pairController == null)
                            {
                                _pairController =
                                    new PairSetupController(_logger, _pairCode);
                            }

                            if (_pairController == null)
                            {
                                _logger.LogError($"Something is wrong here, closing tcp connection");
                                throw new ArgumentException($"Something is wrong here, closing tcp connection");
                            }

                            var data = _pairController.Post(parts, session);

                            var raw = TlvParser.Serialize(data.TlvData);

                            if (!data.Ok)
                            {
                                _pairController = null;
                                _sessions.TryRemove(connectionId, out var _);
                            }
                            else if (data.State == 5)
                            {
                                PairingCompleted?.Invoke(this, new PairSetupCompleteEventArgs(data.Ltsk, data.Ltpk));
                                _pairController = null;
                            }

                            return(new Tuple <string, byte[]>(PairSetupReturn.ContentType, raw));
                        }

                        if (url.EndsWith("pair-verify"))
                        {
                            _logger.LogDebug($"Working on pair-verify request");
                            if (string.IsNullOrEmpty(HapControllerServer.HapControllerLtsk))
                            {
                                return(ReturnError(state, ErrorCodes.Busy));
                            }

                            var verify = new PairVerifyController(_logger);
                            var data   = verify.Post(parts, _sessions[connectionId]);

                            if (data.HapSession != null)
                            {
                                _sessions[connectionId] = data.HapSession;
                            }

                            var raw = TlvParser.Serialize(data.TlvData);
                            return(new Tuple <string, byte[]>(data.ContentType, raw));
                        }

                        if (url.EndsWith("pairings"))
                        {
                            _logger.LogDebug($"Working on pairings");
                            var pair = new PairingController(_logger);
                            var data = pair.Post(parts);

                            var raw = TlvParser.Serialize(data.TlvData);
                            return(new Tuple <string, byte[]>(data.ContentType, raw));
                        }

                        if (url.EndsWith("identify"))
                        {
                            var identify = new IdentifyController();
                            var data     = identify.Post(inputData);

                            return(new Tuple <string, byte[]>(data.ContentType, new byte[0]));
                        }
                    }
                    else if (method == "PUT")
                    {
                        if (url.EndsWith("characteristics"))
                        {
                            _logger.LogDebug($"Working on characteristics");
                            var c    = new CharacteristicsController(_logger);
                            var data = c.Put(inputData, _sessions[connectionId], _homeKitServer);

                            // response with no data if the call was successful - errors need to be implemented
                            return(new Tuple <string, byte[]>(data.ContentType, new byte[0]));
                        }
                    }
                }
                else
                {
                    if (method == "GET")
                    {
                        if (url.EndsWith("accessories"))
                        {
                            _logger.LogDebug($"Working on accessories");

                            var accessoryController = new AccesoriesController(_logger);
                            var accessories         = accessoryController.Get(_homeKitServer, queryString);
                            var strValue            = JsonConvert.SerializeObject(accessories, JsonSettings);

                            _logger.LogDebug($"Sending {strValue}");

                            return(new Tuple <string, byte[]>("application/hap+json", Encoding.UTF8.GetBytes(strValue)));
                        }

                        if (url.EndsWith("characteristics"))
                        {
                            _logger.LogDebug($"Working on characteristics");

                            var ids      = queryString["id"].Split(",");
                            var c        = new CharacteristicsController(_logger);
                            var data     = c.Get(ids, _homeKitServer);
                            var strValue = JsonConvert.SerializeObject(data, JsonSettings);

                            _logger.LogDebug($"Sending {strValue}");

                            return(new Tuple <string, byte[]>(data.ContentType, Encoding.UTF8.GetBytes(strValue)));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"{e}: Error occured while processing request");
                _pairController = null;
            }

            throw new InvalidOperationException();
        }
示例#4
0
        public Tuple <string, byte[]> Invoke(string connectionId, string url, string method, byte[] inputData)
        {
            if (!_sessions.ContainsKey(connectionId))
            {
                _sessions.Add(connectionId, new HapSession());
            }

            var queryString = new NameValueCollection();

            if (url.Contains("?"))
            {
                var parts = url.Split('?');

                queryString = HttpUtility.ParseQueryString(parts[1]);
                url         = parts[0];
            }

            try
            {
                if (inputData.Length > 0)
                {
                    var parts = TlvParser.Parse(inputData);

                    if (method == "POST")
                    {
                        var state = parts.GetTypeAsInt(Constants.State);

                        if (url.EndsWith("pair-setup"))
                        {
                            if (_pairController != null && state == 1)
                            {
                                return(ReturnError(state, ErrorCodes.Busy));
                            }

                            if (state == 1 && _pairController == null)
                            {
                                _pairController =
                                    new PairSetupController(_logger, _pairCode); //Todo: change code to param
                            }

                            var data = _pairController.Post(parts);

                            var raw = TlvParser.Serialise(data.TlvData);

                            if (!data.Ok)
                            {
                                _pairController = null;
                                _sessions.Remove(connectionId);
                            }
                            else if (data.State == 5)
                            {
                                PairingCompleted?.Invoke(this, new PairSetupCompleteEventArgs(data.Ltsk, data.Ltpk));
                                _pairController = null;
                            }

                            return(new Tuple <string, byte[]>(data.ContentType, raw));
                        }

                        if (url.EndsWith("pair-verify"))
                        {
                            if (string.IsNullOrEmpty(HapControllerServer.HapControllerLtsk))
                            {
                                return(ReturnError(state, ErrorCodes.Busy));
                            }

                            var verify = new PairVerifyController(_logger);
                            var data   = verify.Post(parts, _sessions[connectionId]);

                            if (data.HapSession != null)
                            {
                                _sessions[connectionId] = data.HapSession;
                            }

                            var raw = TlvParser.Serialise(data.TlvData);
                            return(new Tuple <string, byte[]>(data.ContentType, raw));
                        }

                        if (url.EndsWith("pairings"))
                        {
                            var pair = new PairingController(_logger);
                            var data = pair.Post(parts);

                            var raw = TlvParser.Serialise(data.TlvData);
                            return(new Tuple <string, byte[]>(data.ContentType, raw));
                        }

                        if (url.EndsWith("identify"))
                        {
                            var identify = new IdentifyController();
                            var data     = identify.Post(inputData);

                            return(new Tuple <string, byte[]>(data.ContentType, new byte[0]));
                        }
                    }
                    else if (method == "PUT")
                    {
                        if (url.EndsWith("characteristics"))
                        {
                            var c    = new CharacteristicsController(_logger);
                            var data = c.Put(inputData, _sessions[connectionId], _homeKitServer);

                            if (data.Characteristics.Count == 0)
                            {
                                return(new Tuple <string, byte[]>(data.ContentType, new byte[0]));
                            }
                            return(new Tuple <string, byte[]>(data.ContentType, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data))));
                        }
                    }
                }
                else
                {
                    if (method == "GET")
                    {
                        if (url.EndsWith("accessories"))
                        {
                            var accessoryController = new AccesoriesController();
                            var accessories         = accessoryController.Get(_homeKitServer, queryString);
                            var strValue            = JsonConvert.SerializeObject(accessories, JsonSettings);

                            return(new Tuple <string, byte[]>("application/hap+json", Encoding.UTF8.GetBytes(strValue)));
                        }

                        if (url.EndsWith("characteristics"))
                        {
                            var ids  = queryString["id"].Split(",");
                            var c    = new CharacteristicsController(_logger);
                            var data = c.Get(ids, _homeKitServer);

                            return(new Tuple <string, byte[]>(data.ContentType, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data, JsonSettings))));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error occured while processing request");
                _pairController = null;
            }

            throw new InvalidOperationException();
        }