1 // Copyright (c) 2009 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. 4 5 #include "net/http/http_byte_range.h" 6 #include "testing/gtest/include/gtest/gtest.h" 7 8 TEST(HttpByteRangeTest, ValidRanges) { 9 const struct { 10 int64 first_byte_position; 11 int64 last_byte_position; 12 int64 suffix_length; 13 bool valid; 14 } tests[] = { 15 { -1, -1, 0, false }, 16 { 0, 0, 0, true }, 17 { -10, 0, 0, false }, 18 { 10, 0, 0, false }, 19 { 10, -1, 0, true }, 20 { -1, -1, -1, false }, 21 { -1, 50, 0, false }, 22 { 10, 10000, 0, true }, 23 { -1, -1, 100000, true }, 24 }; 25 26 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 27 net::HttpByteRange range; 28 range.set_first_byte_position(tests[i].first_byte_position); 29 range.set_last_byte_position(tests[i].last_byte_position); 30 range.set_suffix_length(tests[i].suffix_length); 31 EXPECT_EQ(tests[i].valid, range.IsValid()); 32 } 33 } 34 35 TEST(HttpByteRangeTest, SetInstanceSize) { 36 const struct { 37 int64 first_byte_position; 38 int64 last_byte_position; 39 int64 suffix_length; 40 int64 instance_size; 41 bool expected_return_value; 42 int64 expected_lower_bound; 43 int64 expected_upper_bound; 44 } tests[] = { 45 { -10, 0, -1, 0, false, -1, -1 }, 46 { 10, 0, -1, 0, false, -1, -1 }, 47 // Zero instance size is valid, this is the case that user has to handle. 48 { -1, -1, -1, 0, true, 0, -1 }, 49 { -1, -1, 500, 0, true, 0, -1 }, 50 { -1, 50, -1, 0, false, -1, -1 }, 51 { -1, -1, 500, 300, true, 0, 299 }, 52 { -1, -1, -1, 100, true, 0, 99 }, 53 { 10, -1, -1, 100, true, 10, 99 }, 54 { -1, -1, 500, 1000, true, 500, 999 }, 55 { 10, 10000, -1, 1000000, true, 10, 10000 }, 56 }; 57 58 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 59 net::HttpByteRange range; 60 range.set_first_byte_position(tests[i].first_byte_position); 61 range.set_last_byte_position(tests[i].last_byte_position); 62 range.set_suffix_length(tests[i].suffix_length); 63 64 bool return_value = range.ComputeBounds(tests[i].instance_size); 65 EXPECT_EQ(tests[i].expected_return_value, return_value); 66 if (return_value) { 67 EXPECT_EQ(tests[i].expected_lower_bound, range.first_byte_position()); 68 EXPECT_EQ(tests[i].expected_upper_bound, range.last_byte_position()); 69 70 // Try to call SetInstanceSize the second time. 71 EXPECT_FALSE(range.ComputeBounds(tests[i].instance_size)); 72 // And expect there's no side effect. 73 EXPECT_EQ(tests[i].expected_lower_bound, range.first_byte_position()); 74 EXPECT_EQ(tests[i].expected_upper_bound, range.last_byte_position()); 75 EXPECT_EQ(tests[i].suffix_length, range.suffix_length()); 76 } 77 } 78 } 79