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