[v3] gdb/testsuite: XFAIL some gdb.base/fileio.exp

Message ID 20231004140247.16091-2-blarsen@redhat.com
State New
Headers
Series [v3] gdb/testsuite: XFAIL some gdb.base/fileio.exp |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Guinevere Larsen Oct. 4, 2023, 2:02 p.m. UTC
  Some gdb.base/fileio.exp tests expect the inferior to not have write
access to some files. If the test is being run as root, this is never
possible. This commit adds a way to identify if the user is root and
xfails the tests that expect no write access.
---

Changes for v3:
* root_user can now identify if the "id" command failed. It returns that
  the user is not root, so that issues are more easily identified.
* Fix issues pointed out by Tom de Vries.

Changes for v2:
* root_user now uses remote_exec instead of exec to see the uid of the
   inferior

---
 gdb/testsuite/gdb.base/fileio.exp |  9 ++++++++-
 gdb/testsuite/lib/gdb.exp         | 20 ++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
  

Comments

Tom de Vries Oct. 4, 2023, 3:33 p.m. UTC | #1
On 10/4/23 16:02, Guinevere Larsen via Gdb-patches wrote:
> diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
> index de22da8d8a8..5b90b8af3b8 100644
> --- a/gdb/testsuite/lib/gdb.exp
> +++ b/gdb/testsuite/lib/gdb.exp
> @@ -9981,5 +9981,25 @@ gdb_caching_proc have_system_header { file } {
>       return [gdb_can_simple_compile have_system_header_$name $src object]
>   }
>   
> +# Return 1 if the test is being run as root, 0 otherwise.
> +
> +gdb_caching_proc root_user {} {
> +    # ID outputs to stdout, we have to use exec to capture it here.
> +    set user [remote_exec target id]
> +
> +    regexp -all "(-?\[0-9\]+).*" $user user ret_val

I can see that this works, but it's a bit unusual (and hard to parse for 
me) to use regexp to pick apart a list. The usual way is using lindex.

> +
> +    # If ret_val is not 0, we couldn't run `id` on the target for some
> +    # reason.  Return that we are not root, so problems are easier to
> +    # spot.
> +    if {[expr $ret_val != 0]} {

The expr is not necessary here.

> +	return 0
> +    }
> +
> +    regexp -all ".*uid=(\[0-9\]+).*" $user user uid
> +

Also, the 'user' variable is used as dummy, better make that explicit.

So, please do:
...
     set res [remote_exec target id]
     set ret_val [lindex $res 0]
     set output [lindex $res 1]

     # If ret_val is not 0, we couldn't run `id` on the target for some 

     # reason.  Return that we are not root, so problems are easier to 

     # spot. 

     if { $ret_val != 0 } {
         return 0
     }

     regexp -all ".*uid=(\[0-9\]+).*" $output dummy uid

     return [expr $uid == 0]
...

Approved with that change.

Approved-By: Tom de Vries <tdevries@suse.de>

Thanks,
- Tom


> +    return [expr $uid == 0]
> +}
> +
>   # Always load compatibility stuff.
>   load_lib future.exp
  
Guinevere Larsen Oct. 4, 2023, 3:47 p.m. UTC | #2
On 04/10/2023 17:33, Tom de Vries wrote:
> On 10/4/23 16:02, Guinevere Larsen via Gdb-patches wrote:
>> diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
>> index de22da8d8a8..5b90b8af3b8 100644
>> --- a/gdb/testsuite/lib/gdb.exp
>> +++ b/gdb/testsuite/lib/gdb.exp
>> @@ -9981,5 +9981,25 @@ gdb_caching_proc have_system_header { file } {
>>       return [gdb_can_simple_compile have_system_header_$name $src 
>> object]
>>   }
>>   +# Return 1 if the test is being run as root, 0 otherwise.
>> +
>> +gdb_caching_proc root_user {} {
>> +    # ID outputs to stdout, we have to use exec to capture it here.
>> +    set user [remote_exec target id]
>> +
>> +    regexp -all "(-?\[0-9\]+).*" $user user ret_val
>
> I can see that this works, but it's a bit unusual (and hard to parse 
> for me) to use regexp to pick apart a list. The usual way is using 
> lindex.

That's a good point.

TCL lists confuse me a lot, and the original plan was to have this along 
with the uid picking part but that failed for other reasons when the 
command failed.

>
>> +
>> +    # If ret_val is not 0, we couldn't run `id` on the target for some
>> +    # reason.  Return that we are not root, so problems are easier to
>> +    # spot.
>> +    if {[expr $ret_val != 0]} {
>
> The expr is not necessary here.
>
>> +    return 0
>> +    }
>> +
>> +    regexp -all ".*uid=(\[0-9\]+).*" $user user uid
>> +
>
> Also, the 'user' variable is used as dummy, better make that explicit.
>
> So, please do:
> ...
>     set res [remote_exec target id]
>     set ret_val [lindex $res 0]
>     set output [lindex $res 1]
>
>     # If ret_val is not 0, we couldn't run `id` on the target for some
>     # reason.  Return that we are not root, so problems are easier to
>     # spot.
>     if { $ret_val != 0 } {
>         return 0
>     }
>
>     regexp -all ".*uid=(\[0-9\]+).*" $output dummy uid
>
>     return [expr $uid == 0]
> ...
>
> Approved with that change.
Thanks! I applied your change and pushed it.
  

Patch

diff --git a/gdb/testsuite/gdb.base/fileio.exp b/gdb/testsuite/gdb.base/fileio.exp
index 33c88d064c4..6a708ed00ed 100644
--- a/gdb/testsuite/gdb.base/fileio.exp
+++ b/gdb/testsuite/gdb.base/fileio.exp
@@ -17,7 +17,6 @@ 
 
 require {!target_info exists gdb,nofileio}
 
-
 standard_testfile
 
 if {[is_remote host]} {
@@ -75,6 +74,10 @@  gdb_test "continue" ".*" ""
 
 catch "system \"chmod -f -w [standard_output_file nowrt.fileio.test]\""
 
+# If the user is root, we will always have write permission.
+if { [root_user] } {
+    setup_xfail *-*-*
+}
 gdb_test continue \
 "Continuing\\..*open 5:.*EACCES$stop_msg" \
 "Open for write but no write permission returns EACCES"
@@ -240,6 +243,10 @@  gdb_test continue \
 if [ishost *cygwin*] {
     setup_xfail "*-*-*"
 }
+# If the user is root, we will always have write permission.
+if { [root_user] } {
+    setup_xfail *-*-*
+}
 gdb_test continue \
 "Continuing\\..*unlink 2:.*EACCES$stop_msg" \
 "Unlinking a file in a directory w/o write access returns EACCES"
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index de22da8d8a8..5b90b8af3b8 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -9981,5 +9981,25 @@  gdb_caching_proc have_system_header { file } {
     return [gdb_can_simple_compile have_system_header_$name $src object]
 }
 
+# Return 1 if the test is being run as root, 0 otherwise.
+
+gdb_caching_proc root_user {} {
+    # ID outputs to stdout, we have to use exec to capture it here.
+    set user [remote_exec target id]
+
+    regexp -all "(-?\[0-9\]+).*" $user user ret_val
+
+    # If ret_val is not 0, we couldn't run `id` on the target for some
+    # reason.  Return that we are not root, so problems are easier to
+    # spot.
+    if {[expr $ret_val != 0]} {
+	return 0
+    }
+
+    regexp -all ".*uid=(\[0-9\]+).*" $user user uid
+
+    return [expr $uid == 0]
+}
+
 # Always load compatibility stuff.
 load_lib future.exp