test_utility.c revision 7e6f80eab960e6e6fb85b0c98cfec6ee72946277
/**
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2015 ForgeRock AS.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <setjmp.h>
#include "am.h"
#include "platform.h"
#include "utility.h"
#include "log.h"
#include "cmocka.h"
/**
* This is the marker we spatter throughout our destination buffer for testing purposes. It must not be null
* because as far as this code is concerned, null is valid data. We need this to be invalid and noticable.
* Thus I chose control A.
*/
#define MARKER 0x1
/**
* Encode the string stored in p and make non printing characters (including nulls) visible by representing
* them as \HH where HH is the hex value in the shortest width possible (so null comes out as \0). We return
* a pointer to a dynamically allocated piece of memory which should be freed. This routine isn't particularly
* efficient.
*/
static char buf[1024];
buf[0] = '\0';
while(len--) {
char buf2[50];
if (*p < ' ' || *p == 127) {
} else {
buf2[0] = *p;
}
p++;
}
}
/**
* Find the first marker character in "dest" and return the number of characters before it.
*/
const char* p = dest;
while (*p != MARKER) {
p++;
}
return p - dest;
}
/**
* Compare the actual and expected values. If they do not match, dump out a representation of both the expected
* and actual values so we can see what has gone wrong. If they do match, print "SUCCESS" and return 0. In
* this way we draw maximum attention to ourselves only if the test fails.
*/
if (actual_len != expected_len) {
printf("actual length and expected lengths are different (%lu vs %lu)\n",
return 1;
}
printf("actual and expected strings are different (although the same length)\n");
return 1;
}
return 0;
}
/************************************************************************************************************/
static const char* richard3 = "Now is the winter of our discontent, "
"Made glorious summer by this son of York";
static const char* as_you_like_it_1 = "All the world's a stage, and all the men and women merely players";
static const char* as_you_like_it_2 = "they have their exits and their entrances";
static const char* as_you_like_it_3 = "and one man in his time plays many parts, his acts being seven ages";
/************************************************************************************************************/
/**
* test mem2cpy, returning 1 if the test fails, 0 if it succeeds.
*/
void test_mem2cpy(void** state) {
(void)state;
char a[] = "ABCDEF";
char b[] = "GHI";
char dest[1000];
}
/**
* test mem3cpy, returning 1 if the test fails, 0 if it succeeds.
*/
void test_mem3cpy(void** state) {
(void)state;
char a[] = "ABCDEF";
char b[] = "GHI";
char c[] = "JKLMN";
char dest[1000];
}
/**
* Test the match function.
*/
void test_match(void** state) {
(void)state;
// for some reason, passing in null results in an "ok" match
}
/**
* Note that the match_groups function isn't tested here because it is only invoked once in the entire codebase.
* Also I can't quite figure what the length parameters should be set to.
*/
static int am_vasprintf_test(char** p, const char* format, ...) {
int result;
return result;
}
/**
* Test the am_vasprintf function.
*/
void test_am_vasprintf(void** state) {
char random[5];
int returned = am_vasprintf_test(&buff, "%s: %s; %s", as_you_like_it_1, as_you_like_it_2, as_you_like_it_3);
int correct_length = strlen(as_you_like_it_1) + 2 + strlen(as_you_like_it_2) + 2 + strlen(as_you_like_it_3);
}
/**
* test the am_asprintf function. Obviously one thing we cannot do is pass it a pointer to the
* stack as it's first parmameter - if we do, it crashes.
*/
void test_am_asprintf(void** state) {
char check[1024];
}
/**
* test the am_free function. Obviously we can't pass a stack-based reference to it, that will cause it
* to crash. Similarly we can only pass a pointer directly returned from one of the memory allocation functions,
* as opposed to something returned and incremented a little.
*/
void test_am_free(void** state) {
/* this is about the only thing I can think of asserting here
* at least it "proves" we made it this far.
*/
assert_true(1);
}
/**
* test am_strldup, which duplicates a lowercase version of the string into dynamic memory.
* In fact am_stridup was only written while tidying up stristr.
*/
void test_am_strldup(void** state) {
assert_string_equal(lowercase_text1, "all the world's a stage, and all the men and women merely players");
/* this at least proves we survived the freeing */
assert_true(1);
}
#define START "NoW iS tHe Win"
#define MIDDLE "TER"
#define END " OF OuR diSCOnteNT"
/**
* Test case insensitive string searching.
*/
void test_stristr(void** state) {
}
/**
* Test base 64 encoding and decoding. Note that https://www.base64encode.org/ has been
* particularly helpful here. Also note the importance of setting "length" correctly before
* its address is passed into the encoding function. The decoding function doesn't care.
*/
void test_base64_encode_decode(void** state) {
const char* in = "Man";
const char* out = "TWFu";
const char* r3_out = "Tm93IGlzIHRoZSB3aW50ZXIgb2Ygb3VyIGRpc2NvbnRlbnQsIE1hZGUgZ2xvcmlvdXMgc3VtbWVyIGJ5IHRoaXMgc29uIG9mIFlvcms=";
}
/**
* Note that I can't think of a good way to test delete_am_cookie_list.
*/
/**
* Test the rather odd char_count function,
*/
void test_char_count(void** state) {
int last;
assert_int_equal(result, 0);
}
/**
* Test encryption and decryption. This function does the obvious test of taking text, encoding
* it and decoding it again to see if we end up with the same thing.
*/
void test_encrypt_decrypt_password(void** state) {
const char* key = "jU7tHgf1iB4gbTR7";
char* clear_text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
// "result" now points to a dynamically allocated area filled with the encrypted password
// now decode back to the original text (hopefully)
}
/**
* Test the escaping of characters supported by xml_entity_escape.
*/
void test_xml_entity_escape(void** state) {
char* amp;
char* apos;
char* quot;
char* gt;
char* lt;
}
/**
* Test the rather odd am_strsep function.
*/
void test_am_strsep(void** state) {
char* match;
int counter = 0;
/* Note: do not use a multi character separator */
counter++;
}
}
#define PROTO1 "http"
#define HOST1 "the.site.com"
#define PORT1 "9010"
#define QUERY1 "?key12=value12&key2=value2#anchor"
#define PROTO2 "https"
#define HOST2 "mostly-landscapes.net"
#define PROTO3 "http"
#define HOST3 "_cryptic-world_.com"
#define PORT3 "1234"
/**
* Test the parsing of URLs.
*/
void test_parse_url(void** state) {
struct url url_struct;
int result;
}
/**
* test the url encode and decode functions.
*/
void test_url_encode_decode(void** state) {
char buff[] = "abc !\"#$'+(here)--[:>>>there<<<]*+/-?@{xxx}.";
char test_a[] = "%20a";
char test_b[] = "a%20";
char test_c[] = "a% %1";
char test_d[] = "% %20%x +%";
char test_e[] = "%C4%81%C4%8D%C4%93%C4%A3%C4%AB%C4%B7%C4%BC%C5%86%C5%A1%C5%AB%C5%BE";
buff[0] = '\0';
}