1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/strings/string_piece.h"
6 #include "base/utf_string_conversions.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "webkit/base/origin_url_conversions.h"
9 #include "webkit/database/database_util.h"
11 using webkit_database::DatabaseUtil
;
13 static void TestVfsFilePath(bool expected_result
,
14 const char* vfs_file_name
,
15 const char* expected_origin_identifier
= "",
16 const char* expected_database_name
= "",
17 const char* expected_sqlite_suffix
= "") {
18 base::string16 origin_identifier
;
19 base::string16 database_name
;
20 base::string16 sqlite_suffix
;
21 EXPECT_EQ(expected_result
,
22 DatabaseUtil::CrackVfsFileName(ASCIIToUTF16(vfs_file_name
),
26 EXPECT_EQ(ASCIIToUTF16(expected_origin_identifier
), origin_identifier
);
27 EXPECT_EQ(ASCIIToUTF16(expected_database_name
), database_name
);
28 EXPECT_EQ(ASCIIToUTF16(expected_sqlite_suffix
), sqlite_suffix
);
31 static GURL
ToAndFromOriginIdentifier(const GURL origin_url
) {
32 base::string16 id
= webkit_base::GetOriginIdentifierFromURL(origin_url
);
33 return webkit_base::GetOriginURLFromIdentifier(id
);
36 static void TestValidOriginIdentifier(bool expected_result
,
37 const base::StringPiece id
) {
38 EXPECT_EQ(expected_result
,
39 DatabaseUtil::IsValidOriginIdentifier(ASCIIToUTF16(id
)));
42 namespace webkit_database
{
44 // Test DatabaseUtil::CrackVfsFilePath on various inputs.
45 TEST(DatabaseUtilTest
, CrackVfsFilePathTest
) {
46 TestVfsFilePath(true, "origin/#", "origin", "", "");
47 TestVfsFilePath(true, "origin/#suffix", "origin", "", "suffix");
48 TestVfsFilePath(true, "origin/db_name#", "origin", "db_name", "");
49 TestVfsFilePath(true, "origin/db_name#suffix", "origin", "db_name", "suffix");
50 TestVfsFilePath(false, "origindb_name#");
51 TestVfsFilePath(false, "origindb_name#suffix");
52 TestVfsFilePath(false, "origin/db_name");
53 TestVfsFilePath(false, "origin#db_name/suffix");
54 TestVfsFilePath(false, "/db_name#");
55 TestVfsFilePath(false, "/db_name#suffix");
58 TEST(DatabaseUtilTest
, OriginIdentifiers
) {
59 const GURL
kFileOrigin(GURL("file:///").GetOrigin());
60 const GURL
kHttpOrigin(GURL("http://bar/").GetOrigin());
61 EXPECT_EQ(kFileOrigin
, ToAndFromOriginIdentifier(kFileOrigin
));
62 EXPECT_EQ(kHttpOrigin
, ToAndFromOriginIdentifier(kHttpOrigin
));
65 TEST(DatabaseUtilTest
, IsValidOriginIdentifier
) {
66 TestValidOriginIdentifier(true, "http_bar_0");
67 TestValidOriginIdentifier(true, "");
68 TestValidOriginIdentifier(false, "bad..id");
69 TestValidOriginIdentifier(false, "bad/id");
70 TestValidOriginIdentifier(false, "bad\\id");
71 TestValidOriginIdentifier(false, base::StringPiece("bad\0id", 6));
74 } // namespace webkit_database