Eric Christopher | f7306f2 | 2013-05-24 23:08:17 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/MD5Test.cpp - MD5 tests ----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements unit tests for the MD5 functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/ADT/ArrayRef.h" |
| 15 | #include "llvm/ADT/SmallString.h" |
| 16 | #include "llvm/Support/MD5.h" |
| 17 | #include "gtest/gtest.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
Eric Christopher | 12378d4 | 2013-05-30 00:43:26 +0000 | [diff] [blame] | 22 | /// \brief Tests an arbitrary set of bytes passed as \p Input. |
Eric Christopher | cbb45aa | 2013-05-31 22:34:52 +0000 | [diff] [blame] | 23 | void TestMD5Sum(ArrayRef<uint8_t> Input, StringRef Final) { |
Eric Christopher | f7306f2 | 2013-05-24 23:08:17 +0000 | [diff] [blame] | 24 | MD5 Hash; |
| 25 | Hash.update(Input); |
| 26 | MD5::MD5Result MD5Res; |
| 27 | Hash.final(MD5Res); |
| 28 | SmallString<32> Res; |
| 29 | MD5::stringifyResult(MD5Res, Res); |
| 30 | EXPECT_EQ(Res, Final); |
| 31 | } |
| 32 | |
Eric Christopher | 769d24a | 2013-05-31 22:34:56 +0000 | [diff] [blame] | 33 | void TestMD5Sum(StringRef Input, StringRef Final) { |
| 34 | MD5 Hash; |
| 35 | Hash.update(Input); |
| 36 | MD5::MD5Result MD5Res; |
| 37 | Hash.final(MD5Res); |
| 38 | SmallString<32> Res; |
| 39 | MD5::stringifyResult(MD5Res, Res); |
| 40 | EXPECT_EQ(Res, Final); |
| 41 | } |
| 42 | |
Eric Christopher | f7306f2 | 2013-05-24 23:08:17 +0000 | [diff] [blame] | 43 | TEST(MD5Test, MD5) { |
Eric Christopher | cbb45aa | 2013-05-31 22:34:52 +0000 | [diff] [blame] | 44 | TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"", (size_t) 0), |
Eric Christopher | f7306f2 | 2013-05-24 23:08:17 +0000 | [diff] [blame] | 45 | "d41d8cd98f00b204e9800998ecf8427e"); |
Eric Christopher | cbb45aa | 2013-05-31 22:34:52 +0000 | [diff] [blame] | 46 | TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"a", (size_t) 1), |
Eric Christopher | f7306f2 | 2013-05-24 23:08:17 +0000 | [diff] [blame] | 47 | "0cc175b9c0f1b6a831c399e269772661"); |
Eric Christopher | 769d24a | 2013-05-31 22:34:56 +0000 | [diff] [blame] | 48 | TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"abcdefghijklmnopqrstuvwxyz", |
| 49 | (size_t) 26), |
Eric Christopher | f7306f2 | 2013-05-24 23:08:17 +0000 | [diff] [blame] | 50 | "c3fcd3d76192e4007dfb496cca67e13b"); |
Eric Christopher | cbb45aa | 2013-05-31 22:34:52 +0000 | [diff] [blame] | 51 | TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"\0", (size_t) 1), |
Eric Christopher | 12378d4 | 2013-05-30 00:43:26 +0000 | [diff] [blame] | 52 | "93b885adfe0da089cdf634904fd59f71"); |
Eric Christopher | cbb45aa | 2013-05-31 22:34:52 +0000 | [diff] [blame] | 53 | TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"a\0", (size_t) 2), |
Eric Christopher | 12378d4 | 2013-05-30 00:43:26 +0000 | [diff] [blame] | 54 | "4144e195f46de78a3623da7364d04f11"); |
Eric Christopher | 769d24a | 2013-05-31 22:34:56 +0000 | [diff] [blame] | 55 | TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"abcdefghijklmnopqrstuvwxyz\0", |
| 56 | (size_t) 27), |
Eric Christopher | 12378d4 | 2013-05-30 00:43:26 +0000 | [diff] [blame] | 57 | "81948d1f1554f58cd1a56ebb01f808cb"); |
Eric Christopher | 769d24a | 2013-05-31 22:34:56 +0000 | [diff] [blame] | 58 | TestMD5Sum("abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b"); |
Eric Christopher | f7306f2 | 2013-05-24 23:08:17 +0000 | [diff] [blame] | 59 | } |
| 60 | } |