michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: #include "secasn1.h" michael@0: michael@0: struct TestCase { michael@0: long value; michael@0: unsigned char data[5]; michael@0: unsigned int len; michael@0: }; michael@0: michael@0: static struct TestCase testCase[] = { michael@0: /* XXX NSS doesn't generate the shortest encoding for negative values. */ michael@0: #if 0 michael@0: { -128, { 0x80 }, 1 }, michael@0: { -129, { 0xFF, 0x7F }, 2 }, michael@0: #endif michael@0: michael@0: { 0, { 0x00 }, 1 }, michael@0: { 127, { 0x7F }, 1 }, michael@0: { 128, { 0x00, 0x80 }, 2 }, michael@0: { 256, { 0x01, 0x00 }, 2 }, michael@0: { 32768, { 0x00, 0x80, 0x00 }, 3 } michael@0: }; michael@0: michael@0: int main() michael@0: { michael@0: PRBool failed = PR_FALSE; michael@0: unsigned int i; michael@0: unsigned int j; michael@0: michael@0: for (i = 0; i < sizeof(testCase)/sizeof(testCase[0]); i++) { michael@0: SECItem encoded; michael@0: if (SEC_ASN1EncodeInteger(NULL, &encoded, testCase[i].value) == NULL) { michael@0: fprintf(stderr, "SEC_ASN1EncodeInteger failed\n"); michael@0: failed = PR_TRUE; michael@0: continue; michael@0: } michael@0: if (encoded.len != testCase[i].len || michael@0: memcmp(encoded.data, testCase[i].data, encoded.len) != 0) { michael@0: fprintf(stderr, "Encoding of %ld is incorrect:", michael@0: testCase[i].value); michael@0: for (j = 0; j < encoded.len; j++) { michael@0: fprintf(stderr, " 0x%02X", (unsigned int)encoded.data[j]); michael@0: } michael@0: fputs("\n", stderr); michael@0: failed = PR_TRUE; michael@0: } michael@0: PORT_Free(encoded.data); michael@0: } michael@0: michael@0: if (failed) { michael@0: fprintf(stderr, "FAIL\n"); michael@0: return 1; michael@0: } michael@0: printf("PASS\n"); michael@0: return 0; michael@0: }