예제 #1
0
        public void RegexValidator_ValidateRegex_ValidTest()
        {
            List <string> inputs = new List <string>()
            {
                @"",
                @".*",
                @"(^|;)key=([^=]+;)?value($|,|;)",
                @"(^|;)key=([^=]+;)?(value1|value2|value3)($|,|;)",
                @"^key=\x01\x02\xAB\xab\u1234\u12aB[0-9a-zA-Z]+[123]*(a|b){4,}$",
                @"^key=value\.*\*+\-?\+{1,2}\[\]{6}\\{3,10000}\?\^\|\(\)\{\}$",
                @"(^|;)key=([^=]+;)?(中文|Français|Deutsch|日本語|Русский язык|Español|عربي|Esperanto|한국어|BIG5)($|,|;)",
                @"key=\w\W+\s*\S+\d{4}\D{4,5}",
                @"(^|;)key=abc{34}\?($|,|;)",
            };

            foreach (string input in inputs)
            {
                Assert.True(RegexTypeValidator.ValidateRegex(input));
            }
        }
예제 #2
0
        public void RegexValidator_ValidateRegex_InvalidTest()
        {
            List <string> inputs = new List <string>()
            {
                @"\a",
                @"\b",
                @"\B",
                @"\",
                @"\t",
                @"\r",
                @"\n",
                @"\x",
                @"\f",
                @"\e",
                @"\A",
                @"\z",
                @"\Z",
                @"\G",
                @"\1",
                @"\2",
                @"\12",
                @"\123",
                @"\1234",
                @"\x1",
                @"\x1x",
                @"\xx1",
                @"\xxa",
                @"aa\xa",
                @"aewrgaer\u",
                @"\u1",
                @"asdfdsaf\u12",
                @"\u123",
                @"\ux115",
                @"\u1x15",
                @"\u11x5",
                @"\u111x",
                @"\X12",
                @"\U1234",
                @"\c",
                @"\c1",
                @"\cC",
                @"(^|;)key=([^=]+;)?va(?'alpha')lue($|,|;)",
                @"(^|;)key=([^=]+;)?(?<double>A)B<double>($|,|;)",
                @"(^|;)key=([^=]+;)?\k<double>A($|,|;)",
                @"(^|;)key=([^=]+;)?Write(?:Line)?($|,|;)",
                @"(^|;)key=([^=]+;)?A\d{2}(?i:\w+)c?($|,|;)",
                @"(^|;)key=([^=]+;)?\w+(?=\.)($|,|;)",
                @"\\\\\\\\\\\\\\\\\\\\\",
                @"(^|;)key=\d*?\.\d($|,|;)",
                @"(^|;)key=be+?($|,|;)",
                @"(^|;)key=rai??n($|,|;)",
                @"(^|;)key=\d{3}?($|,|;)",
                @"(^|;)key=\d{2,}?($|,|;)",
                @"(^|;)key=\d{3,5}?($|,|;)",
                @"(^|;)key=${name}($|,|;)",
                @"(^|;)key=(?# comment)($|,|;)",
                @"tooooooooooooolooooooooooooooooooooooooongtooooooooooooolooooooooooooooooooooooooongtooooooooooooolooooooooooooooooooooooooongtooooooooooooolooooooooooooooooooooooooongtooooooooooooolooooooooooooooooooooooooongtooooooooooooolooooooooooooooooooooooooongtooooooooooooolooooooooooooooooooooooooongtooooooooooooolooooooooooooooooooooooooongtooooooooooooolooooooooooooooooooooooooongtooooooooooooolooooooooooooooooooooooooong",
            };

            foreach (string input in inputs)
            {
                bool validLength = (Encoding.UTF8.GetByteCount(input) <= PsMessageBuilder.MaxProfileSearchExtraDataLengthBytes);
                if (validLength)
                {
                    Assert.False(RegexTypeValidator.ValidateRegex(input));
                }
            }
        }
        /// <summary>
        /// Checks whether the activity search request is valid.
        /// </summary>
        /// <param name="ActivitySearchRequest">Activity search request part of the client's request message.</param>
        /// <param name="MessageBuilder">Client's network message builder.</param>
        /// <param name="RequestMessage">Full request message from client.</param>
        /// <param name="ErrorResponse">If the function fails, this is filled with error response message that is ready to be sent to the client.</param>
        /// <returns>true if the search request is valid, false otherwise.</returns>
        public static bool ValidateActivitySearchRequest(ActivitySearchRequest ActivitySearchRequest, ProxMessageBuilder MessageBuilder, ProxProtocolMessage RequestMessage, out ProxProtocolMessage ErrorResponse)
        {
            log.Trace("()");

            bool res = false;

            ErrorResponse = null;
            string details = null;

            int responseResultLimit = ProxMessageProcessor.ActivitySearchMaxResponseRecords;
            int totalResultLimit    = ProxMessageProcessor.ActivitySearchMaxTotalRecords;

            bool maxResponseRecordCountValid = (1 <= ActivitySearchRequest.MaxResponseRecordCount) &&
                                               (ActivitySearchRequest.MaxResponseRecordCount <= responseResultLimit);

            if (!maxResponseRecordCountValid)
            {
                log.Debug("Invalid maxResponseRecordCount value '{0}'.", ActivitySearchRequest.MaxResponseRecordCount);
                details = "maxResponseRecordCount";
            }

            if (details == null)
            {
                bool maxTotalRecordCountValid = (1 <= ActivitySearchRequest.MaxTotalRecordCount) &&
                                                (ActivitySearchRequest.MaxTotalRecordCount <= totalResultLimit) &&
                                                (ActivitySearchRequest.MaxResponseRecordCount <= ActivitySearchRequest.MaxTotalRecordCount);
                if (!maxTotalRecordCountValid)
                {
                    log.Debug("Invalid maxTotalRecordCount value '{0}'.", ActivitySearchRequest.MaxTotalRecordCount);
                    details = "maxTotalRecordCount";
                }
            }

            if ((details == null) && (ActivitySearchRequest.OwnerNetworkId.Length > 0))
            {
                bool ownerNetworkIdValid = ActivitySearchRequest.OwnerNetworkId.Length == ProtocolHelper.NetworkIdentifierLength;
                if (!ownerNetworkIdValid)
                {
                    log.Debug("Invalid owner network ID length '{0}'.", ActivitySearchRequest.OwnerNetworkId.Length);
                    details = "ownerNetworkId";
                }
            }

            if ((details == null) && (ActivitySearchRequest.Type != null))
            {
                bool typeValid = Encoding.UTF8.GetByteCount(ActivitySearchRequest.Type) <= ProxMessageBuilder.MaxActivitySearchTypeLengthBytes;
                if (!typeValid)
                {
                    log.Debug("Invalid type value length '{0}'.", ActivitySearchRequest.Type.Length);
                    details = "type";
                }
            }

            if ((details == null) && ((ActivitySearchRequest.StartNotAfter != 0) || (ActivitySearchRequest.ExpirationNotBefore != 0)))
            {
                DateTime?startNotAfter       = null;
                DateTime?expirationNotBefore = null;

                if (ActivitySearchRequest.StartNotAfter != 0)
                {
                    startNotAfter = ProtocolHelper.UnixTimestampMsToDateTime(ActivitySearchRequest.StartNotAfter);
                    bool startNotAfterValid = startNotAfter != null;
                    if (!startNotAfterValid)
                    {
                        log.Debug("Start not after {0} is not a valid timestamp value.", ActivitySearchRequest.StartNotAfter);
                        details = "startNotAfter";
                    }
                }

                if ((details == null) && (ActivitySearchRequest.ExpirationNotBefore != 0))
                {
                    expirationNotBefore = ProtocolHelper.UnixTimestampMsToDateTime(ActivitySearchRequest.ExpirationNotBefore);
                    bool expirationNotBeforeValid = expirationNotBefore != null;
                    if (!expirationNotBeforeValid)
                    {
                        log.Debug("Expiration not before {0} is not a valid timestamp value.", ActivitySearchRequest.ExpirationNotBefore);
                        details = "expirationNotBefore";
                    }
                    else if (ActivitySearchRequest.StartNotAfter != 0)
                    {
                        expirationNotBeforeValid = ActivitySearchRequest.StartNotAfter <= ActivitySearchRequest.ExpirationNotBefore;
                        if (!expirationNotBeforeValid)
                        {
                            log.Debug("Expiration not before {0} is smaller than start not after {1}.", ActivitySearchRequest.StartNotAfter, ActivitySearchRequest.ExpirationNotBefore);
                            details = "expirationNotBefore";
                        }
                    }
                }
            }

            if ((details == null) && (ActivitySearchRequest.Latitude != GpsLocation.NoLocationLocationType))
            {
                GpsLocation locLat  = new GpsLocation(ActivitySearchRequest.Latitude, 0);
                GpsLocation locLong = new GpsLocation(0, ActivitySearchRequest.Longitude);
                if (!locLat.IsValid())
                {
                    log.Debug("Latitude '{0}' is not a valid GPS latitude value.", ActivitySearchRequest.Latitude);
                    details = "latitude";
                }
                else if (!locLong.IsValid())
                {
                    log.Debug("Longitude '{0}' is not a valid GPS longitude value.", ActivitySearchRequest.Longitude);
                    details = "longitude";
                }
            }

            if ((details == null) && (ActivitySearchRequest.Latitude != GpsLocation.NoLocationLocationType))
            {
                bool radiusValid = ActivitySearchRequest.Radius > 0;
                if (!radiusValid)
                {
                    log.Debug("Invalid radius value '{0}'.", ActivitySearchRequest.Radius);
                    details = "radius";
                }
            }

            if ((details == null) && (ActivitySearchRequest.ExtraData != null))
            {
                bool validLength    = (Encoding.UTF8.GetByteCount(ActivitySearchRequest.ExtraData) <= ProxMessageBuilder.MaxActivitySearchExtraDataLengthBytes);
                bool extraDataValid = RegexTypeValidator.ValidateRegex(ActivitySearchRequest.ExtraData);
                if (!validLength || !extraDataValid)
                {
                    log.Debug("Invalid extraData regular expression filter.");
                    details = "extraData";
                }
            }

            if (details == null)
            {
                res = true;
            }
            else
            {
                ErrorResponse = MessageBuilder.CreateErrorInvalidValueResponse(RequestMessage, details);
            }

            log.Trace("(-):{0}", res);
            return(res);
        }
예제 #4
0
        /// <summary>
        /// Checks whether the profile search request is valid.
        /// </summary>
        /// <param name="ProfileSearchRequest">Profile search request part of the client's request message.</param>
        /// <param name="MessageBuilder">Client's network message builder.</param>
        /// <param name="RequestMessage">Full request message from client.</param>
        /// <param name="ErrorResponse">If the function fails, this is filled with error response message that is ready to be sent to the client.</param>
        /// <returns>true if the profile search request is valid, false otherwise.</returns>
        public static bool ValidateProfileSearchRequest(ProfileSearchRequest ProfileSearchRequest, PsMessageBuilder MessageBuilder, PsProtocolMessage RequestMessage, out PsProtocolMessage ErrorResponse)
        {
            log.Trace("()");

            bool res = false;

            ErrorResponse = null;
            string details = null;

            if (ProfileSearchRequest == null)
            {
                ProfileSearchRequest = new ProfileSearchRequest();
            }

            bool includeImages       = ProfileSearchRequest.IncludeThumbnailImages;
            int  responseResultLimit = includeImages ? PsMessageProcessor.ProfileSearchMaxResponseRecordsWithImage : PsMessageProcessor.ProfileSearchMaxResponseRecordsWithoutImage;
            int  totalResultLimit    = includeImages ? PsMessageProcessor.ProfileSearchMaxTotalRecordsWithImage : PsMessageProcessor.ProfileSearchMaxTotalRecordsWithoutImage;

            bool maxResponseRecordCountValid = (1 <= ProfileSearchRequest.MaxResponseRecordCount) &&
                                               (ProfileSearchRequest.MaxResponseRecordCount <= responseResultLimit);

            if (!maxResponseRecordCountValid)
            {
                log.Debug("Invalid maxResponseRecordCount value '{0}'.", ProfileSearchRequest.MaxResponseRecordCount);
                details = "maxResponseRecordCount";
            }

            if (details == null)
            {
                bool maxTotalRecordCountValid = (1 <= ProfileSearchRequest.MaxTotalRecordCount) &&
                                                (ProfileSearchRequest.MaxTotalRecordCount <= totalResultLimit) &&
                                                (ProfileSearchRequest.MaxResponseRecordCount <= ProfileSearchRequest.MaxTotalRecordCount);

                if (!maxTotalRecordCountValid)
                {
                    log.Debug("Invalid maxTotalRecordCount value '{0}'.", ProfileSearchRequest.MaxTotalRecordCount);
                    details = "maxTotalRecordCount";
                }
            }

            if ((details == null) && (ProfileSearchRequest.Type != null))
            {
                bool typeValid = Encoding.UTF8.GetByteCount(ProfileSearchRequest.Type) <= PsMessageBuilder.MaxProfileSearchTypeLengthBytes;
                if (!typeValid)
                {
                    log.Debug("Invalid type value length '{0}'.", ProfileSearchRequest.Type.Length);
                    details = "type";
                }
            }

            if ((details == null) && (ProfileSearchRequest.Name != null))
            {
                bool nameValid = Encoding.UTF8.GetByteCount(ProfileSearchRequest.Name) <= PsMessageBuilder.MaxProfileSearchNameLengthBytes;
                if (!nameValid)
                {
                    log.Debug("Invalid name value length '{0}'.", ProfileSearchRequest.Name.Length);
                    details = "name";
                }
            }

            if ((details == null) && (ProfileSearchRequest.Latitude != GpsLocation.NoLocationLocationType))
            {
                GpsLocation locLat  = new GpsLocation(ProfileSearchRequest.Latitude, 0);
                GpsLocation locLong = new GpsLocation(0, ProfileSearchRequest.Longitude);
                if (!locLat.IsValid())
                {
                    log.Debug("Latitude '{0}' is not a valid GPS latitude value.", ProfileSearchRequest.Latitude);
                    details = "latitude";
                }
                else if (!locLong.IsValid())
                {
                    log.Debug("Longitude '{0}' is not a valid GPS longitude value.", ProfileSearchRequest.Longitude);
                    details = "longitude";
                }
            }

            if ((details == null) && (ProfileSearchRequest.Latitude != GpsLocation.NoLocationLocationType))
            {
                bool radiusValid = ProfileSearchRequest.Radius > 0;
                if (!radiusValid)
                {
                    log.Debug("Invalid radius value '{0}'.", ProfileSearchRequest.Radius);
                    details = "radius";
                }
            }

            if ((details == null) && (ProfileSearchRequest.ExtraData != null))
            {
                bool validLength    = (Encoding.UTF8.GetByteCount(ProfileSearchRequest.ExtraData) <= PsMessageBuilder.MaxProfileSearchExtraDataLengthBytes);
                bool extraDataValid = RegexTypeValidator.ValidateRegex(ProfileSearchRequest.ExtraData);
                if (!validLength || !extraDataValid)
                {
                    log.Debug("Invalid extraData regular expression filter.");
                    details = "extraData";
                }
            }

            if (details == null)
            {
                res = true;
            }
            else
            {
                ErrorResponse = MessageBuilder.CreateErrorInvalidValueResponse(RequestMessage, details);
            }

            log.Trace("(-):{0}", res);
            return(res);
        }