1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/cmd/tests/encodeinttest.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,61 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include <stdio.h> 1.9 + 1.10 +#include "secasn1.h" 1.11 + 1.12 +struct TestCase { 1.13 + long value; 1.14 + unsigned char data[5]; 1.15 + unsigned int len; 1.16 +}; 1.17 + 1.18 +static struct TestCase testCase[] = { 1.19 + /* XXX NSS doesn't generate the shortest encoding for negative values. */ 1.20 +#if 0 1.21 + { -128, { 0x80 }, 1 }, 1.22 + { -129, { 0xFF, 0x7F }, 2 }, 1.23 +#endif 1.24 + 1.25 + { 0, { 0x00 }, 1 }, 1.26 + { 127, { 0x7F }, 1 }, 1.27 + { 128, { 0x00, 0x80 }, 2 }, 1.28 + { 256, { 0x01, 0x00 }, 2 }, 1.29 + { 32768, { 0x00, 0x80, 0x00 }, 3 } 1.30 +}; 1.31 + 1.32 +int main() 1.33 +{ 1.34 + PRBool failed = PR_FALSE; 1.35 + unsigned int i; 1.36 + unsigned int j; 1.37 + 1.38 + for (i = 0; i < sizeof(testCase)/sizeof(testCase[0]); i++) { 1.39 + SECItem encoded; 1.40 + if (SEC_ASN1EncodeInteger(NULL, &encoded, testCase[i].value) == NULL) { 1.41 + fprintf(stderr, "SEC_ASN1EncodeInteger failed\n"); 1.42 + failed = PR_TRUE; 1.43 + continue; 1.44 + } 1.45 + if (encoded.len != testCase[i].len || 1.46 + memcmp(encoded.data, testCase[i].data, encoded.len) != 0) { 1.47 + fprintf(stderr, "Encoding of %ld is incorrect:", 1.48 + testCase[i].value); 1.49 + for (j = 0; j < encoded.len; j++) { 1.50 + fprintf(stderr, " 0x%02X", (unsigned int)encoded.data[j]); 1.51 + } 1.52 + fputs("\n", stderr); 1.53 + failed = PR_TRUE; 1.54 + } 1.55 + PORT_Free(encoded.data); 1.56 + } 1.57 + 1.58 + if (failed) { 1.59 + fprintf(stderr, "FAIL\n"); 1.60 + return 1; 1.61 + } 1.62 + printf("PASS\n"); 1.63 + return 0; 1.64 +}