1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <assert.h>
#include "../src/io.h"
#include "../src/portsentry.h"
void TestSubstStringNormalCase(void);
void TestSubstStringNoMatch(void);
void TestSubstStringEmptyReplace(void);
void TestSubstStringNullInputs(void);
void TestSubstStringInvalidSize(void);
void TestSubstStringEmptyFindToken(void);
void TestSubstStringBufferOverflow(void);
void TestSubstStringExactFit(void);
void TestSubstStringMultipleReplacements(void);
void TestSubstStringOverlappingTokens(void);
void TestSubstStringSpecialCharacters(void);
void TestSubstStringUnicodeSafe(void);
void TestSubstStringBoundaryConditions(void);
void TestSubstStringNormalCase(void) {
char dest[100];
const char *source = "Hello world, hello universe";
const char *findToken = "hello";
const char *replaceToken = "hi";
int result = SubstString(replaceToken, findToken, source, dest, sizeof(dest));
assert(result == 1);
assert(strcmp(dest, "Hello world, hi universe") == 0);
printf("Normal case test passed\n");
}
void TestSubstStringNoMatch(void) {
char dest[100];
const char *source = "Hello world";
const char *findToken = "xyz";
const char *replaceToken = "abc";
int result = SubstString(replaceToken, findToken, source, dest, sizeof(dest));
assert(result == 0);
assert(strcmp(dest, "Hello world") == 0);
printf("No match test passed\n");
}
void TestSubstStringEmptyReplace(void) {
char dest[100];
const char *source = "Hello world hello";
const char *findToken = "hello";
const char *replaceToken = "";
int result = SubstString(replaceToken, findToken, source, dest, sizeof(dest));
assert(result == 1);
assert(strcmp(dest, "Hello world ") == 0);
printf("Empty replace token test passed\n");
}
void TestSubstStringNullInputs(void) {
char dest[100];
const char *source = "test";
const char *findToken = "test";
const char *replaceToken = "new";
assert(SubstString(NULL, findToken, source, dest, sizeof(dest)) == ERROR);
assert(SubstString(replaceToken, NULL, source, dest, sizeof(dest)) == ERROR);
assert(SubstString(replaceToken, findToken, NULL, dest, sizeof(dest)) == ERROR);
assert(SubstString(replaceToken, findToken, source, NULL, sizeof(dest)) == ERROR);
printf("Null inputs test passed\n");
}
void TestSubstStringInvalidSize(void) {
char dest[100];
const char *source = "test";
const char *findToken = "test";
const char *replaceToken = "new";
assert(SubstString(replaceToken, findToken, source, dest, 0) == ERROR);
assert(SubstString(replaceToken, findToken, source, dest, -1) == ERROR);
printf("Invalid size test passed\n");
}
void TestSubstStringEmptyFindToken(void) {
char dest[100];
const char *source = "test";
const char *findToken = "";
const char *replaceToken = "new";
assert(SubstString(replaceToken, findToken, source, dest, sizeof(dest)) == ERROR);
printf("Empty find token test passed\n");
}
void TestSubstStringBufferOverflow(void) {
char dest[10];
const char *source = "Hello world hello universe";
const char *findToken = "hello";
const char *replaceToken = "very long replacement string";
int result = SubstString(replaceToken, findToken, source, dest, sizeof(dest));
assert(result == ERROR);
printf("Buffer overflow test passed\n");
}
void TestSubstStringExactFit(void) {
char dest[15];
const char *source = "Hello world";
const char *findToken = "world";
const char *replaceToken = "universe";
int result = SubstString(replaceToken, findToken, source, dest, sizeof(dest));
assert(result == 1);
assert(strcmp(dest, "Hello universe") == 0);
printf("Exact fit test passed\n");
}
void TestSubstStringMultipleReplacements(void) {
char dest[200];
const char *source = "aaa aaa aaa aaa";
const char *findToken = "aaa";
const char *replaceToken = "bbb";
int result = SubstString(replaceToken, findToken, source, dest, sizeof(dest));
assert(result == 4);
assert(strcmp(dest, "bbb bbb bbb bbb") == 0);
printf("Multiple replacements test passed\n");
}
void TestSubstStringOverlappingTokens(void) {
char dest[100];
const char *source = "aaaaa";
const char *findToken = "aaa";
const char *replaceToken = "b";
int result = SubstString(replaceToken, findToken, source, dest, sizeof(dest));
assert(result == 1);
assert(strcmp(dest, "baa") == 0);
printf("Overlapping tokens test passed\n");
}
void TestSubstStringSpecialCharacters(void) {
char dest[100];
const char *source = "Hello\nworld\thello";
const char *findToken = "\n";
const char *replaceToken = " ";
int result = SubstString(replaceToken, findToken, source, dest, sizeof(dest));
assert(result == 1);
assert(strcmp(dest, "Hello world\thello") == 0);
printf("Special characters test passed\n");
}
void TestSubstStringUnicodeSafe(void) {
char dest[100];
const char *source = "Hello\x80\x81\x82 world";
const char *findToken = "\x80\x81";
const char *replaceToken = "test";
int result = SubstString(replaceToken, findToken, source, dest, sizeof(dest));
assert(result == 1);
assert(strcmp(dest, "Hellotest\x82 world") == 0);
printf("Unicode safe test passed\n");
}
void TestSubstStringBoundaryConditions(void) {
char dest[3];
const char *source = "test";
const char *findToken = "test";
const char *replaceToken = "new";
// Should fail due to insufficient buffer space
int result = SubstString(replaceToken, findToken, source, dest, sizeof(dest));
assert(result == ERROR);
// Should succeed with exact space
char dest2[4];
result = SubstString(replaceToken, findToken, source, dest2, sizeof(dest2));
assert(result == 1); // "new" is 3 chars + null terminator = 4, which fits in dest2[4]
char dest3[5];
result = SubstString(replaceToken, findToken, source, dest3, sizeof(dest3));
assert(result == 1);
assert(strcmp(dest3, "new") == 0);
printf("Boundary conditions test passed\n");
}
int main(void) {
TestSubstStringNormalCase();
TestSubstStringNoMatch();
TestSubstStringEmptyReplace();
TestSubstStringNullInputs();
TestSubstStringInvalidSize();
TestSubstStringEmptyFindToken();
TestSubstStringBufferOverflow();
TestSubstStringExactFit();
TestSubstStringMultipleReplacements();
TestSubstStringOverlappingTokens();
TestSubstStringSpecialCharacters();
TestSubstStringUnicodeSafe();
TestSubstStringBoundaryConditions();
printf("All SubstString tests passed!\n");
return 0;
}
|