summaryrefslogtreecommitdiffstats
path: root/security/nss/cmd/tests/dertimetest.c
blob: 2deedbc065a23b0e87187ce4601e0e92accff42f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <stdio.h>
#include <stdlib.h>

#include "secder.h"
#include "secerr.h"

int
main()
{
    SECItem badTime;
    PRTime prtime;
    SECStatus rv;
    int error;
    PRBool failed = PR_FALSE;

    /* A UTCTime string with an embedded null. */
    badTime.type = siBuffer;
    badTime.data = (unsigned char *)"091219000000Z\0junkjunkjunkjunkjunkjunk";
    badTime.len = 38;
    rv = DER_UTCTimeToTime(&prtime, &badTime);
    if (rv == SECSuccess) {
        fprintf(stderr, "DER_UTCTimeToTime should have failed but "
                        "succeeded\n");
        failed = PR_TRUE;
    } else {
        error = PORT_GetError();
        if (error != SEC_ERROR_INVALID_TIME) {
            fprintf(stderr, "DER_UTCTimeToTime failed with error %d, "
                            "expected error %d\n",
                    error, SEC_ERROR_INVALID_TIME);
            failed = PR_TRUE;
        }
    }

    /* A UTCTime string with junk after a valid date/time. */
    badTime.type = siBuffer;
    badTime.data = (unsigned char *)"091219000000Zjunk";
    badTime.len = 17;
    rv = DER_UTCTimeToTime(&prtime, &badTime);
    if (rv == SECSuccess) {
        fprintf(stderr, "DER_UTCTimeToTime should have failed but "
                        "succeeded\n");
        failed = PR_TRUE;
    } else {
        error = PORT_GetError();
        if (error != SEC_ERROR_INVALID_TIME) {
            fprintf(stderr, "DER_UTCTimeToTime failed with error %d, "
                            "expected error %d\n",
                    error, SEC_ERROR_INVALID_TIME);
            failed = PR_TRUE;
        }
    }

    /* A GeneralizedTime string with an embedded null. */
    badTime.type = siBuffer;
    badTime.data = (unsigned char *)"20091219000000Z\0junkjunkjunkjunkjunkjunk";
    badTime.len = 40;
    rv = DER_GeneralizedTimeToTime(&prtime, &badTime);
    if (rv == SECSuccess) {
        fprintf(stderr, "DER_GeneralizedTimeToTime should have failed but "
                        "succeeded\n");
        failed = PR_TRUE;
    } else {
        error = PORT_GetError();
        if (error != SEC_ERROR_INVALID_TIME) {
            fprintf(stderr, "DER_GeneralizedTimeToTime failed with error %d, "
                            "expected error %d\n",
                    error, SEC_ERROR_INVALID_TIME);
            failed = PR_TRUE;
        }
    }

    /* A GeneralizedTime string with junk after a valid date/time. */
    badTime.type = siBuffer;
    badTime.data = (unsigned char *)"20091219000000Zjunk";
    badTime.len = 19;
    rv = DER_GeneralizedTimeToTime(&prtime, &badTime);
    if (rv == SECSuccess) {
        fprintf(stderr, "DER_GeneralizedTimeToTime should have failed but "
                        "succeeded\n");
        failed = PR_TRUE;
    } else {
        error = PORT_GetError();
        if (error != SEC_ERROR_INVALID_TIME) {
            fprintf(stderr, "DER_GeneralizedTimeToTime failed with error %d, "
                            "expected error %d\n",
                    error, SEC_ERROR_INVALID_TIME);
            failed = PR_TRUE;
        }
    }

    if (failed) {
        fprintf(stderr, "FAIL\n");
        return 1;
    }
    printf("PASS\n");
    return 0;
}