1f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher//===- llvm/unittest/Support/MD5Test.cpp - MD5 tests ----------------------===//
2f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher//
3f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher//                     The LLVM Compiler Infrastructure
4f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher//
5f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher// This file is distributed under the University of Illinois Open Source
6f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher// License. See LICENSE.TXT for details.
7f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher//
8f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher//===----------------------------------------------------------------------===//
9f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher//
10f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher// This file implements unit tests for the MD5 functions.
11f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher//
12f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher//===----------------------------------------------------------------------===//
13f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher
14f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher#include "llvm/ADT/ArrayRef.h"
15f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher#include "llvm/ADT/SmallString.h"
16f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher#include "llvm/Support/MD5.h"
17f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher#include "gtest/gtest.h"
18f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher
19f7306f224e7f85c2690256636613422c4a7b8230Eric Christopherusing namespace llvm;
20f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher
21f7306f224e7f85c2690256636613422c4a7b8230Eric Christophernamespace {
2212378d4872c7b522a9f6ea4ac362780bae97ce91Eric Christopher/// \brief Tests an arbitrary set of bytes passed as \p Input.
23cbb45aa123c42dabb38f14bc1879848d77896998Eric Christophervoid TestMD5Sum(ArrayRef<uint8_t> Input, StringRef Final) {
24f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher  MD5 Hash;
25f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher  Hash.update(Input);
26f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher  MD5::MD5Result MD5Res;
27f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher  Hash.final(MD5Res);
28f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher  SmallString<32> Res;
29f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher  MD5::stringifyResult(MD5Res, Res);
30f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher  EXPECT_EQ(Res, Final);
31f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher}
32f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher
33769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christophervoid TestMD5Sum(StringRef Input, StringRef Final) {
34769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher  MD5 Hash;
35769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher  Hash.update(Input);
36769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher  MD5::MD5Result MD5Res;
37769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher  Hash.final(MD5Res);
38769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher  SmallString<32> Res;
39769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher  MD5::stringifyResult(MD5Res, Res);
40769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher  EXPECT_EQ(Res, Final);
41769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher}
42769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher
43f7306f224e7f85c2690256636613422c4a7b8230Eric ChristopherTEST(MD5Test, MD5) {
44cbb45aa123c42dabb38f14bc1879848d77896998Eric Christopher  TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"", (size_t) 0),
45f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher             "d41d8cd98f00b204e9800998ecf8427e");
46cbb45aa123c42dabb38f14bc1879848d77896998Eric Christopher  TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"a", (size_t) 1),
47f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher             "0cc175b9c0f1b6a831c399e269772661");
48769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher  TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"abcdefghijklmnopqrstuvwxyz",
49769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher                               (size_t) 26),
50f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher             "c3fcd3d76192e4007dfb496cca67e13b");
51cbb45aa123c42dabb38f14bc1879848d77896998Eric Christopher  TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"\0", (size_t) 1),
5212378d4872c7b522a9f6ea4ac362780bae97ce91Eric Christopher             "93b885adfe0da089cdf634904fd59f71");
53cbb45aa123c42dabb38f14bc1879848d77896998Eric Christopher  TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"a\0", (size_t) 2),
5412378d4872c7b522a9f6ea4ac362780bae97ce91Eric Christopher             "4144e195f46de78a3623da7364d04f11");
55769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher  TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"abcdefghijklmnopqrstuvwxyz\0",
56769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher                               (size_t) 27),
5712378d4872c7b522a9f6ea4ac362780bae97ce91Eric Christopher             "81948d1f1554f58cd1a56ebb01f808cb");
58769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher  TestMD5Sum("abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b");
59f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher}
60f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher}
61