Added SILC Thread Queue API
[silc.git] / lib / silcacc / tests / test_softacc.c
1 /* Software accelerator tests */
2
3 #include "silc.h"
4
5 SilcSchedule schedule;
6 SilcPublicKey public_key, accpub;
7 SilcPrivateKey private_key, accprv;
8 SilcHash hash;
9 unsigned char data[] = "Single block msg";
10 int data_len = 16;
11 int s = 200;
12
13 void sign_compl(SilcBool success, const unsigned char *signature,
14                 SilcUInt32 signature_len, void *context)
15 {
16   SILC_LOG_DEBUG(("Sign compl %s", success ? "Ok" : "failed"));
17 }
18
19 SILC_TASK_CALLBACK(stats)
20 {
21   silc_stack_stats(silc_crypto_stack());
22   silc_schedule_task_add_timeout(schedule, stats, NULL, 1, 1);
23 }
24
25 SILC_TASK_CALLBACK(quit)
26 {
27   silc_schedule_stop(schedule);
28 }
29
30 SILC_TASK_CALLBACK(sign)
31 {
32   silc_pkcs_sign(accprv, data, data_len, TRUE, hash, NULL, sign_compl, NULL);
33   if (--s > 0)
34     silc_schedule_task_add_timeout(schedule, sign, NULL, 0, 60000);
35 }
36
37 int main(int argc, char **argv)
38 {
39   SilcBool success = FALSE;
40   SilcAccelerator softacc;
41
42   if (argc > 1 && !strcmp(argv[1], "-d")) {
43     silc_log_debug(TRUE);
44     silc_log_quick(TRUE);
45     silc_log_debug_hexdump(TRUE);
46     silc_log_set_debug_string("*acc*");
47   }
48
49   silc_crypto_init(NULL);
50   if (!silc_hash_alloc("sha1", &hash))
51     goto err;
52
53   if (!silc_create_key_pair("rsa", 2048, "pubkey.pub", "privkey.prv", NULL,
54                             "", &public_key, &private_key, FALSE))
55     goto err;
56
57   schedule = silc_schedule_init(0, NULL, NULL);
58
59   softacc = silc_acc_find("softacc");
60   if (!softacc)
61     goto err;
62
63   if (!silc_acc_init(softacc, schedule, "min_threads", 2, "max_threads", 
64                      8, NULL))
65     goto err;
66
67   accpub = silc_acc_public_key(softacc, public_key);
68   if (!accpub)
69     goto err;
70   accprv = silc_acc_private_key(softacc, private_key);
71   if (!accprv)
72     goto err;
73
74   if (silc_acc_get_public_key(softacc, accpub) != public_key)
75     goto err;
76   if (silc_acc_get_private_key(softacc, accprv) != private_key)
77     goto err;
78
79   silc_schedule_task_add_timeout(schedule, sign, NULL, 0, 1);
80   silc_schedule_task_add_timeout(schedule, stats, NULL, 1, 1);
81   silc_schedule_task_add_timeout(schedule, quit, NULL, 19, 0);
82   silc_schedule(schedule);
83
84   silc_acc_uninit(softacc);
85   silc_schedule_uninit(schedule);
86   silc_crypto_uninit();
87
88   success = TRUE;
89
90  err:
91   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
92   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
93
94   return success;
95 }