security/nss/cmd/tests/encodeinttest.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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 }

mercurial