@@ -125,3 +125,85 @@ def test_readMemory(self):
125125 self .assertFalse (mem ["success" ], "expect fail on reading memory." )
126126
127127 self .continue_to_exit ()
128+
129+ def test_writeMemory (self ):
130+ """
131+ Tests the 'writeMemory' request
132+ """
133+ program = self .getBuildArtifact ("a.out" )
134+ self .build_and_launch (program )
135+ source = "main.cpp"
136+ self .source_path = os .path .join (os .getcwd (), source )
137+ self .set_source_breakpoints (
138+ source ,
139+ [line_number (source , "// Breakpoint" )],
140+ )
141+ self .continue_to_next_stop ()
142+
143+ # Get the 'not_a_ptr' writable variable reference address.
144+ ptr_deref = self .dap_server .request_evaluate ("not_a_ptr" )["body" ]
145+ memref = ptr_deref ["memoryReference" ]
146+
147+ # Write the decimal value 50 (0x32 in hexadecimal) to memory.
148+ # This corresponds to the ASCII character '2' and encodes to Base64 as "Mg==".
149+ mem_response = self .writeMemory (memref , 50 , 0 , True )
150+ self .assertEqual (mem_response ["success" ], True )
151+ self .assertEqual (mem_response ["body" ]["bytesWritten" ], 1 )
152+
153+ # Read back the modified memory and verify that the written data matches
154+ # the expected result.
155+ mem_response = self .dap_server .request_readMemory (memref , 0 , 1 )
156+ self .assertEqual (mem_response ["success" ], True )
157+ self .assertEqual (mem_response ["body" ]["data" ], "Mg==" )
158+
159+ # Write the decimal value 100 (0x64 in hexadecimal) to memory.
160+ # This corresponds to the ASCII character 'd' and encodes to Base64 as "ZA==".
161+ # allowPartial=False
162+ mem_response = self .writeMemory (memref , 100 , 0 , False )
163+ self .assertEqual (mem_response ["success" ], True )
164+ self .assertEqual (mem_response ["body" ]["bytesWritten" ], 1 )
165+
166+ # Read back the modified memory and verify that the written data matches
167+ # the expected result.
168+ mem_response = self .dap_server .request_readMemory (memref , 0 , 1 )
169+ self .assertEqual (mem_response ["success" ], True )
170+ self .assertEqual (mem_response ["body" ]["data" ], "ZA==" )
171+
172+ # Memory write failed for 0x0.
173+ mem_response = self .writeMemory ("0x0" , 50 , 0 , True )
174+ self .assertEqual (mem_response ["success" ], False )
175+
176+ # Malformed memory reference.
177+ mem_response = self .writeMemory ("12345" , 50 , 0 , True )
178+ self .assertEqual (mem_response ["success" ], False )
179+
180+ ptr_deref = self .dap_server .request_evaluate ("nonWritable" )["body" ]
181+ memref = ptr_deref ["memoryReference" ]
182+
183+ # Writing to non-writable region should return an appropriate error.
184+ mem_response = self .writeMemory (memref , 50 , 0 , False )
185+ self .assertEqual (mem_response ["success" ], False )
186+ self .assertRegex (
187+ mem_response ["body" ]["error" ]["format" ],
188+ r"Memory " + memref + " region is not writable" ,
189+ )
190+
191+ # Trying to write empty value; data=""
192+ mem_response = self .writeMemory (memref )
193+ self .assertEqual (mem_response ["success" ], False )
194+ self .assertRegex (
195+ mem_response ["body" ]["error" ]["format" ],
196+ r"Data cannot be empty value. Provide valid data" ,
197+ )
198+
199+ # Verify that large memory writes fail if the range spans non-writable
200+ # or non -contiguous regions.
201+ data = bytes ([0xFF ] * 8192 )
202+ mem_response = self .writeMemory (
203+ memref , int .from_bytes (data , byteorder = "little" ), 0 , False
204+ )
205+ self .assertEqual (mem_response ["success" ], False )
206+ self .assertRegex (
207+ mem_response ["body" ]["error" ]["format" ],
208+ r"Memory " + memref + " region is not writable" ,
209+ )
0 commit comments