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