Bug 1325185 - Fix operator precedence in GMPUtils' ToHexString(). r=gerald draft
authorChris Pearce <cpearce@mozilla.com>
Thu, 22 Dec 2016 09:20:17 +1300
changeset 452642 b42a325a00f5939ee3c4945c1a5cee826f89c385
parent 452404 10a5e2fc24e4e24fbe115fb190bcc28df4627ee2
child 540256 59ed1f0476afdf563ba920e1b5ddbbdee1510a47
push id39435
push usercpearce@mozilla.com
push dateWed, 21 Dec 2016 22:35:10 +0000
reviewersgerald
bugs1325185
milestone53.0a1
Bug 1325185 - Fix operator precedence in GMPUtils' ToHexString(). r=gerald MozReview-Commit-ID: 41KoItfOYts
dom/media/gmp/GMPUtils.cpp
dom/media/gtest/TestGMPUtils.cpp
--- a/dom/media/gmp/GMPUtils.cpp
+++ b/dom/media/gmp/GMPUtils.cpp
@@ -59,17 +59,17 @@ ToHexString(const uint8_t * aBytes, uint
     '0', '1', '2', '3',
     '4', '5', '6', '7',
     '8', '9', 'a', 'b',
     'c', 'd', 'e', 'f'
   };
   nsCString str;
   for (uint32_t i = 0; i < aLength; i++) {
     char buf[3];
-    buf[0] = hex[aBytes[i] & 0xf0 >> 4];
+    buf[0] = hex[(aBytes[i] & 0xf0) >> 4];
     buf[1] = hex[aBytes[i] & 0x0f];
     buf[2] = 0;
     str.AppendASCII(buf);
   }
   return str;
 }
 
 nsCString
--- a/dom/media/gtest/TestGMPUtils.cpp
+++ b/dom/media/gtest/TestGMPUtils.cpp
@@ -25,20 +25,20 @@ void TestSplitAt(const char* aInput,
   nsCString input(aInput);
   nsTArray<nsCString> tokens;
   SplitAt(aDelims, input, tokens);
   EXPECT_EQ(tokens.Length(), aNumExpectedTokens) << "Should get expected number of tokens";
   for (size_t i = 0; i < tokens.Length(); i++) {
     EXPECT_TRUE(tokens[i].EqualsASCII(aExpectedTokens[i]))
       << "Tokenize fail; expected=" << aExpectedTokens[i] << " got=" <<
       tokens[i].BeginReading();
-  }    
+  }
 }
 
-TEST(GeckoMediaPlugins, GMPUtils) {
+TEST(GeckoMediaPlugins, TestSplitAt) {
   {
     const char* input = "1,2,3,4";
     const char* delims = ",";
     const char* tokens[] = { "1", "2", "3", "4" };
     TestSplitAt(input, delims, MOZ_ARRAY_LENGTH(tokens), tokens);
   }
 
   {
@@ -54,8 +54,40 @@ TEST(GeckoMediaPlugins, GMPUtils) {
       "line2\r" // Old MacOSX
       "line3\n" // Unix
       "line4";
     const char* delims = "\r\n";
     const char* tokens[] = { "line1", "line2", "line3", "line4" };
     TestSplitAt(input, delims, MOZ_ARRAY_LENGTH(tokens), tokens);
   }
 }
+
+TEST(GeckoMediaPlugins, ToHexString) {
+  struct Test {
+    nsTArray<uint8_t> bytes;
+    string hex;
+  };
+
+  static const Test tests[] = {
+    { {0x00, 0x00}, "0000" },
+    { {0xff, 0xff}, "ffff" },
+    { {0xff, 0x00}, "ff00" },
+    { {0x00, 0xff}, "00ff" },
+    { {0xf0, 0x10}, "f010" },
+    { {0x05, 0x50}, "0550" },
+    { {0xf}, "0f" },
+    { {0x10}, "10" },
+    { {}, "" },
+    {
+      {
+        0x00, 0x11, 0x22, 0x33,
+        0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb,
+        0xcc, 0xdd, 0xee, 0xff
+      },
+      "00112233445566778899aabbccddeeff"
+    },
+  };
+
+  for (const Test& test : tests) {
+    EXPECT_STREQ(test.hex.c_str(), ToHexString(test.bytes).get());
+  }
+}