Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1193)

Side by Side Diff: ppapi/tests/test_file_io.cc

Issue 915403003: Enable size_t to int truncation warnings in PPAPI (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ppapi_unittests win x64 Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ppapi/tests/test_file_io.h ('k') | ppapi/tests/test_file_mapping.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "ppapi/tests/test_file_io.h" 5 #include "ppapi/tests/test_file_io.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <stdio.h> 9 #include <stdio.h>
10 #include <string.h> 10 #include <string.h>
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 } 143 }
144 144
145 int32_t WriteEntireBuffer(PP_Instance instance, 145 int32_t WriteEntireBuffer(PP_Instance instance,
146 pp::FileIO* file_io, 146 pp::FileIO* file_io,
147 int32_t offset, 147 int32_t offset,
148 const std::string& data, 148 const std::string& data,
149 CallbackType callback_type) { 149 CallbackType callback_type) {
150 TestCompletionCallback callback(instance, callback_type); 150 TestCompletionCallback callback(instance, callback_type);
151 int32_t write_offset = offset; 151 int32_t write_offset = offset;
152 const char* buf = data.c_str(); 152 const char* buf = data.c_str();
153 int32_t size = data.size(); 153 int32_t size = static_cast<int32_t>(data.size());
154 154
155 while (write_offset < offset + size) { 155 while (write_offset < offset + size) {
156 callback.WaitForResult(file_io->Write(write_offset, 156 callback.WaitForResult(file_io->Write(write_offset,
157 &buf[write_offset - offset], 157 &buf[write_offset - offset],
158 size - write_offset + offset, 158 size - write_offset + offset,
159 callback.GetCallback())); 159 callback.GetCallback()));
160 if (callback.result() < 0) 160 if (callback.result() < 0)
161 return callback.result(); 161 return callback.result();
162 if (callback.result() == 0) 162 if (callback.result() == 0)
163 return PP_ERROR_FAILED; 163 return PP_ERROR_FAILED;
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after
774 CHECK_CALLBACK_BEHAVIOR(callback); 774 CHECK_CALLBACK_BEHAVIOR(callback);
775 ASSERT_EQ(PP_OK, callback.result()); 775 ASSERT_EQ(PP_OK, callback.result());
776 776
777 // Set up testing contents. 777 // Set up testing contents.
778 int32_t rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0, 778 int32_t rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0,
779 "abcdefghijkl", callback_type()); 779 "abcdefghijkl", callback_type());
780 ASSERT_EQ(PP_OK, rv); 780 ASSERT_EQ(PP_OK, rv);
781 781
782 // Parallel read operations. 782 // Parallel read operations.
783 const char* border = "__border__"; 783 const char* border = "__border__";
784 const int32_t border_size = strlen(border); 784 const int32_t border_size = static_cast<int32_t>(strlen(border));
785 785
786 TestCompletionCallback callback_1(instance_->pp_instance(), callback_type()); 786 TestCompletionCallback callback_1(instance_->pp_instance(), callback_type());
787 int32_t read_offset_1 = 0; 787 int32_t read_offset_1 = 0;
788 int32_t size_1 = 3; 788 int32_t size_1 = 3;
789 std::vector<char> extended_buf_1(border_size * 2 + size_1); 789 std::vector<char> extended_buf_1(border_size * 2 + size_1);
790 char* buf_1 = &extended_buf_1[border_size]; 790 char* buf_1 = &extended_buf_1[border_size];
791 memcpy(&extended_buf_1[0], border, border_size); 791 memcpy(&extended_buf_1[0], border, border_size);
792 memcpy(buf_1 + size_1, border, border_size); 792 memcpy(buf_1 + size_1, border, border_size);
793 793
794 TestCompletionCallback callback_2(instance_->pp_instance(), callback_type()); 794 TestCompletionCallback callback_2(instance_->pp_instance(), callback_type());
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 PP_FILEOPENFLAG_READ | 858 PP_FILEOPENFLAG_READ |
859 PP_FILEOPENFLAG_WRITE, 859 PP_FILEOPENFLAG_WRITE,
860 callback.GetCallback())); 860 callback.GetCallback()));
861 CHECK_CALLBACK_BEHAVIOR(callback); 861 CHECK_CALLBACK_BEHAVIOR(callback);
862 ASSERT_EQ(PP_OK, callback.result()); 862 ASSERT_EQ(PP_OK, callback.result());
863 863
864 // Parallel write operations. 864 // Parallel write operations.
865 TestCompletionCallback callback_1(instance_->pp_instance(), callback_type()); 865 TestCompletionCallback callback_1(instance_->pp_instance(), callback_type());
866 int32_t write_offset_1 = 0; 866 int32_t write_offset_1 = 0;
867 const char* buf_1 = "abc"; 867 const char* buf_1 = "abc";
868 int32_t size_1 = strlen(buf_1); 868 int32_t size_1 = static_cast<int32_t>(strlen(buf_1));
869 869
870 TestCompletionCallback callback_2(instance_->pp_instance(), callback_type()); 870 TestCompletionCallback callback_2(instance_->pp_instance(), callback_type());
871 int32_t write_offset_2 = size_1; 871 int32_t write_offset_2 = size_1;
872 const char* buf_2 = "defghijkl"; 872 const char* buf_2 = "defghijkl";
873 int32_t size_2 = strlen(buf_2); 873 int32_t size_2 = static_cast<int32_t>(strlen(buf_2));
874 874
875 int32_t rv_1 = PP_OK; 875 int32_t rv_1 = PP_OK;
876 int32_t rv_2 = PP_OK; 876 int32_t rv_2 = PP_OK;
877 while (size_1 >= 0 && size_2 >= 0 && size_1 + size_2 > 0) { 877 while (size_1 >= 0 && size_2 >= 0 && size_1 + size_2 > 0) {
878 if (size_1 > 0) { 878 if (size_1 > 0) {
879 // Copy the buffer so we can erase it below. 879 // Copy the buffer so we can erase it below.
880 std::string str_1(buf_1); 880 std::string str_1(buf_1);
881 rv_1 = file_io.Write( 881 rv_1 = file_io.Write(
882 write_offset_1, &str_1[0], str_1.size(), callback_1.GetCallback()); 882 write_offset_1, &str_1[0], static_cast<int32_t>(str_1.size()),
883 callback_1.GetCallback());
883 // Erase the buffer to test that async writes copy it. 884 // Erase the buffer to test that async writes copy it.
884 std::fill(str_1.begin(), str_1.end(), 0); 885 std::fill(str_1.begin(), str_1.end(), 0);
885 } 886 }
886 if (size_2 > 0) { 887 if (size_2 > 0) {
887 // Copy the buffer so we can erase it below. 888 // Copy the buffer so we can erase it below.
888 std::string str_2(buf_2); 889 std::string str_2(buf_2);
889 rv_2 = file_io.Write( 890 rv_2 = file_io.Write(
890 write_offset_2, &str_2[0], str_2.size(), callback_2.GetCallback()); 891 write_offset_2, &str_2[0], static_cast<int32_t>(str_2.size()),
892 callback_2.GetCallback());
891 // Erase the buffer to test that async writes copy it. 893 // Erase the buffer to test that async writes copy it.
892 std::fill(str_2.begin(), str_2.end(), 0); 894 std::fill(str_2.begin(), str_2.end(), 0);
893 } 895 }
894 896
895 if (size_1 > 0) { 897 if (size_1 > 0) {
896 callback_1.WaitForResult(rv_1); 898 callback_1.WaitForResult(rv_1);
897 CHECK_CALLBACK_BEHAVIOR(callback_1); 899 CHECK_CALLBACK_BEHAVIOR(callback_1);
898 ASSERT_TRUE(callback_1.result() > 0); 900 ASSERT_TRUE(callback_1.result() > 0);
899 write_offset_1 += callback_1.result(); 901 write_offset_1 += callback_1.result();
900 buf_1 += callback_1.result(); 902 buf_1 += callback_1.result();
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
944 PP_FILEOPENFLAG_TRUNCATE | 946 PP_FILEOPENFLAG_TRUNCATE |
945 PP_FILEOPENFLAG_READ | 947 PP_FILEOPENFLAG_READ |
946 PP_FILEOPENFLAG_WRITE, 948 PP_FILEOPENFLAG_WRITE,
947 callback.GetCallback())); 949 callback.GetCallback()));
948 CHECK_CALLBACK_BEHAVIOR(callback); 950 CHECK_CALLBACK_BEHAVIOR(callback);
949 ASSERT_EQ(PP_OK, callback.result()); 951 ASSERT_EQ(PP_OK, callback.result());
950 952
951 TestCompletionCallback callback_1(instance_->pp_instance(), PP_REQUIRED); 953 TestCompletionCallback callback_1(instance_->pp_instance(), PP_REQUIRED);
952 int32_t write_offset_1 = 0; 954 int32_t write_offset_1 = 0;
953 const char* buf_1 = "mnopqrstuvw"; 955 const char* buf_1 = "mnopqrstuvw";
954 int32_t rv_1 = file_io.Write(write_offset_1, buf_1, strlen(buf_1), 956 int32_t rv_1 = file_io.Write(write_offset_1, buf_1,
957 static_cast<int32_t>(strlen(buf_1)),
955 callback_1.GetCallback()); 958 callback_1.GetCallback());
956 ASSERT_EQ(PP_OK_COMPLETIONPENDING, rv_1); 959 ASSERT_EQ(PP_OK_COMPLETIONPENDING, rv_1);
957 960
958 TestCompletionCallback callback_2(instance_->pp_instance(), callback_type()); 961 TestCompletionCallback callback_2(instance_->pp_instance(), callback_type());
959 int32_t read_offset_2 = 4; 962 int32_t read_offset_2 = 4;
960 char buf_2[3]; 963 char buf_2[3];
961 callback_2.WaitForResult(file_io.Read(read_offset_2, buf_2, sizeof(buf_2), 964 callback_2.WaitForResult(file_io.Read(read_offset_2, buf_2, sizeof(buf_2),
962 callback_2.GetCallback())); 965 callback_2.GetCallback()));
963 CHECK_CALLBACK_BEHAVIOR(callback_2); 966 CHECK_CALLBACK_BEHAVIOR(callback_2);
964 ASSERT_EQ(PP_ERROR_INPROGRESS, callback_2.result()); 967 ASSERT_EQ(PP_ERROR_INPROGRESS, callback_2.result());
965 callback_1.WaitForResult(rv_1); 968 callback_1.WaitForResult(rv_1);
966 CHECK_CALLBACK_BEHAVIOR(callback_1); 969 CHECK_CALLBACK_BEHAVIOR(callback_1);
967 970
968 // Cannot query while a write is pending. 971 // Cannot query while a write is pending.
969 rv_1 = file_io.Write(write_offset_1, buf_1, strlen(buf_1), 972 rv_1 = file_io.Write(write_offset_1, buf_1,
973 static_cast<int32_t>(strlen(buf_1)),
970 callback_1.GetCallback()); 974 callback_1.GetCallback());
971 ASSERT_EQ(PP_OK_COMPLETIONPENDING, rv_1); 975 ASSERT_EQ(PP_OK_COMPLETIONPENDING, rv_1);
972 PP_FileInfo info; 976 PP_FileInfo info;
973 callback_2.WaitForResult(file_io.Query(&info, callback_2.GetCallback())); 977 callback_2.WaitForResult(file_io.Query(&info, callback_2.GetCallback()));
974 CHECK_CALLBACK_BEHAVIOR(callback_2); 978 CHECK_CALLBACK_BEHAVIOR(callback_2);
975 ASSERT_EQ(PP_ERROR_INPROGRESS, callback_2.result()); 979 ASSERT_EQ(PP_ERROR_INPROGRESS, callback_2.result());
976 callback_1.WaitForResult(rv_1); 980 callback_1.WaitForResult(rv_1);
977 CHECK_CALLBACK_BEHAVIOR(callback_1); 981 CHECK_CALLBACK_BEHAVIOR(callback_1);
978 982
979 // Cannot touch while a write is pending. 983 // Cannot touch while a write is pending.
980 rv_1 = file_io.Write(write_offset_1, buf_1, strlen(buf_1), 984 rv_1 = file_io.Write(write_offset_1, buf_1,
985 static_cast<int32_t>(strlen(buf_1)),
981 callback_1.GetCallback()); 986 callback_1.GetCallback());
982 ASSERT_EQ(PP_OK_COMPLETIONPENDING, rv_1); 987 ASSERT_EQ(PP_OK_COMPLETIONPENDING, rv_1);
983 callback_2.WaitForResult(file_io.Touch(1234.0, 5678.0, 988 callback_2.WaitForResult(file_io.Touch(1234.0, 5678.0,
984 callback_2.GetCallback())); 989 callback_2.GetCallback()));
985 CHECK_CALLBACK_BEHAVIOR(callback_2); 990 CHECK_CALLBACK_BEHAVIOR(callback_2);
986 ASSERT_EQ(PP_ERROR_INPROGRESS, callback_2.result()); 991 ASSERT_EQ(PP_ERROR_INPROGRESS, callback_2.result());
987 callback_1.WaitForResult(rv_1); 992 callback_1.WaitForResult(rv_1);
988 CHECK_CALLBACK_BEHAVIOR(callback_1); 993 CHECK_CALLBACK_BEHAVIOR(callback_1);
989 994
990 // Cannot set length while a write is pending. 995 // Cannot set length while a write is pending.
991 rv_1 = file_io.Write(write_offset_1, buf_1, strlen(buf_1), 996 rv_1 = file_io.Write(write_offset_1, buf_1,
997 static_cast<int32_t>(strlen(buf_1)),
992 callback_1.GetCallback()); 998 callback_1.GetCallback());
993 ASSERT_EQ(PP_OK_COMPLETIONPENDING, rv_1); 999 ASSERT_EQ(PP_OK_COMPLETIONPENDING, rv_1);
994 callback_2.WaitForResult(file_io.SetLength(123, callback_2.GetCallback())); 1000 callback_2.WaitForResult(file_io.SetLength(123, callback_2.GetCallback()));
995 CHECK_CALLBACK_BEHAVIOR(callback_2); 1001 CHECK_CALLBACK_BEHAVIOR(callback_2);
996 ASSERT_EQ(PP_ERROR_INPROGRESS, callback_2.result()); 1002 ASSERT_EQ(PP_ERROR_INPROGRESS, callback_2.result());
997 callback_1.WaitForResult(rv_1); 1003 callback_1.WaitForResult(rv_1);
998 CHECK_CALLBACK_BEHAVIOR(callback_1); 1004 CHECK_CALLBACK_BEHAVIOR(callback_1);
999 1005
1000 PASS(); 1006 PASS();
1001 } 1007 }
(...skipping 29 matching lines...) Expand all
1031 int fd = _open_osfhandle(reinterpret_cast<intptr_t>(handle), 1037 int fd = _open_osfhandle(reinterpret_cast<intptr_t>(handle),
1032 _O_RDWR | _O_BINARY); 1038 _O_RDWR | _O_BINARY);
1033 #else 1039 #else
1034 int fd = handle; 1040 int fd = handle;
1035 #endif 1041 #endif
1036 if (fd < 0) 1042 if (fd < 0)
1037 return "FileIO::RequestOSFileHandle() returned a bad file descriptor."; 1043 return "FileIO::RequestOSFileHandle() returned a bad file descriptor.";
1038 1044
1039 // Check write(2) for the native FD. 1045 // Check write(2) for the native FD.
1040 const std::string msg = "foobar"; 1046 const std::string msg = "foobar";
1041 ssize_t cnt = write(fd, msg.data(), msg.size()); 1047 ssize_t cnt = write(fd, msg.data(), static_cast<unsigned>(msg.size()));
1042 if (cnt < 0) 1048 if (cnt < 0)
1043 return ReportError("write for native FD returned error", errno); 1049 return ReportError("write for native FD returned error", errno);
1044 if (cnt != static_cast<ssize_t>(msg.size())) 1050 if (cnt != static_cast<ssize_t>(msg.size()))
1045 return ReportError("write for native FD count mismatch", cnt); 1051 return ReportError("write for native FD count mismatch", cnt);
1046 1052
1047 // Check lseek(2) for the native FD. 1053 // Check lseek(2) for the native FD.
1048 off_t off = lseek(fd, 0, SEEK_CUR); 1054 off_t off = lseek(fd, 0, SEEK_CUR);
1049 if (off == static_cast<off_t>(-1)) 1055 if (off == static_cast<off_t>(-1))
1050 return ReportError("lseek for native FD returned error", errno); 1056 return ReportError("lseek for native FD returned error", errno);
1051 if (off != static_cast<off_t>(msg.size())) 1057 if (off != static_cast<off_t>(msg.size()))
1052 return ReportError("lseek for native FD offset mismatch", off); 1058 return ReportError("lseek for native FD offset mismatch", off);
1053 1059
1054 off = lseek(fd, 0, SEEK_SET); 1060 off = lseek(fd, 0, SEEK_SET);
1055 if (off == static_cast<off_t>(-1)) 1061 if (off == static_cast<off_t>(-1))
1056 return ReportError("lseek for native FD returned error", errno); 1062 return ReportError("lseek for native FD returned error", errno);
1057 if (off != 0) 1063 if (off != 0)
1058 return ReportError("lseek for native FD offset mismatch", off); 1064 return ReportError("lseek for native FD offset mismatch", off);
1059 1065
1060 // Check read(2) for the native FD. 1066 // Check read(2) for the native FD.
1061 std::string buf(msg.size(), '\0'); 1067 std::string buf(msg.size(), '\0');
1062 cnt = read(fd, &buf[0], msg.size()); 1068 cnt = read(fd, &buf[0], static_cast<unsigned>(msg.size()));
1063 if (cnt < 0) 1069 if (cnt < 0)
1064 return ReportError("read for native FD returned error", errno); 1070 return ReportError("read for native FD returned error", errno);
1065 if (cnt != static_cast<ssize_t>(msg.size())) 1071 if (cnt != static_cast<ssize_t>(msg.size()))
1066 return ReportError("read for native FD count mismatch", cnt); 1072 return ReportError("read for native FD count mismatch", cnt);
1067 if (msg != buf) 1073 if (msg != buf)
1068 return ReportMismatch("read for native FD", buf, msg); 1074 return ReportMismatch("read for native FD", buf, msg);
1069 PASS(); 1075 PASS();
1070 } 1076 }
1071 1077
1072 // Calling RequestOSFileHandle with the FileIO that is opened with 1078 // Calling RequestOSFileHandle with the FileIO that is opened with
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
1267 // END mmap(2) test with a file handle opened in READONLY mode. 1273 // END mmap(2) test with a file handle opened in READONLY mode.
1268 1274
1269 if (close(fd) < 0) 1275 if (close(fd) < 0)
1270 return ReportError("close for native FD returned error", errno); 1276 return ReportError("close for native FD returned error", errno);
1271 #endif // !defined(PPAPI_OS_WIN) 1277 #endif // !defined(PPAPI_OS_WIN)
1272 1278
1273 PASS(); 1279 PASS();
1274 } 1280 }
1275 1281
1276 std::string TestFileIO::MatchOpenExpectations(pp::FileSystem* file_system, 1282 std::string TestFileIO::MatchOpenExpectations(pp::FileSystem* file_system,
1277 size_t open_flags, 1283 int32_t open_flags,
1278 size_t expectations) { 1284 int32_t expectations) {
1279 std::string bad_argument = 1285 std::string bad_argument =
1280 "TestFileIO::MatchOpenExpectations has invalid input arguments."; 1286 "TestFileIO::MatchOpenExpectations has invalid input arguments.";
1281 bool invalid_combination = !!(expectations & INVALID_FLAG_COMBINATION); 1287 bool invalid_combination = !!(expectations & INVALID_FLAG_COMBINATION);
1282 if (invalid_combination) { 1288 if (invalid_combination) {
1283 if (expectations != INVALID_FLAG_COMBINATION) 1289 if (expectations != INVALID_FLAG_COMBINATION)
1284 return bad_argument; 1290 return bad_argument;
1285 } else { 1291 } else {
1286 // Validate that one and only one of <some_expectation> and 1292 // Validate that one and only one of <some_expectation> and
1287 // DONT_<some_expectation> is specified. 1293 // DONT_<some_expectation> is specified.
1288 for (size_t remains = expectations, end = END_OF_OPEN_EXPECATION_PAIRS; 1294 for (size_t remains = expectations, end = END_OF_OPEN_EXPECATION_PAIRS;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1352 if ((invalid_combination && callback.result() == PP_OK) || 1358 if ((invalid_combination && callback.result() == PP_OK) ||
1353 (!invalid_combination && 1359 (!invalid_combination &&
1354 ((callback.result() == PP_OK) != create_if_doesnt_exist))) { 1360 ((callback.result() == PP_OK) != create_if_doesnt_exist))) {
1355 return ReportOpenError(open_flags); 1361 return ReportOpenError(open_flags);
1356 } 1362 }
1357 1363
1358 return std::string(); 1364 return std::string();
1359 } 1365 }
1360 1366
1361 // TODO(viettrungluu): Test Close(). crbug.com/69457 1367 // TODO(viettrungluu): Test Close(). crbug.com/69457
OLDNEW
« no previous file with comments | « ppapi/tests/test_file_io.h ('k') | ppapi/tests/test_file_mapping.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698