vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / locale / catalogTest.cpp
blobd64c910e86ac0de942d473590bd2809122853217
1 /*
2 ** Copyright 2003, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved.
3 ** Distributed under the terms of the MIT License.
4 */
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
11 #include <Application.h>
12 #include <Catalog.h>
13 #include <DefaultCatalog.h>
14 #include <Entry.h>
15 #include <Locale.h>
16 #include <Path.h>
17 #include <Roster.h>
19 class CatalogTest {
20 public:
21 void Run();
22 void Check();
25 #define B_TRANSLATION_CONTEXT "CatalogTest"
28 #define catSig "x-vnd.Be.locale.catalogTest"
29 #define catName catSig".catalog"
31 void
32 CatalogTest::Run()
34 printf("app...");
35 status_t res;
36 BString s;
37 s << "string" << "\x01" << B_TRANSLATION_CONTEXT << "\x01";
38 size_t hashVal = CatKey::HashFun(s.String());
39 assert(be_locale != NULL);
40 system("mkdir -p ./locale/catalogs/"catSig);
42 // create an empty catalog of default type...
43 BPrivate::EditableCatalog cata("Default", catSig, "German");
44 assert(cata.InitCheck() == B_OK);
46 // ...and populate the catalog with some data:
47 res = cata.SetString("string", "Schnur", B_TRANSLATION_CONTEXT);
48 assert(res == B_OK);
49 res = cata.SetString(hashVal, "Schnur_id");
50 // add a second entry for the same hash-value, but with different
51 // translation
52 assert(res == B_OK);
53 res = cata.SetString("string", "String", "programming");
54 assert(res == B_OK);
55 res = cata.SetString("string", "Textpuffer", "programming",
56 "Deutsches Fachbuch");
57 assert(res == B_OK);
58 res = cata.SetString("string", "Leine", B_TRANSLATION_CONTEXT,
59 "Deutsches Fachbuch");
60 assert(res == B_OK);
61 res = cata.WriteToFile("./locale/catalogs/"catSig"/german.catalog");
62 assert(res == B_OK);
64 // check if we are getting back the correct strings:
65 s = cata.GetString(("string"), B_TRANSLATION_CONTEXT);
66 assert(s == "Schnur");
67 s = cata.GetString(hashVal);
68 assert(s == "Schnur_id");
69 s = cata.GetString("string", "programming");
70 assert(s == "String");
71 s = cata.GetString("string", "programming", "Deutsches Fachbuch");
72 assert(s == "Textpuffer");
73 s = cata.GetString("string", B_TRANSLATION_CONTEXT, "Deutsches Fachbuch");
74 assert(s == "Leine");
76 // now we create a new (base) catalog and embed this one into the app-file:
77 BPrivate::EditableCatalog catb("Default", catSig, "English");
78 assert(catb.InitCheck() == B_OK);
79 // the following string is unique to the embedded catalog:
80 res = catb.SetString("string", "string", "base");
81 assert(res == B_OK);
82 // the following id is unique to the embedded catalog:
83 res = catb.SetString(32, "hashed string");
84 assert(res == B_OK);
85 // the following string will be hidden by the definition inside the
86 // german catalog:
87 res = catb.SetString("string", "hidden", B_TRANSLATION_CONTEXT);
88 assert(res == B_OK);
89 app_info appInfo;
90 res = be_app->GetAppInfo(&appInfo);
91 assert(res == B_OK);
92 // embed created catalog into application file (catalogTest):
93 res = catb.WriteToResource(&appInfo.ref);
94 assert(res == B_OK);
96 printf("ok.\n");
97 Check();
101 void
102 CatalogTest::Check()
104 status_t res;
105 printf("app-check...");
106 BString s;
107 s << "string" << "\x01" << B_TRANSLATION_CONTEXT << "\x01";
108 size_t hashVal = CatKey::HashFun(s.String());
109 // ok, we now try to re-load the catalog that has just been written:
111 // actually, the following code can be seen as an example of what an
112 // app needs in order to translate strings:
113 BCatalog cat;
114 res = be_locale->GetAppCatalog(&cat);
115 assert(res == B_OK);
116 // fetch basic data:
117 uint32 fingerprint = 0;
118 res = cat.GetFingerprint(&fingerprint);
119 assert(res == B_OK);
120 BString lang;
121 res = cat.GetLanguage(&lang);
122 assert(res == B_OK);
123 BString sig;
124 res = cat.GetSignature(&sig);
125 assert(res == B_OK);
127 // now check strings:
128 s = B_TRANSLATE_ID(hashVal);
129 assert(s == "Schnur_id");
130 s = B_TRANSLATE_ALL("string", "programming", "");
131 assert(s == "String");
132 s = B_TRANSLATE_ALL("string", "programming", "Deutsches Fachbuch");
133 assert(s == "Textpuffer");
134 s = B_TRANSLATE_COMMENT("string", "Deutsches Fachbuch");
135 assert(s == "Leine");
136 // the following string should be found in the embedded catalog only:
137 s = B_TRANSLATE_ALL("string", "base", NULL);
138 assert(s == "string");
139 // the following id should be found in the embedded catalog only:
140 s = B_TRANSLATE_ID(32);
141 assert(s == "hashed string");
142 // the following id doesn't exist anywhere (hopefully):
143 s = B_TRANSLATE_ID(-1);
144 assert(s == "");
145 // the following string exists twice, in the embedded as well as in the
146 // external catalog. So we should get the external translation (as it should
147 // override the embedded one):
148 s = B_TRANSLATE("string");
149 assert(s == "Schnur");
151 // now check if trying to access same catalog by specifying its data works:
152 BCatalog cat2(sig.String(), lang.String(), fingerprint);
153 assert(cat2.InitCheck() == B_OK);
154 // now check if trying to access same catalog with wrong fingerprint fails:
155 BCatalog cat3(sig.String(), lang.String(), fingerprint*-1);
156 assert(cat3.InitCheck() == B_NO_INIT);
157 // translating through an invalid catalog should yield the native string:
158 s = cat3.GetString("string");
159 assert(s == "string");
161 printf("ok.\n");
166 main(int argc, char **argv)
168 BApplication* testApp
169 = new BApplication("application/"catSig);
171 // change to app-folder:
172 app_info appInfo;
173 be_app->GetAppInfo(&appInfo);
174 BEntry appEntry(&appInfo.ref);
175 BEntry appFolder;
176 appEntry.GetParent( &appFolder);
177 BPath appPath;
178 appFolder.GetPath( &appPath);
179 chdir( appPath.Path());
181 CatalogTest catTest;
182 catTest.Run();
184 char cwd[B_FILE_NAME_LENGTH];
185 getcwd(cwd, B_FILE_NAME_LENGTH);
186 BString addonName(cwd);
187 addonName << "/" "catalogTestAddOn";
188 image_id image = load_add_on(addonName.String());
189 assert(image >= B_OK);
190 void (*runAddonFunc)() = 0;
191 get_image_symbol(image, "run_test_add_on",
192 B_SYMBOL_TYPE_TEXT, (void **)&runAddonFunc);
193 assert(runAddonFunc);
194 runAddonFunc();
196 catTest.Check();
198 delete testApp;