]>
git.saurik.com Git - apple/libc.git/blob - tests/timingsafe_bcmp.c
4 #include <darwintest.h>
6 T_DECL(timingsafe_bcmp
, "tests for timingsafe_bcmp(3)")
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
);
14 T_ASSERT_EQ(0, timingsafe_bcmp("foo", "foo", strlen("foo")), NULL
);
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
);
22 // all possible bitwise differences
24 for (i
= 1; i
< 256; i
+= 1) {
26 unsigned char b
= (unsigned char)i
;
28 T_ASSERT_EQ(1, timingsafe_bcmp(&a
, &b
, sizeof(a
)), NULL
);
33 arc4random_buf(buf
, sizeof(buf
));
34 T_ASSERT_EQ(0, timingsafe_bcmp(buf
, buf
, sizeof(buf
)), NULL
);
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
);
38 memcpy(buf
+128, buf
, 128);
39 T_ASSERT_EQ(0, timingsafe_bcmp(buf
, buf
+ 128, 128), NULL
);