Skip to content

Commit affcd0c

Browse files
committed
add test unit
1 parent d62f7e8 commit affcd0c

File tree

3 files changed

+29
-97
lines changed

3 files changed

+29
-97
lines changed

.github/workflows/c-cpp.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ jobs:
1313

1414
steps:
1515
- uses: actions/checkout@v3
16-
- name: make
16+
- name: Install dependencies
17+
run: sudo apt-get install -y libcunit1 libcunit1-doc libcunit1-dev
18+
- name: Make
1719
run: make
18-
- name: test
20+
- name: Test
1921
run: make test
2022

Makefile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
TARGET=keeloq
2-
31
default: build
42

53
build:
64
gcc keeloq.c -c
75

86
test:build
9-
gcc test.c $(TARGET).o -o $(TARGET)
10-
./$(TARGET)
7+
gcc test.c keeloq.o -o test -lcunit
8+
./test
119

1210
clean:
13-
rm $(TARGET) $(TARGET).o
11+
rm *.o test

test.c

Lines changed: 22 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,33 @@
1+
#include <CUnit/CUnit.h>
2+
#include <CUnit/Basic.h>
3+
14
#include <stdio.h>
25
#include "keeloq.h"
36

4-
int test_simple()
7+
void test_simple()
58
{
6-
int err=0;
7-
89
// KEELOQ Key LSB-first
910
uint8_t key[]= {0x49,0xd9,0x9f,0xb7,0x01,0x67,0xec,0x5c};
1011

1112
uint32_t plaintext_ok= 0xf741e2db;
1213
uint32_t ciphertext_ok= 0xe44f4cdf;
1314
uint32_t temp;
1415

15-
printf("------- Simple test -------\r\n");
16-
1716
temp=plaintext_ok;
1817

19-
printf("Text=0x%08x\r\n",temp);
20-
printf("N=%d\r\n",KEELOQ_NROUNDS);
21-
2218
// Encrypt plaintext to ciphertext
2319
keeloq_encrypt(key,&temp,KEELOQ_NROUNDS);
2420

25-
printf("Encrypted to 0x%08x ",temp);
26-
if(temp==ciphertext_ok) printf("[ OK ]");
27-
else
28-
{
29-
printf("[ ERROR! ]");
30-
err++;
31-
}
32-
printf("\r\n");
21+
CU_ASSERT_EQUAL(temp, ciphertext_ok);
3322

3423
// Decrypt ciphertext to plaintext
3524
keeloq_decrypt(key,&temp,KEELOQ_NROUNDS);
3625

37-
printf("Decrypted to 0x%08x ",temp);
38-
if(temp==plaintext_ok) printf("[ OK ]");
39-
else
40-
{
41-
printf("[ ERROR! ]");
42-
err++;
43-
}
44-
printf("\r\n---------------------------\r\n");
45-
46-
return err;
26+
CU_ASSERT_EQUAL(temp, plaintext_ok);
4727
}
4828

49-
int test_normal()
29+
void test_normal()
5030
{
51-
int err=0;
52-
5331
// KEELOQ Key LSB-first
5432
uint8_t mf_key[]={0xef,0xcd,0xab,0x89,0x67,0x45,0x23,0x01};
5533
uint32_t ser=0x6C46ACA;
@@ -59,47 +37,24 @@ int test_normal()
5937
uint32_t ciphertext_ok= 0xecf4c92d;
6038
uint32_t temp;
6139

62-
printf("------- Normal test -------\r\n");
63-
6440
temp=plaintext_ok;
6541

66-
printf("Text=0x%08x\r\n",temp);
67-
printf("N=%d\r\n",KEELOQ_NROUNDS);
68-
6942
// Generate normal key
7043
keeloq_gen_normal_key(key,mf_key,ser);
7144

7245
// Encrypt plaintext to ciphertext
7346
keeloq_encrypt(key,&temp,KEELOQ_NROUNDS);
7447

75-
printf("Encrypted to 0x%08x ",temp);
76-
if(temp==ciphertext_ok) printf("[ OK ]");
77-
else
78-
{
79-
printf("[ ERROR! ]");
80-
err++;
81-
}
82-
printf("\r\n");
48+
CU_ASSERT_EQUAL(temp, ciphertext_ok);
8349

8450
// Decrypt ciphertext to plaintext
8551
keeloq_decrypt(key,&temp,KEELOQ_NROUNDS);
8652

87-
printf("Decrypted to 0x%08x ",temp);
88-
if(temp==plaintext_ok) printf("[ OK ]");
89-
else
90-
{
91-
printf("[ ERROR! ]");
92-
err++;
93-
}
94-
printf("\r\n---------------------------\r\n");
95-
96-
return err;
53+
CU_ASSERT_EQUAL(temp, plaintext_ok);
9754
}
9855

99-
int test_secure()
56+
void test_secure()
10057
{
101-
int err=0;
102-
10358
// KEELOQ Key LSB-first
10459
uint8_t mf_key[]={0xef,0xcd,0xab,0x89,0x67,0x45,0x23,0x01};
10560
uint32_t ser= 0x0B6AF9A8;
@@ -110,54 +65,31 @@ int test_secure()
11065
uint32_t ciphertext_ok= 0xc9ebe007;
11166
uint32_t temp;
11267

113-
printf("------- Secure test -------\r\n");
114-
11568
temp=plaintext_ok;
11669

117-
printf("Text=0x%08x\r\n",temp);
118-
printf("N=%d\r\n",KEELOQ_NROUNDS);
119-
12070
// Generate secure key
12171
keeloq_gen_secure_key(key,mf_key,seed,ser);
12272

12373
// Encrypt plaintext to ciphertext
12474
keeloq_encrypt(key,&temp,KEELOQ_NROUNDS);
12575

126-
printf("Encrypted to 0x%08x ",temp);
127-
if(temp==ciphertext_ok) printf("[ OK ]");
128-
else
129-
{
130-
printf("[ ERROR! ]");
131-
err++;
132-
}
133-
printf("\r\n");
76+
CU_ASSERT_EQUAL(temp, ciphertext_ok);
13477

13578
// Decrypt ciphertext to plaintext
13679
keeloq_decrypt(key,&temp,KEELOQ_NROUNDS);
13780

138-
printf("Decrypted to 0x%08x ",temp);
139-
if(temp==plaintext_ok) printf("[ OK ]");
140-
else
141-
{
142-
printf("[ ERROR! ]");
143-
err++;
144-
}
145-
printf("\r\n---------------------------\r\n");
146-
147-
return err;
81+
CU_ASSERT_EQUAL(temp, plaintext_ok);
14882
}
14983

15084
int main()
15185
{
152-
int err=0;
153-
err= test_simple();
154-
err+=test_normal();
155-
err+=test_secure();
156-
157-
printf("Test Status : ");
158-
if(err)printf("FAIL!");
159-
else printf("PASS");
160-
printf("\r\n---------------------------\r\n");
161-
162-
return err;
163-
}
86+
CU_initialize_registry();
87+
CU_pSuite suite = CU_add_suite("Test Suite", 0, 0);
88+
CU_add_test(suite, "Simple Test", test_simple);
89+
CU_add_test(suite, "Normal Test", test_normal);
90+
CU_add_test(suite, "Secure Test", test_secure);
91+
CU_basic_set_mode(CU_BRM_VERBOSE);
92+
CU_basic_run_tests();
93+
CU_cleanup_registry();
94+
return CU_get_error();
95+
}

0 commit comments

Comments
 (0)