Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same string content but different lengths

I'm having a hard time trying to figure this out. I'm writing a Unit test that verifies that the MD5 that a site displays matches the actual MD5 of the file. I do this by simply grabbing what the page displays and then calculating my own MD5 of the file. I get the text on the page by using Selenium WebDriver.

As expected, the strings show up as the same...or it appears to be

When I try to test the two strings using Assert.AreEqual or Assert.IsTrue, it fails no matter how I try to compare them

I've tried the following ways:

Assert.AreEqual(md5, md5Text); //Fails
Assert.IsTrue(md5 == md5Text); //Fails
Assert.IsTrue(String.Equals(md5, md5Text)); //Fails
Assert.IsTrue(md5.Normalize() == md5Text.Normalize()); //Fails
Assert.AreEqul(md5.Normalize(), md5Text.Normalize()); //Fails

At first, I thought the strings were actual different, but looking at them in the debugger shows that both strings are exactly the same enter image description here

So I tried looking at their lengths, that's when I saw why

enter image description here

The strings are different lengths..so I tried to substring the md5 variable to match the size of the md5Text variable. My thinking here was maybe md5 had a bunch of 0 width characters. However doing this got rid of the last half of md5

SOO, this must mean their in different encodings correct? But wouldn't Normalize() fix that?

This is how the variable md5 is created

string md5;
using (var stream = file.Open()) //file is a custom class with an Open() method that returns a Stream
{
    using (var generator = MD5.Create())
    {
        md5 = BitConverter.ToString(generator.ComputeHash(stream)).Replace("-", "‌​").ToLower().Trim();
    }
}

and this is how the md5Text variable is created

//I'm using Selenium WebDrvier to grab the text from the page
var md5Element = row.FindElements(By.XPath("//span[@data-bind='text: MD5Hash']")).Where(e => e.Visible()).First();
var md5Text = md5Element.Text;

How can I make this test pass? as it should be passing (since they are the same)

UPDATE:

The comments suggested I turn the strings into a char[] and iterate over it. Here are the results of that (http://pastebin.com/DX335wU8) and the code I added to do it

        char[] md5Characters = md5.ToCharArray();
        char[] md5TextCharacters = md5Text.ToCharArray();

        //Use md5 length since it's bigger
        for (int i = 0; i < md5Characters.Length; i++)
        {
            System.Diagnostics.Debug.Write("md5: " + md5Characters[i]);

            if (i >= md5TextCharacters.Length)
            {
                System.Diagnostics.Debug.Write(" | Exhausted md5Text characters..");
            }
            else
            {
                System.Diagnostics.Debug.Write(" | md5Text: " + md5TextCharacters[i]);
            }
            System.Diagnostics.Debug.WriteLine("");
        }

One thing I found interesting is that the md5 char array has a bunch of random characters inside of it every 2 letters

enter image description here

like image 986
edkek Avatar asked Jul 20 '26 21:07

edkek


1 Answers

.Replace("-", "‌​")

Your "" is not empty, there is actually a "‌​ then unicode zero width non-joiner + zero width space then " so you are not replacing "-" with an empty string rather you are inserting additional characters.

Delete and retype "" or use String.Empty.

like image 52
Alex K. Avatar answered Jul 22 '26 12:07

Alex K.