]> git.proxmox.com Git - ovs.git/blame - tests/test-sha1.c
raft: Fix the problem of stuck in candidate role forever.
[ovs.git] / tests / test-sha1.c
CommitLineData
d138cd2c 1/*
e9c04800 2 * Copyright (c) 2009, 2011, 2012 Nicira, Inc.
d138cd2c 3 *
a14bc59f
BP
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
d138cd2c 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
d138cd2c
BP
15 */
16
17#include <config.h>
3f636c7e 18#undef NDEBUG
d138cd2c 19#include "sha1.h"
3f636c7e 20#include <assert.h>
d138cd2c
BP
21#include <inttypes.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
3f636c7e 25#include "ovstest.h"
d138cd2c
BP
26#include "random.h"
27#include "util.h"
d138cd2c
BP
28
29struct test_vector {
30 char *data;
31 size_t size;
32 const uint8_t output[20];
33};
34
35static const struct test_vector vectors[] = {
36 /* FIPS 180-1. */
37 {
38 "abc", 3,
39 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
40 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D }
41 }, {
42 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
43 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
44 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
45 },
46
47 /* RFC 3174. */
48 {
49 "0123456701234567012345670123456701234567012345670123456701234567"
50 "0123456701234567012345670123456701234567012345670123456701234567"
51 "0123456701234567012345670123456701234567012345670123456701234567"
52 "0123456701234567012345670123456701234567012345670123456701234567"
53 "0123456701234567012345670123456701234567012345670123456701234567"
54 "0123456701234567012345670123456701234567012345670123456701234567"
55 "0123456701234567012345670123456701234567012345670123456701234567"
56 "0123456701234567012345670123456701234567012345670123456701234567"
57 "0123456701234567012345670123456701234567012345670123456701234567"
58 "0123456701234567012345670123456701234567012345670123456701234567",
59 64 * 10,
60 { 0xDE, 0xA3, 0x56, 0xA2, 0xCD, 0xDD, 0x90, 0xC7, 0xA7, 0xEC,
61 0xED, 0xC5, 0xEB, 0xB5, 0x63, 0x93, 0x4F, 0x46, 0x04, 0x52 },
62 },
63
64 /* http://www.febooti.com/products/filetweak/members/hash-and-crc/test-vectors/ */
65 {
66 "", 0,
67 { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55,
68 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 }
69 }, {
70 "Test vector from febooti.com", 28,
71 { 0xa7, 0x63, 0x17, 0x95, 0xf6, 0xd5, 0x9c, 0xd6, 0xd1, 0x4e,
72 0xbd, 0x00, 0x58, 0xa6, 0x39, 0x4a, 0x4b, 0x93, 0xd8, 0x68 }
73 },
74
75 /* http://en.wikipedia.org/wiki/SHA_hash_functions */
76 {
77 "The quick brown fox jumps over the lazy dog", 43,
78 { 0x2f, 0xd4, 0xe1, 0xc6, 0x7a, 0x2d, 0x28, 0xfc, 0xed, 0x84,
79 0x9e, 0xe1, 0xbb, 0x76, 0xe7, 0x39, 0x1b, 0x93, 0xeb, 0x12 },
80 }, {
81 "The quick brown fox jumps over the lazy cog", 43,
82 { 0xde, 0x9f, 0x2c, 0x7f, 0xd2, 0x5e, 0x1b, 0x3a, 0xfa, 0xd3,
83 0xe8, 0x5a, 0x0b, 0xd1, 0x7d, 0x9b, 0x10, 0x0d, 0xb4, 0xb3 },
84 },
85
86 /* http://www.hashcash.org/docs/sha1-hashcash.html */
87 {
88 "0:030626:adam@cypherspace.org:6470e06d773e05a8", 46,
89 { 0x00, 0x00, 0x00, 0x00, 0xc7, 0x0d, 0xb7, 0x38, 0x9f, 0x24,
90 0x1b, 0x8f, 0x44, 0x1f, 0xcf, 0x06, 0x8a, 0xea, 0xd3, 0xf0 },
91 },
92};
93
94static void
95test_one(const struct test_vector *vec)
96{
5eccf359 97 uint8_t md[SHA1_DIGEST_SIZE];
d138cd2c
BP
98 int i;
99
100 /* All at once. */
5eccf359
BP
101 sha1_bytes(vec->data, vec->size, md);
102 assert(!memcmp(md, vec->output, SHA1_DIGEST_SIZE));
d138cd2c
BP
103
104 /* In two pieces. */
105 for (i = 0; i < 20; i++) {
106 int n0 = vec->size ? random_range(vec->size) : 0;
107 int n1 = vec->size - n0;
5eccf359
BP
108 struct sha1_ctx sha1;
109
110 sha1_init(&sha1);
e9c04800
BP
111 sha1_update(&sha1, vec->data, n0);
112 sha1_update(&sha1, vec->data + n0, n1);
5eccf359
BP
113 sha1_final(&sha1, md);
114 assert(!memcmp(md, vec->output, SHA1_DIGEST_SIZE));
d138cd2c
BP
115 }
116
117 putchar('.');
118 fflush(stdout);
119}
120
121static void
122test_big_vector(void)
123{
124 enum { SIZE = 1000000 };
125 struct test_vector vec = {
126 NULL, SIZE,
127 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
128 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
129 };
c4cac9ab 130 size_t i;
d138cd2c
BP
131
132 vec.data = xmalloc(SIZE);
c4cac9ab
BP
133 for (i = 0; i < SIZE; i++) {
134 vec.data[i] = 'a';
135 }
d138cd2c
BP
136 test_one(&vec);
137 free(vec.data);
138}
139
eadd1644
AZ
140static void
141test_shar1_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
d138cd2c
BP
142{
143 int i;
144
145 for (i = 0; i < ARRAY_SIZE(vectors); i++) {
146 test_one(&vectors[i]);
147 }
148
149 test_big_vector();
150
5eccf359 151 putchar('\n');
d138cd2c 152}
eadd1644
AZ
153
154OVSTEST_REGISTER("test-sha1", test_shar1_main);