home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / ntcode / ntperlb / t / nt / registry.nt next >
Encoding:
Text File  |  1995-05-19  |  3.3 KB  |  169 lines

  1. print "1..14\n";
  2.  
  3. unshift(@INC, "d:\\perl-4.036\\lib");
  4.  
  5. require "registry.pl";
  6.  
  7. #
  8. # test the NT Registry access functions
  9. #
  10.  
  11. # create a key
  12.  
  13. $subkey_string = "SOFTWARE\\Intergraph\\PerlTest";
  14.  
  15. if (&RegOpenKeyEx ($HKEY_LOCAL_MACHINE, $subkey_string, 0, 
  16.            $KEY_ALL_ACCESS, $key)) {
  17.     &DeleteAllKeys($key);
  18.     &RegCloseKey($key);
  19.     &RegDeleteKey($HKEY_LOCAL_MACHINE, $subkey_string) ||
  20.     die "Can't delete $subkey_string: $!\n";
  21. }
  22.  
  23. $return = &RegCreateKeyEx($HKEY_LOCAL_MACHINE, $subkey_string, 0, "testclass", 
  24.               $REG_OPTION_NON_VOLATILE, $KEY_ALL_ACCESS, 0, $key, 
  25.               $disposition);
  26. if ($return != 1) {
  27.     print "not ok 1 # $!\n";
  28.     exit(0);
  29. }
  30. print "ok 1\n";
  31.  
  32. # set a value
  33.  
  34. $return = &RegSetValueEx ($key, "Test1", 0, $REG_SZ, "foobar");
  35. if ($return != 1) {
  36.     print "not ok 2\n";
  37.     exit(0);
  38. }
  39. print "ok 2\n";
  40.  
  41. $return = &RegCloseKey ($key);
  42. if ($return != 1) {
  43.     print "not ok 3\n";
  44.     exit(0);
  45. }
  46. print "ok 3\n";
  47.  
  48. $return = &RegOpenKey($HKEY_LOCAL_MACHINE, $subkey_string, $key);
  49. if ($return != 1) {
  50.     print "not ok 4\n";
  51.     exit(0);
  52. }
  53. print "ok 4\n";
  54.  
  55. $return = &RegQueryValue ($key, "Test1", $val);
  56. if ($return != 1) {
  57.     print "not ok 5\n";
  58.     exit(0);
  59. }
  60. print "ok 5\n";
  61.  
  62. $return = &RegSaveKey($key, "save.$$");
  63. # check for privledge violation
  64. if ($return != 0 && $! != 1314) {
  65.     print "not ok 6\n";
  66.     exit(0);
  67. }
  68. print "ok 6\n";
  69.  
  70. unlink "save.$$" if -e "save.$$";
  71.  
  72. &RegCloseKey($key);
  73.     
  74. if ($val eq "foobar") {
  75.     print "ok 7\n";
  76. else {
  77.     print "not ok 7\n";
  78. }
  79.  
  80. $return = &RegCreateKey($HKEY_LOCAL_MACHINE, $subkey_string, $key);
  81. if ($return != 1) {
  82.     print "not ok 8\n";
  83.     exit(0);
  84. }
  85. print "ok 8\n";
  86.  
  87. $return = &RegDeleteKey($HKEY_LOCAL_MACHINE, $subkey_string);
  88. if ($return != 1) {
  89.     print "not ok 9\n";
  90.     exit(0);
  91. }
  92. print "ok 9\n";
  93.     
  94. #
  95. # shouldn't be able to open it now
  96. #
  97.  
  98. $return = &RegOpenKey($HKEY_LOCAL_MACHINE, 
  99.               "SOFTWARE\\Intergraph\\PerlTest", $key);
  100. if ($return == 0) {print "ok 10\n";} else {print "not ok 10\n";}
  101.  
  102.  
  103. #
  104. # Now make a  few keys and values, then test the enumerating functions
  105. #
  106.  
  107. $return = &RegCreateKey($HKEY_LOCAL_MACHINE, $subkey_string, $key);
  108. if ($return != 1) {
  109.     print "not ok 11\n";
  110.     exit(0);
  111. }
  112. print "ok 11\n";
  113.  
  114. $return = &RegCreateKey($key, "Key1", $nkey);
  115. if ($return != 1) {
  116.     print "not ok 12\n";
  117.     exit(0);
  118. }
  119. print "ok 12\n";
  120. &RegCloseKey($nkey);
  121.  
  122. $return = &RegCreateKey($key, "Key2", $nkey);
  123. if ($return != 1) {
  124.     print "not ok 13\n";
  125.     exit(0);
  126. }
  127. print "ok 13\n";
  128. &RegCloseKey($nkey);
  129.  
  130. $idx = 0;
  131. $cnt = 0;
  132.  
  133. while(&RegEnumKeyEx($key, $idx++, $name,0, $class, $time)) {
  134.     if ($name ne "Key$idx") {
  135.     print "not ok 14\n";
  136.     exit;
  137.     }
  138.     &RegDeleteKey($key, $name);
  139. }
  140. print "ok 14\n";
  141.  
  142. #
  143. # clean up
  144. #
  145.  
  146. &DeleteAllKeys($key);
  147. &RegCloseKey($key);
  148. &RegDeleteKey($HKEY_LOCAL_MACHINE, $subkey_string);
  149.  
  150. exit;
  151.  
  152. sub DeleteAllKeys {
  153.     local($key) = @_;
  154.     local($idx) = 0;
  155.     local($newkey, $name);
  156.  
  157.     $idx = 0;
  158.     while (&RegEnumKey($key, $idx++, $name)) {
  159.     &RegOpenKeyEx($key, $name, 0, $KEY_ALL_ACCESS, $newkey) ||
  160.         die "Can't open $name subkey: $!\n";
  161.     &DeleteAllKeys($newkey);
  162.     &RegCloseKey($newkey);
  163.     &RegDeleteKey($key, $name) ||
  164.         die "Can't delete key $name: $!\n";
  165.     }
  166. }
  167.     
  168.