Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a test showing how to get conflicts on CRC. #559

Merged
merged 1 commit into from
Aug 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion src/utils/Crc.cxxtest
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "utils/Crc.hxx"

#include <stdlib.h>
#include "utils/format_utils.hxx"
#include "utils/test_main.hxx"
#include <stdlib.h>

extern uint8_t reverse(uint8_t data);

Expand Down Expand Up @@ -105,3 +106,72 @@ TEST(Crc8Test, Fuzz)
EXPECT_EQ(c0.get(), c256.get());
}
}

string get_sample(unsigned suffix)
{
const uint8_t prefixbytes[] = {0, 0x99, 0x11, 0x22, 0x33};
string s((const char *)prefixbytes, 5);
s.push_back(suffix & 0xff);
s[4] ^= (suffix >> 8);
return s;
}

// Shows the collision behavior of CRC8.
TEST(Crc8Test, Collision)
{
std::map<uint8_t, unsigned> rev_lookup;
unsigned c1, c2;
for (unsigned i = 0; i < 2 * 256; i++)
{
string s = get_sample(i);
Crc8DallasMaxim m;
for (unsigned j = 0; j < 6; j++)
{
m.update256(s[j]);
}
uint8_t crc = m.get();
if (rev_lookup.find(crc) != rev_lookup.end())
{
c1 = i;
c2 = rev_lookup[crc];
LOG(INFO, "collision: %d %d on %02x", c1, c2, crc);
break;
}
rev_lookup[crc] = i;
}
string s1 = get_sample(c1), s2 = get_sample(c2);
Crc8DallasMaxim m1, m2;
for (unsigned i = 0; i < 6; i++)
{
m1.update256(s1[i]);
m2.update256(s2[i]);
}
LOG(INFO, "1 %02x %02x %d", m1.get(), m2.get(), s1 == s2);
for (unsigned i = 0; i < 6; i++)
{
m1.update256(s1[i]);
m2.update256(s2[i]);
}
LOG(INFO, "2 %02x %02x", m1.get(), m2.get());
for (unsigned i = 0; i < 6; i++)
{
m1.update256(s1[i]);
m2.update256(s2[i]);
}
LOG(INFO, "3 %02x %02x", m1.get(), m2.get());
for (unsigned cc = 0; cc < 256; cc++)
{
m1.init();
m1.update256(cc);
m2.init();
m2.update256(cc);
for (unsigned i = 0; i < 6; i++)
{
m1.update256(s1[i]);
m2.update256(s2[i]);
}
LOG(VERBOSE, "start %02x one %02x two %02x", cc, m1.get(), m2.get());
}
LOG(INFO, "s1=%s s2=%s", string_to_hex(s1).c_str(),
string_to_hex(s2).c_str());
}