blob: 08d85fec43c5b72fc1069d0f0858af60d415d899 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8/*
9 test_sha1.cpp - test program of
10
11 ============
12 SHA-1 in C++
13 ============
14
15 100% Public Domain.
16
17 Original C Code
18 -- Steve Reid <steve@edmweb.com>
19 Small changes to fit into bglibs
20 -- Bruce Guenter <bruce@untroubled.org>
21 Translation to simpler C++ Code
22 -- Volker Grabsch <vog@notjusthosting.com>
23*/
24
25#include <string>
26
27#include "gtest/gtest.h"
28#include "wpi/sha1.h"
29
30namespace wpi {
31
32/*
33 * The 3 test vectors from FIPS PUB 180-1
34 */
35
36TEST(SHA1Test, Standard1) {
37 SHA1 checksum;
38 checksum.Update("abc");
39 ASSERT_EQ(checksum.Final(), "a9993e364706816aba3e25717850c26c9cd0d89d");
40}
41
42TEST(SHA1Test, Standard2) {
43 SHA1 checksum;
44 checksum.Update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
45 ASSERT_EQ(checksum.Final(), "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
46}
47
48TEST(SHA1Test, Standard3) {
49 SHA1 checksum;
50 // A million repetitions of 'a'
51 for (int i = 0; i < 1000000 / 200; ++i) {
52 checksum.Update(
53 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
54 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
55 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
56 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
57 }
58 ASSERT_EQ(checksum.Final(), "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
59}
60
61/*
62 * Other tests
63 */
64
65TEST(SHA1Test, OtherNoString) {
66 SHA1 checksum;
67 ASSERT_EQ(checksum.Final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709");
68}
69
70TEST(SHA1Test, OtherEmptyString) {
71 SHA1 checksum;
72 checksum.Update("");
73 ASSERT_EQ(checksum.Final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709");
74}
75
76TEST(SHA1Test, OtherABCDE) {
77 SHA1 checksum;
78 checksum.Update("abcde");
79 ASSERT_EQ(checksum.Final(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334");
80}
81
82TEST(SHA1Test, Concurrent) {
83 // Two concurrent checksum calculations
84 SHA1 checksum1, checksum2;
85 checksum1.Update("abc");
86 ASSERT_EQ(checksum2.Final(),
87 "da39a3ee5e6b4b0d3255bfef95601890afd80709"); /* "" */
88 ASSERT_EQ(checksum1.Final(),
89 "a9993e364706816aba3e25717850c26c9cd0d89d"); /* "abc" */
90}
91
92} // namespace wpi