xref: /aosp_15_r20/external/cronet/net/http/http_byte_range_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2009 The Chromium Authors
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 
7 #include "testing/gtest/include/gtest/gtest.h"
8 
9 namespace net {
10 
11 namespace {
12 
TEST(HttpByteRangeTest,ValidRanges)13 TEST(HttpByteRangeTest, ValidRanges) {
14   const struct {
15     int64_t first_byte_position;
16     int64_t last_byte_position;
17     int64_t suffix_length;
18     bool valid;
19   } tests[] = {
20     {  -1, -1,  0, false },
21     {   0,  0,  0, true  },
22     { -10,  0,  0, false },
23     {  10,  0,  0, false },
24     {  10, -1,  0, true  },
25     {  -1, -1, -1, false },
26     {  -1, 50,  0, false },
27     {  10, 10000, 0, true },
28     {  -1, -1, 100000, true },
29   };
30 
31   for (const auto& test : tests) {
32     HttpByteRange range;
33     range.set_first_byte_position(test.first_byte_position);
34     range.set_last_byte_position(test.last_byte_position);
35     range.set_suffix_length(test.suffix_length);
36     EXPECT_EQ(test.valid, range.IsValid());
37   }
38 }
39 
TEST(HttpByteRangeTest,SetInstanceSize)40 TEST(HttpByteRangeTest, SetInstanceSize) {
41   const struct {
42     int64_t first_byte_position;
43     int64_t last_byte_position;
44     int64_t suffix_length;
45     int64_t instance_size;
46     bool expected_return_value;
47     int64_t expected_lower_bound;
48     int64_t expected_upper_bound;
49   } tests[] = {
50     { -10,  0,  -1,   0, false,  -1,  -1 },
51     {  10,  0,  -1,   0, false,  -1,  -1 },
52     // Zero instance size is valid, this is the case that user has to handle.
53     {  -1, -1,  -1,   0,  true,   0,  -1 },
54     {  -1, -1, 500,   0,  true,   0,  -1 },
55     {  -1, 50,  -1,   0, false,  -1,  -1 },
56     {  -1, -1, 500, 300,  true,   0, 299 },
57     {  -1, -1,  -1, 100,  true,   0,  99 },
58     {  10, -1,  -1, 100,  true,  10,  99 },
59     {  -1, -1, 500, 1000, true, 500, 999 },
60     {  10, 10000, -1, 1000000, true, 10, 10000 },
61   };
62 
63   for (const auto& test : tests) {
64     HttpByteRange range;
65     range.set_first_byte_position(test.first_byte_position);
66     range.set_last_byte_position(test.last_byte_position);
67     range.set_suffix_length(test.suffix_length);
68 
69     bool return_value = range.ComputeBounds(test.instance_size);
70     EXPECT_EQ(test.expected_return_value, return_value);
71     if (return_value) {
72       EXPECT_EQ(test.expected_lower_bound, range.first_byte_position());
73       EXPECT_EQ(test.expected_upper_bound, range.last_byte_position());
74 
75       // Try to call SetInstanceSize the second time.
76       EXPECT_FALSE(range.ComputeBounds(test.instance_size));
77       // And expect there's no side effect.
78       EXPECT_EQ(test.expected_lower_bound, range.first_byte_position());
79       EXPECT_EQ(test.expected_upper_bound, range.last_byte_position());
80       EXPECT_EQ(test.suffix_length, range.suffix_length());
81     }
82   }
83 }
84 
TEST(HttpByteRangeTest,GetHeaderValue)85 TEST(HttpByteRangeTest, GetHeaderValue) {
86   static const struct {
87     HttpByteRange range;
88     const char* expected;
89   } tests[] = {
90       {HttpByteRange::Bounded(0, 0), "bytes=0-0"},
91       {HttpByteRange::Bounded(0, 100), "bytes=0-100"},
92       {HttpByteRange::Bounded(0, -1), "bytes=0-"},
93       {HttpByteRange::RightUnbounded(100), "bytes=100-"},
94       {HttpByteRange::Suffix(100), "bytes=-100"},
95   };
96   for (const auto& test : tests) {
97     EXPECT_EQ(test.expected, test.range.GetHeaderValue());
98   }
99 }
100 
101 }  // namespace
102 
103 }  // namespace net
104