home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libreg / tests / regtest.c < prev   
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.0 KB  |  439 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <io.h>
  22. #include <fcntl.h>
  23. #include <sys/stat.h>
  24. #include <string.h>
  25.  
  26. #include "NSReg.h"
  27. #include "VerReg.h"
  28.  
  29. extern void interp(void);
  30.  
  31. #define REGFILE "c:\\temp\\reg.dat"
  32.  
  33. char *gRegistry;
  34.  
  35. int main(int argc, char *argv[]);
  36.  
  37. char *errstr(REGERR err)
  38. {
  39.  
  40.     switch( err )
  41.     {
  42.     case REGERR_OK:
  43.         return "REGERR_OK";
  44.     case REGERR_FAIL:
  45.         return "REGERR_FAIL";
  46.     case REGERR_NOMORE:
  47.         return "REGERR_MORE";
  48.     case REGERR_NOFIND:
  49.         return "REGERR_NOFIND";
  50.     case REGERR_BADREAD:
  51.         return "REGERR_BADREAD";
  52.     case REGERR_BADLOCN:
  53.         return "REGERR_BADLOCN";
  54.     case REGERR_PARAM:
  55.         return "REGERR_PARAM";
  56.     case REGERR_BADMAGIC:
  57.         return "REGERR_BADMAGIC";
  58.     default:
  59.         return "<Unknown>";
  60.     }
  61.  
  62. }    // errstr
  63.  
  64.  
  65. int CreateEmptyRegistry(void)
  66. {
  67.  
  68. #if 0
  69.     int fh;
  70.     remove(REGFILE);    // ignore errors like file not found
  71.  
  72.     fh = _open(REGFILE, _O_CREAT, _S_IREAD|_S_IWRITE);
  73.     if (fh < 0)
  74.         return -1;
  75.     close(fh);
  76.     return 0;
  77. #endif
  78.  
  79.     return VR_CreateRegistry(CR_NEWREGISTRY, "4.0");
  80.  
  81. }    // CreateEmptyRegistry
  82.         
  83.  
  84.  
  85. int BuildTree(void)
  86. {
  87.  
  88.     REGERR err;
  89.  
  90.     err = NR_RegAdd(0,"/Machine/Old");
  91.     if (err != REGERR_OK)
  92.     {
  93.         printf("NR_RegAdd() returned %s.\n", errstr(err));
  94.         return err;
  95.     }
  96.     err = NR_RegAdd(0,"/User");
  97.     if (err != REGERR_OK)
  98.     {
  99.         printf("NR_RegAdd() returned %s.\n", errstr(err));
  100.         return err;
  101.     }
  102.     err = NR_RegAdd(0,"/Machine/4.0/Name1=Val1");
  103.     if (err != REGERR_OK)
  104.     {
  105.         printf("NR_RegAdd() returned %s.\n", errstr(err));
  106.         return err;
  107.     }
  108.     err = NR_RegAdd(0,"/Machine/4.0/Name2=Val2");
  109.     if (err != REGERR_OK)
  110.     {
  111.         printf("NR_RegAdd() returned %s.\n", errstr(err));
  112.         return err;
  113.     }
  114.     err = NR_RegAdd(0,"/Machine/4.0/Name2=Val3");
  115.     if (err != REGERR_OK)
  116.     {
  117.         printf("NR_RegAdd() returned %s.\n", errstr(err));
  118.         return err;
  119.     }
  120.     err = NR_RegAdd(0,"/Machine/4.0/Name3=Val4");
  121.     if (err != REGERR_OK)
  122.     {
  123.         printf("NR_RegAdd() returned %s.\n", errstr(err));
  124.         return err;
  125.     }
  126.  
  127.     return VR_Checkpoint();
  128.  
  129. }    // BuildTree
  130.  
  131.  
  132. int FindKeys(void)
  133. {
  134.  
  135.     RKEY key;
  136.     REGERR err;
  137.     char buf[80];
  138.  
  139.     if (NR_RegGetKey(0, "", &key) == REGERR_OK)
  140.     {
  141.         printf("NR_RegGetKey returns ok for an empty path.\n");
  142.         return 1;
  143.     }
  144.  
  145.     if (NR_RegGetKey(0, "/", &key) != REGERR_OK)
  146.     {
  147.         printf("NR_RegGetKey couldn't find root.\n");
  148.         return 1;
  149.     }
  150.  
  151.     if (NR_RegGetKey(0, "/Machine/Old", &key) != REGERR_OK)
  152.     {
  153.         printf("NR_RegGetKey couldn't find Old\n");
  154.         return 1;
  155.     }
  156.     printf("NR_RegGetKey returns key for Old as: 0x%lx\n", (long) key);
  157.  
  158.     if (NR_RegGetKey(0, "/Machine/4.0", &key) != REGERR_OK)
  159.     {
  160.         printf("NR_RegGetKey couldn't find 4.0\n");
  161.         return 1;
  162.     }
  163.     printf("NR_RegGetKey returns key for 4.0 as: 0x%lx\n", (long) key);
  164.  
  165.     // ----------------------------------------
  166.     if ((err = NR_RegFindValue(0, "/Machine/4.0/Name3", 64, buf)) != REGERR_OK)
  167.     {
  168.         printf("NR_RegFindValue (no key) returns %s\n", errstr(err));
  169.         return 1;
  170.     }
  171.     printf("NR_RegFindValue (no key) of Name3 = %s\n", buf);
  172.  
  173.     if (NR_RegFindValue(key, "Aliens", 64, buf) == REGERR_OK)
  174.     {
  175.         printf("NR_RegFindValue finds Aliens.\n");
  176.         return 1;
  177.     }
  178.  
  179.     if ((err = NR_RegFindValue(key, "Name3", 64, buf)) != REGERR_OK)
  180.     {
  181.         printf("NR_RegFindValue (w/key) returns %s\n", errstr(err));
  182.         return 1;
  183.     }
  184.     printf("NR_RegFindValue (w/key) of Name3 = %s\n", buf);
  185.  
  186.     return 0;
  187.  
  188. }    // FindTree
  189.  
  190. int DumpTree(void)
  191. {
  192.  
  193.     char *path;
  194.     char *line = "------------------------------------------------------------";
  195.  
  196.     path = malloc(2048);
  197.     if (!path)
  198.         return REGERR_FAIL;
  199.  
  200.     strcpy(path, "/");
  201.     puts(line);
  202.     puts(path);
  203.  
  204.     while (NR_RegNext( 0, 512, path ) == REGERR_OK)
  205.     {
  206.         puts(path);
  207.     }
  208.  
  209.     puts(line);
  210.  
  211.     return 0;
  212.  
  213. }    // DumpTree
  214.  
  215.  
  216. int ChangeKeys(void)
  217. {
  218.  
  219.     REGERR err;
  220.  
  221.     err = NR_RegUpdate(0,"/Machine/4.0/name3", "Infospect Software, Inc.");
  222.     if (err)
  223.     {
  224.         printf("Couldn't update name3's value to ISI.\n");
  225.         return err;
  226.     }
  227.  
  228.     err = NR_RegUpdate(0,"/Machine/4.0/name3=Infospect Software, Inc.", "\"Jonathan=Kid1\"");
  229.     if (err)
  230.     {
  231.         printf("Couldn't update name3's value to Jon.\n");
  232.         return err;
  233.     }
  234.  
  235.     err = NR_RegRename(0,"/Machine/4.0/name3=\"Jonathan=Kid1\"", "First born");
  236.     if (err)
  237.     {
  238.         printf("Couldn't update name3's name to First born.\n");
  239.         return err;
  240.     }
  241.  
  242.     err = NR_RegUpdate(0,"/Machine/4.0/name2=Val2", "Kelley Ann");
  243.     if (err)
  244.     {
  245.         printf("Couldn't update name2's value to Kelley.\n");
  246.         return err;
  247.     }
  248.  
  249.  
  250.     return VR_Checkpoint();
  251.  
  252. }    // ChangeKeys
  253.  
  254.  
  255. int DeleteKeys(void)
  256. {
  257.  
  258.     REGERR err;
  259.  
  260.     err = NR_RegDelete(0, "/User");
  261.     if (err)
  262.     {
  263.         printf("NR_RegDelete returned %s.\n", errstr(err));
  264.         return err;
  265.     }
  266.  
  267.     return VR_Checkpoint();
  268.  
  269. }    // DeleteKeys
  270.  
  271.  
  272. int StressTest(void)
  273. {
  274.  
  275.     REGERR err;
  276.     RKEY key;
  277.  
  278.     printf("Starting stress...\n");
  279.  
  280.     err = NR_RegGetKey(0, "/Machine/4.0", &key);
  281.     if (err)
  282.     {
  283.         printf("Error getting key for 4.0 = %s\n", errstr(err));
  284.         return err;
  285.     }
  286.  
  287.     err = NR_RegAdd(key, "A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z/"
  288.         "A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z/"
  289.         "A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z/" );
  290.  
  291.     if (err)
  292.     {
  293.         printf("Adding humungous string returned %s\n", errstr(err));
  294.         return err;
  295.     }
  296.  
  297.     // TODO: Add a value to one of the middle keys, get it back.
  298.  
  299.     printf("Stress done.\n");
  300.     return 0;
  301.  
  302. }    // StressTest
  303.  
  304.  
  305. int Install(void)
  306. {
  307.  
  308.     int err;
  309.     VERSION ver;
  310.  
  311.     ver.major = 4;
  312.     ver.minor = 2;
  313.     ver.release = 10;
  314.     ver.build = 937;
  315.     ver.check = 0;
  316.  
  317.     err = VR_Install("Web/Navigator/netscape.exe", 
  318.         "c:\\Netscape\\NETSCAPE.EXE", &ver);
  319.     if (err)
  320.         return err;
  321.  
  322.     ver.release = 19;
  323.     ver.build = 722;
  324.     ver.check = 0;
  325.     err = VR_Install("Web/Navigator/nspr.dll", 
  326.         "c:\\Netscape\\System\\Vtcprac.386", &ver);
  327.  
  328.     if (err)
  329.         return err;
  330.  
  331.     return VR_Checkpoint();
  332.  
  333. }
  334.  
  335. int GetInfo(void)
  336. {
  337.  
  338.     int err; 
  339.     char buf[256];
  340.     VERSION ver;
  341.  
  342.     err = VR_GetPath("Web/Navigator/nspr.dll", 256, buf);
  343.     if (err)
  344.         return err;
  345.  
  346.     printf("GetPath(nspr.dll) returns %s\n", buf);
  347.  
  348.     err = VR_GetVersion("Web/Navigator/netscape.exe", &ver);
  349.     if (err)
  350.         return err;
  351.     printf("GetVersion(netscape.exe) returns %d.%d.%d.%d and check=%d\n",
  352.         ver.major, ver.minor, ver.release, ver.build, ver.check);
  353.  
  354.     return 0;
  355.  
  356. }
  357.  
  358.  
  359. int main(int argc, char *argv[])
  360. {
  361.  
  362.     printf("Registry Test 10/01/96.\n");
  363.  
  364.     if (argc > 1)
  365.     {
  366.         gRegistry = argv[1];
  367.     }
  368.     else
  369.     {
  370.         gRegistry = REGFILE;
  371.     }
  372.     VR_RegistryName(gRegistry);
  373.  
  374. #if 1
  375.     if (NR_RegOpen(gRegistry) != REGERR_OK)
  376.         VR_CreateRegistry(CR_NEWREGISTRY, "4.0");
  377.     interp();
  378. #else
  379.     if (CreateEmptyRegistry())
  380.         goto abort;
  381.  
  382.     if (Install())
  383.         goto done;
  384.  
  385.     if (DumpTree())
  386.         goto done;
  387.  
  388.     if (GetInfo())
  389.         goto done;
  390.  
  391.  
  392. #if defined(TEST_NR)
  393.     if ((err = NR_RegOpen(REGFILE)) != REGERR_OK)
  394.     {
  395.         printf("NR_RegOpen(%s) returned %s...Test aborted.\n", REGFILE, errstr(err));
  396.         goto abort;
  397.     }
  398.  
  399.     if (BuildTree())
  400.         goto done;
  401.  
  402.     if (FindKeys())
  403.         goto done;
  404.  
  405.     if (DumpTree())
  406.         goto done;
  407.  
  408.     if (ChangeKeys())
  409.         goto done;
  410.  
  411.     if (DeleteKeys())
  412.         goto done;
  413.  
  414.     if (DumpTree())
  415.         goto done;
  416.  
  417.     if (StressTest())
  418.         goto done;
  419.  
  420.     if (DumpTree())
  421.         goto done;
  422.  
  423. done:
  424.     NR_RegClose();
  425. #else
  426. done:
  427. #endif
  428.  
  429. abort:
  430.     puts("Press Enter to continue...");
  431.     getchar();
  432. #endif
  433.  
  434.     return 0;
  435.  
  436. }
  437.  
  438.  
  439.