|
21 | 21 | #define GRUEZI_INI_PATH "ressources/gruezi.ini"
|
22 | 22 | #define UTF8_INI_PATH "ressources/utf8.ini"
|
23 | 23 | #define TMP_INI_PATH "ressources/tmp.ini"
|
| 24 | +#define MISFORMED_INI_SEC0 "[12345" |
| 25 | +#define MISFORMED_INI_SEC1 "12345]" |
| 26 | +#define MISFORMED_INI_SEC2 "123]45" |
| 27 | +#define MISFORMED_INI_ATTR "1111" |
24 | 28 | #define QUOTES_INI_PATH "ressources/quotes.ini"
|
25 | 29 | #define QUOTES_INI_SEC "quotes"
|
26 | 30 | #define QUOTES_INI_ATTR0 "string0"
|
@@ -1049,6 +1053,159 @@ static void create_empty_ini_file(const char *filename)
|
1049 | 1053 | fclose(ini);
|
1050 | 1054 | }
|
1051 | 1055 |
|
| 1056 | +void Test_iniparser_misformed(CuTest *tc) |
| 1057 | +{ |
| 1058 | + dictionary *dic; |
| 1059 | + FILE *ini; |
| 1060 | + int ret; |
| 1061 | + |
| 1062 | + create_empty_ini_file(TMP_INI_PATH); |
| 1063 | + dic = iniparser_load(TMP_INI_PATH); |
| 1064 | + |
| 1065 | + if (!dic) { |
| 1066 | + fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH); |
| 1067 | + return; |
| 1068 | + } |
| 1069 | + |
| 1070 | + ret = iniparser_set(dic, MISFORMED_INI_SEC0, NULL); |
| 1071 | + |
| 1072 | + if (ret < 0) { |
| 1073 | + fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC0, |
| 1074 | + TMP_INI_PATH); |
| 1075 | + goto del_dic; |
| 1076 | + } |
| 1077 | + |
| 1078 | + iniparser_set(dic, MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, "2222"); |
| 1079 | + /* test dictionary */ |
| 1080 | + CuAssertIntEquals(tc, 2222, iniparser_getint(dic, |
| 1081 | + MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, -1)); |
| 1082 | + ini = fopen(TMP_INI_PATH, "w+"); |
| 1083 | + |
| 1084 | + if (!ini) { |
| 1085 | + fprintf(stderr, "iniparser: cannot open %s\n", TMP_INI_PATH); |
| 1086 | + goto del_dic; |
| 1087 | + } |
| 1088 | + |
| 1089 | + iniparser_dump_ini(dic, ini); |
| 1090 | + fclose(ini); |
| 1091 | + dictionary_del(dic); |
| 1092 | + /* check if section has been written as expected */ |
| 1093 | + dic = iniparser_load(TMP_INI_PATH); |
| 1094 | + |
| 1095 | + if (!dic) { |
| 1096 | + fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH); |
| 1097 | + goto rm_ini; |
| 1098 | + } |
| 1099 | + |
| 1100 | + CuAssertIntEquals(tc, 2222, iniparser_getint(dic, |
| 1101 | + MISFORMED_INI_SEC0 ":" MISFORMED_INI_ATTR, -1)); |
| 1102 | + dictionary_del(dic); |
| 1103 | + ret = remove(TMP_INI_PATH); |
| 1104 | + |
| 1105 | + if (ret) { |
| 1106 | + fprintf(stderr, "cannot remove file: %s\n", TMP_INI_PATH); |
| 1107 | + return; |
| 1108 | + } |
| 1109 | + |
| 1110 | + create_empty_ini_file(TMP_INI_PATH); |
| 1111 | + dic = iniparser_load(TMP_INI_PATH); |
| 1112 | + |
| 1113 | + if (!dic) { |
| 1114 | + fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH); |
| 1115 | + return; |
| 1116 | + } |
| 1117 | + |
| 1118 | + ret = iniparser_set(dic, MISFORMED_INI_SEC1, NULL); |
| 1119 | + |
| 1120 | + if (ret < 0) { |
| 1121 | + fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC1, |
| 1122 | + TMP_INI_PATH); |
| 1123 | + goto del_dic; |
| 1124 | + } |
| 1125 | + |
| 1126 | + iniparser_set(dic, MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, "2222"); |
| 1127 | + /* test dictionary */ |
| 1128 | + CuAssertIntEquals(tc, 2222, iniparser_getint(dic, |
| 1129 | + MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, -1)); |
| 1130 | + ini = fopen(TMP_INI_PATH, "w+"); |
| 1131 | + |
| 1132 | + if (!ini) { |
| 1133 | + fprintf(stderr, "iniparser: cannot open %s\n", TMP_INI_PATH); |
| 1134 | + goto del_dic; |
| 1135 | + } |
| 1136 | + |
| 1137 | + iniparser_dump_ini(dic, ini); |
| 1138 | + fclose(ini); |
| 1139 | + dictionary_del(dic); |
| 1140 | + /* check if section has been written as expected */ |
| 1141 | + dic = iniparser_load(TMP_INI_PATH); |
| 1142 | + |
| 1143 | + if (!dic) { |
| 1144 | + fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH); |
| 1145 | + return; |
| 1146 | + } |
| 1147 | + |
| 1148 | + CuAssertIntEquals(tc, 2222, iniparser_getint(dic, |
| 1149 | + MISFORMED_INI_SEC1 ":" MISFORMED_INI_ATTR, -1)); |
| 1150 | + dictionary_del(dic); |
| 1151 | + ret = remove(TMP_INI_PATH); |
| 1152 | + |
| 1153 | + if (ret) { |
| 1154 | + fprintf(stderr, "cannot remove file: %s\n", TMP_INI_PATH); |
| 1155 | + return; |
| 1156 | + } |
| 1157 | + |
| 1158 | + create_empty_ini_file(TMP_INI_PATH); |
| 1159 | + dic = iniparser_load(TMP_INI_PATH); |
| 1160 | + |
| 1161 | + if (!dic) { |
| 1162 | + fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH); |
| 1163 | + return; |
| 1164 | + } |
| 1165 | + |
| 1166 | + ret = iniparser_set(dic, MISFORMED_INI_SEC2, NULL); |
| 1167 | + |
| 1168 | + if (ret < 0) { |
| 1169 | + fprintf(stderr, "cannot set section %s in: %s\n", MISFORMED_INI_SEC2, |
| 1170 | + TMP_INI_PATH); |
| 1171 | + goto del_dic; |
| 1172 | + } |
| 1173 | + |
| 1174 | + iniparser_set(dic, MISFORMED_INI_SEC2 ":" MISFORMED_INI_ATTR, "2222"); |
| 1175 | + /* test dictionary */ |
| 1176 | + CuAssertIntEquals(tc, 2222, iniparser_getint(dic, |
| 1177 | + MISFORMED_INI_SEC2 ":" MISFORMED_INI_ATTR, -1)); |
| 1178 | + ini = fopen(TMP_INI_PATH, "w+"); |
| 1179 | + |
| 1180 | + if (!ini) { |
| 1181 | + fprintf(stderr, "iniparser: cannot open %s\n", TMP_INI_PATH); |
| 1182 | + goto del_dic; |
| 1183 | + } |
| 1184 | + |
| 1185 | + iniparser_dump_ini(dic, ini); |
| 1186 | + fclose(ini); |
| 1187 | + dictionary_del(dic); |
| 1188 | + /* check if section has been written as expected */ |
| 1189 | + dic = iniparser_load(TMP_INI_PATH); |
| 1190 | + |
| 1191 | + if (!dic) { |
| 1192 | + fprintf(stderr, "cannot parse file: %s\n", TMP_INI_PATH); |
| 1193 | + return; |
| 1194 | + } |
| 1195 | + |
| 1196 | + CuAssertIntEquals(tc, 2222, iniparser_getint(dic, |
| 1197 | + MISFORMED_INI_SEC2 ":" MISFORMED_INI_ATTR, -1)); |
| 1198 | +del_dic: |
| 1199 | + dictionary_del(dic); |
| 1200 | +rm_ini: |
| 1201 | + ret = remove(TMP_INI_PATH); |
| 1202 | + |
| 1203 | + if (ret) { |
| 1204 | + fprintf(stderr, "cannot remove file: %s\n", TMP_INI_PATH); |
| 1205 | + return; |
| 1206 | + } |
| 1207 | +} |
| 1208 | + |
1052 | 1209 | void Test_iniparser_quotes(CuTest *tc)
|
1053 | 1210 | {
|
1054 | 1211 | dictionary *dic;
|
|
0 commit comments