Skip to content

Instantly share code, notes, and snippets.

@Karm
Last active December 17, 2015 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Karm/5645769 to your computer and use it in GitHub Desktop.
Save Karm/5645769 to your computer and use it in GitHub Desktop.
A test for a possible solution (or shall we call it a workaround? :-D) to https://issues.jboss.org/browse/MODCLUSTER-339
/**
* author Michal Karm Babacek <mbabacek@redhat.com>
* Test zone remover
*/
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#define LEN(x) (sizeof(x) / sizeof(x[0]))
/**
* remove_zone function to be tested
*/
char* remove_zone(char* host) {
char *tmp = (char *) malloc(strlen(host) * sizeof(char));
strcpy(tmp, host);
char *p_read = tmp, *p_write = tmp;
int flag = 0;
if (*p_read == '[') {
while (*p_read) {
*p_write = *p_read++;
if ((*p_write == '%' || flag) && *p_write != ']') {
flag = 1;
} else {
p_write++;
}
}
*p_write = '\0';
}
return tmp;
}
/**
* let's test it
*/
int main() {
char *addresses[] = {
"[2620:52:0:105f::ffff:60]",
"[2620:52:0:105f::ffff:60%]",
"[2620:52:0:105f::ffff:60%ETH]",
"[2620:52:0:105f::ffff:60%%]",
"[2620:52:0:105f::ffff:60%12]",
"[2:::1%2]",
"[124.22.33.44%ETH]",
"124.22.33.44%ETH",
"124.22.33.44%",
"124.22.33.44",
"[]",
"[::]",
"%",
"[%",
"%]",
"]",
"[",
"test.node.red.hat.com",
"utterly-complete.debug",
"test.node.red.hat.com%10" };
char *addresses_done[] = {
"[2620:52:0:105f::ffff:60]",
"[2620:52:0:105f::ffff:60]",
"[2620:52:0:105f::ffff:60]",
"[2620:52:0:105f::ffff:60]",
"[2620:52:0:105f::ffff:60]",
"[2:::1]",
"[124.22.33.44]",
"124.22.33.44%ETH",
"124.22.33.44%",
"124.22.33.44",
"[]",
"[::]",
"%",
"[",
"%]",
"]",
"[",
"test.node.red.hat.com",
"utterly-complete.debug",
"test.node.red.hat.com%10" };
int addresses_len = LEN(addresses);
assert(addresses_len == LEN(addresses_done));
for (int i = 1; i < addresses_len; i++) {
char* result = remove_zone(addresses[i]);
printf("Testing \"%s\" Expected: \"%s\" Got:\"%s\"\n", addresses[i], addresses_done[i], result);
assert(strcmp(result, addresses_done[i]) == 0);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment