security/nss/cmd/tests/dertimetest.c

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

     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>
     6 #include <stdlib.h>
     8 #include "secder.h"
     9 #include "secerr.h"
    11 int main()
    12 {
    13     SECItem badTime;
    14     PRTime prtime;
    15     SECStatus rv;
    16     int error;
    17     PRBool failed = PR_FALSE;
    19     /* A UTCTime string with an embedded null. */
    20     badTime.type = siBuffer;
    21     badTime.data = (unsigned char *)"091219000000Z\0junkjunkjunkjunkjunkjunk";
    22     badTime.len = 38;
    23     rv = DER_UTCTimeToTime(&prtime, &badTime);
    24     if (rv == SECSuccess) {
    25         fprintf(stderr, "DER_UTCTimeToTime should have failed but "
    26                 "succeeded\n");
    27         failed = PR_TRUE;
    28     } else {
    29         error = PORT_GetError();
    30         if (error != SEC_ERROR_INVALID_TIME) {
    31             fprintf(stderr, "DER_UTCTimeToTime failed with error %d, "
    32                     "expected error %d\n", error, SEC_ERROR_INVALID_TIME);
    33             failed = PR_TRUE;
    34         }
    35     }
    37     /* A UTCTime string with junk after a valid date/time. */
    38     badTime.type = siBuffer;
    39     badTime.data = (unsigned char *)"091219000000Zjunk";
    40     badTime.len = 17;
    41     rv = DER_UTCTimeToTime(&prtime, &badTime);
    42     if (rv == SECSuccess) {
    43         fprintf(stderr, "DER_UTCTimeToTime should have failed but "
    44                 "succeeded\n");
    45         failed = PR_TRUE;
    46     } else {
    47         error = PORT_GetError();
    48         if (error != SEC_ERROR_INVALID_TIME) {
    49             fprintf(stderr, "DER_UTCTimeToTime failed with error %d, "
    50                     "expected error %d\n", error, SEC_ERROR_INVALID_TIME);
    51             failed = PR_TRUE;
    52         }
    53     }
    55     /* A GeneralizedTime string with an embedded null. */
    56     badTime.type = siBuffer;
    57     badTime.data = (unsigned char *)"20091219000000Z\0junkjunkjunkjunkjunkjunk";
    58     badTime.len = 40;
    59     rv = DER_GeneralizedTimeToTime(&prtime, &badTime);
    60     if (rv == SECSuccess) {
    61         fprintf(stderr, "DER_GeneralizedTimeToTime should have failed but "
    62                 "succeeded\n");
    63         failed = PR_TRUE;
    64     } else {
    65         error = PORT_GetError();
    66         if (error != SEC_ERROR_INVALID_TIME) {
    67             fprintf(stderr, "DER_GeneralizedTimeToTime failed with error %d, "
    68                     "expected error %d\n", error, SEC_ERROR_INVALID_TIME);
    69             failed = PR_TRUE;
    70         }
    71     }
    73     /* A GeneralizedTime string with junk after a valid date/time. */
    74     badTime.type = siBuffer;
    75     badTime.data = (unsigned char *)"20091219000000Zjunk";
    76     badTime.len = 19;
    77     rv = DER_GeneralizedTimeToTime(&prtime, &badTime);
    78     if (rv == SECSuccess) {
    79         fprintf(stderr, "DER_GeneralizedTimeToTime should have failed but "
    80                 "succeeded\n");
    81         failed = PR_TRUE;
    82     } else {
    83         error = PORT_GetError();
    84         if (error != SEC_ERROR_INVALID_TIME) {
    85             fprintf(stderr, "DER_GeneralizedTimeToTime failed with error %d, "
    86                     "expected error %d\n", error, SEC_ERROR_INVALID_TIME);
    87             failed = PR_TRUE;
    88         }
    89     }
    91     if (failed) {
    92         fprintf(stderr, "FAIL\n");
    93         return 1;
    94     }
    95     printf("PASS\n");
    96     return 0;
    97 }

mercurial