5f81f4c4c58f0b02ee78bb00db44d2743fdc6274
[platform/upstream/connman.git] / unit / test-prf-sha1.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <glib.h>
30
31 #include "src/shared/sha1.h"
32
33 struct prf_data {
34         const char *key;
35         unsigned int key_len;
36         const char *prefix;
37         unsigned int prefix_len;
38         const char *data;
39         unsigned int data_len;
40         const char *prf;
41 };
42
43 static void prf_test(gconstpointer data)
44 {
45         const struct prf_data *test = data;
46         unsigned int prf_len;
47         unsigned char output[512];
48         char prf[128];
49         unsigned int i;
50         int result;
51
52         prf_len = strlen(test->prf) / 2;
53
54         if (g_test_verbose() == TRUE) {
55                 g_print("PRF    = %s (%d octects)\n", test->prf, prf_len);
56         }
57
58         result = prf_sha1(test->key, test->key_len, test->prefix,
59                                 test->prefix_len, test->data, test->data_len,
60                                                 output, prf_len);
61
62         g_assert(result == 0);
63
64         for (i = 0; i < prf_len; i++)
65                 sprintf(prf + (i * 2), "%02x", output[i]);
66
67         if (g_test_verbose() == TRUE) {
68                 g_print("Result = %s\n", prf);
69         }
70
71         g_assert(strcmp(test->prf, prf) == 0);
72 }
73
74 static const struct prf_data test_case_1 = {
75         .key            = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
76                           "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
77         .key_len        = 20,
78         .prefix         = "prefix",
79         .prefix_len     = 6,
80         .data           = "Hi There",
81         .data_len       = 8,
82         .prf            = "bcd4c650b30b9684951829e0d75f9d54"
83                           "b862175ed9f00606e17d8da35402ffee"
84                           "75df78c3d31e0f889f012120c0862beb"
85                           "67753e7439ae242edb8373698356cf5a",
86 };
87
88 static const struct prf_data test_case_2 = {
89         .key            = "Jefe",
90         .key_len        = 4,
91         .prefix         = "prefix",
92         .prefix_len     = 6,
93         .data           = "what do ya want for nothing?",
94         .data_len       = 28,
95         .prf            = "51f4de5b33f249adf81aeb713a3c20f4"
96                           "fe631446fabdfa58244759ae58ef9009"
97                           "a99abf4eac2ca5fa87e692c440eb4002"
98                           "3e7babb206d61de7b92f41529092b8fc",
99 };
100
101 static const struct prf_data test_case_3 = {
102         .key            = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
103                           "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
104         .key_len        = 20,
105         .prefix         = "prefix",
106         .prefix_len     = 6,
107         .data           = "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
108                           "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
109                           "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
110                           "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
111                           "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
112         .data_len       = 50,
113         .prf            = "e1ac546ec4cb636f9976487be5c86be1"
114                           "7a0252ca5d8d8df12cfb0473525249ce"
115                           "9dd8d177ead710bc9b590547239107ae"
116                           "f7b4abd43d87f0a68f1cbd9e2b6f7607",
117 };
118
119 int main(int argc, char *argv[])
120 {
121         g_test_init(&argc, &argv, NULL);
122
123         g_test_add_data_func("/prf-sha1/Test case 1",
124                                         &test_case_1, prf_test);
125         g_test_add_data_func("/prf-sha1/Test case 2",
126                                         &test_case_2, prf_test);
127         g_test_add_data_func("/prf-sha1/Test case 3",
128                                         &test_case_3, prf_test);
129
130         return g_test_run();
131 }