]> git.saurik.com Git - apple/libc.git/blame - tests/timingsafe_bcmp.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / tests / timingsafe_bcmp.c
CommitLineData
fc5ea90f
A
1#include <string.h>
2#include <stdlib.h>
3
4#include <darwintest.h>
5
6T_DECL(timingsafe_bcmp, "tests for timingsafe_bcmp(3)")
7{
8 // empty
9 T_ASSERT_EQ(0, timingsafe_bcmp(NULL, NULL, 0), NULL);
10 T_ASSERT_EQ(0, timingsafe_bcmp("foo", "foo", 0), NULL);
11 T_ASSERT_EQ(0, timingsafe_bcmp("foo", "bar", 0), NULL);
12
13 // equal
14 T_ASSERT_EQ(0, timingsafe_bcmp("foo", "foo", strlen("foo")), NULL);
15
16 // unequal
507116e3
A
17 T_ASSERT_EQ(1, timingsafe_bcmp("foo", "bar", strlen("foo")), NULL);
18 T_ASSERT_EQ(1, timingsafe_bcmp("foo", "goo", strlen("foo")), NULL);
19 T_ASSERT_EQ(1, timingsafe_bcmp("foo", "fpo", strlen("foo")), NULL);
20 T_ASSERT_EQ(1, timingsafe_bcmp("foo", "fop", strlen("foo")), NULL);
21
22 // all possible bitwise differences
23 int i;
24 for (i = 1; i < 256; i += 1) {
25 unsigned char a = 0;
26 unsigned char b = (unsigned char)i;
27
28 T_ASSERT_EQ(1, timingsafe_bcmp(&a, &b, sizeof(a)), NULL);
29 }
fc5ea90f
A
30
31 // large
32 char buf[1024 * 16];
33 arc4random_buf(buf, sizeof(buf));
34 T_ASSERT_EQ(0, timingsafe_bcmp(buf, buf, sizeof(buf)), NULL);
507116e3
A
35 T_ASSERT_EQ(1, timingsafe_bcmp(buf, buf + 1, sizeof(buf) - 1), NULL);
36 T_ASSERT_EQ(1, timingsafe_bcmp(buf, buf + 128, 128), NULL);
fc5ea90f
A
37
38 memcpy(buf+128, buf, 128);
39 T_ASSERT_EQ(0, timingsafe_bcmp(buf, buf + 128, 128), NULL);
40}