Ejemplo n.º 1
0
        /// <summary>
        /// Describe the Comparison test defined by the module developer.
        /// </summary>
        /// <returns>Returns whether the status is successful or not.</returns>
        public bool Run()
        {
            this.logger.LogInfo("Testing started");
            if (!File.Exists(@path))
            {
                this.logger.LogError("File path =" + path + " does not exist");
                this.logger.LogError("Test failed");
                return(false);
            }

            string[] lines      = File.ReadAllLines(path: @path);
            int      lineNumber = 0;
            int      numOfKeys  = int.Parse(lines[lineNumber], CultureInfo.CurrentCulture);

            lineNumber += 1;

            Dictionary <string, string> testDictionary = new Dictionary <string, string>();

            //// adding all string to string tags
            while (numOfKeys > 0)
            {
                numOfKeys -= 1;
                string[] keyValue = lines[lineNumber].Split(':');
                lineNumber += 1;
                testDictionary.Add(keyValue[0], keyValue[1]);
            }

            lineNumber += 1;
            this.logger.LogInfo("Test file read successfully");
            MessageSchema messageSchema = new MessageSchema();

            this.logger.LogInfo("Encoding data started");

            // encoding data with help of imageSchema Encode function
            string encodedata = messageSchema.Encode(testDictionary);

            this.logger.LogInfo("Data encoding completed");

            // decoding data with help of imageSchema Encode function
            this.logger.LogInfo("Decoding data started");
            Dictionary <string, string> decodedDictionary = new Dictionary <string, string>(messageSchema.Decode(encodedata, false));

            // checking all the key value pair are same or not for full decoding
            this.logger.LogInfo("Decoding data ended");
            this.logger.LogInfo("checking if encoding decoding is correct");
            foreach (string key in testDictionary.Keys)
            {
                if (!(decodedDictionary.ContainsKey(key) & decodedDictionary[key] == testDictionary[key]))
                {
                    this.logger.LogError("Test failed");
                    return(false);
                }
            }

            this.logger.LogSuccess("Test successful");
            return(true);
        }
        /// <summary>
        /// Tests encoding and decoding functionality by adding noise
        /// </summary>
        /// <returns>Returns whether the status is successful or not.</returns>
        public bool Run()
        {
            this.logger.LogInfo("Testing started");
            if (!File.Exists(@path))
            {
                this.logger.LogError("File path =" + path + " does not exist");
                this.logger.LogError("Test failed");
                return(false);
            }

            string[] lines      = File.ReadAllLines(path: @path);
            int      lineNumber = 0;
            int      numOfKeys  = int.Parse(lines[lineNumber], CultureInfo.CurrentCulture);

            lineNumber += 1;

            Dictionary <string, string> testDictionary = new Dictionary <string, string>();

            // adding all string to string tags
            while (numOfKeys > 0)
            {
                numOfKeys -= 1;
                string[] keyValue = lines[lineNumber].Split(':');
                lineNumber += 1;
                testDictionary.Add(keyValue[0], keyValue[1]);
            }

            this.logger.LogInfo("Test file read successfully");
            MessageSchema messageSchema = new MessageSchema();

            this.logger.LogInfo("Encoding data started");

            // encoding data with help of imageSchema Encode function
            string encodedata = messageSchema.Encode(testDictionary);

            this.logger.LogInfo("Data encoding completed");

            // decoding data with help of imageSchema Encode function
            this.logger.LogInfo("Inserting some random string into encoded data");
            Random rnd         = new Random();
            int    randomIndex = rnd.Next(1, encodedata.Length);

            encodedata = encodedata.Substring(0, randomIndex) + "noise" + encodedata.Substring(randomIndex);
            this.logger.LogInfo("Encoding data started");

            // Successful operation if decoding throws error
            try
            {
                messageSchema.Decode(encodedata, false);
            }
            catch
            {
                this.logger.LogInfo("Corrupted data found, cannot decode.");
                this.logger.LogSuccess("Test successful");
                return(true);
            }

            // Unsuccessful operation if decoding throws no error
            this.logger.LogError("Test unsuccessful");
            return(false);
        }