|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include <stdio.h> |
|
6 |
|
7 #include "secasn1.h" |
|
8 |
|
9 struct TestCase { |
|
10 long value; |
|
11 unsigned char data[5]; |
|
12 unsigned int len; |
|
13 }; |
|
14 |
|
15 static struct TestCase testCase[] = { |
|
16 /* XXX NSS doesn't generate the shortest encoding for negative values. */ |
|
17 #if 0 |
|
18 { -128, { 0x80 }, 1 }, |
|
19 { -129, { 0xFF, 0x7F }, 2 }, |
|
20 #endif |
|
21 |
|
22 { 0, { 0x00 }, 1 }, |
|
23 { 127, { 0x7F }, 1 }, |
|
24 { 128, { 0x00, 0x80 }, 2 }, |
|
25 { 256, { 0x01, 0x00 }, 2 }, |
|
26 { 32768, { 0x00, 0x80, 0x00 }, 3 } |
|
27 }; |
|
28 |
|
29 int main() |
|
30 { |
|
31 PRBool failed = PR_FALSE; |
|
32 unsigned int i; |
|
33 unsigned int j; |
|
34 |
|
35 for (i = 0; i < sizeof(testCase)/sizeof(testCase[0]); i++) { |
|
36 SECItem encoded; |
|
37 if (SEC_ASN1EncodeInteger(NULL, &encoded, testCase[i].value) == NULL) { |
|
38 fprintf(stderr, "SEC_ASN1EncodeInteger failed\n"); |
|
39 failed = PR_TRUE; |
|
40 continue; |
|
41 } |
|
42 if (encoded.len != testCase[i].len || |
|
43 memcmp(encoded.data, testCase[i].data, encoded.len) != 0) { |
|
44 fprintf(stderr, "Encoding of %ld is incorrect:", |
|
45 testCase[i].value); |
|
46 for (j = 0; j < encoded.len; j++) { |
|
47 fprintf(stderr, " 0x%02X", (unsigned int)encoded.data[j]); |
|
48 } |
|
49 fputs("\n", stderr); |
|
50 failed = PR_TRUE; |
|
51 } |
|
52 PORT_Free(encoded.data); |
|
53 } |
|
54 |
|
55 if (failed) { |
|
56 fprintf(stderr, "FAIL\n"); |
|
57 return 1; |
|
58 } |
|
59 printf("PASS\n"); |
|
60 return 0; |
|
61 } |