Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
5 #include <stdio.h>
7 #include "secasn1.h"
9 struct TestCase {
10 long value;
11 unsigned char data[5];
12 unsigned int len;
13 };
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
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 };
29 int main()
30 {
31 PRBool failed = PR_FALSE;
32 unsigned int i;
33 unsigned int j;
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 }
55 if (failed) {
56 fprintf(stderr, "FAIL\n");
57 return 1;
58 }
59 printf("PASS\n");
60 return 0;
61 }