tregistry2.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. { %TARGET=win32,win64,wince,linux,solaris,openbsd }
  2. {
  3. This unit tests mostly TRegIniFile to work properly and be Delphi compatible.
  4. This test also runs on non-Windows platforms where XML registry is used.
  5. Please keep this test Delphi compatible.
  6. }
  7. {$ifdef FPC} {$mode delphi} {$endif}
  8. uses
  9. {$ifdef unix}
  10. cwstring,
  11. {$endif unix}
  12. SysUtils, Classes, registry;
  13. {$ifdef FPC}
  14. {$WARN implicit_string_cast_loss off}
  15. {$WARN symbol_deprecated off}
  16. {$endif FPC}
  17. const
  18. STestRegPath = 'Software\FPC-RegTest';
  19. procedure TestFailed(ErrCode: integer);
  20. begin
  21. writeln('Test FAILED. Error code: ' + IntToStr(ErrCode));
  22. Halt(ErrCode);
  23. end;
  24. procedure ClearReg(const KeyName: string = '');
  25. begin
  26. with TRegistry.Create do
  27. try
  28. DeleteKey(STestRegPath);
  29. finally
  30. Free;
  31. end;
  32. end;
  33. function NormPath(const s: string): string;
  34. begin
  35. Result:=StringReplace(s, '/', '\', [rfReplaceAll]);
  36. end;
  37. procedure DoRegTest2;
  38. var
  39. reg: TRegistry;
  40. ri: TRegIniFile;
  41. rini: TRegistryIniFile;
  42. sl: TStringList;
  43. begin
  44. ClearReg;
  45. try
  46. reg:=TRegistry.Create;
  47. try
  48. { The test key must be deleted by ClearReg() }
  49. if reg.KeyExists(STestRegPath) then
  50. TestFailed(1);
  51. if reg.OpenKey(STestRegPath, False) then
  52. TestFailed(2);
  53. if not reg.OpenKey(STestRegPath, True) then
  54. TestFailed(5);
  55. if NormPath(reg.CurrentPath) <> STestRegPath then
  56. TestFailed(6);
  57. reg.WriteString('Item1', '1');
  58. if not reg.OpenKey('\' + STestRegPath + '\1', True) then
  59. TestFailed(10);
  60. reg.WriteString('Item2', '2');
  61. if NormPath(reg.CurrentPath) <> STestRegPath + '\1' then
  62. TestFailed(15);
  63. reg.CloseKey;
  64. if NormPath(reg.CurrentPath) <> '' then
  65. TestFailed(20);
  66. if reg.KeyExists(STestRegPath + '\' + STestRegPath) then
  67. TestFailed(21);
  68. finally
  69. reg.Free;
  70. end;
  71. ri:=TRegIniFile.Create(STestRegPath);
  72. with ri do
  73. try
  74. if ReadString('', 'Item1', '') <> '1' then
  75. TestFailed(101);
  76. if ReadString('1', 'Item2', '') <> '2' then
  77. TestFailed(105);
  78. if NormPath(ri.CurrentPath) <> STestRegPath then
  79. TestFailed(110);
  80. if ReadString('', 'Item1', '') <> '1' then
  81. TestFailed(115);
  82. if not ValueExists('Item1') then
  83. TestFailed(120);
  84. WriteInteger('1', 'Item3', 3);
  85. sl:=TStringList.Create;
  86. try
  87. ReadSectionValues('1', sl);
  88. if sl.Count <> 2 then
  89. TestFailed(125);
  90. if sl.Values['Item2'] <> '2' then
  91. TestFailed(130);
  92. if sl.Values['Item3'] <> '3' then
  93. TestFailed(135);
  94. finally
  95. sl.Free;
  96. end;
  97. WriteInteger('', 'Item4', 4);
  98. WriteInteger('', 'Item41', 41);
  99. WriteInteger('', 'Item42', 42);
  100. if GetDataType('Item4') <> rdString then
  101. TestFailed(140);
  102. if ReadString('', 'Item41', '') <> '41' then
  103. TestFailed(141);
  104. if ReadString('', 'Item42', '') <> '42' then
  105. TestFailed(142);
  106. finally
  107. Free;
  108. end;
  109. { \ at the beginning of the path must be accepted }
  110. ri:=TRegIniFile.Create('\' + STestRegPath);
  111. with ri do
  112. try
  113. if ReadString('', 'Item1', '') <> '1' then
  114. TestFailed(145);
  115. finally
  116. Free;
  117. end;
  118. { Write to non-existing key must work }
  119. ri:=TRegIniFile.Create(STestRegPath + '\2\3\4');
  120. with ri do
  121. try
  122. if FileName <> NormPath(CurrentPath) then
  123. TestFailed(147);
  124. if CurrentKey = 0 then
  125. TestFailed(148);
  126. WriteInteger('', 'Item5', 5);
  127. WriteInteger('5', 'Item6', 6);
  128. if ReadInteger('', 'Item5', 0) <> 5 then
  129. TestFailed(150);
  130. if ReadInteger('5', 'Item6', 0) <> 6 then
  131. TestFailed(160);
  132. finally
  133. Free;
  134. end;
  135. rini:=TRegistryIniFile.Create(STestRegPath);
  136. with rini do
  137. try
  138. if ReadString('', 'Item1', '') <> '1' then
  139. TestFailed(201);
  140. { \ is not allowed as a section name }
  141. if ReadString('\', 'Item1', '') = '1' then
  142. TestFailed(202);
  143. if ReadString('1', 'Item2', '') <> '2' then
  144. TestFailed(205);
  145. { Trailing \ is allowed }
  146. if ReadString('1\', 'Item2', '') <> '2' then
  147. TestFailed(206);
  148. if ReadString('', 'Item1', '') <> '1' then
  149. TestFailed(210);
  150. if not ValueExists('', 'Item4') then
  151. TestFailed(215);
  152. if not ValueExists('1', 'Item2') then
  153. TestFailed(220);
  154. if ReadInteger('2\3\4\5', 'Item6', 0) <> 6 then
  155. TestFailed(225);
  156. if ReadInteger('2\3\4', 'Item5', 0) <> 5 then
  157. TestFailed(230);
  158. EraseSection('2');
  159. if SectionExists('2\3') then
  160. TestFailed(245);
  161. if ValueExists('2\3\4', 'Item5') then
  162. TestFailed(240);
  163. WriteString('2\3\4', 'Item10', '10');
  164. if ReadInteger('2\3\4', 'Item10', 0) <> 10 then
  165. TestFailed(245);
  166. { Check access via a full path }
  167. if not SectionExists('\' + STestRegPath) then
  168. TestFailed(250);
  169. if ReadInteger('\2\3\4', 'Item10', 0) = 10 then
  170. TestFailed(255);
  171. if ReadInteger('\' + STestRegPath + '\2\3\4', 'Item10', 0) <> 10 then
  172. TestFailed(260);
  173. finally
  174. Free;
  175. end;
  176. finally
  177. ClearReg;
  178. end;
  179. { Test if all test keys have been deleted by ClearReg() }
  180. reg:=TRegistry.Create;
  181. try
  182. if reg.KeyExists(STestRegPath) then
  183. TestFailed(501);
  184. if reg.OpenKey(STestRegPath, False) then
  185. TestFailed(502);
  186. if reg.OpenKey(STestRegPath + '\2', False) then
  187. TestFailed(503);
  188. finally
  189. reg.Free;
  190. end;
  191. end;
  192. procedure DeleteUserXmlFile;
  193. begin
  194. {$ifdef FPC}
  195. DeleteFile(Includetrailingpathdelimiter(GetAppConfigDir(False))+'reg.xml');
  196. RemoveDir(GetAppConfigDir(False));
  197. {$endif FPC}
  198. end;
  199. begin
  200. try
  201. DoRegTest2;
  202. finally
  203. DeleteUserXmlFile;
  204. end;
  205. end.