|
| 1 | +using DockerComposeBuilder.Model.Services; |
| 2 | +using System; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace DockerComposeBuilder.Tests; |
| 6 | + |
| 7 | +public class UnixFileModeTests |
| 8 | +{ |
| 9 | + [Theory] |
| 10 | + [InlineData("0400", 256, UnixFileModeNotation.Octal)] |
| 11 | + [InlineData("0440", 288, UnixFileModeNotation.Octal)] |
| 12 | + [InlineData("0444", 292, UnixFileModeNotation.Octal)] |
| 13 | + [InlineData("0644", 420, UnixFileModeNotation.Octal)] |
| 14 | + [InlineData("0755", 493, UnixFileModeNotation.Octal)] |
| 15 | + [InlineData("0777", 511, UnixFileModeNotation.Octal)] |
| 16 | + public void Parse_OctalNotation_ReturnsCorrectValue(string input, int expectedIntValue, UnixFileModeNotation expectedNotation) |
| 17 | + { |
| 18 | + var mode = UnixFileMode.Parse(input); |
| 19 | + |
| 20 | + Assert.Equal(expectedIntValue, mode.IntValue); |
| 21 | + Assert.Equal(expectedNotation, mode.Notation); |
| 22 | + } |
| 23 | + |
| 24 | + [Theory] |
| 25 | + [InlineData("0o400", 256, UnixFileModeNotation.OctalWithO)] |
| 26 | + [InlineData("0o440", 288, UnixFileModeNotation.OctalWithO)] |
| 27 | + [InlineData("0o444", 292, UnixFileModeNotation.OctalWithO)] |
| 28 | + [InlineData("0o644", 420, UnixFileModeNotation.OctalWithO)] |
| 29 | + [InlineData("0o755", 493, UnixFileModeNotation.OctalWithO)] |
| 30 | + [InlineData("0o777", 511, UnixFileModeNotation.OctalWithO)] |
| 31 | + [InlineData("0O755", 493, UnixFileModeNotation.OctalWithO)] |
| 32 | + public void Parse_OctalWithONotation_ReturnsCorrectValue(string input, int expectedIntValue, UnixFileModeNotation expectedNotation) |
| 33 | + { |
| 34 | + var mode = UnixFileMode.Parse(input); |
| 35 | + |
| 36 | + Assert.Equal(expectedIntValue, mode.IntValue); |
| 37 | + Assert.Equal(expectedNotation, mode.Notation); |
| 38 | + } |
| 39 | + |
| 40 | + [Theory] |
| 41 | + [InlineData("256", 256, UnixFileModeNotation.RawInt)] |
| 42 | + [InlineData("288", 288, UnixFileModeNotation.RawInt)] |
| 43 | + [InlineData("292", 292, UnixFileModeNotation.RawInt)] |
| 44 | + [InlineData("420", 420, UnixFileModeNotation.RawInt)] |
| 45 | + [InlineData("493", 493, UnixFileModeNotation.RawInt)] |
| 46 | + [InlineData("511", 511, UnixFileModeNotation.RawInt)] |
| 47 | + public void Parse_RawIntNotation_ReturnsCorrectValue(string input, int expectedIntValue, UnixFileModeNotation expectedNotation) |
| 48 | + { |
| 49 | + var mode = UnixFileMode.Parse(input); |
| 50 | + |
| 51 | + Assert.Equal(expectedIntValue, mode.IntValue); |
| 52 | + Assert.Equal(expectedNotation, mode.Notation); |
| 53 | + } |
| 54 | + |
| 55 | + [Theory] |
| 56 | + [InlineData("04777", 2559)] |
| 57 | + [InlineData("02755", 1517)] |
| 58 | + [InlineData("01755", 1005)] |
| 59 | + [InlineData("06755", 3565)] |
| 60 | + [InlineData("07777", 4095)] |
| 61 | + public void Parse_SetuidSetgidStickyBit_ReturnsCorrectValue(string input, int expectedIntValue) |
| 62 | + { |
| 63 | + var mode = UnixFileMode.Parse(input); |
| 64 | + |
| 65 | + Assert.Equal(expectedIntValue, mode.IntValue); |
| 66 | + Assert.Equal(UnixFileModeNotation.Octal, mode.Notation); |
| 67 | + } |
| 68 | + |
| 69 | + [Theory] |
| 70 | + [InlineData("0o4777", 2559)] |
| 71 | + [InlineData("0o2755", 1517)] |
| 72 | + [InlineData("0o1755", 1005)] |
| 73 | + [InlineData("0o7777", 4095)] |
| 74 | + public void Parse_SetuidSetgidStickyBitWithO_ReturnsCorrectValue(string input, int expectedIntValue) |
| 75 | + { |
| 76 | + var mode = UnixFileMode.Parse(input); |
| 77 | + |
| 78 | + Assert.Equal(expectedIntValue, mode.IntValue); |
| 79 | + Assert.Equal(UnixFileModeNotation.OctalWithO, mode.Notation); |
| 80 | + } |
| 81 | + |
| 82 | + [Theory] |
| 83 | + [InlineData("2559", 2559)] |
| 84 | + [InlineData("1517", 1517)] |
| 85 | + [InlineData("1005", 1005)] |
| 86 | + [InlineData("4095", 4095)] |
| 87 | + public void Parse_SetuidSetgidStickyBitRawInt_ReturnsCorrectValue(string input, int expectedIntValue) |
| 88 | + { |
| 89 | + var mode = UnixFileMode.Parse(input); |
| 90 | + |
| 91 | + Assert.Equal(expectedIntValue, mode.IntValue); |
| 92 | + Assert.Equal(UnixFileModeNotation.RawInt, mode.Notation); |
| 93 | + } |
| 94 | + |
| 95 | + [Theory] |
| 96 | + [InlineData("0800")] |
| 97 | + [InlineData("0888")] |
| 98 | + [InlineData("0999")] |
| 99 | + [InlineData("0o800")] |
| 100 | + [InlineData("0o888")] |
| 101 | + public void Parse_InvalidOctalDigits_ThrowsException(string input) |
| 102 | + { |
| 103 | + Assert.ThrowsAny<Exception>(() => UnixFileMode.Parse(input)); |
| 104 | + } |
| 105 | + |
| 106 | + [Theory] |
| 107 | + [InlineData("800", 800)] |
| 108 | + [InlineData("888", 888)] |
| 109 | + [InlineData("999", 999)] |
| 110 | + [InlineData("789", 789)] |
| 111 | + [InlineData("918", 918)] |
| 112 | + public void Parse_RawIntWithAnyDigits_ParsesAsDecimal(string input, int expectedValue) |
| 113 | + { |
| 114 | + var mode = UnixFileMode.Parse(input); |
| 115 | + |
| 116 | + Assert.Equal(expectedValue, mode.IntValue); |
| 117 | + Assert.Equal(UnixFileModeNotation.RawInt, mode.Notation); |
| 118 | + } |
| 119 | + |
| 120 | + [Fact] |
| 121 | + public void Parse_EmptyString_ThrowsArgumentException() |
| 122 | + { |
| 123 | + Assert.Throws<ArgumentException>(() => UnixFileMode.Parse("")); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public void Parse_NullString_ThrowsArgumentException() |
| 128 | + { |
| 129 | + Assert.Throws<ArgumentException>(() => UnixFileMode.Parse(null!)); |
| 130 | + } |
| 131 | + |
| 132 | + [Theory] |
| 133 | + [InlineData("0400", "0400")] |
| 134 | + [InlineData("0755", "0755")] |
| 135 | + [InlineData("04777", "04777")] |
| 136 | + [InlineData("0o400", "0o400")] |
| 137 | + [InlineData("0o755", "0o755")] |
| 138 | + [InlineData("0o4777", "0o4777")] |
| 139 | + [InlineData("256", "256")] |
| 140 | + [InlineData("493", "493")] |
| 141 | + [InlineData("2559", "2559")] |
| 142 | + public void ToNotationString_PreservesFormat(string input, string expected) |
| 143 | + { |
| 144 | + var mode = UnixFileMode.Parse(input); |
| 145 | + Assert.Equal(expected, mode.ToNotationString()); |
| 146 | + } |
| 147 | + |
| 148 | + [Theory] |
| 149 | + [InlineData("0400", "0400")] |
| 150 | + [InlineData("0o400", "0400")] |
| 151 | + [InlineData("256", "0400")] |
| 152 | + public void ToOctalString_AllNotations_ReturnsOctalFormat(string input, string expected) |
| 153 | + { |
| 154 | + var mode = UnixFileMode.Parse(input); |
| 155 | + Assert.Equal(expected, mode.ToOctalString()); |
| 156 | + } |
| 157 | + |
| 158 | + [Fact] |
| 159 | + public void FromDecimal_ValidValue_CreatesMode() |
| 160 | + { |
| 161 | + var mode = UnixFileMode.FromDecimal(256); |
| 162 | + |
| 163 | + Assert.Equal(256, mode.IntValue); |
| 164 | + Assert.Equal(UnixFileModeNotation.RawInt, mode.Notation); |
| 165 | + } |
| 166 | + |
| 167 | + [Fact] |
| 168 | + public void ImplicitConversion_IntToMode_Works() |
| 169 | + { |
| 170 | + UnixFileMode mode = 256; |
| 171 | + |
| 172 | + Assert.Equal(256, mode.IntValue); |
| 173 | + } |
| 174 | + |
| 175 | + [Fact] |
| 176 | + public void ImplicitConversion_ModeToInt_Works() |
| 177 | + { |
| 178 | + var mode = UnixFileMode.Parse("0400"); |
| 179 | + |
| 180 | + int value = mode; |
| 181 | + |
| 182 | + Assert.Equal(256, value); |
| 183 | + } |
| 184 | + |
| 185 | + [Fact] |
| 186 | + public void ImplicitConversion_Roundtrip_IsEqual() |
| 187 | + { |
| 188 | + UnixFileMode original = 256; |
| 189 | + |
| 190 | + int intValue = original; |
| 191 | + UnixFileMode roundtripped = intValue; |
| 192 | + |
| 193 | + Assert.Equal(original, roundtripped); |
| 194 | + } |
| 195 | + |
| 196 | + [Fact] |
| 197 | + public void Equality_SameValueAndNotation_AreEqual() |
| 198 | + { |
| 199 | + var mode1 = UnixFileMode.Parse("0400"); |
| 200 | + var mode2 = UnixFileMode.Parse("0400"); |
| 201 | + |
| 202 | + Assert.Equal(mode1, mode2); |
| 203 | + Assert.True(mode1 == mode2); |
| 204 | + } |
| 205 | + |
| 206 | + [Fact] |
| 207 | + public void Equality_SameValueDifferentNotation_AreNotEqual() |
| 208 | + { |
| 209 | + var mode1 = UnixFileMode.Parse("0400"); |
| 210 | + var mode2 = UnixFileMode.Parse("0o400"); |
| 211 | + var mode3 = UnixFileMode.Parse("256"); |
| 212 | + |
| 213 | + Assert.NotEqual(mode1, mode2); |
| 214 | + Assert.NotEqual(mode2, mode3); |
| 215 | + Assert.NotEqual(mode1, mode3); |
| 216 | + Assert.Equal(mode1.IntValue, mode2.IntValue); |
| 217 | + Assert.Equal(mode2.IntValue, mode3.IntValue); |
| 218 | + } |
| 219 | + |
| 220 | + [Fact] |
| 221 | + public void Equality_DifferentValue_AreNotEqual() |
| 222 | + { |
| 223 | + var mode1 = UnixFileMode.Parse("0400"); |
| 224 | + var mode2 = UnixFileMode.Parse("0755"); |
| 225 | + |
| 226 | + Assert.NotEqual(mode1, mode2); |
| 227 | + Assert.True(mode1 != mode2); |
| 228 | + } |
| 229 | + |
| 230 | + [Fact] |
| 231 | + public void GetHashCode_SameValueAndNotation_SameHash() |
| 232 | + { |
| 233 | + var mode1 = UnixFileMode.Parse("0400"); |
| 234 | + var mode2 = UnixFileMode.Parse("0400"); |
| 235 | + |
| 236 | + Assert.Equal(mode1.GetHashCode(), mode2.GetHashCode()); |
| 237 | + } |
| 238 | + |
| 239 | + [Fact] |
| 240 | + public void ParseAsOctetFromInt_ValidValue_Works() |
| 241 | + { |
| 242 | + var mode = UnixFileMode.ParseAsOctetFromInt(755); |
| 243 | + |
| 244 | + Assert.Equal(493, mode.IntValue); |
| 245 | + Assert.Equal(UnixFileModeNotation.RawInt, mode.Notation); |
| 246 | + } |
| 247 | + |
| 248 | + [Fact] |
| 249 | + public void ParseAsOctetFromInt_InvalidDigit_ThrowsArgumentException() |
| 250 | + { |
| 251 | + var ex = Assert.Throws<ArgumentException>(() => UnixFileMode.ParseAsOctetFromInt(800)); |
| 252 | + Assert.Contains("Invalid octal digit 8", ex.Message); |
| 253 | + } |
| 254 | +} |
0 commit comments