OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/files/file_enumerator.h" | 10 #include "base/files/file_enumerator.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "components/history/core/browser/thumbnail_database.h" | 15 #include "components/history/core/browser/thumbnail_database.h" |
16 #include "components/history/core/test/database_test_utils.h" | 16 #include "components/history/core/test/database_test_utils.h" |
17 #include "sql/connection.h" | 17 #include "sql/connection.h" |
18 #include "sql/recovery.h" | 18 #include "sql/recovery.h" |
19 #include "sql/test/scoped_error_ignorer.h" | 19 #include "sql/test/scoped_error_expecter.h" |
20 #include "sql/test/test_helpers.h" | 20 #include "sql/test/test_helpers.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "third_party/sqlite/sqlite3.h" | 22 #include "third_party/sqlite/sqlite3.h" |
23 #include "url/gurl.h" | 23 #include "url/gurl.h" |
24 | 24 |
25 namespace history { | 25 namespace history { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // Blobs for the bitmap tests. These aren't real bitmaps. Golden | 29 // Blobs for the bitmap tests. These aren't real bitmaps. Golden |
(...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
793 | 793 |
794 // Database should be corrupt at the SQLite level. | 794 // Database should be corrupt at the SQLite level. |
795 { | 795 { |
796 sql::Connection raw_db; | 796 sql::Connection raw_db; |
797 EXPECT_TRUE(raw_db.Open(file_name_)); | 797 EXPECT_TRUE(raw_db.Open(file_name_)); |
798 ASSERT_NE("ok", sql::test::IntegrityCheck(&raw_db)); | 798 ASSERT_NE("ok", sql::test::IntegrityCheck(&raw_db)); |
799 } | 799 } |
800 | 800 |
801 // Open the database and access the corrupt index. | 801 // Open the database and access the corrupt index. |
802 { | 802 { |
803 sql::ScopedErrorIgnorer ignore_errors; | 803 sql::test::ScopedErrorExpecter expecter; |
804 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 804 expecter.ExpectError(SQLITE_CORRUPT); |
805 ThumbnailDatabase db(NULL); | 805 ThumbnailDatabase db(NULL); |
806 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); | 806 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); |
807 | 807 |
808 // Data for kPageUrl2 was deleted, but the index entry remains, | 808 // Data for kPageUrl2 was deleted, but the index entry remains, |
809 // this will throw SQLITE_CORRUPT. The corruption handler will | 809 // this will throw SQLITE_CORRUPT. The corruption handler will |
810 // recover the database and poison the handle, so the outer call | 810 // recover the database and poison the handle, so the outer call |
811 // fails. | 811 // fails. |
812 EXPECT_FALSE(db.GetIconMappingsForPageURL(kPageUrl2, NULL)); | 812 EXPECT_FALSE(db.GetIconMappingsForPageURL(kPageUrl2, NULL)); |
813 | 813 |
814 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 814 ASSERT_TRUE(expecter.SawExpectedErrors()); |
815 } | 815 } |
816 | 816 |
817 // Check that the database is recovered at the SQLite level. | 817 // Check that the database is recovered at the SQLite level. |
818 { | 818 { |
819 sql::Connection raw_db; | 819 sql::Connection raw_db; |
820 EXPECT_TRUE(raw_db.Open(file_name_)); | 820 EXPECT_TRUE(raw_db.Open(file_name_)); |
821 ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db)); | 821 ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db)); |
822 | 822 |
823 // Check that the expected tables exist. | 823 // Check that the expected tables exist. |
824 VerifyTablesAndColumns(&raw_db); | 824 VerifyTablesAndColumns(&raw_db); |
(...skipping 15 matching lines...) Expand all Loading... |
840 kLargeSize, | 840 kLargeSize, |
841 sizeof(kBlob1), | 841 sizeof(kBlob1), |
842 kBlob1)); | 842 kBlob1)); |
843 } | 843 } |
844 | 844 |
845 // Corrupt the database again by adjusting the header. | 845 // Corrupt the database again by adjusting the header. |
846 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); | 846 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); |
847 | 847 |
848 // Database is unusable at the SQLite level. | 848 // Database is unusable at the SQLite level. |
849 { | 849 { |
850 sql::ScopedErrorIgnorer ignore_errors; | 850 sql::test::ScopedErrorExpecter expecter; |
851 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 851 expecter.ExpectError(SQLITE_CORRUPT); |
852 sql::Connection raw_db; | 852 sql::Connection raw_db; |
853 EXPECT_TRUE(raw_db.Open(file_name_)); | 853 EXPECT_TRUE(raw_db.Open(file_name_)); |
854 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); | 854 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); |
855 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 855 ASSERT_TRUE(expecter.SawExpectedErrors()); |
856 } | 856 } |
857 | 857 |
858 // Database should be recovered during open. | 858 // Database should be recovered during open. |
859 { | 859 { |
860 sql::ScopedErrorIgnorer ignore_errors; | 860 sql::test::ScopedErrorExpecter expecter; |
861 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 861 expecter.ExpectError(SQLITE_CORRUPT); |
862 ThumbnailDatabase db(NULL); | 862 ThumbnailDatabase db(NULL); |
863 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); | 863 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); |
864 | 864 |
865 EXPECT_FALSE(db.GetIconMappingsForPageURL(kPageUrl2, NULL)); | 865 EXPECT_FALSE(db.GetIconMappingsForPageURL(kPageUrl2, NULL)); |
866 EXPECT_TRUE(CheckPageHasIcon(&db, | 866 EXPECT_TRUE(CheckPageHasIcon(&db, |
867 kPageUrl1, | 867 kPageUrl1, |
868 favicon_base::FAVICON, | 868 favicon_base::FAVICON, |
869 kIconUrl1, | 869 kIconUrl1, |
870 kLargeSize, | 870 kLargeSize, |
871 sizeof(kBlob1), | 871 sizeof(kBlob1), |
872 kBlob1)); | 872 kBlob1)); |
873 | 873 |
874 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 874 ASSERT_TRUE(expecter.SawExpectedErrors()); |
875 } | 875 } |
876 } | 876 } |
877 | 877 |
878 TEST_F(ThumbnailDatabaseTest, Recovery7) { | 878 TEST_F(ThumbnailDatabaseTest, Recovery7) { |
879 // This code tests the recovery module in concert with Chromium's | 879 // This code tests the recovery module in concert with Chromium's |
880 // custom recover virtual table. Under USE_SYSTEM_SQLITE, this is | 880 // custom recover virtual table. Under USE_SYSTEM_SQLITE, this is |
881 // not available. This is detected dynamically because corrupt | 881 // not available. This is detected dynamically because corrupt |
882 // databases still need to be handled, perhaps by Raze(), and the | 882 // databases still need to be handled, perhaps by Raze(), and the |
883 // recovery module is an obvious layer to abstract that to. | 883 // recovery module is an obvious layer to abstract that to. |
884 // TODO(shess): Handle that case for real! | 884 // TODO(shess): Handle that case for real! |
(...skipping 20 matching lines...) Expand all Loading... |
905 // Database should be corrupt at the SQLite level. | 905 // Database should be corrupt at the SQLite level. |
906 { | 906 { |
907 sql::Connection raw_db; | 907 sql::Connection raw_db; |
908 EXPECT_TRUE(raw_db.Open(file_name_)); | 908 EXPECT_TRUE(raw_db.Open(file_name_)); |
909 ASSERT_NE("ok", sql::test::IntegrityCheck(&raw_db)); | 909 ASSERT_NE("ok", sql::test::IntegrityCheck(&raw_db)); |
910 } | 910 } |
911 | 911 |
912 // Open the database and access the corrupt index. Note that this upgrades | 912 // Open the database and access the corrupt index. Note that this upgrades |
913 // the database. | 913 // the database. |
914 { | 914 { |
915 sql::ScopedErrorIgnorer ignore_errors; | 915 sql::test::ScopedErrorExpecter expecter; |
916 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 916 expecter.ExpectError(SQLITE_CORRUPT); |
917 ThumbnailDatabase db(NULL); | 917 ThumbnailDatabase db(NULL); |
918 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); | 918 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); |
919 | 919 |
920 // Data for kPageUrl2 was deleted, but the index entry remains, | 920 // Data for kPageUrl2 was deleted, but the index entry remains, |
921 // this will throw SQLITE_CORRUPT. The corruption handler will | 921 // this will throw SQLITE_CORRUPT. The corruption handler will |
922 // recover the database and poison the handle, so the outer call | 922 // recover the database and poison the handle, so the outer call |
923 // fails. | 923 // fails. |
924 EXPECT_FALSE(db.GetIconMappingsForPageURL(kPageUrl2, NULL)); | 924 EXPECT_FALSE(db.GetIconMappingsForPageURL(kPageUrl2, NULL)); |
925 | 925 |
926 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 926 ASSERT_TRUE(expecter.SawExpectedErrors()); |
927 } | 927 } |
928 | 928 |
929 // Check that the database is recovered at the SQLite level. | 929 // Check that the database is recovered at the SQLite level. |
930 { | 930 { |
931 sql::Connection raw_db; | 931 sql::Connection raw_db; |
932 EXPECT_TRUE(raw_db.Open(file_name_)); | 932 EXPECT_TRUE(raw_db.Open(file_name_)); |
933 ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db)); | 933 ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db)); |
934 | 934 |
935 // Check that the expected tables exist. | 935 // Check that the expected tables exist. |
936 VerifyTablesAndColumns(&raw_db); | 936 VerifyTablesAndColumns(&raw_db); |
(...skipping 15 matching lines...) Expand all Loading... |
952 kLargeSize, | 952 kLargeSize, |
953 sizeof(kBlob1), | 953 sizeof(kBlob1), |
954 kBlob1)); | 954 kBlob1)); |
955 } | 955 } |
956 | 956 |
957 // Corrupt the database again by adjusting the header. | 957 // Corrupt the database again by adjusting the header. |
958 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); | 958 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); |
959 | 959 |
960 // Database is unusable at the SQLite level. | 960 // Database is unusable at the SQLite level. |
961 { | 961 { |
962 sql::ScopedErrorIgnorer ignore_errors; | 962 sql::test::ScopedErrorExpecter expecter; |
963 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 963 expecter.ExpectError(SQLITE_CORRUPT); |
964 sql::Connection raw_db; | 964 sql::Connection raw_db; |
965 EXPECT_TRUE(raw_db.Open(file_name_)); | 965 EXPECT_TRUE(raw_db.Open(file_name_)); |
966 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); | 966 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); |
967 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 967 ASSERT_TRUE(expecter.SawExpectedErrors()); |
968 } | 968 } |
969 | 969 |
970 // Database should be recovered during open. | 970 // Database should be recovered during open. |
971 { | 971 { |
972 sql::ScopedErrorIgnorer ignore_errors; | 972 sql::test::ScopedErrorExpecter expecter; |
973 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 973 expecter.ExpectError(SQLITE_CORRUPT); |
974 ThumbnailDatabase db(NULL); | 974 ThumbnailDatabase db(NULL); |
975 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); | 975 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); |
976 | 976 |
977 EXPECT_FALSE(db.GetIconMappingsForPageURL(kPageUrl2, NULL)); | 977 EXPECT_FALSE(db.GetIconMappingsForPageURL(kPageUrl2, NULL)); |
978 EXPECT_TRUE(CheckPageHasIcon(&db, | 978 EXPECT_TRUE(CheckPageHasIcon(&db, |
979 kPageUrl1, | 979 kPageUrl1, |
980 favicon_base::FAVICON, | 980 favicon_base::FAVICON, |
981 kIconUrl1, | 981 kIconUrl1, |
982 kLargeSize, | 982 kLargeSize, |
983 sizeof(kBlob1), | 983 sizeof(kBlob1), |
984 kBlob1)); | 984 kBlob1)); |
985 | 985 |
986 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 986 ASSERT_TRUE(expecter.SawExpectedErrors()); |
987 } | 987 } |
988 } | 988 } |
989 | 989 |
990 TEST_F(ThumbnailDatabaseTest, Recovery6) { | 990 TEST_F(ThumbnailDatabaseTest, Recovery6) { |
991 // TODO(shess): See comment at top of Recovery test. | 991 // TODO(shess): See comment at top of Recovery test. |
992 if (!sql::Recovery::FullRecoverySupported()) | 992 if (!sql::Recovery::FullRecoverySupported()) |
993 return; | 993 return; |
994 | 994 |
995 // Create an example database without loading into ThumbnailDatabase | 995 // Create an example database without loading into ThumbnailDatabase |
996 // (which would upgrade it). | 996 // (which would upgrade it). |
997 EXPECT_TRUE(CreateDatabaseFromSQL(file_name_, "Favicons.v6.sql")); | 997 EXPECT_TRUE(CreateDatabaseFromSQL(file_name_, "Favicons.v6.sql")); |
998 | 998 |
999 // Corrupt the database by adjusting the header. This form of corruption will | 999 // Corrupt the database by adjusting the header. This form of corruption will |
1000 // cause immediate failures during Open(), before the migration code runs, so | 1000 // cause immediate failures during Open(), before the migration code runs, so |
1001 // the recovery code will run. | 1001 // the recovery code will run. |
1002 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); | 1002 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); |
1003 | 1003 |
1004 // Database is unusable at the SQLite level. | 1004 // Database is unusable at the SQLite level. |
1005 { | 1005 { |
1006 sql::ScopedErrorIgnorer ignore_errors; | 1006 sql::test::ScopedErrorExpecter expecter; |
1007 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 1007 expecter.ExpectError(SQLITE_CORRUPT); |
1008 sql::Connection raw_db; | 1008 sql::Connection raw_db; |
1009 EXPECT_TRUE(raw_db.Open(file_name_)); | 1009 EXPECT_TRUE(raw_db.Open(file_name_)); |
1010 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); | 1010 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); |
1011 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 1011 ASSERT_TRUE(expecter.SawExpectedErrors()); |
1012 } | 1012 } |
1013 | 1013 |
1014 // Database open should succeed. | 1014 // Database open should succeed. |
1015 { | 1015 { |
1016 sql::ScopedErrorIgnorer ignore_errors; | 1016 sql::test::ScopedErrorExpecter expecter; |
1017 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 1017 expecter.ExpectError(SQLITE_CORRUPT); |
1018 ThumbnailDatabase db(NULL); | 1018 ThumbnailDatabase db(NULL); |
1019 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); | 1019 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); |
1020 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 1020 ASSERT_TRUE(expecter.SawExpectedErrors()); |
1021 } | 1021 } |
1022 | 1022 |
1023 // The database should be usable at the SQLite level, with a current schema | 1023 // The database should be usable at the SQLite level, with a current schema |
1024 // and no data. | 1024 // and no data. |
1025 { | 1025 { |
1026 sql::Connection raw_db; | 1026 sql::Connection raw_db; |
1027 EXPECT_TRUE(raw_db.Open(file_name_)); | 1027 EXPECT_TRUE(raw_db.Open(file_name_)); |
1028 ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db)); | 1028 ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db)); |
1029 | 1029 |
1030 // Check that the expected tables exist. | 1030 // Check that the expected tables exist. |
(...skipping 13 matching lines...) Expand all Loading... |
1044 // (which would upgrade it). | 1044 // (which would upgrade it). |
1045 EXPECT_TRUE(CreateDatabaseFromSQL(file_name_, "Favicons.v5.sql")); | 1045 EXPECT_TRUE(CreateDatabaseFromSQL(file_name_, "Favicons.v5.sql")); |
1046 | 1046 |
1047 // Corrupt the database by adjusting the header. This form of corruption will | 1047 // Corrupt the database by adjusting the header. This form of corruption will |
1048 // cause immediate failures during Open(), before the migration code runs, so | 1048 // cause immediate failures during Open(), before the migration code runs, so |
1049 // the recovery code will run. | 1049 // the recovery code will run. |
1050 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); | 1050 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); |
1051 | 1051 |
1052 // Database is unusable at the SQLite level. | 1052 // Database is unusable at the SQLite level. |
1053 { | 1053 { |
1054 sql::ScopedErrorIgnorer ignore_errors; | 1054 sql::test::ScopedErrorExpecter expecter; |
1055 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 1055 expecter.ExpectError(SQLITE_CORRUPT); |
1056 sql::Connection raw_db; | 1056 sql::Connection raw_db; |
1057 EXPECT_TRUE(raw_db.Open(file_name_)); | 1057 EXPECT_TRUE(raw_db.Open(file_name_)); |
1058 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); | 1058 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); |
1059 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 1059 ASSERT_TRUE(expecter.SawExpectedErrors()); |
1060 } | 1060 } |
1061 | 1061 |
1062 // Database open should succeed. | 1062 // Database open should succeed. |
1063 { | 1063 { |
1064 sql::ScopedErrorIgnorer ignore_errors; | 1064 sql::test::ScopedErrorExpecter expecter; |
1065 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 1065 expecter.ExpectError(SQLITE_CORRUPT); |
1066 ThumbnailDatabase db(NULL); | 1066 ThumbnailDatabase db(NULL); |
1067 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); | 1067 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); |
1068 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 1068 ASSERT_TRUE(expecter.SawExpectedErrors()); |
1069 } | 1069 } |
1070 | 1070 |
1071 // The database should be usable at the SQLite level, with a current schema | 1071 // The database should be usable at the SQLite level, with a current schema |
1072 // and no data. | 1072 // and no data. |
1073 { | 1073 { |
1074 sql::Connection raw_db; | 1074 sql::Connection raw_db; |
1075 EXPECT_TRUE(raw_db.Open(file_name_)); | 1075 EXPECT_TRUE(raw_db.Open(file_name_)); |
1076 ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db)); | 1076 ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db)); |
1077 | 1077 |
1078 // Check that the expected tables exist. | 1078 // Check that the expected tables exist. |
(...skipping 27 matching lines...) Expand all Loading... |
1106 ThumbnailDatabase db(NULL); | 1106 ThumbnailDatabase db(NULL); |
1107 ASSERT_EQ(sql::INIT_OK, db.Init(db_path)); | 1107 ASSERT_EQ(sql::INIT_OK, db.Init(db_path)); |
1108 | 1108 |
1109 // Verify that the resulting schema is correct, whether it | 1109 // Verify that the resulting schema is correct, whether it |
1110 // involved razing the file or fixing things in place. | 1110 // involved razing the file or fixing things in place. |
1111 VerifyTablesAndColumns(&db.db_); | 1111 VerifyTablesAndColumns(&db.db_); |
1112 } | 1112 } |
1113 } | 1113 } |
1114 | 1114 |
1115 } // namespace history | 1115 } // namespace history |
OLD | NEW |